Hello you should use the event:
<event name="controller_front_send_response_before">
<observer name="Observer" instance="Vendor\Module\Observer\Observer" shared="false" />
</event>
The observer:
<?php
namespace Vendor\Module\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\App\Request\Http;
use Vendor\Module\Cookie\Aid;
class Observer implements ObserverInterface
{
protected $request;
protected $cookie;
public function __construct(
Http $request,
Aid $cookie,
)
{
$this->request = $request;
$this->cookie = $cookie;
}
public function execute(\Magento\Framework\Event\Observer $observer)
{
$this->request->getParams(); // all params
$param = $this->request->getParam('aid');
$cookieExists = $this->cookie->get();
if($param != "" && !$cookieExists){
$this->cookie->set($param, 86400);
}
}
}
i've created a separated class for the Cookie in Vendor\Module\Cookie\Aid
<?php
namespace Vendor\Module\Cookie;
use Magento\Framework\Stdlib\CookieManagerInterface;
use Magento\Framework\Stdlib\Cookie\CookieMetadataFactory;
use Magento\Framework\Session\SessionManagerInterface;
class Aid
{
/**
* Name of cookie that holds private content version
*/
const COOKIE_NAME = 'aid';
/**
* CookieManager
*
* @var CookieManagerInterface
*/
private $cookieManager;
/**
* @var CookieMetadataFactory
*/
private $cookieMetadataFactory;
/**
* @var SessionManagerInterface
*/
private $sessionManager;
/**
* @param CookieManagerInterface $cookieManager
* @param CookieMetadataFactory $cookieMetadataFactory
* @param SessionManagerInterface $sessionManager
*/
public function __construct(
CookieManagerInterface $cookieManager,
CookieMetadataFactory $cookieMetadataFactory,
SessionManagerInterface $sessionManager,
) {
$this->cookieManager = $cookieManager;
$this->cookieMetadataFactory = $cookieMetadataFactory;
$this->sessionManager = $sessionManager;
}
/**
* Get form key cookie
*
* @return string
*/
public function get()
{
return $this->cookieManager->getCookie(self::COOKIE_NAME);
}
/**
* @param string $value
* @param int $duration
* @return void
*/
public function set($value, $duration = 86400)
{
$metadata = $this->cookieMetadataFactory
->createPublicCookieMetadata()
->setDuration($duration)
->setPath($this->sessionManager->getCookiePath())
->setDomain($this->sessionManager->getCookieDomain());
$this->cookieManager->setPublicCookie(
self::COOKIE_NAME,
$value,
$metadata
);
}
/**
* @return void
*/
public function delete()
{
$metadata = $this->cookieMetadataFactory
->createPublicCookieMetadata()
->setDuration(36000)
->setPath($this->sessionManager->getCookiePath())
->setDomain($this->sessionManager->getCookieDomain());
$this->cookieManager->deleteCookie(
self::COOKIE_NAME,
$metadata
);
}
}
With all of this you'll be able to use the cookie in your success page using the event "checkout_submit_all_after" if you need to send a curl or check if there's your cookie, or just updating the layout: checkout_onepage_success.xml adding your PHTML to the page