Skip to content

Commit f703211

Browse files
Enhanced Google Analytics 4 Integration and Ecommerce Tracking Improvements (OpenMage#3305)
Co-authored-by: Fabrizio Balliano <[email protected]>
1 parent 6391ed9 commit f703211

File tree

6 files changed

+314
-111
lines changed

6 files changed

+314
-111
lines changed

app/code/core/Mage/GoogleAnalytics/Block/Ga.php

Lines changed: 235 additions & 107 deletions
Large diffs are not rendered by default.

app/code/core/Mage/GoogleAnalytics/Helper/Data.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ class Mage_GoogleAnalytics_Helper_Data extends Mage_Core_Helper_Abstract
2828
public const XML_PATH_TYPE = 'google/analytics/type';
2929
public const XML_PATH_ACCOUNT = 'google/analytics/account';
3030
public const XML_PATH_ANONYMIZATION = 'google/analytics/anonymization';
31+
public const XML_PATH_DEBUG = 'google/analytics/debug';
32+
public const XML_PATH_USERID = 'google/analytics/user_id';
3133

3234
/**
3335
* @var string google analytics 4
@@ -104,4 +106,37 @@ public function isUseAnalytics4($store = null)
104106
{
105107
return Mage::getStoreConfig(self::XML_PATH_TYPE, $store) == self::TYPE_ANALYTICS4;
106108
}
109+
110+
/**
111+
* Whether GA Debug Mode is enabled (only for development IP)
112+
*
113+
* @param null $store
114+
* @return bool
115+
*/
116+
public function isDebugModeEnabled($store = null)
117+
{
118+
return Mage::getStoreConfigFlag(self::XML_PATH_DEBUG, $store) && Mage::helper('core')->isDevAllowed();
119+
}
120+
121+
/**
122+
* Log debug message
123+
*
124+
* @param string $message
125+
*/
126+
public function log($message = null)
127+
{
128+
$filename = sprintf('google%s.log', Mage::getStoreConfig(self::XML_PATH_TYPE));
129+
Mage::log($message, Zend_Log::DEBUG, $filename, true);
130+
}
131+
132+
/**
133+
* Whether GA IP Anonymization is enabled
134+
*
135+
* @param null $store
136+
* @return bool
137+
*/
138+
public function isUserIdEnabled($store = null)
139+
{
140+
return Mage::getStoreConfigFlag(self::XML_PATH_USERID, $store);
141+
}
107142
}

app/code/core/Mage/GoogleAnalytics/Model/Observer.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,10 @@ public function removeItemFromCartGoogleAnalytics(Varien_Event_Observer $observe
5959
{
6060
$productRemoved = $observer->getEvent()->getQuoteItem()->getProduct();
6161
if ($productRemoved) {
62-
Mage::getSingleton('core/session')->setRemovedProductCart($productRemoved->getId());
62+
$_removedProducts = Mage::getSingleton('core/session')->getRemovedProductsCart() ?: [];
63+
$_removedProducts[] = $productRemoved->getId();
64+
$_removedProducts = array_unique($_removedProducts);
65+
Mage::getSingleton('core/session')->setRemovedProductsCart($_removedProducts);
6366
}
6467
}
6568

@@ -72,7 +75,14 @@ public function addItemToCartGoogleAnalytics(Varien_Event_Observer $observer)
7275
{
7376
$productAdded = $observer->getEvent()->getQuoteItem()->getProduct();
7477
if ($productAdded) {
75-
Mage::getSingleton('core/session')->setAddedProductCart($productAdded->getId());
78+
// Fix double add to cart for configurable products, skip child product
79+
if ($productAdded->getParentProductId()) {
80+
return;
81+
}
82+
$_addedProducts = Mage::getSingleton('core/session')->getAddedProductsCart() ?: [];
83+
$_addedProducts[] = $productAdded->getParentItem() ? $productAdded->getParentItem()->getId() : $productAdded->getId();
84+
$_addedProducts = array_unique($_addedProducts);
85+
Mage::getSingleton('core/session')->setAddedProductsCart($_addedProducts);
7686
}
7787
}
7888
}

app/code/core/Mage/GoogleAnalytics/etc/system.xml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,32 @@
5757
<show_in_website>1</show_in_website>
5858
<show_in_store>1</show_in_store>
5959
</account>
60+
<user_id translate="label comment">
61+
<label>User ID tracking</label>
62+
<frontend_type>select</frontend_type>
63+
<source_model>adminhtml/system_config_source_yesno</source_model>
64+
<sort_order>21</sort_order>
65+
<show_in_default>1</show_in_default>
66+
<show_in_website>1</show_in_website>
67+
<show_in_store>1</show_in_store>
68+
<comment>Enable GA4 User_id tracking for logged in customers.</comment>
69+
<depends>
70+
<type>analytics4</type>
71+
</depends>
72+
</user_id>
73+
<debug translate="label comment">
74+
<label>Debug</label>
75+
<frontend_type>select</frontend_type>
76+
<source_model>adminhtml/system_config_source_yesno</source_model>
77+
<sort_order>22</sort_order>
78+
<show_in_default>1</show_in_default>
79+
<show_in_website>1</show_in_website>
80+
<show_in_store>1</show_in_store>
81+
<comment>Enable GA4 Debug Real Time view for Development IP.</comment>
82+
<depends>
83+
<type>analytics4</type>
84+
</depends>
85+
</debug>
6086
<anonymization translate="label">
6187
<label>Enable IP anonymization</label>
6288
<frontend_type>select</frontend_type>
@@ -65,6 +91,9 @@
6591
<show_in_default>1</show_in_default>
6692
<show_in_website>1</show_in_website>
6793
<show_in_store>1</show_in_store>
94+
<depends>
95+
<type>universal</type>
96+
</depends>
6897
</anonymization>
6998
</fields>
7099
</analytics>

app/design/frontend/base/default/template/googleanalytics/ga.phtml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@ $_accountId = $_helper->getAccountId();
2525
<script>
2626
window.dataLayer = window.dataLayer || [];
2727
function gtag(){dataLayer.push(arguments);}
28-
gtag('js', new Date());
29-
gtag('config', '<?= $_accountId ?>');
28+
<?php echo $this->_getPageTrackingCode($_accountId) ?>
3029
<?php echo $this->_getOrdersTrackingCodeAnalytics4() ?>
3130
</script>
3231
<!-- END GOOGLE ANALYTICS 4 CODE -->

app/locale/en_US/Mage_GoogleAnalytics.csv

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,5 @@
66
"Type","Type"
77
"Universal Analytics","Universal Analytics"
88
"Google Analytics 4","Google Analytics 4"
9+
"Enable GA4 User_id tracking for logged in customers.","Enable GA4 User_id tracking for logged in customers."
10+
"Enable GA4 Debug Real Time view for Development IP.","Enable GA4 Debug Real Time view for Development IP."

0 commit comments

Comments
 (0)