Skip to content

Commit 55ad975

Browse files
authored
Merge pull request #29 from ivanhrytsaim/10738-Third-Party-Google-Analytics-and-AdWords
10738-Third-Party-Google-Analytics-and-AdWords
2 parents 4d9179e + 4a4ba83 commit 55ad975

File tree

5 files changed

+83
-0
lines changed

5 files changed

+83
-0
lines changed

Model/Config.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ class Config
5959
* Speed optimization config
6060
*/
6161
public const XML_PATH_SPEED_OPTIMIZATION_ENABLED = 'mfgoogletagmanager/page_speed_optimization/enabled';
62+
public const XML_PATH_THIRD_PARTY_GA = 'mfgoogletagmanager/third_party_ga/enabled';
6263

6364
/**
6465
* @var ScopeConfigInterface
@@ -300,4 +301,13 @@ public function isSpeedOptimizationEnabled(string $storeId = null): bool
300301
{
301302
return (bool)$this->getConfig(self::XML_PATH_SPEED_OPTIMIZATION_ENABLED, $storeId);
302303
}
304+
305+
/**
306+
* @param string|null $storeId
307+
* @return bool
308+
*/
309+
public function isThirdPartyGaEnabled(string $storeId = null): bool
310+
{
311+
return (bool)$this->getConfig(self::XML_PATH_THIRD_PARTY_GA, $storeId);
312+
}
303313
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
namespace Magefan\GoogleTagManager\Plugin\Magento\Framework\App\Config;
4+
5+
use Magento\Framework\App\Config\ScopeConfigInterface;
6+
use Magefan\GoogleTagManager\Model\Config;
7+
class ScopeConfig
8+
{
9+
/**
10+
* @var Config
11+
*/
12+
private $config;
13+
14+
const PATHS = ['google/adwords/active',
15+
'google/analytics/active',
16+
'google/gtag/analytics4/active',
17+
'google/gtag/adwords/active',
18+
'googletagmanager/general/active',
19+
'am_ga4/general/enable',
20+
'weltpixel_ga4/general/enable',
21+
'mst_gtm/general/enabled',
22+
'googletagmanager/general/enabled'
23+
];
24+
25+
/**
26+
* @param Config $config
27+
*/
28+
public function __construct(
29+
Config $config
30+
)
31+
{
32+
$this->config = $config;
33+
}
34+
35+
/**
36+
* @param ScopeConfigInterface $subject
37+
* @param $result
38+
* @param $path
39+
* @param string $scopeType
40+
* @param $scopeCode
41+
* @return mixed|string
42+
*/
43+
public function afterGetValue(
44+
ScopeConfigInterface $subject,
45+
$result,
46+
$path,
47+
$scopeType = ScopeConfigInterface::SCOPE_TYPE_DEFAULT,
48+
$scopeCode = null
49+
)
50+
{
51+
if (in_array($path, self::PATHS)) {
52+
if ($this->config->isEnabled() && !$this->config->isThirdPartyGaEnabled()) {
53+
$result = '0';
54+
}
55+
}
56+
57+
return $result;
58+
}
59+
}

etc/adminhtml/system.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,14 @@ To create a new secret, navigate in the
370370
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
371371
</field>
372372
</group>
373+
<group id="third_party_ga" translate="label" type="text" sortOrder="65" showInDefault="1" showInWebsite="1" showInStore="1">
374+
<label>Third-Party Google Analytics and AdWords</label>
375+
<field id="enabled" translate="label comment" type="select" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1" canRestore="1">
376+
<label>Enable</label>
377+
<comment>If enabled Magefan Google Tag Manager Extension will not prevent the execution of default Magento and custom javascripts for Google Analytics, AdWords, and GTM. Please note that this can lead to event duplications and double tracking.</comment>
378+
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
379+
</field>
380+
</group>
373381
<group id="customer_data" translate="label" type="text" sortOrder="70" showInDefault="1" showInWebsite="1" showInStore="1">
374382
<label>GDPR/CCPA/LGPD (Customer Data Protection Regulation)</label>
375383
<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
@@ -71,6 +71,9 @@
7171
<page_speed_optimization>
7272
<enabled>0</enabled>
7373
</page_speed_optimization>
74+
<third_party_ga>
75+
<enabled>0</enabled>
76+
</third_party_ga>
7477
<customer_data>
7578
<protect>0</protect>
7679
<load_before_consent>0</load_before_consent>

etc/di.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222
<argument name="container" xsi:type="object">Magefan\GoogleTagManager\Model\WebContainer</argument>
2323
</arguments>
2424
</type>
25+
<type name="Magento\Framework\App\Config\ScopeConfigInterface">
26+
<plugin name="Plugin_Magefan_GoogleTagManager_ScopeConfigInterface" type="Magefan\GoogleTagManager\Plugin\Magento\Framework\App\Config\ScopeConfig" sortOrder="10" disabled="false"/>
27+
</type>
2528
<type name="Magento\Customer\CustomerData\Customer">
2629
<plugin name="Magefan_GoogleTagManager_Plugin_Magento_Customer_CustomerData_Customer" type="Magefan\GoogleTagManager\Plugin\Magento\Customer\CustomerData\Customer" sortOrder="10" disabled="false"/>
2730
</type>

0 commit comments

Comments
 (0)