Skip to content

Commit 13b84b6

Browse files
committed
Add support for custom domain using GTM JS client
1 parent c3b232b commit 13b84b6

File tree

7 files changed

+121
-10
lines changed

7 files changed

+121
-10
lines changed

Block/DataLayer.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,4 +95,22 @@ public function addJsInHead($store_id = null)
9595
{
9696
return (int) $this->_gtmHelper->addJsInHead($store_id);
9797
}
98+
99+
/**
100+
* @param null $store_id
101+
* @return bool
102+
*/
103+
public function isAdvancedSettingsEnabled($store_id = null)
104+
{
105+
return $this->_gtmHelper->isAdvancedSettingsEnabled($store_id);
106+
}
107+
108+
/**
109+
* @param null $store_id
110+
* @return string
111+
*/
112+
public function getAdvancedSettingsJsCode($store_id = null)
113+
{
114+
return $this->_gtmHelper->getAdvancedSettingsJsCode($store_id);
115+
}
98116
}

Block/GtmCode.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,22 @@ protected function _toHtml()
7272

7373
return parent::_toHtml();
7474
}
75+
76+
/**
77+
* @param null $store_id
78+
* @return bool
79+
*/
80+
public function isAdvancedSettingsEnabled($store_id = null)
81+
{
82+
return $this->_gtmHelper->isAdvancedSettingsEnabled($store_id);
83+
}
84+
85+
/**
86+
* @param null $store_id
87+
* @return string
88+
*/
89+
public function getAdvancedSettingsIframeCode($store_id = null)
90+
{
91+
return $this->_gtmHelper->getAdvancedSettingsIframeCode($store_id);
92+
}
7593
}

Helper/Data.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,45 @@ public function isCategoryLayerEnabled($store_id = null)
219219
);
220220
}
221221

222+
/**
223+
* @param null $store_id
224+
* @return bool
225+
*/
226+
public function isAdvancedSettingsEnabled($store_id = null)
227+
{
228+
return $this->scopeConfig->isSetFlag(
229+
'googletagmanager/advanced_settings/enabled',
230+
ScopeInterface::SCOPE_STORE,
231+
$store_id
232+
);
233+
}
234+
235+
/**
236+
* @param null $store_id
237+
* @return string
238+
*/
239+
public function getAdvancedSettingsJsCode($store_id = null)
240+
{
241+
return $this->scopeConfig->getValue(
242+
'googletagmanager/advanced_settings/js_code',
243+
ScopeInterface::SCOPE_STORE,
244+
$store_id
245+
);
246+
}
247+
248+
/**
249+
* @param null $store_id
250+
* @return string
251+
*/
252+
public function getAdvancedSettingsIframeCode($store_id = null)
253+
{
254+
return $this->scopeConfig->getValue(
255+
'googletagmanager/advanced_settings/iframe_code',
256+
ScopeInterface::SCOPE_STORE,
257+
$store_id
258+
);
259+
}
260+
222261
/**
223262
* @param ProductInterface $product
224263
* @param array $viewItem

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
"magepal/magento2-google-analytics4": "Prepare for the future with Google Analytics 4. Learn more at https://www.magepal.com/google-analytics-4-for-google-tag-manager.html"
4545
},
4646
"type": "magento2-module",
47-
"version": "2.7.4",
47+
"version": "2.7.5",
4848
"autoload": {
4949
"files": [
5050
"registration.php"

etc/adminhtml/system.xml

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,15 @@
3232
that make it very similar, yet more powerful than Enhanced Ecommerce.
3333
Gain access to GA4 new approach to privacy-first tracking, channel measurement, and AI based predictive data with our new Google Analytics 4 extension.
3434
</div>
35+
<div id="upgrade-to-datalayer">
36+
Adding Facebook Pixel, Bing UET, or other third-party JavaScript to your site using Google Tag Manager?
37+
Learn how simple and easy it is to integrate any third party service with our new <a href="http://bit.ly/MPE-DLGTM">DataLayer extension</a>.
38+
</div>
3539
<div id="upgrade-to-enhanced-ecommerce">
3640
Want to learn more about your customers? Gain valuable insight on your customers shopping behavior, sales performance and more.
3741
Upgrade to our new <a href="http://bit.ly/MPE-EEGTM">Enhanced E-commerce</a> today,
3842
to take full advantage of Google Analytics most valuable features and reports.
3943
</div>
40-
<div id="upgrade-to-datalayer">
41-
Adding Facebook Pixel, Bing UET, or other third-party JavaScript to your site using Google Tag Manager?
42-
Learn how simple and easy it is to integrate any third party service with our new <a href="http://bit.ly/MPE-DLGTM">DataLayer extension</a>.
43-
</div>
4444
<hr class="magepal-hr" />
4545
]]>
4646
</comment>
@@ -99,9 +99,7 @@
9999
<depends>
100100
<field id="*/*/enabled">0</field>
101101
</depends>
102-
<comment>
103-
Note: Data Layer event firing Order may change
104-
</comment>
102+
<comment>Note: Data Layer events firing sequence may change.</comment>
105103
</field>
106104
<field id="option" translate="label" type="select" sortOrder="20" showInDefault="1" showInWebsite="1" showInStore="1">
107105
<label>Options</label>
@@ -128,6 +126,36 @@
128126
</depends>
129127
</field>
130128
</group>
129+
<group id="advanced_settings" translate="label" type="text" sortOrder="12" showInDefault="1" showInWebsite="1" showInStore="1">
130+
<label>Advanced Settings</label>
131+
<comment><![CDATA[Utilize advanced settings to set up the most sophisticated Google Tag Manager configuration for Multiple Environments & Custom Domain.]]></comment>
132+
<field id="enabled" translate="label" type="select" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
133+
<label>Enable Advanced Settings</label>
134+
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
135+
</field>
136+
137+
<field id="js_code" translate="label" type="textarea" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
138+
<label>Google Tag Manager JavaScript Code</label>
139+
<comment>
140+
<![CDATA[Copy and Paste the entire Google Tag Manager JavaScript Code.]]>
141+
</comment>
142+
<validate>required-entry</validate>
143+
<depends>
144+
<field id="*/*/enabled">1</field>
145+
</depends>
146+
</field>
147+
148+
<field id="iframe_code" translate="label" type="textarea" sortOrder="20" showInDefault="1" showInWebsite="1" showInStore="1">
149+
<label>Google Tag Manager iFrame Code</label>
150+
<comment>
151+
<![CDATA[Copy and Paste the entire Google Tag Manager iFrame HTML Code.]]>
152+
</comment>
153+
<validate>required-entry</validate>
154+
<depends>
155+
<field id="*/*/enabled">1</field>
156+
</depends>
157+
</field>
158+
</group>
131159
<group id="gtm_container" translate="label" type="text" sortOrder="12" showInDefault="1" showInWebsite="1" showInStore="1">
132160
<label>GTM Container</label>
133161
<field id="enabled" translate="label" type="select" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">

