Skip to content

Commit 943b31c

Browse files
authored
Merge pull request #7 from KozakSerhii/8646-Google-Tag-Manager-and-GA4-prices-and-taxes
8473-Server-Side-Tracking-for-GTM-Stape
2 parents 9841efa + 9e53c7d commit 943b31c

File tree

7 files changed

+241
-9
lines changed

7 files changed

+241
-9
lines changed

Block/GtmCode.php

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Magento\Framework\Exception\NoSuchEntityException;
1212
use Magento\Framework\View\Element\Template;
1313
use Magefan\GoogleTagManager\Model\Config;
14+
use Magefan\GoogleTagManager\Model\LoaderPool;
1415

1516
class GtmCode extends Template
1617
{
@@ -19,19 +20,26 @@ class GtmCode extends Template
1920
*/
2021
private $config;
2122

23+
/**
24+
* @var LoaderPool
25+
*/
26+
private $loaderPool;
27+
2228
/**
2329
* GtmCode constructor.
24-
*
2530
* @param Template\Context $context
2631
* @param Config $config
32+
* @param LoaderPool $loaderPool
2733
* @param array $data
2834
*/
2935
public function __construct(
3036
Template\Context $context,
3137
Config $config,
38+
LoaderPool $loaderPool,
3239
array $data = []
3340
) {
3441
$this->config = $config;
42+
$this->loaderPool = $loaderPool;
3543
parent::__construct($context, $data);
3644
}
3745

@@ -45,6 +53,30 @@ public function getPublicId(): string
4553
return $this->config->getPublicId();
4654
}
4755

56+
/**
57+
* @return string
58+
*/
59+
public function getTemplate()
60+
{
61+
$typeName = str_replace('_', '-', $this->config->getGTMLoaderType());
62+
if ('mfgtm.nojscode' === $this->getNameInLayout()) {
63+
$typeName = 'body-' . $typeName;
64+
} elseif ('mfgtm.jscode' === $this->getNameInLayout()) {
65+
$typeName = 'head-' . $typeName;
66+
} else {
67+
$typeName = null;
68+
}
69+
70+
if ($typeName) {
71+
$loaderTemplate = $this->loaderPool->getLoader($this->config->getGTMLoaderType(), $typeName);
72+
if ($loaderTemplate) {
73+
return $loaderTemplate;
74+
}
75+
}
76+
77+
return parent::getTemplate();
78+
}
79+
4880
/**
4981
* Check if protect customer data is enabled
5082
*
@@ -79,4 +111,12 @@ protected function _toHtml(): string
79111

80112
return '';
81113
}
114+
115+
/**
116+
* @return Config
117+
*/
118+
public function getConfig()
119+
{
120+
return $this->config;
121+
}
82122
}

Model/Config.php

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,15 @@ class Config
1818
* General config
1919
*/
2020
public const XML_PATH_EXTENSION_ENABLED = 'mfgoogletagmanager/general/enabled';
21-
public const XML_PATH_ACCOUNT_ID = 'mfgoogletagmanager/general/account_id';
22-
public const XML_PATH_CONTAINER_ID = 'mfgoogletagmanager/general/container_id';
23-
public const XML_PATH_PUBLIC_ID = 'mfgoogletagmanager/general/public_id';
21+
public const XML_PATH_GTM_LOADER_TYPE = 'mfgoogletagmanager/general/loader_type';
22+
23+
/**
24+
* Web Container config
25+
*/
26+
public const XML_PATH_WEB_CONTAINER_ENABLED = 'mfgoogletagmanager/web_container/enabled';
27+
public const XML_PATH_ACCOUNT_ID = 'mfgoogletagmanager/web_container/account_id';
28+
public const XML_PATH_CONTAINER_ID = 'mfgoogletagmanager/web_container/container_id';
29+
public const XML_PATH_WEB_PUBLIC_ID = 'mfgoogletagmanager/web_container/public_id';
2430

2531
/**
2632
* Analytics config
@@ -68,6 +74,17 @@ public function isEnabled(string $storeId = null): bool
6874
$this->getPublicId($storeId);
6975
}
7076

77+
/**
78+
* Retrieve true if web container enabled
79+
*
80+
* @param string|null $storeId
81+
* @return bool
82+
*/
83+
public function webContainerEnabled(string $storeId = null): bool
84+
{
85+
return (bool)$this->getConfig(self::XML_PATH_WEB_CONTAINER_ENABLED, $storeId);
86+
}
87+
7188
/**
7289
* Retrieve GTM account ID
7390
*
@@ -76,7 +93,7 @@ public function isEnabled(string $storeId = null): bool
7693
*/
7794
public function getAccountId(string $storeId = null): string
7895
{
79-
return trim((string)$this->getConfig(self::XML_PATH_ACCOUNT_ID, $storeId));
96+
return (string)$this->getConfig(self::XML_PATH_ACCOUNT_ID, $storeId);
8097
}
8198

