Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improvements to Google Analytics 4 code #3342

Merged
merged 32 commits into from
Jun 23, 2023
Merged
Show file tree
Hide file tree
Changes from 25 commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
20f7b9c
Enhanced Google Analytics 4 Integration and Ecommerce Tracking Improv…
empiricompany Jun 8, 2023
a5aa41f
string to float 0.00 value
empiricompany Jun 8, 2023
9ac5f48
PHPCS
fballiano Jun 8, 2023
695f6b3
PHPCS
fballiano Jun 8, 2023
c0c6e75
PHPCS
fballiano Jun 9, 2023
9977ad4
whitespace
fballiano Jun 9, 2023
e15f0e6
remove 3rd party partial support
empiricompany Jun 9, 2023
9fe6da1
case
fballiano Jun 9, 2023
153efca
Update app/code/core/Mage/GoogleAnalytics/etc/system.xml
empiricompany Jun 10, 2023
a13656b
Update app/code/core/Mage/GoogleAnalytics/etc/system.xml
empiricompany Jun 10, 2023
df9db8a
Update app/code/core/Mage/GoogleAnalytics/etc/system.xml
empiricompany Jun 10, 2023
d1f6cf3
add translations
empiricompany Jun 10, 2023
2ff3161
fix item_brand false or null in analytics view
empiricompany Jun 10, 2023
f967f4b
fire cart actions events also if redirect_to_cart is false
empiricompany Jun 10, 2023
bc5b167
catch multi products cart actions
empiricompany Jun 10, 2023
1f6f3e7
item_category tracking for ga4 ecommerce events
empiricompany Jun 14, 2023
cce8991
PHPCS
empiricompany Jun 14, 2023
5694d3a
PHPCS
empiricompany Jun 14, 2023
eb3c49d
cosmetic
fballiano Jun 14, 2023
c594404
suggestion by @addison74
fballiano Jun 14, 2023
1f6d227
Merge branch 'main' into ga4-improvement
fballiano Jun 14, 2023
db60c7b
fix notice PR 3323 and same improvement
empiricompany Jun 19, 2023
1e8eda9
Merge branch 'main' into ga4-improvement
empiricompany Jun 21, 2023
f213763
spaces
empiricompany Jun 21, 2023
5c00f54
refactor variable names
empiricompany Jun 21, 2023
9bc325f
Update app/code/core/Mage/GoogleAnalytics/Helper/Data.php
empiricompany Jun 21, 2023
b66c7f0
Update app/code/core/Mage/GoogleAnalytics/Helper/Data.php
empiricompany Jun 21, 2023
929a1eb
Update app/code/core/Mage/GoogleAnalytics/Block/Ga.php
empiricompany Jun 21, 2023
5056997
fix space
empiricompany Jun 21, 2023
1450557
Merge branch 'ga4-improvement' of https://github.com/empiricompany/ma…
empiricompany Jun 21, 2023
7ad1527
Update app/code/core/Mage/GoogleAnalytics/Block/Ga.php
empiricompany Jun 21, 2023
37b9767
cast int instead of intval()
empiricompany Jun 21, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 24 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,16 @@ protected function _getOrdersTrackingCodeAnalytics4()
'item_id' => $_product->getSku(),
'item_name' => $_product->getName(),
'price' => number_format($_product->getFinalPrice(), 2),

empiricompany marked this conversation as resolved.
Show resolved Hide resolved
'quantity' => intval($productInCart->getQty()),
empiricompany marked this conversation as resolved.
Show resolved Hide resolved
];
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 +437,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 +476,18 @@ protected function _getOrdersTrackingCodeAnalytics4()
$_item = [
'item_id' => $item->getSku(),
'item_name' => $item->getName(),
'quantity' => $item->getQtyOrdered(),
'price' => $item->getBasePrice(),
'discount' => $item->getBaseDiscountAmount()
'quantity' => intval($item->getQtyOrdered()),
empiricompany marked this conversation as resolved.
Show resolved Hide resolved
'price' => number_format($item->getBasePrice(), 2),
elidrissidev marked this conversation as resolved.
Show resolved Hide resolved
'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
16 changes: 16 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,20 @@
{
return Mage::getStoreConfigFlag(self::XML_PATH_USERID, $store);
}

/**
* Returns last category name
*
* @param Mage_Catalog_Model_Product $product
* @return string
*/
public function getLastCategoryName($product)
empiricompany marked this conversation as resolved.
Show resolved Hide resolved
{
$_categoryIds = $product->getCategoryIds();
if ($_categoryIds) {

Check failure on line 152 in app/code/core/Mage/GoogleAnalytics/Helper/Data.php

View workflow job for this annotation

GitHub Actions / PHPStan / Analyze

Method Mage_GoogleAnalytics_Helper_Data::getLastCategoryName() should return string but return statement is missing.
$_lastCat = array_pop($_categoryIds);
$_cat = Mage::getModel('catalog/category')->load($_lastCat);
return $_cat->getName();
}
empiricompany marked this conversation as resolved.
Show resolved Hide resolved
}
}
Loading