Skip to content

Commit

Permalink
Improvements to Google Analytics 4 code (#3342)
Browse files Browse the repository at this point in the history
Co-authored-by: Fabrizio Balliano <fabrizio.balliano@gmail.com>
Co-authored-by: Mohamed ELIDRISSI <67818913+elidrissidev@users.noreply.github.com>
  • Loading branch information
3 people committed Jun 23, 2023
1 parent 080a873 commit b4cee73
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 23 deletions.
46 changes: 23 additions & 23 deletions app/code/core/Mage/GoogleAnalytics/Block/Ga.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) . ");";
Expand Down Expand Up @@ -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) . ");";
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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);
}
Expand Down
17 changes: 17 additions & 0 deletions app/code/core/Mage/GoogleAnalytics/Helper/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 '';
}
}

0 comments on commit b4cee73

Please sign in to comment.