Skip to content

Commit 11b20bd

Browse files
committed
Refactored last merge request
1 parent ca95f1e commit 11b20bd

File tree

2 files changed

+39
-38
lines changed

2 files changed

+39
-38
lines changed

etc/adminhtml/system.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ height="0" width="0" style="display:none;visibility:hid
169169
<label>Page Speed Optimization</label>
170170
<field id="enabled" translate="label comment" type="select" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1" canRestore="1">
171171
<label>Enable Deferred Script Load</label>
172-
<comment>If enabled Facebook Pixel JavaScript will be loaded only after users first click, scroll, or mouse touch. It is recommended to keep it enabled for better page speed performance.</comment>
172+
<comment>If enabled Google Tag Manager JavaScript will be loaded only after users first click, scroll, or mouse touch. You can enable it for better page speed performance.</comment>
173173
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
174174
</field>
175175
</group>

view/frontend/templates/js_code.phtml

Lines changed: 38 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)