@@ -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