From b4cee7359c755b1c7ca824df6258333f49156982 Mon Sep 17 00:00:00 2001 From: Tony Date: Fri, 23 Jun 2023 11:27:18 +0200 Subject: [PATCH] Improvements to Google Analytics 4 code (#3342) Co-authored-by: Fabrizio Balliano Co-authored-by: Mohamed ELIDRISSI <67818913+elidrissidev@users.noreply.github.com> --- .../core/Mage/GoogleAnalytics/Block/Ga.php | 46 +++++++++---------- .../core/Mage/GoogleAnalytics/Helper/Data.php | 17 +++++++ 2 files changed, 40 insertions(+), 23 deletions(-) diff --git a/app/code/core/Mage/GoogleAnalytics/Block/Ga.php b/app/code/core/Mage/GoogleAnalytics/Block/Ga.php index 8ff63df5a15..62cc5f09e3e 100644 --- a/app/code/core/Mage/GoogleAnalytics/Block/Ga.php +++ b/app/code/core/Mage/GoogleAnalytics/Block/Ga.php @@ -265,10 +265,9 @@ 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(); + $itemCategory = Mage::helper('googleanalytics')->getLastCategoryName($_removedProduct); + if ($itemCategory) { + $_item['item_category'] = $itemCategory; } array_push($eventData['items'], $_item); $result[] = "gtag('event', 'remove_from_cart', " . json_encode($eventData, JSON_THROW_ON_ERROR) . ");"; @@ -297,10 +296,10 @@ 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(); + + $itemCategory = Mage::helper('googleanalytics')->getLastCategoryName($_addedProduct); + if ($itemCategory) { + $_item['item_category'] = $itemCategory; } array_push($eventData['items'], $_item); $result[] = "gtag('event', 'add_to_cart', " . json_encode($eventData, JSON_THROW_ON_ERROR) . ");"; @@ -398,14 +397,15 @@ protected function _getOrdersTrackingCodeAnalytics4() 'item_id' => $_product->getSku(), 'item_name' => $_product->getName(), 'price' => number_format($_product->getFinalPrice(), 2), + 'quantity' => (int) $productInCart->getQty(), ]; 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(); + + $itemCategory = Mage::helper('googleanalytics')->getLastCategoryName($_product); + if ($itemCategory) { + $_item['item_category'] = $itemCategory; } array_push($eventData['items'], $_item); $eventData['value'] += $_product->getFinalPrice(); @@ -436,10 +436,10 @@ 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(); + + $itemCategory = Mage::helper('googleanalytics')->getLastCategoryName($_product); + if ($itemCategory) { + $_item['item_category'] = $itemCategory; } array_push($eventData['items'], $_item); $eventData['value'] += $_product->getFinalPrice(); @@ -475,18 +475,18 @@ protected function _getOrdersTrackingCodeAnalytics4() $_item = [ 'item_id' => $item->getSku(), 'item_name' => $item->getName(), - 'quantity' => $item->getQtyOrdered(), - 'price' => $item->getBasePrice(), - 'discount' => $item->getBaseDiscountAmount() + 'quantity' => (int) $item->getQtyOrdered(), + 'price' => number_format($item->getBasePrice(), 2), + 'discount' => number_format($item->getBaseDiscountAmount(), 2) ]; $_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(); + + $itemCategory = Mage::helper('googleanalytics')->getLastCategoryName($_product); + if ($itemCategory) { + $_item['item_category'] = $itemCategory; } array_push($orderData['items'], $_item); } diff --git a/app/code/core/Mage/GoogleAnalytics/Helper/Data.php b/app/code/core/Mage/GoogleAnalytics/Helper/Data.php index 237d2c2b0ff..7136ea224ec 100644 --- a/app/code/core/Mage/GoogleAnalytics/Helper/Data.php +++ b/app/code/core/Mage/GoogleAnalytics/Helper/Data.php @@ -139,4 +139,21 @@ public function isUserIdEnabled($store = null) { return Mage::getStoreConfigFlag(self::XML_PATH_USERID, $store); } + + /** + * Returns last category name + * + * @param Mage_Catalog_Model_Product $product + * @return string + */ + public function getLastCategoryName($product): string + { + $_categoryIds = $product->getCategoryIds(); + if ($_categoryIds) { + $_lastCat = array_pop($_categoryIds); + $_cat = Mage::getModel('catalog/category')->load($_lastCat); + return $_cat->getName(); + } + return ''; + } }