@@ -14,42 +14,21 @@ if (!isset($escaper)) {
1414?>
1515<!-- Google Tag Manager -->
1616<script>
17- function mfLoadGtm() {
18- <?php if ('use_public_id ' === $ block ->getConfig ()->getInstallGtm ()) { ?>
19- <?php if ($ block ->isSpeedOptimizationEnabled ()) { ?>
20- var actionDetected = false;
21- document.addEventListener('scroll', initGTMPixel);
22- document.addEventListener('mousemove', initGTMPixel);
23- document.addEventListener('touchstart', initGTMPixel);
24- function initGTMPixel()
25- {
26- if (actionDetected) return false;
27- document.removeEventListener('scroll', initGTMPixel);
28- document.removeEventListener('mousemove', initGTMPixel);
29- document.removeEventListener('touchstart', initGTMPixel);
30- actionDetected = true;
31-
32- if (triedToLoadPixel) {
33- mfLoadGTMPixel();
34- }
35- }
36- <?php } else { ?>
37- var actionDetected = true;
38- <?php } ?>
39-
40- function mfLoadGTMPixel() {
41- triedToLoadPixel = true;
42- if (!actionDetected) return false;
43- !function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
44- new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
45- j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
46- 'https:<?= '/ ' . '/ ' ?> www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
47- }(window,document,'script','dataLayer','<?= $ escaper ->escapeHtml ($ block ->getPublicId ()) ?> ');
17+ <?php if ($ block ->isSpeedOptimizationEnabled ()) { ?>
18+ document.addEventListener('scroll', mfGtmUserActionDetect);
19+ document.addEventListener('mousemove', mfGtmUserActionDetect);
20+ document.addEventListener('touchstart', mfGtmUserActionDetect);
21+ function mfGtmUserActionDetect()
22+ {
23+ document.removeEventListener('scroll', mfGtmUserActionDetect);
24+ document.removeEventListener('mousemove', mfGtmUserActionDetect);
25+ document.removeEventListener('touchstart', mfGtmUserActionDetect);
26+ window.mfGtmUserActionDetected = true;
27+ mfLoadGtm();
4828 }
49- <?php } elseif ('use_head_and_body_script ' === $ block ->getConfig ()->getInstallGtm ()) { ?>
50- <?= $ block ->getGtmScript (); ?>
51- <?php } ?>
52- }
29+ <?php } else { ?>
30+ window.mfGtmUserActionDetected = true;
31+ <?php } ?>
5332
5433 <?php if ($ block ->isProtectCustomerDataEnabled ()) { ?>
5534 document.addEventListener('DOMContentLoaded', function () {
@@ -86,19 +65,41 @@ if (!isset($escaper)) {
8665 }
8766
8867 if (customerDataAllowed()) {
68+ window.mfGtmUserCookiesAllowed = true;
8969 mfLoadGtm();
9070 } else {
9171 let interval = setInterval(function () {
9272 if (!customerDataAllowed()) return;
9373 clearInterval(interval);
74+ window.mfGtmUserCookiesAllowed = true;
9475 mfLoadGtm();
9576 }, 1000);
9677 }
9778 });
9879
9980 <?php } else { ?>
100- mfLoadGtm();
81+ window.mfGtmUserCookiesAllowed = true;
82+ mfLoadGtm();
10183 <?php } ?>
10284
85+
86+ function mfLoadGtm() {
87+ if (!window.mfGtmUserActionDetected) return false;
88+ if (!window.mfGtmUserCookiesAllowed) return false;
89+
90+ if (window.mfGTMTriedToLoad) return;
91+ window.mfGTMTriedToLoad = true;
92+
93+ <?php if ('use_public_id ' === $ block ->getConfig ()->getInstallGtm ()) { ?>
94+ (function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
95+ new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
96+ j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
97+ 'https:<?= '/ ' . '/ ' ?> www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
98+ })(window,document,'script','dataLayer','<?= $ escaper ->escapeHtml ($ block ->getPublicId ()) ?> ');
99+ <?php } elseif ('use_head_and_body_script ' === $ block ->getConfig ()->getInstallGtm ()) { ?>
100+ <?= $ block ->getGtmScript (); ?>
101+ <?php } ?>
102+ }
103+
103104</script>
104- <!-- End Google Tag Manager -->
105+ <!-- End Google Tag Manager -->
0 commit comments