8299
/**
@@ -87,7 +104,7 @@ public function getAccountId(string $storeId = null): string
87104
*/
88105
public function getContainerId(string $storeId = null): string
89106
{
90-
return trim((string)$this->getConfig(self::XML_PATH_CONTAINER_ID, $storeId));
107+
return (string)$this->getConfig(self::XML_PATH_CONTAINER_ID, $storeId);
91108
}
92109

93110
/**
@@ -98,7 +115,7 @@ public function getContainerId(string $storeId = null): string
98115
*/
99116
public function getPublicId(string $storeId = null): string
100117
{
101-
return trim((string)$this->getConfig(self::XML_PATH_PUBLIC_ID, $storeId));
118+
return (string)$this->getConfig(self::XML_PATH_WEB_PUBLIC_ID, $storeId);
102119
}
103120

104121
/**
@@ -120,7 +137,7 @@ public function isAnalyticsEnabled(string $storeId = null): bool
120137
*/
121138
public function getMeasurementId(string $storeId = null): string
122139
{
123-
return trim((string)$this->getConfig(self::XML_PATH_ANALYTICS_MEASUREMENT_ID, $storeId));
140+
return (string)$this->getConfig(self::XML_PATH_ANALYTICS_MEASUREMENT_ID, $storeId);
124141
}
125142

126143
/**
@@ -190,4 +207,13 @@ public function getConfig(string $path, string $storeId = null)
190207
{
191208
return $this->scopeConfig->getValue($path, ScopeInterface::SCOPE_STORE, $storeId);
192209
}
210+
211+
/**
212+
* @param string|null $storeId
213+
* @return int
214+
*/
215+
public function getGTMLoaderType(string $storeId = null): string
216+
{
217+
return (string)$this->getConfig(self::XML_PATH_GTM_LOADER_TYPE, $storeId);
218+
}
193219
}

Model/LoaderPool.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
/**
3+
* Copyright © Magefan ([email protected]). All rights reserved.
4+
* Please visit Magefan.com for license details (https://magefan.com/end-user-license-agreement).
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
namespace Magefan\GoogleTagManager\Model;
10+
11+
class LoaderPool
12+
{
13+
/**
14+
* Loaders objects
15+
*
16+
* @var []
17+
*/
18+
private $loaders;
19+
20+
/**
21+
* LoaderPool constructor.
22+
* @param array $loaders
23+
// */
24+
public function __construct(array $loaders)
25+
{
26+
$this->loaders = $loaders;
27+
}
28+
29+
/**
30+
* Retrieve all loaders for type
31+
* @param $loaderType
32+
* @return array
33+
*/
34+
public function getAll(string $loaderType):array
35+
{
36+
return $this->loaders[$loaderType] ?? [];
37+
}
38+
39+
/**
40+
* Retrieve template
41+
* @param string $loaderType
42+
* @param string $name
43+
* @return string
44+
*/
45+
public function getLoader(string $loaderType, string $name):string
46+
{
47+
if (isset($this->loaders[$loaderType]) &&
48+
(is_array($this->loaders[$loaderType]) || $this->loaders[$loaderType] instanceof Countable)
49+
) {
50+
foreach ($this->loaders[$loaderType] as $item) {
51+
if (isset($item['value']) && $item['value'] == $name) {
52+
return $item['template'];
53+
}
54+
}
55+
}
56+
return '';
57+
}
58+
}

Setup/Patch/Schema/ChangePath.php

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
/**
3+
* Copyright © Magefan ([email protected]). All rights reserved.
4+
* Please visit Magefan.com for license details (https://magefan.com/end-user-license-agreement).
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magefan\GoogleTagManager\Setup\Patch\Schema;
9+
10+
use Magento\Framework\Setup\Patch\SchemaPatchInterface;
11+
use Magento\Framework\Setup\SchemaSetupInterface;
12+
13+
class ChangePath implements SchemaPatchInterface
14+
{
15+
16+
/**
17+
* @var SchemaSetupInterface
18+
*/
19+
private $schemaSetup;
20+
21+
/**
22+
* Constructor
23+
*
24+
* @param SchemaSetupInterface $schemaSetup
25+
*/
26+
public function __construct(
27+
SchemaSetupInterface $schemaSetup
28+
) {
29+
$this->schemaSetup = $schemaSetup;
30+
}
31+
32+
/**
33+
* {@inheritdoc}
34+
*/
35+
public function apply()
36+
{
37+
$this->schemaSetup->startSetup();
38+
$connection = $this->schemaSetup->getConnection();
39+
40+
$table = $this->schemaSetup->getTable('core_config_data');
41+
42+
$changedConfigurationFields = [
43+
'mfgoogletagmanager/general/public_id' => 'mfgoogletagmanager/web_container/public_id',
44+
'mfgoogletagmanager/general/account_id' => 'mfgoogletagmanager/web_container/account_id',
45+
'mfgoogletagmanager/general/container_id' => 'mfgoogletagmanager/web_container/container_id',
46+
];
47+
48+
foreach ($changedConfigurationFields as $oldPath => $newPath) {
49+
$sql = 'UPDATE ' . $table . ' SET `path` = "' . $newPath . '" WHERE `path` = "' . $oldPath . '";';
50+
$this->schemaSetup->run($sql);
51+
}
52+
53+
$this->schemaSetup->endSetup();
54+
}
55+
56+
/**
57+
* @return array
58+
*/
59+
public function getAliases()
60+
{
61+
return [];
62+
}
63+
64+
/**
65+
* @return array
66+
*/
67+
public static function getDependencies()
68+
{
69+
return [];
70+
}
71+
}