view/frontend/templates/iframe.phtml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
?>
1010

1111
<!-- Google Tag Manager by MagePal -->
12+
<?php if (!$block->isAdvancedSettingsEnabled()): ?>
1213
<noscript><iframe src="<?= '/' . '/' ?>www.googletagmanager.com/ns.html?id=<?= /* @noEscape */ $block->getEmbeddedAccountId() ?>"
1314
height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
15+
<?php else: ?>
16+
<?= /* @noEscape */ $block->getAdvancedSettingsIframeCode() ?>
17+
<?php endif; ?>
1418
<!-- End Google Tag Manager by MagePal -->

view/frontend/templates/js.phtml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ $containerCode = $block->getEmbeddedCode();
1515
<script type="text/javascript">
1616
window.<?= /* @noEscape */ $dataLayerName ?> = window.<?= /* @noEscape */ $dataLayerName ?> || [];
1717

18-
<?php if (!$block->isGdprEnabled() && $block->addJsInHead()): ?>
18+
<?php if (!$block->isGdprEnabled() && $block->addJsInHead() && !$block->isAdvancedSettingsEnabled()): ?>
1919
<?= /* @noEscape */ $block->getDataLayerJs() ?>
2020
(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
2121
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
@@ -25,7 +25,11 @@ $containerCode = $block->getEmbeddedCode();
2525
<?php endif; ?>
2626
</script>
2727

28-
<?php if ($block->isGdprEnabled() || !$block->addJsInHead()): ?>
28+
<?php if ($block->isAdvancedSettingsEnabled()): ?>
29+
<?= /* @noEscape */ $block->getAdvancedSettingsJsCode() ?>
30+
<?php endif; ?>
31+
32+
<?php if (($block->isGdprEnabled() || !$block->addJsInHead()) && !$block->isAdvancedSettingsEnabled()) : ?>
2933
<script type="text/x-magento-init">
3034
{
3135
"*": {

0 commit comments

Comments
 (0)