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

Enhanced Google Analytics 4 Integration and Ecommerce Tracking Improvements #3305

Merged
merged 21 commits into from
Jun 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
342 changes: 235 additions & 107 deletions app/code/core/Mage/GoogleAnalytics/Block/Ga.php

Large diffs are not rendered by default.

35 changes: 35 additions & 0 deletions app/code/core/Mage/GoogleAnalytics/Helper/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -104,4 +106,37 @@ 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();
}

/**
* 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
*
* @param null $store
* @return bool
*/
public function isUserIdEnabled($store = null)
{
return Mage::getStoreConfigFlag(self::XML_PATH_USERID, $store);
}
}
14 changes: 12 additions & 2 deletions app/code/core/Mage/GoogleAnalytics/Model/Observer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand All @@ -72,7 +75,14 @@ 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);
}
}
}
29 changes: 29 additions & 0 deletions app/code/core/Mage/GoogleAnalytics/etc/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,32 @@
<show_in_website>1</show_in_website>
<show_in_store>1</show_in_store>
</account>
<user_id translate="label comment">
<label>User ID tracking</label>
<frontend_type>select</frontend_type>
<source_model>adminhtml/system_config_source_yesno</source_model>
<sort_order>21</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>1</show_in_store>
<comment>Enable GA4 User_id tracking for logged in customers.</comment>
<depends>
<type>analytics4</type>
</depends>
</user_id>
<debug translate="label comment">
<label>Debug</label>
<frontend_type>select</frontend_type>
<source_model>adminhtml/system_config_source_yesno</source_model>
<sort_order>22</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>1</show_in_store>
<comment>Enable GA4 Debug Real Time view for Development IP.</comment>
<depends>
<type>analytics4</type>
</depends>
</debug>
<anonymization translate="label">
<label>Enable IP anonymization</label>
<frontend_type>select</frontend_type>
Expand All @@ -65,6 +91,9 @@
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>1</show_in_store>
<depends>
<type>universal</type>
</depends>
</anonymization>
</fields>
</analytics>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ $_accountId = $_helper->getAccountId();
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', '<?= $_accountId ?>');
<?php echo $this->_getPageTrackingCode($_accountId) ?>
<?php echo $this->_getOrdersTrackingCodeAnalytics4() ?>
</script>
<!-- END GOOGLE ANALYTICS 4 CODE -->
Expand Down
2 changes: 2 additions & 0 deletions app/locale/en_US/Mage_GoogleAnalytics.csv
Original file line number Diff line number Diff line change
Expand Up @@ -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."