Skip to content

Commit 6531b99

Browse files
committed
Added option to Load GTM Script Before Consent
1 parent 8a3cf06 commit 6531b99

File tree

6 files changed

+98
-56
lines changed

6 files changed

+98
-56
lines changed

Block/Adminhtml/System/Config/Form/ProtectCustomerData.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class ProtectCustomerData extends Field
2222
public function render(AbstractElement $element): string
2323
{
2424
$url = $this->getUrl('*/*/*/section/web');
25-
$comment = 'When enabled, data won\'t be sent to Google, until the customer allows cookies.<br/><br/>
25+
$comment = 'When enabled, data won\'t be sent to Google, until the customer provide consent.<br/><br/>
2626
<strong>Note</strong>, that this option will work only when Cookie Restriction Mode at
2727
<a href="' . $url . '" target="_blank">Stores > Configuration > General > Web > Default Cookie Settings</a>
2828
is enabled.';

Block/GtmCode.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,17 @@ public function isProtectCustomerDataEnabled(): bool
8383
return $this->config->isProtectCustomerDataEnabled();
8484
}
8585

86+
87+
/**
88+
* Retrieve true if gtm script should be loaded before customer provice consent.
89+
*
90+
* @return bool
91+
*/
92+
public function isLoadBeforeConsent(): bool
93+
{
94+
return $this->config->isLoadBeforeConsent();
95+
}
96+
8697
/**
8798
* Get current website ID
8899
*

Model/Config.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ class Config
5353
* Customer data protection regulation config
5454
*/
5555
public const XML_PATH_PROTECT_CUSTOMER_DATA = 'mfgoogletagmanager/customer_data/protect';
56+
public const XML_PATH_LOAD_BEFORE_CONSENT = 'mfgoogletagmanager/customer_data/load_before_consent';
5657