etc/adminhtml/system.xml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,18 @@
2929
<label>Product Key</label>
3030
<frontend_model>Magefan\Community\Block\Adminhtml\System\Config\Form\ProductKeyField</frontend_model>
3131
</field>
32+
</group>
33+
<group id="web_container" translate="label" type="text" sortOrder="20" showInDefault="1" showInWebsite="1" showInStore="1" canRestore="1">
34+
<label>Web Container</label>
35+
<attribute type="expanded">1</attribute>
36+
<field id="enable" translate="label comment" type="select" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
37+
<label>Enable</label>
38+
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
39+
</field>
3240
<field id="public_id" translate="label comment" type="text" sortOrder="40" showInDefault="1" showInWebsite="1" showInStore="1">
3341
<label>Public ID</label>
3442
<comment><![CDATA[
43+
<div class="default_loader_mfgtm_js_example">
3544
E.g. <strong>GTM-XXXXXXX</strong><br/>
3645
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:
3746
<pre style="border: 1px dashed #5f5f5f;padding: 5px;background: #f4f4f4;">
@@ -49,6 +58,9 @@ j=d.createElement(s),dl=l!=&#39;dataLayer&#39;?&#39;&amp;l=&#39;+l:&#39;&#39;;j.
4958
height=&quot;0&quot; width=&quot;0&quot; style=&quot;display:none;visibility:hidden&quot;&gt;&lt;/iframe&gt;&lt;/noscript&gt;
5059
&lt;!-- End Google Tag Manager (noscript) --&gt;
5160
</pre>
61+
</div>
62+
<div id="mfgtm_js_example"></div>
63+
5264
]]></comment>
5365
</field>
5466
<field id="account_id" translate="label comment" type="text" sortOrder="50" showInDefault="1" showInWebsite="1" showInStore="1">
@@ -118,6 +130,9 @@ height=&quot;0&quot; width=&quot;0&quot; style=&quot;display:none;visibility:hid
118130
<label>Export Container</label>
119131
<field id="export" translate="label comment" type="select" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
120132
<frontend_model>Magefan\GoogleTagManager\Block\Adminhtml\System\Config\Form\Button</frontend_model>
133+
<depends>
134+
<field id="mfgoogletagmanager/general/loader_type" separator="|">default_loader|stape_loader</field>
135+
</depends>
121136
</field>
122137
</group>
123138
</section>

etc/config.xml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,13 @@
1212
<enabled>0</enabled>
1313
<mfmodule>GoogleTagManager</mfmodule>
1414
<mftype>1</mftype>
15+
</general>
16+
<web_container>
17+
<enable>0</enable>
1518
<account_id/>
1619
<container_id/>
1720
<public_id/>
18-
</general>
21+
</web_container>
1922
<analytics>
2023
<enable>0</enable>
2124
<measurement_id/>

etc/di.xml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,23 @@
1313
<preference for="Magefan\GoogleTagManager\Api\DataLayer\Cart\ItemInterface" type="Magefan\GoogleTagManager\Model\DataLayer\Cart\Item" />
1414
<preference for="Magefan\GoogleTagManager\Api\DataLayer\Order\ItemInterface" type="Magefan\GoogleTagManager\Model\DataLayer\Order\Item" />
1515
<preference for="Magefan\GoogleTagManager\Api\DataLayer\Product\ItemInterface" type="Magefan\GoogleTagManager\Model\DataLayer\Product\Item" />
16+
17+
<type name="Magefan\GoogleTagManager\Model\LoaderPool">
18+
<arguments>
19+
<argument name="loaders" xsi:type="array">
20+
<item name="default_loader" xsi:type="array">
21+
<item name="head" xsi:type="array">
22+
<item name="label" xsi:type="string">Default Body Loader</item>
23+
<item name="value" xsi:type="string">body-default-loader</item>
24+
<item name="template" xsi:type="string"></item>
25+
</item>
26+
<item name="body" xsi:type="array">
27+
<item name="label" xsi:type="string">Default Head Loader</item>
28+
<item name="value" xsi:type="string">head-default-loader</item>
29+
<item name="template" xsi:type="string"></item>
30+
</item>
31+
</item>
32+
</argument>
33+
</arguments>
34+
</type>
1635
</config>

0 commit comments

Comments
 (0)