Skip to content

Commit 0587b9a

Browse files
committed
Code refactor
1 parent bcff704 commit 0587b9a

File tree

9 files changed

+59
-42
lines changed

9 files changed

+59
-42
lines changed

Block/GtmCode.php

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,22 @@ public function __construct(
3939
*/
4040
public function getGtmScript(): string
4141
{
42-
return $this->config->getGtmScript();
42+
$partsForRemove = [
43+
'<!-- Google Tag Manager -->',
44+
'<!-- End Google Tag Manager -->',
45+
'<script>',
46+
'</script>'
47+
];
48+
$gtmScript = $this->config->getGtmScript();
49+
if ($gtmScript) {
50+
foreach ($partsForRemove as $part) {
51+
$gtmScript = str_replace($part, '', $gtmScript);
52+
}
53+
54+
return $gtmScript;
55+
}
56+
57+
return '';
4358
}
4459

4560
/**
@@ -58,30 +73,6 @@ public function getPublicId(): string
5873
return $this->config->getPublicId();
5974
}
6075

61-
/**
62-
* @param string|null $storeId
63-
* @return string
64-
*/
65-
public function getFormattedGtmScript(string $storeId = null): string
66-
{
67-
$partsForRemove = [
68-
'<!-- Google Tag Manager -->',
69-
'<!-- End Google Tag Manager -->',
70-
'<script>',
71-
'</script>'
72-
];
73-
$gtmScript = $this->getGtmScript();
74-
if ($gtmScript) {
75-
foreach ($partsForRemove as $part) {
76-
$gtmScript = str_replace($part, '', $gtmScript);
77-
}
78-
79-
return $gtmScript;
80-
}
81-
82-
return '';
83-
}
84-
8576
/**
8677
* Check if protect customer data is enabled
8778
*

Model/AbstractDataLayer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ protected function getProductAttributeValue(Product $product, ?string $attribute
236236
protected function getCustomerGroupCode(): string
237237
{
238238
if (null === $this->customerGroupCode) {
239-
$this->customerGroupCode = '';
239+
$this->customerGroupCode = 'Guest';
240240
$customerGroupId = $this->session->getCustomerGroupId();
241241
if ($customerGroupId) {
242242
try {

Model/Config.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,8 @@ public function __construct(
7373
public function isEnabled(string $storeId = null): bool
7474
{
7575
return (bool)$this->getConfig(self::XML_PATH_EXTENSION_ENABLED, $storeId) && (
76-
$this->getPublicId($storeId)
77-
|| $this->getGtmScript($storeId)
76+
($this->getPublicId($storeId) && 'use_public_id' === $this->getInstallGtm($storeId))
77+
|| ($this->getGtmScript($storeId) && 'use_head_and_body_script' === $this->getInstallGtm($storeId))
7878
);
7979
}
8080

Model/Config/Source/InstallGtmOptions.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ class InstallGtmOptions implements OptionSourceInterface
2626
public function toOptionArray(): array
2727
{
2828
if ($this->options === null) {
29-
$this->options[] = ['value' => 'use_public_id', 'label' => __('Using Public ID')];
30-
$this->options[] = ['value' => 'use_head_and_body_script', 'label' => __('Using Head Script and Body Noscript')];
29+
$this->options[] = ['value' => 'use_public_id', 'label' => __('Public ID')];
30+
$this->options[] = ['value' => 'use_head_and_body_script', 'label' => __('Head Script and Body Noscript')];
3131
}
3232

3333
return $this->options;

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"email": "[email protected]"
1010
}
1111
],
12-
"version": "2.2.0",
12+
"version": "2.3.0",
1313
"require": {
1414
"magefan/module-community" : ">=2.1.16"
1515
},

etc/adminhtml/system.xml

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,15 @@
4040
</field>
4141
-->
4242
<field id="install_gtm" translate="label comment" type="select" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1" canRestore="1">
43-
<label>Install Google Tag Manager</label>
43+
<label>Google Tag Manager Installation Method</label>
4444
<source_model>Magefan\GoogleTagManager\Model\Config\Source\InstallGtmOptions</source_model>
4545
</field>
4646
<field id="public_id" translate="label comment" type="text" sortOrder="20" showInDefault="1" showInWebsite="1" showInStore="1">
4747
<label>Public ID</label>
4848
<comment><![CDATA[
4949
<div class="default_loader_mfgtm_js_example">
5050
E.g. <strong>GTM-XXXXXXX</strong><br/>
51-
Please note that we do not allow pasting the GTM JavaScript code directly for security reasons. Public ID is used to automatically insert the code to your pages:
51+
Public ID is used to insert the code to your pages automatically:
5252
<pre style="border: 1px dashed #5f5f5f;padding: 5px;background: #f4f4f4;">
5353
&lt;!-- Google Tag Manager --&gt;
5454
&lt;script&gt;(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({&#39;gtm.start&#39;:
@@ -66,23 +66,49 @@ height=&quot;0&quot; width=&quot;0&quot; style=&quot;display:none;visibility:hid
6666
</pre>
6767
</div>
6868
<div id="mfgtm_js_example"></div>
69+
If you want to paste the GTM script and noscript manually please use the "Head Script and Body Noscript" option as Google Tag Manager Installation Method.
6970
7071
]]></comment>
7172
<depends>
7273
<field id="mfgoogletagmanager/web_container/install_gtm">use_public_id</field>
7374
</depends>
7475
</field>
7576
<field id="script_content" translate="label comment" type="textarea" sortOrder="50" showInDefault="1" showInWebsite="1" showInStore="1">
76-
<label>GTM Script</label>
77+
<label>Head Script</label>
7778
<depends>
7879
<field id="mfgoogletagmanager/web_container/install_gtm">use_head_and_body_script</field>
7980
</depends>
81+
<comment><![CDATA[
82+
<p>Please navigate to <a href="https://tagmanager.google.com/" title="Google Tag Manager" target="_blank">Google Tag Manager</a> Workspace > Admin > Install Google Tag Manager and copy the <strong>&lt;head&gt;</strong> script.<br/> E.g.<br/>
83+
<pre style="border: 1px dashed #5f5f5f;padding: 5px;background: #f4f4f4;">
84+
&lt;!-- Google Tag Manager --&gt;
85+
&lt;script&gt;(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({&#39;gtm.start&#39;:
86+
new Date().getTime(),event:&#39;gtm.js&#39;});var f=d.getElementsByTagName(s)[0],
87+
j=d.createElement(s),dl=l!=&#39;dataLayer&#39;?&#39;&amp;l=&#39;+l:&#39;&#39;;j.async=true;j.src=
88+
&#39;https://www.googletagmanager.com/gtm.js?id=&#39;+i+dl;f.parentNode.insertBefore(j,f);
89+
})(window,document,&#39;script&#39;,&#39;dataLayer&#39;,&#39;<strong>GTM-XXXXXXX</strong>&#39;);&lt;/script&gt;
90+
&lt;!-- End Google Tag Manager --&gt;
91+
</pre></p>
92+
<p>Also you can use scripts from the Stape.io or other alternative services for custom GTM Loaders.</p>
93+
]]>
94+
</comment>
8095
</field>
8196
<field id="noscript_content" translate="label comment" type="textarea" sortOrder="50" showInDefault="1" showInWebsite="1" showInStore="1">
82-
<label>GTM Noscript</label>
97+
<label>Body Noscript</label>
8398
<depends>
8499
<field id="mfgoogletagmanager/web_container/install_gtm">use_head_and_body_script</field>
85100
</depends>
101+
<comment><![CDATA[
102+
<p>Please navigate to <a href="https://tagmanager.google.com/" title="Google Tag Manager" target="_blank">Google Tag Manager</a> Workspace > Admin > Install Google Tag Manager and copy the <strong>&lt;body&gt;</strong> noscript.<br/> E.g.<br/>
103+
<pre style="border: 1px dashed #5f5f5f;padding: 5px;background: #f4f4f4;">
104+
&lt;!-- Google Tag Manager (noscript) --&gt;
105+
&lt;noscript&gt;&lt;iframe src=&quot;https://www.googletagmanager.com/ns.html?id=<strong>GTM-XXXXXXX</strong>&quot;
106+
height=&quot;0&quot; width=&quot;0&quot; style=&quot;display:none;visibility:hidden&quot;&gt;&lt;/iframe&gt;&lt;/noscript&gt;
107+
&lt;!-- End Google Tag Manager (noscript) --&gt;
108+
</pre></p>
109+
<p>Also you can use scripts from the Stape.io or other alternative services for custom GTM Loaders.</p>
110+
]]>
111+
</comment>
86112
</field>
87113
<field id="account_id" translate="label comment" type="text" sortOrder="50" showInDefault="1" showInWebsite="1" showInStore="1">
88114
<label>Account ID</label>

etc/config.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212
<enabled>0</enabled>
1313
<mfmodule>GoogleTagManager</mfmodule>
1414
<mftype>1</mftype>
15-
<install_gtm>use_public_id</install_gtm>
1615
</general>
1716
<web_container>
17+
<install_gtm>use_public_id</install_gtm>
1818
<script_content/>
1919
<noscript_content/>
2020
<enable>1</enable>

view/frontend/templates/js_code.phtml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@ if (!isset($escaper)) {
1515
<!-- Google Tag Manager -->
1616
<script>
1717
function mfLoadGtm() {
18-
<?php if ($block->getConfig()->getInstallGtm() == 'use_public_id') { ?>
18+
<?php if ('use_public_id' === $block->getConfig()->getInstallGtm()) { ?>
1919
(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
2020
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
2121
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
2222
'https:<?= '/' . '/' ?>www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
2323
})(window,document,'script','dataLayer','<?= $escaper->escapeHtml($block->getPublicId()) ?>');
24-
<?php } elseif ($block->getConfig()->getInstallGtm() == 'use_head_and_body_script') { ?>
25-
<?= $block->getFormattedGtmScript(); ?>
24+
<?php } elseif ('use_head_and_body_script' === $block->getConfig()->getInstallGtm()) { ?>
25+
<?= $block->getGtmScript(); ?>
2626
<?php } ?>
2727
}
2828

view/frontend/templates/no_js_code.phtml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ if (!isset($escaper)) {
1414
?>
1515
<?php if (!$block->isProtectCustomerDataEnabled()) { ?>
1616
<!-- Google Tag Manager (noscript) -->
17-
<?php if ($block->getConfig()->getInstallGtm() == 'use_public_id') { ?>
17+
<?php if ('use_public_id' === $block->getConfig()->getInstallGtm()) { ?>
1818
<noscript><iframe src="https://www.googletagmanager.com/ns.html?id=<?= $escaper->escapeHtml($block->getPublicId()) ?>"
1919
height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
2020
<!-- End Google Tag Manager (noscript) -->
21-
<?php } elseif ($block->getConfig()->getInstallGtm() == 'use_head_and_body_script') { ?>
22-
<?= $block->getConfig()->getGtmNoScript() ?>
21+
<?php } elseif ('use_head_and_body_script' === $block->getConfig()->getInstallGtm()) { ?>
22+
<?= $block->getGtmNoScript() ?>
2323
<?php } ?>
2424
<?php } ?>

0 commit comments

Comments
 (0)