5758
/**
5859
* Speed optimization config
@@ -234,6 +235,19 @@ public function isProtectCustomerDataEnabled(string $storeId = null): bool
234235
$this->isCookieRestrictionModeEnabled($storeId);
235236
}
236237

238+
/**
239+
* Retrieve true if gtm script should be loaded before customer provice consent.
240+
*
241+
* @param string|null $storeId
242+
* @return bool
243+
*/
244+
public function isLoadBeforeConsent(string $storeId = null): bool
245+
{
246+
return (bool)$this->getConfig(self::XML_PATH_LOAD_BEFORE_CONSENT, $storeId) &&
247+
$this->isCookieRestrictionModeEnabled($storeId);
248+
}
249+
250+
237251
/**
238252
* Retrieve true if cookie restriction mode enabled
239253
*

etc/adminhtml/system.xml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,15 @@ To create a new secret, navigate in the
377377
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
378378
<frontend_model>Magefan\GoogleTagManager\Block\Adminhtml\System\Config\Form\ProtectCustomerData</frontend_model>
379379
</field>
380+
<field id="load_before_consent" translate="label comment" type="select" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1" canRestore="1">
381+
<label>Load GTM Script Before Consent</label>
382+
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
383+
<comment>If set to "Yes" Google Tag Manager JavaScript will be loaded before the customer provide consent.
384+
It will still be waiting for consent for sending user data related to advertising and analytics.</comment>
385+
<depends>
386+
<field id="protect">1</field>
387+
</depends>
388+
</field>
380389
</group>
381390
<group id="container" translate="label" type="text" sortOrder="80" showInDefault="1" showInWebsite="1" showInStore="1">
382391
<label>Export Web Container</label>

etc/config.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@
7373
</page_speed_optimization>
7474
<customer_data>
7575
<protect>0</protect>
76+
<load_before_consent>0</load_before_consent>
7677
</customer_data>
7778
</mfgoogletagmanager>
7879
</default>

view/frontend/templates/js_code.phtml

Lines changed: 62 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -31,62 +31,79 @@ if (!isset($escaper)) {
3131
<?php } ?>
3232

3333
<?php if ($block->isProtectCustomerDataEnabled()) { ?>
34+
window.dataLayer = window.dataLayer || [];
35+
function gtag() { dataLayer.push(arguments); }
36+
gtag('consent', 'default', {
37+
'ad_user_data': 'denied',
38+
'ad_personalization': 'denied',
39+
'ad_storage': 'denied',
40+
'analytics_storage': 'denied',
41+
'wait_for_update': 500,
42+
});
43+
44+
<?php if ($block->isLoadBeforeConsent()) { ?>
45+
window.mfGtmLoadBeforeCookieAllowed = true;
46+
mfLoadGtm();
47+
<?php } ?>
3448

35-
window.dataLayer = window.dataLayer || [];
36-
function gtag() { dataLayer.push(arguments); }
37-
gtag('consent', 'default', {
38-
'ad_user_data': 'denied',
39-
'ad_personalization': 'denied',
40-
'ad_storage': 'denied',
41-
'analytics_storage': 'denied',
42-
'wait_for_update': 500,
43-
});
44-
45-
(function () {
46-
function getCookieValue(cookieName) {
47-
let name = cookieName + '=';
48-
let cookieSplit = document.cookie.split(';');
49+
(function () {
50+
function getCookieValue(cookieName) {
51+
let name = cookieName + '=';
52+
let cookieSplit = document.cookie.split(';');
4953

50-
for (let i = 0; i < cookieSplit.length; i++) {
51-
let a = cookieSplit[i];
54+
for (let i = 0; i < cookieSplit.length; i++) {
55+
let a = cookieSplit[i];
5256

53-
while (a.charAt(0) === ' ') {
54-
a = a.substring(1);
55-
}
57+
while (a.charAt(0) === ' ') {
58+
a = a.substring(1);
59+
}
5660

57-
if (a.indexOf(name) === 0) {
58-
return a.substring(name.length, a.length);
61+
if (a.indexOf(name) === 0) {
62+
return a.substring(name.length, a.length);
63+
}
5964
}
65+
return '';
6066
}
61-
return '';
62-
}
6367

64-
function customerDataAllowed() {
65-
let cookie = getCookieValue(
66-
'<?= $escaper->escapeHtml(\Magento\Cookie\Helper\Cookie::IS_USER_ALLOWED_SAVE_COOKIE) ?>'
67-
);
68-
if (cookie) {
69-
cookie = JSON.parse(decodeURIComponent(cookie));
70-
if (cookie[<?= $escaper->escapeHtml($block->getWebsiteId()) ?>]) {
71-
return true;
68+
function customerDataAllowed() {
69+
let cookie = getCookieValue(
70+
'<?= $escaper->escapeHtml(\Magento\Cookie\Helper\Cookie::IS_USER_ALLOWED_SAVE_COOKIE) ?>'
71+
);
72+
if (cookie) {
73+
cookie = JSON.parse(decodeURIComponent(cookie));
74+
if (cookie[<?= $escaper->escapeHtml($block->getWebsiteId()) ?>]) {
75+
return true;
76+
}
7277
}
73-
}
7478

75-
return false
76-
}
79+
return false
80+
}
7781

78-
if (customerDataAllowed()) {
79-
window.mfGtmUserCookiesAllowed = true;
80-
mfLoadGtm();
81-
} else {
82-
let interval = setInterval(function () {
83-
if (!customerDataAllowed()) return;
84-
clearInterval(interval);
82+
function grantConsent()
83+
{
8584
window.mfGtmUserCookiesAllowed = true;
85+
<?php if ($block->isProtectCustomerDataEnabled()) { ?>
86+
gtag('consent', 'update', {
87+
ad_user_data: 'granted',
88+
ad_personalization: 'granted',
89+
ad_storage: 'granted',
90+
analytics_storage: 'granted'
91+
});
92+
<?php } ?>
93+
}
94+
95+
if (customerDataAllowed()) {
96+
grantConsent();
8697
mfLoadGtm();
87-
}, 1000);
88-
}
89-
})();
98+
} else {
99+
let interval = setInterval(function () {
100+
if (!customerDataAllowed()) return;
101+
clearInterval(interval);
102+
grantConsent();
103+
mfLoadGtm();
104+
}, 1000);
105+
}
106+
})();
90107

91108
<?php } else { ?>
92109
window.mfGtmUserCookiesAllowed = true;
@@ -96,20 +113,11 @@ if (!isset($escaper)) {
96113

97114
function mfLoadGtm() {
98115
if (!window.mfGtmUserActionDetected) return false;
99-
if (!window.mfGtmUserCookiesAllowed) return false;
116+
if (!window.mfGtmLoadBeforeCookieAllowed && !window.mfGtmUserCookiesAllowed) return false;
100117

101118
if (window.mfGTMTriedToLoad) return;
102119
window.mfGTMTriedToLoad = true;
103120

104-
<?php if ($block->isProtectCustomerDataEnabled()) { ?>
105-
gtag('consent', 'update', {
106-
ad_user_data: 'granted',
107-
ad_personalization: 'granted',
108-
ad_storage: 'granted',
109-
analytics_storage: 'granted'
110-
});
111-
<?php } ?>
112-
113121
<?php if ('use_public_id' === $block->getConfig()->getInstallGtm()) { ?>
114122
(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
115123
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
@@ -120,6 +128,5 @@ if (!isset($escaper)) {
120128
<?= $block->getGtmScript(); ?>
121129
<?php } ?>
122130
}
123-
124131
</script>
125132
<!-- End Google Tag Manager -->

0 commit comments

Comments
 (0)