diff --git a/vendor/magento/module-google-tag-manager/Controller/Index/Get.php b/vendor/magento/module-google-tag-manager/Controller/Index/Get.php new file mode 100644 index 00000000000..a53555a3a99 --- /dev/null +++ b/vendor/magento/module-google-tag-manager/Controller/Index/Get.php @@ -0,0 +1,122 @@ +request = $request; + $this->resultFactory = $resultFactory; + $this->data = $data; + $this->sessionManager = $sessionManager; + $this->cookieManager = $cookieManager; + $this->jsonFactory = $jsonFactory; + } + + /** + * Get add to cart advanced list + * + * @return ResponseInterface|Json|Raw|ResultInterface + * @throws NotFoundException + */ + public function execute() + { + if ($this->isRequestAllowed() && + $this->cookieManager->getCookie(CookieData::GOOGLE_ANALYTICS_COOKIE_ADVANCED_NAME) + ) { + $addToCartProductList = $this->sessionManager->getAddToCartAdvanced(); + + if ($addToCartProductList) { + $this->sessionManager->unsAddToCartAdvanced(); + $resultJson = $this->jsonFactory->create(); + return $resultJson->setData($addToCartProductList); + } + } + $resultRaw = $this->resultFactory->create(ResultFactory::TYPE_RAW); + $resultRaw->setHttpResponseCode(404); + return $resultRaw; + } + + /** + * Check if request is allowed. + * + * @return bool + */ + private function isRequestAllowed(): bool + { + if (!$this->data->isGoogleAnalyticsAvailable() && !$this->data->isTagManagerAvailable()) { + return false; + } + return $this->request->isAjax() && $this->request->isPost(); + } +} diff --git a/vendor/magento/module-google-tag-manager/Helper/CookieData.php b/vendor/magento/module-google-tag-manager/Helper/CookieData.php new file mode 100644 index 00000000000..7596ec305e7 --- /dev/null +++ b/vendor/magento/module-google-tag-manager/Helper/CookieData.php @@ -0,0 +1,16 @@ +sessionManager = $sessionManager; + } + + /** + * Save product list to session instead of cookie for cookie size > 4096 bytes + * + * @param PhpCookieManager $subject + * @param Closure $proceed + * @param array $args + * @return void + * + * @SuppressWarnings(PHPMD.UnusedFormalParameter) + */ + public function aroundSetPublicCookie(PhpCookieManager $subject, Closure $proceed, ...$args): void + { + if ($args[0] == CookieData::GOOGLE_ANALYTICS_COOKIE_NAME) { + if ($this->sizeOfCookie($args[0], $args[1]) < PhpCookieManager::MAX_COOKIE_SIZE) { + $proceed(...$args); + } else { + $proceed(CookieData::GOOGLE_ANALYTICS_COOKIE_ADVANCED_NAME, true, $args[2]); + $this->sessionManager->setAddToCartAdvanced($args[1]); + } + } else { + $proceed(...$args); + } + } + + /** + * Retrieve the size of a cookie. + * + * The size of a cookie is determined by the length of 'name=value' portion of the cookie. + * + * @param string $name + * @param string $value + * @return int + */ + private function sizeOfCookie(string $name, string $value): int + { + // The constant '1' is the length of the equal sign in 'name=value'. + return strlen($name) + 1 + strlen($value); + } +} diff --git a/vendor/magento/module-google-tag-manager/etc/frontend/di.xml b/vendor/magento/module-google-tag-manager/etc/frontend/di.xml index 349ac32ac37..1ae1e003c53 100644 --- a/vendor/magento/module-google-tag-manager/etc/frontend/di.xml +++ b/vendor/magento/module-google-tag-manager/etc/frontend/di.xml @@ -17,4 +17,7 @@ + + + diff --git a/vendor/magento/module-google-tag-manager/etc/frontend/routes.xml b/vendor/magento/module-google-tag-manager/etc/frontend/routes.xml new file mode 100644 index 00000000000..a5b15741e74 --- /dev/null +++ b/vendor/magento/module-google-tag-manager/etc/frontend/routes.xml @@ -0,0 +1,14 @@ + + + + + + + + + diff --git a/vendor/magento/module-google-tag-manager/view/frontend/templates/js.phtml b/vendor/magento/module-google-tag-manager/view/frontend/templates/js.phtml index d0ac6355be3..50630b26d59 100644 --- a/vendor/magento/module-google-tag-manager/view/frontend/templates/js.phtml +++ b/vendor/magento/module-google-tag-manager/view/frontend/templates/js.phtml @@ -5,15 +5,24 @@ */ /** @var \Magento\GoogleTagManager\Block\ListJson $block */ + +use Magento\GoogleTagManager\Helper\CookieData; +use Magento\GoogleTagManager\Helper\Data; + +/** + * @var array $blockNames + */ +$blockNames = json_encode(array_values((array)$block->getData('impression_block_list'))); ?>