Skip to content

Commit ca95f1e

Browse files
authored
Merge pull request #11 from ihormandzyuk/8875-speed-optimization-script
8875 speed optimization script
2 parents 3d5b86a + 7d7b409 commit ca95f1e

File tree

5 files changed

+64
-2
lines changed

5 files changed

+64
-2
lines changed

Block/GtmCode.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,4 +113,14 @@ public function getConfig()
113113
{
114114
return $this->config;
115115
}
116+
117+
/**
118+
* Retrieve true if speed optimization is enabled
119+
*
120+
* @return bool
121+
*/
122+
public function isSpeedOptimizationEnabled(): bool
123+
{
124+
return (bool)$this->config->isSpeedOptimizationEnabled();
125+
}
116126
}

Model/Config.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@ class Config
4848
*/
4949
public const XML_PATH_PROTECT_CUSTOMER_DATA = 'mfgoogletagmanager/customer_data/protect';
5050

51+
/**
52+
* Speed optimization config
53+
*/
54+
public const XML_PATH_SPEED_OPTIMIZATION_ENABLED = 'mfgoogletagmanager/page_speed_optimization/enabled';
55+
5156
/**
5257
* @var ScopeConfigInterface
5358
*/
@@ -247,4 +252,15 @@ public function getConfig(string $path, string $storeId = null)
247252
{
248253
return $this->scopeConfig->getValue($path, ScopeInterface::SCOPE_STORE, $storeId);
249254
}
255+
256+
/**
257+
* Retrieve true if speed optimization is enabled
258+
*
259+
* @param string|null $storeId
260+
* @return bool
261+
*/
262+
public function isSpeedOptimizationEnabled(string $storeId = null): bool
263+
{
264+
return (bool)$this->getConfig(self::XML_PATH_SPEED_OPTIMIZATION_ENABLED, $storeId);
265+
}
250266
}

etc/adminhtml/system.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,14 @@ height="0" width="0" style="display:none;visibility:hid
165165
<comment><![CDATA[<strong class="colorRed">Warning!</strong> Enabling this option may cause the performance impact.]]></comment>
166166
</field>
167167
</group>
168+
<group id="page_speed_optimization" translate="label" type="text" sortOrder="65" showInDefault="1" showInWebsite="1" showInStore="1">
169+
<label>Page Speed Optimization</label>
170+
<field id="enabled" translate="label comment" type="select" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1" canRestore="1">
171+
<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>
173+
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
174+
</field>
175+
</group>
168176
<group id="customer_data" translate="label" type="text" sortOrder="70" showInDefault="1" showInWebsite="1" showInStore="1">
169177
<label>GDPR/CCPA/LGPD (Customer Data Protection Regulation)</label>
170178
<field id="protect" translate="label comment" type="select" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1" canRestore="1">

etc/config.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@
3131
<brand/>
3232
<categories/>
3333
</attributes>
34+
<page_speed_optimization>
35+
<enabled>0</enabled>
36+
</page_speed_optimization>
3437
<customer_data>
3538
<protect>0</protect>
3639
</customer_data>

view/frontend/templates/js_code.phtml

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,36 @@ if (!isset($escaper)) {
1616
<script>
1717
function mfLoadGtm() {
1818
<?php if ('use_public_id' === $block->getConfig()->getInstallGtm()) { ?>
19-
(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
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':
2044
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
2145
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
2246
'https:<?= '/' . '/' ?>www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
23-
})(window,document,'script','dataLayer','<?= $escaper->escapeHtml($block->getPublicId()) ?>');
47+
}(window,document,'script','dataLayer','<?= $escaper->escapeHtml($block->getPublicId()) ?>');
48+
}
2449
<?php } elseif ('use_head_and_body_script' === $block->getConfig()->getInstallGtm()) { ?>
2550
<?= $block->getGtmScript(); ?>
2651
<?php } ?>

0 commit comments

Comments
 (0)