Skip to content

Commit 8bb21fa

Browse files
authored
Added support for Google Tag Manager injection, compatible with Analytics 4 and enhanced ecommerce data (#3356)
1 parent e7e10cf commit 8bb21fa

File tree

7 files changed

+146
-55
lines changed

7 files changed

+146
-55
lines changed

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

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -109,15 +109,10 @@ protected function _getPageTrackingCodeAnalytics($accountId)
109109
*
110110
* @return string
111111
* @throws Mage_Core_Model_Store_Exception
112+
* @deprecated
112113
*/
113114
protected function _getOrdersTrackingCode()
114115
{
115-
/** @var Mage_GoogleAnalytics_Helper_Data $helper */
116-
$helper = $this->helper('googleanalytics');
117-
if ($helper->isUseAnalytics4()) {
118-
return $this->_getOrdersTrackingCodeAnalytics4();
119-
}
120-
121116
return '';
122117
}
123118

@@ -127,7 +122,7 @@ protected function _getOrdersTrackingCode()
127122
* @return string
128123
* @throws JsonException
129124
*/
130-
protected function _getOrdersTrackingCodeAnalytics4()
125+
protected function _getEnhancedEcommerceDataForAnalytics4()
131126
{
132127
$result = [];
133128
$request = $this->getRequest();
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
/**
3+
* OpenMage
4+
*
5+
* This source file is subject to the Open Software License (OSL 3.0)
6+
* that is bundled with this package in the file LICENSE.txt.
7+
* It is also available at https://opensource.org/license/osl-3-0-php
8+
*
9+
* @category Mage
10+
* @package Mage_GoogleAnalytics
11+
* @copyright Copyright (c) 2006-2020 Magento, Inc. (https://www.magento.com)
12+
* @copyright Copyright (c) 2022 The OpenMage Contributors (https://www.openmage.org)
13+
* @license https://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
14+
*/
15+
16+
/**
17+
* GoogleTagManager Page Block
18+
*
19+
* @category Mage
20+
* @package Mage_GoogleAnalytics
21+
*/
22+
class Mage_GoogleAnalytics_Block_Gtm extends Mage_Core_Block_Template
23+
{
24+
/**
25+
* @return bool
26+
*/
27+
protected function _isAvailable()
28+
{
29+
return Mage::helper('googleanalytics')->isGoogleTagManagerAvailable();
30+
}
31+
32+
/**
33+
* Render GA tracking scripts
34+
*
35+
* @return string
36+
*/
37+
protected function _toHtml()
38+
{
39+
if (!$this->_isAvailable()) {
40+
return '';
41+
}
42+
return parent::_toHtml();
43+
}
44+
}

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

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ class Mage_GoogleAnalytics_Helper_Data extends Mage_Core_Helper_Abstract
3030
public const XML_PATH_DEBUG = 'google/analytics/debug';
3131
public const XML_PATH_USERID = 'google/analytics/user_id';
3232

33+
public const XML_PATH_GTM_ACTIVE = 'google/gtm/active';
34+
public const XML_PATH_GTM_CONTAINER_ID = 'google/gtm/container_id';
35+
3336
/**
3437
* @var string google analytics 4
3538
*/
@@ -40,6 +43,18 @@ class Mage_GoogleAnalytics_Helper_Data extends Mage_Core_Helper_Abstract
4043
*/
4144
protected $_moduleName = 'Mage_GoogleAnalytics';
4245

46+
/**
47+
* Whether GTM is ready to use
48+
*
49+
* @param mixed $store
50+
* @return bool
51+
*/
52+
public function isGoogleTagManagerAvailable($store = null)
53+
{
54+
$containerId = Mage::getStoreConfig(self::XML_PATH_GTM_CONTAINER_ID, $store);
55+
return $containerId && Mage::getStoreConfigFlag(self::XML_PATH_GTM_ACTIVE, $store);
56+
}
57+
4358
/**
4459
* Whether GA is ready to use
4560
*
@@ -75,6 +90,17 @@ public function getAccountId($store = null)
7590
return Mage::getStoreConfig(self::XML_PATH_ACCOUNT, $store);
7691
}
7792

93+
/**
94+
* Get GTM account id
95+
*
96+
* @param string $store
97+
* @return string
98+
*/
99+
public function getGoogleTagManagerContainerId($store = null)
100+
{
101+
return Mage::getStoreConfig(self::XML_PATH_GTM_CONTAINER_ID, $store);
102+
}
103+
78104
/**
79105
* Returns true if should use Google Universal Analytics
80106
*
@@ -87,6 +113,17 @@ public function isUseUniversalAnalytics($store = null)
87113
return false;
88114
}
89115

116+
/**
117+
* Returns true if should use Google Tag Manager
118+
*
119+
* @param string $store
120+
* @return bool
121+
*/
122+
public function isUseGoogleTagManager($store = null)
123+
{
124+
return Mage::getStoreConfigFlag(self::XML_PATH_GTM_ACTIVE, $store);
125+
}
126+
90127
/**
91128
* Returns true if should use Google Universal Analytics 4
92129
*

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

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,31 @@
2424
<show_in_website>1</show_in_website>
2525
<show_in_store>1</show_in_store>
2626
<groups>
27+
<gtm translate="label">
28+
<label>Google Tag Manager</label>
29+
<sort_order>10</sort_order>
30+
<show_in_default>1</show_in_default>
31+
<show_in_website>1</show_in_website>
32+
<show_in_store>1</show_in_store>
33+
<fields>
34+
<active translate="label">
35+
<label>Enable</label>
36+
<frontend_type>select</frontend_type>
37+
<source_model>adminhtml/system_config_source_yesno</source_model>
38+
<sort_order>10</sort_order>
39+
<show_in_default>1</show_in_default>
40+
<show_in_website>1</show_in_website>
41+
<show_in_store>1</show_in_store>
42+
</active>
43+
<container_id translate="label">
44+
<label>Container ID</label>
45+
<sort_order>20</sort_order>
46+
<show_in_default>1</show_in_default>
47+
<show_in_website>1</show_in_website>
48+
<show_in_store>1</show_in_store>
49+
</container_id>
50+
</fields>
51+
</gtm>
2752
<analytics translate="label">
2853
<label>Google Analytics</label>
2954
<sort_order>10</sort_order>
@@ -83,18 +108,6 @@
83108
<type>analytics4</type>
84109
</depends>
85110
</debug>
86-
<anonymization translate="label">
87-
<label>Enable IP anonymization</label>
88-
<frontend_type>select</frontend_type>
89-
<source_model>adminhtml/system_config_source_yesno</source_model>
90-
<sort_order>30</sort_order>
91-
<show_in_default>1</show_in_default>
92-
<show_in_website>1</show_in_website>
93-
<show_in_store>1</show_in_store>
94-
<depends>
95-
<type>universal</type>
96-
</depends>
97-
</anonymization>
98111
</fields>
99112
</analytics>
100113
</groups>

app/design/frontend/base/default/layout/googleanalytics.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ Default layout, loads most of the pages
2323
<default>
2424
<!-- Mage_GoogleAnalytics -->
2525
<reference name="head" before="-">
26+
<block type="googleanalytics/gtm" name="google_tag_manager" as="google_tag_manager" template="googleanalytics/gtm.phtml" />
2627
<block type="googleanalytics/ga" name="google_analytics" as="google_analytics" template="googleanalytics/ga.phtml" />
2728
</reference>
2829
</default>

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

Lines changed: 4 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -16,50 +16,18 @@
1616
<?php
1717
/** @var Mage_GoogleAnalytics_Block_Ga $this */
1818
$_helper = $this->helper('googleanalytics');
19-
$_accountId = $_helper->getAccountId();
2019
?>
2120
<?php if (!$this->helper('core/cookie')->isUserNotAllowSaveCookie()): ?>
2221
<?php if ($_helper->isUseAnalytics4()): ?>
22+
<?php $_gaAccountId = $_helper->getAccountId() ?>
2323
<!-- BEGIN GOOGLE ANALYTICS 4 CODE -->
24-
<script async src="https://www.googletagmanager.com/gtag/js?id=<?= $_accountId ?>"></script>
24+
<script async src="https://www.googletagmanager.com/gtag/js?id=<?= $_gaAccountId ?>"></script>
2525
<script>
2626
window.dataLayer = window.dataLayer || [];
2727
function gtag(){dataLayer.push(arguments);}
28-
<?php echo $this->_getPageTrackingCode($_accountId) ?>
29-
<?php echo $this->_getOrdersTrackingCodeAnalytics4() ?>
28+
<?php echo $this->_getPageTrackingCode($_gaAccountId) ?>
29+
<?php echo $this->_getEnhancedEcommerceDataForAnalytics4() ?>
3030
</script>
3131
<!-- END GOOGLE ANALYTICS 4 CODE -->
32-
<?php elseif ($_helper->isUseUniversalAnalytics()): ?>
33-
<!-- BEGIN GOOGLE UNIVERSAL ANALYTICS CODE -->
34-
<script type="text/javascript">
35-
//<![CDATA[
36-
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
37-
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
38-
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
39-
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
40-
41-
<?php echo $this->_getPageTrackingCode($_accountId) ?>
42-
<?php echo $this->_getOrdersTrackingCode() ?>
43-
44-
//]]>
45-
</script>
46-
<!-- END GOOGLE UNIVERSAL ANALYTICS CODE -->
47-
<?php else: ?>
48-
<!-- BEGIN GOOGLE ANALYTICS CODE -->
49-
<script type="text/javascript">
50-
//<![CDATA[
51-
var _gaq = _gaq || [];
52-
<?php echo $this->_getPageTrackingCode($_accountId) ?>
53-
<?php echo $this->_getOrdersTrackingCode() ?>
54-
55-
(function() {
56-
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
57-
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
58-
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
59-
})();
60-
61-
//]]>
62-
</script>
63-
<!-- END GOOGLE ANALYTICS CODE -->
6432
<?php endif ?>
6533
<?php endif ?>
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
/**
3+
* OpenMage
4+
*
5+
* This source file is subject to the Academic Free License (AFL 3.0)
6+
* that is bundled with this package in the file LICENSE_AFL.txt.
7+
* It is also available at https://opensource.org/license/afl-3-0-php
8+
*
9+
* @category design
10+
* @package base_default
11+
* @copyright Copyright (c) 2006-2020 Magento, Inc. (https://www.magento.com)
12+
* @copyright Copyright (c) 2021 The OpenMage Contributors (https://www.openmage.org)
13+
* @license https://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
14+
*/
15+
?>
16+
<?php
17+
/** @var Mage_GoogleAnalytics_Block_Gtm $this */
18+
$_helper = $this->helper('googleanalytics');
19+
?>
20+
<?php if (!$this->helper('core/cookie')->isUserNotAllowSaveCookie()): ?>
21+
<?php if ($_helper->isUseGoogleTagManager()): ?>
22+
<?php $_gtmAccountId = $_helper->getGoogleTagManagerContainerId() ?>
23+
<!-- BEGIN GOOGLE TAG MANAGER CODE -->
24+
<script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
25+
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
26+
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
27+
'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
28+
})(window,document,'script','dataLayer','<?= $_gtmAccountId ?>');</script>
29+
<noscript><iframe src="https://www.googletagmanager.com/ns.html?id=<?= $_gtmAccountId ?>"
30+
height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
31+
<!-- END GOOGLE TAG MANAGER CODE -->
32+
<?php endif ?>
33+
<?php endif ?>

0 commit comments

Comments
 (0)