From 20f7b9c002799b7dd3475751c5c5e58d30650bee Mon Sep 17 00:00:00 2001 From: "Author: Tony" <5071467+empiricompany@users.noreply.github.com> Date: Thu, 8 Jun 2023 08:50:12 +0200 Subject: [PATCH 01/20] Enhanced Google Analytics 4 Integration and Ecommerce Tracking Improvements --- .../core/Mage/GoogleAnalytics/Block/Ga.php | 200 +++++++++++------- .../core/Mage/GoogleAnalytics/Helper/Data.php | 24 +++ .../core/Mage/GoogleAnalytics/etc/system.xml | 29 +++ .../default/template/googleanalytics/ga.phtml | 3 +- 4 files changed, 176 insertions(+), 80 deletions(-) diff --git a/app/code/core/Mage/GoogleAnalytics/Block/Ga.php b/app/code/core/Mage/GoogleAnalytics/Block/Ga.php index 581214a3543..b664d7cd17e 100644 --- a/app/code/core/Mage/GoogleAnalytics/Block/Ga.php +++ b/app/code/core/Mage/GoogleAnalytics/Block/Ga.php @@ -72,11 +72,49 @@ protected function _getPageTrackingCode($accountId) { /** @var Mage_GoogleAnalytics_Helper_Data $helper */ $helper = $this->helper('googleanalytics'); - if ($helper->isUseUniversalAnalytics()) { + if ($helper->isUseAnalytics4()) { + return $this->_getPageTrackingCodeAnalytics4($accountId); + } elseif ($helper->isUseUniversalAnalytics()) { return $this->_getPageTrackingCodeUniversal($accountId); + } else { + return $this->_getPageTrackingCodeAnalytics($accountId); + } + } + + /** + * Render regular page tracking javascript code + * + * @param string $accountId + * @return string + */ + protected function _getPageTrackingCodeAnalytics4($accountId) + { + $trackingCode = " +gtag('js', new Date()); +"; + if (!$this->helper('googleanalytics')->isDebugModeEnabled()) { + $trackingCode.= " +gtag('config', '{$this->jsQuoteEscape($accountId)}'); +"; + } else { + $trackingCode.= " +gtag('config', '{$this->jsQuoteEscape($accountId)}', { 'debug_mode': true }); +"; + } + + //add user_id + if ($this->helper('googleanalytics')->isUserIdEnabled() && Mage::getSingleton('customer/session')->isLoggedIn()) { + $customer = Mage::getSingleton('customer/session')->getCustomer(); + $trackingCode.= " +gtag('set', 'user_id', '{$customer->getId()}'); +"; + } + + if ($this->helper('googleanalytics')->isDebugModeEnabled()) { + Mage::log($trackingCode, Zend_Log::DEBUG, 'googleanalytics4.log', true); } - return $this->_getPageTrackingCodeAnalytics($accountId); + return $trackingCode; } /** @@ -202,51 +240,21 @@ protected function _getOrdersTrackingCodeAnalytics4() $moduleName = $request->getModuleName(); $controllerName = $request->getControllerName(); - //purchase events - $orderIds = $this->getOrderIds(); - if (!empty($orderIds) && is_array($orderIds)) { - $collection = Mage::getResourceModel('sales/order_collection') - ->addFieldToFilter('entity_id', ['in' => $orderIds]); - /** @var Mage_Sales_Model_Order $order */ - foreach ($collection as $order) { - $orderData = [ - 'currency' => $order->getBaseCurrencyCode(), - 'transaction_id' => $order->getIncrementId(), - 'value' => number_format($order->getBaseGrandTotal(), 2), - 'coupon' => strtoupper($order->getCouponCode()), - 'shipping' => number_format($order->getBaseShippingAmount(), 2), - 'tax' => number_format($order->getBaseTaxAmount(), 2), - 'items' => [] - ]; - - /** @var Mage_Sales_Model_Order_Item $item */ - foreach ($order->getAllVisibleItems() as $item) { - $orderData['items'][] = [ - 'item_id' => $item->getSku(), - 'item_name' => $item->getName(), - 'quantity' => $item->getQtyOrdered(), - 'price' => $item->getBasePrice(), - 'discount' => $item->getBaseDiscountAmount() - ]; - } - $result[] = "gtag('event', 'purchase', " . json_encode($orderData, JSON_THROW_ON_ERROR) . ");"; - } - } - //Begin advanced eCommerce events //product page if ($moduleName == 'catalog' && $controllerName == 'product') { $productViewed = Mage::registry('current_product'); + $category = Mage::registry('current_category') ? Mage::registry('current_category')->getName() : false; $eventData = []; $eventData['currency'] = Mage::app()->getStore()->getCurrentCurrencyCode(); $eventData['value'] = number_format($productViewed->getFinalPrice(), 2); $eventData['items'] = []; $eventData['items'][] = [ - 'id' => $productViewed->getSku(), - 'name' => $productViewed->getName(), + 'item_id' => $productViewed->getSku(), + 'item_name' => $productViewed->getName(), 'list_name' => 'Product Detail Page', - 'brand' => $productViewed->getAttributeText('manufacturer'), - 'category' => 'Products', + 'item_brand' => $productViewed->getAttributeText('manufacturer'), + 'item_category' => $category, 'price' => number_format($productViewed->getFinalPrice(), 2), ]; @@ -258,30 +266,36 @@ protected function _getOrdersTrackingCodeAnalytics4() $layer = Mage::getSingleton('catalog/layer'); $category = $layer->getCurrentCategory(); $productCollection = clone $layer->getProductCollection(); + $productCollection->addAttributeToSelect('sku'); + $toolbarBlock = Mage::app()->getLayout()->getBlock('product_list_toolbar'); $pageSize = $toolbarBlock->getLimit(); $currentPage = $toolbarBlock->getCurrentPage(); if ($pageSize !== 'all') { $productCollection->setPageSize($pageSize)->setCurPage($currentPage); } + $eventData = []; $eventData['currency'] = Mage::app()->getStore()->getCurrentCurrencyCode(); - $eventData['value'] = 0.00; + $eventData['value'] = '0.00'; $eventData['item_list_id'] = 'category_'.$category->getUrlKey(); $eventData['item_list_name'] = $category->getName(); $eventData['items'] = []; - foreach ($productCollection as $productViewed) { + $index = 1; + foreach ($productCollection as $key => $productViewed) { $eventData['items'][] = [ - 'id' => $productViewed->getSku(), - 'name' => $productViewed->getName(), - 'list_name' => 'Product Detail Page', - 'brand' => $productViewed->getAttributeText('manufacturer'), - 'category' => 'Products', + 'item_id' => $productViewed->getSku(), + 'index' => $index, + 'item_name' => $productViewed->getName(), + 'item_brand' => $productViewed->getAttributeText('manufacturer'), + 'item_category' => $productViewed->getCategory()->getName(), 'price' => number_format($productViewed->getFinalPrice(), 2), ]; + $index++; $eventData['value'] += $productViewed->getFinalPrice(); } + $eventData['value'] = number_format($eventData['value'], 2); $result[] = "gtag('event', 'view_item_list', " . json_encode($eventData, JSON_THROW_ON_ERROR) . ");"; } @@ -296,11 +310,9 @@ protected function _getOrdersTrackingCodeAnalytics4() $eventData['value'] = number_format($_removedProduct->getFinalPrice(), 2); $eventData['items'] = []; $eventData['items'][] = [ - 'id' => $_removedProduct->getSku(), - 'name' => $_removedProduct->getName(), - 'list_name' => 'Product Detail Page', - 'brand' => $_removedProduct->getAttributeText('manufacturer'), - 'category' => 'Products', + 'item_id' => $_removedProduct->getSku(), + 'item_name' => $_removedProduct->getName(), + 'item_brand' => $_removedProduct->getAttributeText('manufacturer'), 'price' => number_format($_removedProduct->getFinalPrice(), 2), ]; $result[] = "gtag('event', 'remove_from_cart', " . json_encode($eventData, JSON_THROW_ON_ERROR) . ");"; @@ -315,11 +327,9 @@ protected function _getOrdersTrackingCodeAnalytics4() $eventData['value'] = number_format($_addedProduct->getFinalPrice(), 2); $eventData['items'] = []; $eventData['items'][] = [ - 'id' => $_addedProduct->getSku(), - 'name' => $_addedProduct->getName(), - 'list_name' => 'Product Detail Page', - 'brand' => $_addedProduct->getAttributeText('manufacturer'), - 'category' => 'Products', + 'item_id' => $_addedProduct->getSku(), + 'item_name' => $_addedProduct->getName(), + 'item_brand' => $_addedProduct->getAttributeText('manufacturer'), 'price' => number_format($_addedProduct->getFinalPrice(), 2), ]; $result[] = "gtag('event', 'add_to_cart', " . json_encode($eventData, JSON_THROW_ON_ERROR) . ");"; @@ -329,45 +339,79 @@ protected function _getOrdersTrackingCodeAnalytics4() $productCollection = Mage::getSingleton('checkout/session')->getQuote()->getAllVisibleItems(); $eventData = []; $eventData['currency'] = Mage::app()->getStore()->getCurrentCurrencyCode(); - $eventData['value'] = 0.00; + $eventData['value'] = '0.00'; $eventData['items'] = []; foreach ($productCollection as $productInCart) { + $_product = Mage::getModel('catalog/product')->load($productInCart->getProductId()); $eventData['items'][] = [ - 'id' => $productInCart->getSku(), - 'name' => $productInCart->getName(), - 'list_name' => 'Product Detail Page', - 'brand' => $productInCart->getAttributeText('manufacturer'), - 'category' => 'Products', - 'price' => number_format($productInCart->getFinalPrice(), 2), + 'item_id' => $_product->getSku(), + 'item_name' => $_product->getName(), + 'item_brand' => $_product->getAttributeText('manufacturer'), + 'price' => number_format($_product->getFinalPrice(), 2), ]; - $eventData['value'] += $productInCart->getFinalPrice(); + $eventData['value'] += $_product->getFinalPrice(); } $eventData['value'] = number_format($eventData['value'], 2); $result[] = "gtag('event', 'view_cart', " . json_encode($eventData, JSON_THROW_ON_ERROR) . ");"; } //begin checkout - elseif ($moduleName == 'checkout' && $controllerName == 'onepage') { + elseif (($moduleName == 'checkout' && $controllerName == 'onepage') || ($moduleName == 'firecheckout' && $controllerName == 'index')) { $productCollection = Mage::getSingleton('checkout/session')->getQuote()->getAllVisibleItems(); - $eventData = []; - $eventData['currency'] = Mage::app()->getStore()->getCurrentCurrencyCode(); - $eventData['value'] = 0.00; - $eventData['items'] = []; + if ($productCollection) { + $eventData = []; + $eventData['currency'] = Mage::app()->getStore()->getCurrentCurrencyCode(); + $eventData['value'] = 0.00; + $eventData['items'] = []; + foreach ($productCollection as $productInCart) { + $_product = Mage::getModel('catalog/product')->load($productInCart->getProductId()); + $eventData['items'][] = [ + 'item_id' => $_product->getSku(), + 'item_name' => $_product->getName(), + 'item_brand' => $_product->getAttributeText('manufacturer'), + 'price' => number_format($_product->getFinalPrice(), 2), + ]; + $eventData['value'] += $_product->getFinalPrice(); + } + $eventData['value'] = number_format($eventData['value'], 2); + $result[] = "gtag('event', 'begin_checkout', " . json_encode($eventData, JSON_THROW_ON_ERROR) . ");"; + } + } - foreach ($productCollection as $productInCart) { - $eventData['items'][] = [ - 'id' => $productInCart->getSku(), - 'name' => $productInCart->getName(), - 'list_name' => 'Product Detail Page', - 'brand' => $productInCart->getAttributeText('manufacturer'), - 'category' => 'Products', - 'price' => number_format($productInCart->getFinalPrice(), 2), + //purchase events + $orderIds = $this->getOrderIds(); + if (!empty($orderIds) && is_array($orderIds)) { + $collection = Mage::getResourceModel('sales/order_collection') + ->addFieldToFilter('entity_id', ['in' => $orderIds]); + /** @var Mage_Sales_Model_Order $order */ + foreach ($collection as $order) { + $orderData = [ + 'currency' => $order->getBaseCurrencyCode(), + 'transaction_id' => $order->getIncrementId(), + 'value' => number_format($order->getBaseGrandTotal(), 2), + 'coupon' => strtoupper($order->getCouponCode()), + 'shipping' => number_format($order->getBaseShippingAmount(), 2), + 'tax' => number_format($order->getBaseTaxAmount(), 2), + 'items' => [] ]; - $eventData['value'] += $productInCart->getFinalPrice(); + + /** @var Mage_Sales_Model_Order_Item $item */ + foreach ($order->getAllVisibleItems() as $item) { + $orderData['items'][] = [ + 'item_id' => $item->getSku(), + 'item_name' => $item->getName(), + 'quantity' => $item->getQtyOrdered(), + 'price' => $item->getBasePrice(), + 'discount' => $item->getBaseDiscountAmount() + ]; + } + $result[] = "gtag('event', 'purchase', " . json_encode($orderData, JSON_THROW_ON_ERROR) . ");"; } - $eventData['value'] = number_format($eventData['value'], 2); - $result[] = "gtag('event', 'begin_checkout', " . json_encode($eventData, JSON_THROW_ON_ERROR) . ");"; + } + + if ($this->helper('googleanalytics')->isDebugModeEnabled() && count($result) > 0) { + Mage::log($result, Zend_Log::DEBUG, 'googleanalytics4.log', true); } return implode("\n", $result); } diff --git a/app/code/core/Mage/GoogleAnalytics/Helper/Data.php b/app/code/core/Mage/GoogleAnalytics/Helper/Data.php index 7cb92cf7528..a2c0bd4331e 100644 --- a/app/code/core/Mage/GoogleAnalytics/Helper/Data.php +++ b/app/code/core/Mage/GoogleAnalytics/Helper/Data.php @@ -28,6 +28,8 @@ class Mage_GoogleAnalytics_Helper_Data extends Mage_Core_Helper_Abstract public const XML_PATH_TYPE = 'google/analytics/type'; public const XML_PATH_ACCOUNT = 'google/analytics/account'; public const XML_PATH_ANONYMIZATION = 'google/analytics/anonymization'; + public const XML_PATH_DEBUG = 'google/analytics/debug'; + public const XML_PATH_USERID = 'google/analytics/user_id'; /** * @var string google analytics 4 @@ -104,4 +106,26 @@ public function isUseAnalytics4($store = null) { return Mage::getStoreConfig(self::XML_PATH_TYPE, $store) == self::TYPE_ANALYTICS4; } + + /** + * Whether GA Debug Mode is enabled (only for development ip) + * + * @param null $store + * @return bool + */ + public function isDebugModeEnabled($store = null) + { + return Mage::getStoreConfigFlag(self::XML_PATH_DEBUG, $store) && Mage::helper('core')->isDevAllowed(); + } + + /** + * Whether GA IP Anonymization is enabled + * + * @param null $store + * @return bool + */ + public function isUserIdEnabled($store = null) + { + return Mage::getStoreConfigFlag(self::XML_PATH_USERID, $store); + } } diff --git a/app/code/core/Mage/GoogleAnalytics/etc/system.xml b/app/code/core/Mage/GoogleAnalytics/etc/system.xml index 64120809aa6..c242fac73fd 100644 --- a/app/code/core/Mage/GoogleAnalytics/etc/system.xml +++ b/app/code/core/Mage/GoogleAnalytics/etc/system.xml @@ -57,6 +57,32 @@ 1 1 + + + select + adminhtml/system_config_source_yesno + 21 + 1 + 1 + 1 + Enable GA4 User_id tracking for logged in customers. + + analytics4 + + + + + select + adminhtml/system_config_source_yesno + 22 + 1 + 1 + 1 + Enable GA4 Debug Real Time view for Development IP. + + analytics4 + + select @@ -65,6 +91,9 @@ 1 1 1 + + universal + diff --git a/app/design/frontend/base/default/template/googleanalytics/ga.phtml b/app/design/frontend/base/default/template/googleanalytics/ga.phtml index 08762b3195b..d0a3fab571b 100644 --- a/app/design/frontend/base/default/template/googleanalytics/ga.phtml +++ b/app/design/frontend/base/default/template/googleanalytics/ga.phtml @@ -25,8 +25,7 @@ $_accountId = $_helper->getAccountId(); From a5aa41f6fa3a0138843b2a74535d1535b0847844 Mon Sep 17 00:00:00 2001 From: Tony Date: Thu, 8 Jun 2023 09:15:50 +0200 Subject: [PATCH 02/20] string to float 0.00 value --- app/code/core/Mage/GoogleAnalytics/Block/Ga.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/code/core/Mage/GoogleAnalytics/Block/Ga.php b/app/code/core/Mage/GoogleAnalytics/Block/Ga.php index b664d7cd17e..3b28f3c4ad4 100644 --- a/app/code/core/Mage/GoogleAnalytics/Block/Ga.php +++ b/app/code/core/Mage/GoogleAnalytics/Block/Ga.php @@ -277,7 +277,7 @@ protected function _getOrdersTrackingCodeAnalytics4() $eventData = []; $eventData['currency'] = Mage::app()->getStore()->getCurrentCurrencyCode(); - $eventData['value'] = '0.00'; + $eventData['value'] = 0.00; $eventData['item_list_id'] = 'category_'.$category->getUrlKey(); $eventData['item_list_name'] = $category->getName(); $eventData['items'] = []; @@ -339,7 +339,7 @@ protected function _getOrdersTrackingCodeAnalytics4() $productCollection = Mage::getSingleton('checkout/session')->getQuote()->getAllVisibleItems(); $eventData = []; $eventData['currency'] = Mage::app()->getStore()->getCurrentCurrencyCode(); - $eventData['value'] = '0.00'; + $eventData['value'] = 0.00; $eventData['items'] = []; foreach ($productCollection as $productInCart) { From 9ac5f485bc9a0ed531fe568f604f504ca8f91ead Mon Sep 17 00:00:00 2001 From: Fabrizio Balliano Date: Thu, 8 Jun 2023 09:30:18 +0200 Subject: [PATCH 03/20] PHPCS --- app/code/core/Mage/GoogleAnalytics/Block/Ga.php | 1 - 1 file changed, 1 deletion(-) diff --git a/app/code/core/Mage/GoogleAnalytics/Block/Ga.php b/app/code/core/Mage/GoogleAnalytics/Block/Ga.php index 3b28f3c4ad4..ae479ee4b71 100644 --- a/app/code/core/Mage/GoogleAnalytics/Block/Ga.php +++ b/app/code/core/Mage/GoogleAnalytics/Block/Ga.php @@ -274,7 +274,6 @@ protected function _getOrdersTrackingCodeAnalytics4() if ($pageSize !== 'all') { $productCollection->setPageSize($pageSize)->setCurPage($currentPage); } - $eventData = []; $eventData['currency'] = Mage::app()->getStore()->getCurrentCurrencyCode(); $eventData['value'] = 0.00; From 695f6b34d93023c30e9a06308099630972fdc4c2 Mon Sep 17 00:00:00 2001 From: Fabrizio Balliano Date: Thu, 8 Jun 2023 09:30:40 +0200 Subject: [PATCH 04/20] PHPCS --- app/code/core/Mage/GoogleAnalytics/Block/Ga.php | 1 - 1 file changed, 1 deletion(-) diff --git a/app/code/core/Mage/GoogleAnalytics/Block/Ga.php b/app/code/core/Mage/GoogleAnalytics/Block/Ga.php index ae479ee4b71..013b12a7530 100644 --- a/app/code/core/Mage/GoogleAnalytics/Block/Ga.php +++ b/app/code/core/Mage/GoogleAnalytics/Block/Ga.php @@ -294,7 +294,6 @@ protected function _getOrdersTrackingCodeAnalytics4() $index++; $eventData['value'] += $productViewed->getFinalPrice(); } - $eventData['value'] = number_format($eventData['value'], 2); $result[] = "gtag('event', 'view_item_list', " . json_encode($eventData, JSON_THROW_ON_ERROR) . ");"; } From c0c6e7510f5dae59e7b794c3cfed59dc8ce89157 Mon Sep 17 00:00:00 2001 From: Fabrizio Balliano Date: Fri, 9 Jun 2023 08:45:33 +0200 Subject: [PATCH 05/20] PHPCS --- app/code/core/Mage/GoogleAnalytics/Block/Ga.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/code/core/Mage/GoogleAnalytics/Block/Ga.php b/app/code/core/Mage/GoogleAnalytics/Block/Ga.php index 013b12a7530..2d4fc8f87cd 100644 --- a/app/code/core/Mage/GoogleAnalytics/Block/Ga.php +++ b/app/code/core/Mage/GoogleAnalytics/Block/Ga.php @@ -93,7 +93,7 @@ protected function _getPageTrackingCodeAnalytics4($accountId) gtag('js', new Date()); "; if (!$this->helper('googleanalytics')->isDebugModeEnabled()) { - $trackingCode.= " + $trackingCode .= " gtag('config', '{$this->jsQuoteEscape($accountId)}'); "; } else { From 9977ad46ae40b97b6b62a2fa79ff6fb2157c7959 Mon Sep 17 00:00:00 2001 From: Fabrizio Balliano Date: Fri, 9 Jun 2023 08:46:10 +0200 Subject: [PATCH 06/20] whitespace --- app/code/core/Mage/GoogleAnalytics/Block/Ga.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/code/core/Mage/GoogleAnalytics/Block/Ga.php b/app/code/core/Mage/GoogleAnalytics/Block/Ga.php index 2d4fc8f87cd..acfc92e59ef 100644 --- a/app/code/core/Mage/GoogleAnalytics/Block/Ga.php +++ b/app/code/core/Mage/GoogleAnalytics/Block/Ga.php @@ -97,8 +97,8 @@ protected function _getPageTrackingCodeAnalytics4($accountId) gtag('config', '{$this->jsQuoteEscape($accountId)}'); "; } else { - $trackingCode.= " -gtag('config', '{$this->jsQuoteEscape($accountId)}', { 'debug_mode': true }); + $trackingCode .= " +gtag('config', '{$this->jsQuoteEscape($accountId)}', {'debug_mode':true}); "; } From e15f0e64b3b3af44df6d095776ba61925e42f78c Mon Sep 17 00:00:00 2001 From: Tony Date: Fri, 9 Jun 2023 10:02:36 +0200 Subject: [PATCH 07/20] remove 3rd party partial support --- app/code/core/Mage/GoogleAnalytics/Block/Ga.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/code/core/Mage/GoogleAnalytics/Block/Ga.php b/app/code/core/Mage/GoogleAnalytics/Block/Ga.php index acfc92e59ef..52e3508d9ec 100644 --- a/app/code/core/Mage/GoogleAnalytics/Block/Ga.php +++ b/app/code/core/Mage/GoogleAnalytics/Block/Ga.php @@ -355,7 +355,7 @@ protected function _getOrdersTrackingCodeAnalytics4() } //begin checkout - elseif (($moduleName == 'checkout' && $controllerName == 'onepage') || ($moduleName == 'firecheckout' && $controllerName == 'index')) { + elseif ($moduleName == 'checkout' && $controllerName == 'onepage') { $productCollection = Mage::getSingleton('checkout/session')->getQuote()->getAllVisibleItems(); if ($productCollection) { $eventData = []; From 9fe6da155fdfc8ed32d83f9cfd8d3e092c20d606 Mon Sep 17 00:00:00 2001 From: Fabrizio Balliano Date: Sat, 10 Jun 2023 01:03:55 +0200 Subject: [PATCH 08/20] case --- app/code/core/Mage/GoogleAnalytics/Helper/Data.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/code/core/Mage/GoogleAnalytics/Helper/Data.php b/app/code/core/Mage/GoogleAnalytics/Helper/Data.php index a2c0bd4331e..54339871ee6 100644 --- a/app/code/core/Mage/GoogleAnalytics/Helper/Data.php +++ b/app/code/core/Mage/GoogleAnalytics/Helper/Data.php @@ -108,7 +108,7 @@ public function isUseAnalytics4($store = null) } /** - * Whether GA Debug Mode is enabled (only for development ip) + * Whether GA Debug Mode is enabled (only for development IP) * * @param null $store * @return bool From 153efcae7e599ee0fa361a9bc86a767b4c523f87 Mon Sep 17 00:00:00 2001 From: Tony Date: Sat, 10 Jun 2023 09:57:41 +0200 Subject: [PATCH 09/20] Update app/code/core/Mage/GoogleAnalytics/etc/system.xml Co-authored-by: Fabrizio Balliano --- app/code/core/Mage/GoogleAnalytics/etc/system.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/code/core/Mage/GoogleAnalytics/etc/system.xml b/app/code/core/Mage/GoogleAnalytics/etc/system.xml index c242fac73fd..b9991e4db47 100644 --- a/app/code/core/Mage/GoogleAnalytics/etc/system.xml +++ b/app/code/core/Mage/GoogleAnalytics/etc/system.xml @@ -58,7 +58,7 @@ 1 - + select adminhtml/system_config_source_yesno 21 From a13656b91c550e8fba39e39236680feca49f08c9 Mon Sep 17 00:00:00 2001 From: Tony Date: Sat, 10 Jun 2023 09:57:51 +0200 Subject: [PATCH 10/20] Update app/code/core/Mage/GoogleAnalytics/etc/system.xml Co-authored-by: Fabrizio Balliano --- app/code/core/Mage/GoogleAnalytics/etc/system.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/code/core/Mage/GoogleAnalytics/etc/system.xml b/app/code/core/Mage/GoogleAnalytics/etc/system.xml index b9991e4db47..4f9aa7623c1 100644 --- a/app/code/core/Mage/GoogleAnalytics/etc/system.xml +++ b/app/code/core/Mage/GoogleAnalytics/etc/system.xml @@ -57,7 +57,7 @@ 1 1 - + select adminhtml/system_config_source_yesno From df9db8a748b0c3b9ccc4da09e4ee621f2d1d2f69 Mon Sep 17 00:00:00 2001 From: Tony Date: Sat, 10 Jun 2023 09:58:03 +0200 Subject: [PATCH 11/20] Update app/code/core/Mage/GoogleAnalytics/etc/system.xml Co-authored-by: Fabrizio Balliano --- app/code/core/Mage/GoogleAnalytics/etc/system.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/code/core/Mage/GoogleAnalytics/etc/system.xml b/app/code/core/Mage/GoogleAnalytics/etc/system.xml index 4f9aa7623c1..9631f0fe728 100644 --- a/app/code/core/Mage/GoogleAnalytics/etc/system.xml +++ b/app/code/core/Mage/GoogleAnalytics/etc/system.xml @@ -70,7 +70,7 @@ analytics4 - + select adminhtml/system_config_source_yesno From d1f6cf322641c0dfd11c2977699ac36dc2d7aab0 Mon Sep 17 00:00:00 2001 From: Tony Date: Sat, 10 Jun 2023 10:06:52 +0200 Subject: [PATCH 12/20] add translations --- app/locale/en_US/Mage_GoogleAnalytics.csv | 2 ++ 1 file changed, 2 insertions(+) diff --git a/app/locale/en_US/Mage_GoogleAnalytics.csv b/app/locale/en_US/Mage_GoogleAnalytics.csv index 1b90846f791..54b35787571 100644 --- a/app/locale/en_US/Mage_GoogleAnalytics.csv +++ b/app/locale/en_US/Mage_GoogleAnalytics.csv @@ -6,3 +6,5 @@ "Type","Type" "Universal Analytics","Universal Analytics" "Google Analytics 4","Google Analytics 4" +"Enable GA4 User_id tracking for logged in customers.","Enable GA4 User_id tracking for logged in customers." +"Enable GA4 Debug Real Time view for Development IP.","Enable GA4 Debug Real Time view for Development IP." \ No newline at end of file From 2ff31615f7d7c1c34600642030afb580adcdb6f3 Mon Sep 17 00:00:00 2001 From: Tony Date: Sat, 10 Jun 2023 10:41:48 +0200 Subject: [PATCH 13/20] fix item_brand false or null in analytics view --- .../core/Mage/GoogleAnalytics/Block/Ga.php | 46 +++++++++++++------ 1 file changed, 33 insertions(+), 13 deletions(-) diff --git a/app/code/core/Mage/GoogleAnalytics/Block/Ga.php b/app/code/core/Mage/GoogleAnalytics/Block/Ga.php index 52e3508d9ec..9d9d85e8514 100644 --- a/app/code/core/Mage/GoogleAnalytics/Block/Ga.php +++ b/app/code/core/Mage/GoogleAnalytics/Block/Ga.php @@ -249,14 +249,17 @@ protected function _getOrdersTrackingCodeAnalytics4() $eventData['currency'] = Mage::app()->getStore()->getCurrentCurrencyCode(); $eventData['value'] = number_format($productViewed->getFinalPrice(), 2); $eventData['items'] = []; - $eventData['items'][] = [ + $_item = [ 'item_id' => $productViewed->getSku(), 'item_name' => $productViewed->getName(), 'list_name' => 'Product Detail Page', - 'item_brand' => $productViewed->getAttributeText('manufacturer'), 'item_category' => $category, 'price' => number_format($productViewed->getFinalPrice(), 2), ]; + if ($productViewed->getAttributeText('manufacturer')) { + $_item['item_brand'] = $productViewed->getAttributeText('manufacturer'); + } + array_push($eventData['items'], $_item); $result[] = "gtag('event', 'view_item', " . json_encode($eventData, JSON_THROW_ON_ERROR) . ");"; } @@ -283,14 +286,19 @@ protected function _getOrdersTrackingCodeAnalytics4() $index = 1; foreach ($productCollection as $key => $productViewed) { - $eventData['items'][] = [ + $_item = [ 'item_id' => $productViewed->getSku(), 'index' => $index, 'item_name' => $productViewed->getName(), - 'item_brand' => $productViewed->getAttributeText('manufacturer'), - 'item_category' => $productViewed->getCategory()->getName(), 'price' => number_format($productViewed->getFinalPrice(), 2), ]; + if ($productViewed->getAttributeText('manufacturer')) { + $_item['item_brand'] = $productViewed->getAttributeText('manufacturer'); + } + if ($productViewed->getCategory()->getName()) { + $_item['item_category'] = $productViewed->getCategory()->getName(); + } + array_push($eventData['items'], $_item); $index++; $eventData['value'] += $productViewed->getFinalPrice(); } @@ -307,12 +315,15 @@ protected function _getOrdersTrackingCodeAnalytics4() $eventData['currency'] = Mage::app()->getStore()->getCurrentCurrencyCode(); $eventData['value'] = number_format($_removedProduct->getFinalPrice(), 2); $eventData['items'] = []; - $eventData['items'][] = [ + $_item = [ 'item_id' => $_removedProduct->getSku(), 'item_name' => $_removedProduct->getName(), - 'item_brand' => $_removedProduct->getAttributeText('manufacturer'), 'price' => number_format($_removedProduct->getFinalPrice(), 2), ]; + if ($_removedProduct->getAttributeText('manufacturer')) { + $_item['item_brand'] = $_removedProduct->getAttributeText('manufacturer'); + } + array_push($eventData['items'], $_item); $result[] = "gtag('event', 'remove_from_cart', " . json_encode($eventData, JSON_THROW_ON_ERROR) . ");"; Mage::getSingleton('core/session')->unsRemovedProductCart(); } @@ -324,12 +335,15 @@ protected function _getOrdersTrackingCodeAnalytics4() $eventData['currency'] = Mage::app()->getStore()->getCurrentCurrencyCode(); $eventData['value'] = number_format($_addedProduct->getFinalPrice(), 2); $eventData['items'] = []; - $eventData['items'][] = [ + $_item = [ 'item_id' => $_addedProduct->getSku(), 'item_name' => $_addedProduct->getName(), - 'item_brand' => $_addedProduct->getAttributeText('manufacturer'), 'price' => number_format($_addedProduct->getFinalPrice(), 2), ]; + if ($_addedProduct->getAttributeText('manufacturer')) { + $_item['item_brand'] = $_addedProduct->getAttributeText('manufacturer'); + } + array_push($eventData['items'], $_item); $result[] = "gtag('event', 'add_to_cart', " . json_encode($eventData, JSON_THROW_ON_ERROR) . ");"; Mage::getSingleton('core/session')->unsAddedProductCart(); } @@ -342,12 +356,15 @@ protected function _getOrdersTrackingCodeAnalytics4() foreach ($productCollection as $productInCart) { $_product = Mage::getModel('catalog/product')->load($productInCart->getProductId()); - $eventData['items'][] = [ + $_item = [ 'item_id' => $_product->getSku(), 'item_name' => $_product->getName(), - 'item_brand' => $_product->getAttributeText('manufacturer'), 'price' => number_format($_product->getFinalPrice(), 2), ]; + if ($_product->getAttributeText('manufacturer')) { + $_item['item_brand'] = $_product->getAttributeText('manufacturer'); + } + array_push($eventData['items'], $_item); $eventData['value'] += $_product->getFinalPrice(); } $eventData['value'] = number_format($eventData['value'], 2); @@ -364,12 +381,15 @@ protected function _getOrdersTrackingCodeAnalytics4() $eventData['items'] = []; foreach ($productCollection as $productInCart) { $_product = Mage::getModel('catalog/product')->load($productInCart->getProductId()); - $eventData['items'][] = [ + $_item = [ 'item_id' => $_product->getSku(), 'item_name' => $_product->getName(), - 'item_brand' => $_product->getAttributeText('manufacturer'), 'price' => number_format($_product->getFinalPrice(), 2), ]; + if ($_product->getAttributeText('manufacturer')) { + $_item['item_brand'] = $_product->getAttributeText('manufacturer'); + } + array_push($eventData['items'], $_item); $eventData['value'] += $_product->getFinalPrice(); } $eventData['value'] = number_format($eventData['value'], 2); From f967f4b4229129ef02f2ca60eb6f18afdd9f0b07 Mon Sep 17 00:00:00 2001 From: Tony Date: Sat, 10 Jun 2023 10:46:05 +0200 Subject: [PATCH 14/20] fire cart actions events also if redirect_to_cart is false --- .../core/Mage/GoogleAnalytics/Block/Ga.php | 81 ++++++++++--------- 1 file changed, 41 insertions(+), 40 deletions(-) diff --git a/app/code/core/Mage/GoogleAnalytics/Block/Ga.php b/app/code/core/Mage/GoogleAnalytics/Block/Ga.php index 9d9d85e8514..a0b60e4c098 100644 --- a/app/code/core/Mage/GoogleAnalytics/Block/Ga.php +++ b/app/code/core/Mage/GoogleAnalytics/Block/Ga.php @@ -241,6 +241,47 @@ protected function _getOrdersTrackingCodeAnalytics4() $controllerName = $request->getControllerName(); //Begin advanced eCommerce events + //cart actions events + $removedProduct = Mage::getSingleton('core/session')->getRemovedProductCart(); + if ($removedProduct) { + $_removedProduct = Mage::getModel('catalog/product')->load($removedProduct); + $eventData = []; + $eventData['currency'] = Mage::app()->getStore()->getCurrentCurrencyCode(); + $eventData['value'] = number_format($_removedProduct->getFinalPrice(), 2); + $eventData['items'] = []; + $_item = [ + 'item_id' => $_removedProduct->getSku(), + 'item_name' => $_removedProduct->getName(), + 'price' => number_format($_removedProduct->getFinalPrice(), 2), + ]; + if ($_removedProduct->getAttributeText('manufacturer')) { + $_item['item_brand'] = $_removedProduct->getAttributeText('manufacturer'); + } + array_push($eventData['items'], $_item); + $result[] = "gtag('event', 'remove_from_cart', " . json_encode($eventData, JSON_THROW_ON_ERROR) . ");"; + Mage::getSingleton('core/session')->unsRemovedProductCart(); + } + + $addedProduct = Mage::getSingleton('core/session')->getAddedProductCart(); + if ($addedProduct) { + $_addedProduct = Mage::getModel('catalog/product')->load($addedProduct); + $eventData = []; + $eventData['currency'] = Mage::app()->getStore()->getCurrentCurrencyCode(); + $eventData['value'] = number_format($_addedProduct->getFinalPrice(), 2); + $eventData['items'] = []; + $_item = [ + 'item_id' => $_addedProduct->getSku(), + 'item_name' => $_addedProduct->getName(), + 'price' => number_format($_addedProduct->getFinalPrice(), 2), + ]; + if ($_addedProduct->getAttributeText('manufacturer')) { + $_item['item_brand'] = $_addedProduct->getAttributeText('manufacturer'); + } + array_push($eventData['items'], $_item); + $result[] = "gtag('event', 'add_to_cart', " . json_encode($eventData, JSON_THROW_ON_ERROR) . ");"; + Mage::getSingleton('core/session')->unsAddedProductCart(); + } + //product page if ($moduleName == 'catalog' && $controllerName == 'product') { $productViewed = Mage::registry('current_product'); @@ -308,46 +349,6 @@ protected function _getOrdersTrackingCodeAnalytics4() //cart elseif ($moduleName == 'checkout' && $controllerName == 'cart') { - $removedProduct = Mage::getSingleton('core/session')->getRemovedProductCart(); - if ($removedProduct) { - $_removedProduct = Mage::getModel('catalog/product')->load($removedProduct); - $eventData = []; - $eventData['currency'] = Mage::app()->getStore()->getCurrentCurrencyCode(); - $eventData['value'] = number_format($_removedProduct->getFinalPrice(), 2); - $eventData['items'] = []; - $_item = [ - 'item_id' => $_removedProduct->getSku(), - 'item_name' => $_removedProduct->getName(), - 'price' => number_format($_removedProduct->getFinalPrice(), 2), - ]; - if ($_removedProduct->getAttributeText('manufacturer')) { - $_item['item_brand'] = $_removedProduct->getAttributeText('manufacturer'); - } - array_push($eventData['items'], $_item); - $result[] = "gtag('event', 'remove_from_cart', " . json_encode($eventData, JSON_THROW_ON_ERROR) . ");"; - Mage::getSingleton('core/session')->unsRemovedProductCart(); - } - - $addedProduct = Mage::getSingleton('core/session')->getAddedProductCart(); - if ($addedProduct) { - $_addedProduct = Mage::getModel('catalog/product')->load($addedProduct); - $eventData = []; - $eventData['currency'] = Mage::app()->getStore()->getCurrentCurrencyCode(); - $eventData['value'] = number_format($_addedProduct->getFinalPrice(), 2); - $eventData['items'] = []; - $_item = [ - 'item_id' => $_addedProduct->getSku(), - 'item_name' => $_addedProduct->getName(), - 'price' => number_format($_addedProduct->getFinalPrice(), 2), - ]; - if ($_addedProduct->getAttributeText('manufacturer')) { - $_item['item_brand'] = $_addedProduct->getAttributeText('manufacturer'); - } - array_push($eventData['items'], $_item); - $result[] = "gtag('event', 'add_to_cart', " . json_encode($eventData, JSON_THROW_ON_ERROR) . ");"; - Mage::getSingleton('core/session')->unsAddedProductCart(); - } - $productCollection = Mage::getSingleton('checkout/session')->getQuote()->getAllVisibleItems(); $eventData = []; $eventData['currency'] = Mage::app()->getStore()->getCurrentCurrencyCode(); From bc5b167601a3ab59cf08ef7bdeac52a225942c20 Mon Sep 17 00:00:00 2001 From: Tony Date: Sat, 10 Jun 2023 11:29:45 +0200 Subject: [PATCH 15/20] catch multi products cart actions --- .../core/Mage/GoogleAnalytics/Block/Ga.php | 72 ++++++++++--------- .../Mage/GoogleAnalytics/Model/Observer.php | 16 ++++- 2 files changed, 52 insertions(+), 36 deletions(-) diff --git a/app/code/core/Mage/GoogleAnalytics/Block/Ga.php b/app/code/core/Mage/GoogleAnalytics/Block/Ga.php index a0b60e4c098..6c597be5154 100644 --- a/app/code/core/Mage/GoogleAnalytics/Block/Ga.php +++ b/app/code/core/Mage/GoogleAnalytics/Block/Ga.php @@ -242,44 +242,48 @@ protected function _getOrdersTrackingCodeAnalytics4() //Begin advanced eCommerce events //cart actions events - $removedProduct = Mage::getSingleton('core/session')->getRemovedProductCart(); - if ($removedProduct) { - $_removedProduct = Mage::getModel('catalog/product')->load($removedProduct); - $eventData = []; - $eventData['currency'] = Mage::app()->getStore()->getCurrentCurrencyCode(); - $eventData['value'] = number_format($_removedProduct->getFinalPrice(), 2); - $eventData['items'] = []; - $_item = [ - 'item_id' => $_removedProduct->getSku(), - 'item_name' => $_removedProduct->getName(), - 'price' => number_format($_removedProduct->getFinalPrice(), 2), - ]; - if ($_removedProduct->getAttributeText('manufacturer')) { - $_item['item_brand'] = $_removedProduct->getAttributeText('manufacturer'); + $removedProducts = Mage::getSingleton('core/session')->getRemovedProductsCart(); + if ($removedProducts) { + foreach ($removedProducts as $removedProduct) { + $_removedProduct = Mage::getModel('catalog/product')->load($removedProduct); + $eventData = []; + $eventData['currency'] = Mage::app()->getStore()->getCurrentCurrencyCode(); + $eventData['value'] = number_format($_removedProduct->getFinalPrice(), 2); + $eventData['items'] = []; + $_item = [ + 'item_id' => $_removedProduct->getSku(), + 'item_name' => $_removedProduct->getName(), + 'price' => number_format($_removedProduct->getFinalPrice(), 2), + ]; + if ($_removedProduct->getAttributeText('manufacturer')) { + $_item['item_brand'] = $_removedProduct->getAttributeText('manufacturer'); + } + array_push($eventData['items'], $_item); + $result[] = "gtag('event', 'remove_from_cart', " . json_encode($eventData, JSON_THROW_ON_ERROR) . ");"; } - array_push($eventData['items'], $_item); - $result[] = "gtag('event', 'remove_from_cart', " . json_encode($eventData, JSON_THROW_ON_ERROR) . ");"; - Mage::getSingleton('core/session')->unsRemovedProductCart(); + Mage::getSingleton('core/session')->unsRemovedProductsCart(); } - $addedProduct = Mage::getSingleton('core/session')->getAddedProductCart(); - if ($addedProduct) { - $_addedProduct = Mage::getModel('catalog/product')->load($addedProduct); - $eventData = []; - $eventData['currency'] = Mage::app()->getStore()->getCurrentCurrencyCode(); - $eventData['value'] = number_format($_addedProduct->getFinalPrice(), 2); - $eventData['items'] = []; - $_item = [ - 'item_id' => $_addedProduct->getSku(), - 'item_name' => $_addedProduct->getName(), - 'price' => number_format($_addedProduct->getFinalPrice(), 2), - ]; - if ($_addedProduct->getAttributeText('manufacturer')) { - $_item['item_brand'] = $_addedProduct->getAttributeText('manufacturer'); + $addedProducts = Mage::getSingleton('core/session')->getAddedProductsCart(); + if ($addedProducts) { + foreach ($addedProducts as $addedProduct) { + $_addedProduct = Mage::getModel('catalog/product')->load($addedProduct); + $eventData = []; + $eventData['currency'] = Mage::app()->getStore()->getCurrentCurrencyCode(); + $eventData['value'] = number_format($_addedProduct->getFinalPrice(), 2); + $eventData['items'] = []; + $_item = [ + 'item_id' => $_addedProduct->getSku(), + 'item_name' => $_addedProduct->getName(), + 'price' => number_format($_addedProduct->getFinalPrice(), 2), + ]; + if ($_addedProduct->getAttributeText('manufacturer')) { + $_item['item_brand'] = $_addedProduct->getAttributeText('manufacturer'); + } + array_push($eventData['items'], $_item); + $result[] = "gtag('event', 'add_to_cart', " . json_encode($eventData, JSON_THROW_ON_ERROR) . ");"; + Mage::getSingleton('core/session')->unsAddedProductsCart(); } - array_push($eventData['items'], $_item); - $result[] = "gtag('event', 'add_to_cart', " . json_encode($eventData, JSON_THROW_ON_ERROR) . ");"; - Mage::getSingleton('core/session')->unsAddedProductCart(); } //product page diff --git a/app/code/core/Mage/GoogleAnalytics/Model/Observer.php b/app/code/core/Mage/GoogleAnalytics/Model/Observer.php index 667241f6f59..cbff1f338c0 100644 --- a/app/code/core/Mage/GoogleAnalytics/Model/Observer.php +++ b/app/code/core/Mage/GoogleAnalytics/Model/Observer.php @@ -59,7 +59,10 @@ public function removeItemFromCartGoogleAnalytics(Varien_Event_Observer $observe { $productRemoved = $observer->getEvent()->getQuoteItem()->getProduct(); if ($productRemoved) { - Mage::getSingleton('core/session')->setRemovedProductCart($productRemoved->getId()); + $_removedProducts = Mage::getSingleton('core/session')->getRemovedProductsCart() ?: []; + $_removedProducts[] = $productRemoved->getId(); + $_removedProducts = array_unique($_removedProducts); + Mage::getSingleton('core/session')->setRemovedProductsCart($_removedProducts); } } @@ -72,7 +75,16 @@ public function addItemToCartGoogleAnalytics(Varien_Event_Observer $observer) { $productAdded = $observer->getEvent()->getQuoteItem()->getProduct(); if ($productAdded) { - Mage::getSingleton('core/session')->setAddedProductCart($productAdded->getId()); + /** + * Fix double add to cart for configurable products, skip child product + */ + if ($productAdded->getParentProductId()) { + return; + } + $_addedProducts = Mage::getSingleton('core/session')->getAddedProductsCart() ?: []; + $_addedProducts[] = $productAdded->getParentItem() ? $productAdded->getParentItem()->getId() : $productAdded->getId(); + $_addedProducts = array_unique($_addedProducts); + Mage::getSingleton('core/session')->setAddedProductsCart($_addedProducts); } } } From 1f6f3e7fc20b2362bc6bc10535547374c7055b31 Mon Sep 17 00:00:00 2001 From: Tony Date: Wed, 14 Jun 2023 08:17:13 +0200 Subject: [PATCH 16/20] item_category tracking for ga4 ecommerce events --- .../core/Mage/GoogleAnalytics/Block/Ga.php | 85 ++++++++++++++++--- .../core/Mage/GoogleAnalytics/Helper/Data.php | 11 +++ 2 files changed, 84 insertions(+), 12 deletions(-) diff --git a/app/code/core/Mage/GoogleAnalytics/Block/Ga.php b/app/code/core/Mage/GoogleAnalytics/Block/Ga.php index 6c597be5154..b60b23f902d 100644 --- a/app/code/core/Mage/GoogleAnalytics/Block/Ga.php +++ b/app/code/core/Mage/GoogleAnalytics/Block/Ga.php @@ -55,6 +55,7 @@ public function getAccount() * Get a specific page name (may be customized via layout) * * @return string + * @deprecated */ public function getPageName() { @@ -84,6 +85,7 @@ protected function _getPageTrackingCode($accountId) /** * Render regular page tracking javascript code * + * @link https://developers.google.com/tag-platform/gtagjs/reference * @param string $accountId * @return string */ @@ -111,7 +113,7 @@ protected function _getPageTrackingCodeAnalytics4($accountId) } if ($this->helper('googleanalytics')->isDebugModeEnabled()) { - Mage::log($trackingCode, Zend_Log::DEBUG, 'googleanalytics4.log', true); + $this->helper('googleanalytics')->log($trackingCode); } return $trackingCode; @@ -230,8 +232,10 @@ protected function _getOrdersTrackingCodeUniversal() } /** + * Render information about specified orders and their items + * * @return string - * @throws Mage_Core_Model_Store_Exception + * @throws JsonException */ protected function _getOrdersTrackingCodeAnalytics4() { @@ -240,8 +244,11 @@ protected function _getOrdersTrackingCodeAnalytics4() $moduleName = $request->getModuleName(); $controllerName = $request->getControllerName(); - //Begin advanced eCommerce events - //cart actions events + /** + * This event signifies that an item was removed from a cart. + * + * @link https://developers.google.com/tag-platform/gtagjs/reference/events#remove_from_cart + */ $removedProducts = Mage::getSingleton('core/session')->getRemovedProductsCart(); if ($removedProducts) { foreach ($removedProducts as $removedProduct) { @@ -258,12 +265,22 @@ protected function _getOrdersTrackingCodeAnalytics4() if ($_removedProduct->getAttributeText('manufacturer')) { $_item['item_brand'] = $_removedProduct->getAttributeText('manufacturer'); } + if ($_removedProduct->getCategoryIds()) { + $_lastCat = end($_removedProduct->getCategoryIds()); + $_cat = Mage::getModel('catalog/category')->load($_lastCat); + $_item['item_category'] = $_cat->getName(); + } array_push($eventData['items'], $_item); $result[] = "gtag('event', 'remove_from_cart', " . json_encode($eventData, JSON_THROW_ON_ERROR) . ");"; } Mage::getSingleton('core/session')->unsRemovedProductsCart(); } + /** + * This event signifies that an item was added to a cart for purchase. + * + * @link https://developers.google.com/tag-platform/gtagjs/reference/events#add_to_cart + */ $addedProducts = Mage::getSingleton('core/session')->getAddedProductsCart(); if ($addedProducts) { foreach ($addedProducts as $addedProduct) { @@ -280,13 +297,22 @@ protected function _getOrdersTrackingCodeAnalytics4() if ($_addedProduct->getAttributeText('manufacturer')) { $_item['item_brand'] = $_addedProduct->getAttributeText('manufacturer'); } + if ($_addedProduct->getCategoryIds()) { + $_lastCat = end($_addedProduct->getCategoryIds()); + $_cat = Mage::getModel('catalog/category')->load($_lastCat); + $_item['item_category'] = $_cat->getName(); + } array_push($eventData['items'], $_item); $result[] = "gtag('event', 'add_to_cart', " . json_encode($eventData, JSON_THROW_ON_ERROR) . ");"; Mage::getSingleton('core/session')->unsAddedProductsCart(); } } - //product page + /** + * This event signifies that some content was shown to the user. Use this event to discover the most popular items viewed. + * + * @link https://developers.google.com/tag-platform/gtagjs/reference/events#view_item + */ if ($moduleName == 'catalog' && $controllerName == 'product') { $productViewed = Mage::registry('current_product'); $category = Mage::registry('current_category') ? Mage::registry('current_category')->getName() : false; @@ -305,11 +331,14 @@ protected function _getOrdersTrackingCodeAnalytics4() $_item['item_brand'] = $productViewed->getAttributeText('manufacturer'); } array_push($eventData['items'], $_item); - $result[] = "gtag('event', 'view_item', " . json_encode($eventData, JSON_THROW_ON_ERROR) . ");"; } - //category page + /** + * Log this event when the user has been presented with a list of items of a certain category. + * + * @link https://developers.google.com/tag-platform/gtagjs/reference/events#view_item_list + */ elseif ($moduleName == 'catalog' && $controllerName == 'category') { $layer = Mage::getSingleton('catalog/layer'); $category = $layer->getCurrentCategory(); @@ -351,7 +380,11 @@ protected function _getOrdersTrackingCodeAnalytics4() $result[] = "gtag('event', 'view_item_list', " . json_encode($eventData, JSON_THROW_ON_ERROR) . ");"; } - //cart + /** + * This event signifies that a user viewed their cart. + * + * @link https://developers.google.com/tag-platform/gtagjs/reference/events#view_cart + */ elseif ($moduleName == 'checkout' && $controllerName == 'cart') { $productCollection = Mage::getSingleton('checkout/session')->getQuote()->getAllVisibleItems(); $eventData = []; @@ -369,6 +402,11 @@ protected function _getOrdersTrackingCodeAnalytics4() if ($_product->getAttributeText('manufacturer')) { $_item['item_brand'] = $_product->getAttributeText('manufacturer'); } + if ($_product->getCategoryIds()) { + $_lastCat = end($_product->getCategoryIds()); + $_cat = Mage::getModel('catalog/category')->load($_lastCat); + $_item['item_category'] = $_cat->getName(); + } array_push($eventData['items'], $_item); $eventData['value'] += $_product->getFinalPrice(); } @@ -376,7 +414,11 @@ protected function _getOrdersTrackingCodeAnalytics4() $result[] = "gtag('event', 'view_cart', " . json_encode($eventData, JSON_THROW_ON_ERROR) . ");"; } - //begin checkout + /** + * This event signifies that a user has begun a checkout. + * + * @link https://developers.google.com/tag-platform/gtagjs/reference/events#begin_checkout + */ elseif ($moduleName == 'checkout' && $controllerName == 'onepage') { $productCollection = Mage::getSingleton('checkout/session')->getQuote()->getAllVisibleItems(); if ($productCollection) { @@ -394,6 +436,11 @@ protected function _getOrdersTrackingCodeAnalytics4() if ($_product->getAttributeText('manufacturer')) { $_item['item_brand'] = $_product->getAttributeText('manufacturer'); } + if ($_product->getCategoryIds()) { + $_lastCat = end($_product->getCategoryIds()); + $_cat = Mage::getModel('catalog/category')->load($_lastCat); + $_item['item_category'] = $_cat->getName(); + } array_push($eventData['items'], $_item); $eventData['value'] += $_product->getFinalPrice(); } @@ -402,7 +449,11 @@ protected function _getOrdersTrackingCodeAnalytics4() } } - //purchase events + /** + * This event signifies when one or more items is purchased by a user. + * + * @link https://developers.google.com/tag-platform/gtagjs/reference/events?hl=it#purchase + */ $orderIds = $this->getOrderIds(); if (!empty($orderIds) && is_array($orderIds)) { $collection = Mage::getResourceModel('sales/order_collection') @@ -421,20 +472,30 @@ protected function _getOrdersTrackingCodeAnalytics4() /** @var Mage_Sales_Model_Order_Item $item */ foreach ($order->getAllVisibleItems() as $item) { - $orderData['items'][] = [ + $_item = [ 'item_id' => $item->getSku(), 'item_name' => $item->getName(), 'quantity' => $item->getQtyOrdered(), 'price' => $item->getBasePrice(), 'discount' => $item->getBaseDiscountAmount() ]; + $_product = Mage::getModel('catalog/product')->load($item->getProductId()); + if ($_product->getAttributeText('manufacturer')) { + $_item['item_brand'] = $_product->getAttributeText('manufacturer'); + } + if ($_product->getCategoryIds()) { + $_lastCat = end($_product->getCategoryIds()); + $_cat = Mage::getModel('catalog/category')->load($_lastCat); + $_item['item_category'] = $_cat->getName(); + } + array_push($orderData['items'], $_item); } $result[] = "gtag('event', 'purchase', " . json_encode($orderData, JSON_THROW_ON_ERROR) . ");"; } } if ($this->helper('googleanalytics')->isDebugModeEnabled() && count($result) > 0) { - Mage::log($result, Zend_Log::DEBUG, 'googleanalytics4.log', true); + $this->helper('googleanalytics')->log($result); } return implode("\n", $result); } diff --git a/app/code/core/Mage/GoogleAnalytics/Helper/Data.php b/app/code/core/Mage/GoogleAnalytics/Helper/Data.php index 54339871ee6..d2dc0863660 100644 --- a/app/code/core/Mage/GoogleAnalytics/Helper/Data.php +++ b/app/code/core/Mage/GoogleAnalytics/Helper/Data.php @@ -118,6 +118,17 @@ public function isDebugModeEnabled($store = null) return Mage::getStoreConfigFlag(self::XML_PATH_DEBUG, $store) && Mage::helper('core')->isDevAllowed(); } + /** + * Log debug message + * + * @param string $message + */ + public function log($message = null) + { + $filename = sprintf('google%s.log', Mage::getStoreConfig(self::XML_PATH_TYPE)); + Mage::log($message, Zend_Log::DEBUG, $filename, true); + } + /** * Whether GA IP Anonymization is enabled * From cce899141d3b3cfa7b39b40b53c151e10dd61df3 Mon Sep 17 00:00:00 2001 From: Tony Date: Wed, 14 Jun 2023 08:53:27 +0200 Subject: [PATCH 17/20] PHPCS --- .../core/Mage/GoogleAnalytics/Block/Ga.php | 26 +++++++++---------- .../core/Mage/GoogleAnalytics/Helper/Data.php | 2 +- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/app/code/core/Mage/GoogleAnalytics/Block/Ga.php b/app/code/core/Mage/GoogleAnalytics/Block/Ga.php index b60b23f902d..e62c43e67c7 100644 --- a/app/code/core/Mage/GoogleAnalytics/Block/Ga.php +++ b/app/code/core/Mage/GoogleAnalytics/Block/Ga.php @@ -244,9 +244,9 @@ protected function _getOrdersTrackingCodeAnalytics4() $moduleName = $request->getModuleName(); $controllerName = $request->getControllerName(); - /** + /** * This event signifies that an item was removed from a cart. - * + * * @link https://developers.google.com/tag-platform/gtagjs/reference/events#remove_from_cart */ $removedProducts = Mage::getSingleton('core/session')->getRemovedProductsCart(); @@ -276,9 +276,9 @@ protected function _getOrdersTrackingCodeAnalytics4() Mage::getSingleton('core/session')->unsRemovedProductsCart(); } - /** + /** * This event signifies that an item was added to a cart for purchase. - * + * * @link https://developers.google.com/tag-platform/gtagjs/reference/events#add_to_cart */ $addedProducts = Mage::getSingleton('core/session')->getAddedProductsCart(); @@ -308,9 +308,9 @@ protected function _getOrdersTrackingCodeAnalytics4() } } - /** + /** * This event signifies that some content was shown to the user. Use this event to discover the most popular items viewed. - * + * * @link https://developers.google.com/tag-platform/gtagjs/reference/events#view_item */ if ($moduleName == 'catalog' && $controllerName == 'product') { @@ -334,9 +334,9 @@ protected function _getOrdersTrackingCodeAnalytics4() $result[] = "gtag('event', 'view_item', " . json_encode($eventData, JSON_THROW_ON_ERROR) . ");"; } - /** + /** * Log this event when the user has been presented with a list of items of a certain category. - * + * * @link https://developers.google.com/tag-platform/gtagjs/reference/events#view_item_list */ elseif ($moduleName == 'catalog' && $controllerName == 'category') { @@ -380,9 +380,9 @@ protected function _getOrdersTrackingCodeAnalytics4() $result[] = "gtag('event', 'view_item_list', " . json_encode($eventData, JSON_THROW_ON_ERROR) . ");"; } - /** + /** * This event signifies that a user viewed their cart. - * + * * @link https://developers.google.com/tag-platform/gtagjs/reference/events#view_cart */ elseif ($moduleName == 'checkout' && $controllerName == 'cart') { @@ -414,9 +414,9 @@ protected function _getOrdersTrackingCodeAnalytics4() $result[] = "gtag('event', 'view_cart', " . json_encode($eventData, JSON_THROW_ON_ERROR) . ");"; } - /** + /** * This event signifies that a user has begun a checkout. - * + * * @link https://developers.google.com/tag-platform/gtagjs/reference/events#begin_checkout */ elseif ($moduleName == 'checkout' && $controllerName == 'onepage') { @@ -451,7 +451,7 @@ protected function _getOrdersTrackingCodeAnalytics4() /** * This event signifies when one or more items is purchased by a user. - * + * * @link https://developers.google.com/tag-platform/gtagjs/reference/events?hl=it#purchase */ $orderIds = $this->getOrderIds(); diff --git a/app/code/core/Mage/GoogleAnalytics/Helper/Data.php b/app/code/core/Mage/GoogleAnalytics/Helper/Data.php index d2dc0863660..237d2c2b0ff 100644 --- a/app/code/core/Mage/GoogleAnalytics/Helper/Data.php +++ b/app/code/core/Mage/GoogleAnalytics/Helper/Data.php @@ -119,7 +119,7 @@ public function isDebugModeEnabled($store = null) } /** - * Log debug message + * Log debug message * * @param string $message */ From 5694d3a35d3f9aac6f0cb191730f4bee671c3633 Mon Sep 17 00:00:00 2001 From: Tony Date: Wed, 14 Jun 2023 08:59:44 +0200 Subject: [PATCH 18/20] PHPCS --- app/code/core/Mage/GoogleAnalytics/Block/Ga.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/code/core/Mage/GoogleAnalytics/Block/Ga.php b/app/code/core/Mage/GoogleAnalytics/Block/Ga.php index e62c43e67c7..9e9e9fd4eda 100644 --- a/app/code/core/Mage/GoogleAnalytics/Block/Ga.php +++ b/app/code/core/Mage/GoogleAnalytics/Block/Ga.php @@ -55,7 +55,7 @@ public function getAccount() * Get a specific page name (may be customized via layout) * * @return string - * @deprecated + * @deprecated */ public function getPageName() { @@ -233,7 +233,7 @@ protected function _getOrdersTrackingCodeUniversal() /** * Render information about specified orders and their items - * + * * @return string * @throws JsonException */ From eb3c49d382f1ca8fe73ed6b7b1399143827a51ca Mon Sep 17 00:00:00 2001 From: Fabrizio Balliano Date: Wed, 14 Jun 2023 15:21:40 +0100 Subject: [PATCH 19/20] cosmetic --- app/code/core/Mage/GoogleAnalytics/Model/Observer.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/app/code/core/Mage/GoogleAnalytics/Model/Observer.php b/app/code/core/Mage/GoogleAnalytics/Model/Observer.php index cbff1f338c0..772312819f5 100644 --- a/app/code/core/Mage/GoogleAnalytics/Model/Observer.php +++ b/app/code/core/Mage/GoogleAnalytics/Model/Observer.php @@ -75,9 +75,7 @@ public function addItemToCartGoogleAnalytics(Varien_Event_Observer $observer) { $productAdded = $observer->getEvent()->getQuoteItem()->getProduct(); if ($productAdded) { - /** - * Fix double add to cart for configurable products, skip child product - */ + // Fix double add to cart for configurable products, skip child product if ($productAdded->getParentProductId()) { return; } From c59440497ad684b04146f2b726debabee56a315d Mon Sep 17 00:00:00 2001 From: Fabrizio Balliano Date: Wed, 14 Jun 2023 23:13:33 +0100 Subject: [PATCH 20/20] suggestion by @addison74 --- app/code/core/Mage/GoogleAnalytics/Block/Ga.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/code/core/Mage/GoogleAnalytics/Block/Ga.php b/app/code/core/Mage/GoogleAnalytics/Block/Ga.php index 9e9e9fd4eda..8ff63df5a15 100644 --- a/app/code/core/Mage/GoogleAnalytics/Block/Ga.php +++ b/app/code/core/Mage/GoogleAnalytics/Block/Ga.php @@ -381,7 +381,7 @@ protected function _getOrdersTrackingCodeAnalytics4() } /** - * This event signifies that a user viewed their cart. + * This event signifies that a user viewed his cart. * * @link https://developers.google.com/tag-platform/gtagjs/reference/events#view_cart */