Skip to content

Commit 356091b

Browse files
committed
12327-cookie-consent-and-gtm-extension
1 parent 9d0dbb0 commit 356091b

File tree

3 files changed

+76
-25
lines changed

3 files changed

+76
-25
lines changed

Block/GtmCode.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,11 @@ public function isCookieRestrictionModeEnabled()
104104
return $this->config->isCookieRestrictionModeEnabled();
105105
}
106106

107+
public function isMfCookieConsentExtensionEnabled()
108+
{
109+
return $this->config->isMfCookieConsentExtensionEnabled();
110+
}
111+
107112
/**
108113
* Get current website ID
109114
*

Model/Config.php

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Magento\Framework\App\Config\ScopeConfigInterface;
1212
use Magento\Store\Model\ScopeInterface;
1313
use Magento\Config\Model\Config\Backend\Admin\Custom;
14+
use Magento\Framework\Module\Manager as ModuleManager;
1415

1516
class Config
1617
{
@@ -61,20 +62,33 @@ class Config
6162
public const XML_PATH_SPEED_OPTIMIZATION_ENABLED = 'mfgoogletagmanager/page_speed_optimization/enabled';
6263
public const XML_PATH_THIRD_PARTY_GA = 'mfgoogletagmanager/third_party_ga/enabled';
6364

65+
/**
66+
* MF cookie consent extension enabled
67+
*/
68+
public const XML_PATH_MF_COOKIE_CONSENT_EXTENSION_ENABLED = 'mf_cookie_consent/general/enabled';
69+
6470
/**
6571
* @var ScopeConfigInterface
6672
*/
6773
private $scopeConfig;
6874

75+
/**
76+
* @var ModuleManager
77+
*/
78+
protected $moduleManager;
79+
6980
/**
7081
* Config constructor.
7182
*
7283
* @param ScopeConfigInterface $scopeConfig
84+
* @param ModuleManager $moduleManager
7385
*/
7486
public function __construct(
75-
ScopeConfigInterface $scopeConfig
87+
ScopeConfigInterface $scopeConfig,
88+
ModuleManager $moduleManager
7689
) {
7790
$this->scopeConfig = $scopeConfig;
91+
$this->moduleManager = $moduleManager;
7892
}
7993

8094
/**
@@ -259,6 +273,18 @@ public function isCookieRestrictionModeEnabled(string $storeId = null)
259273
return (bool)$this->getConfig(Custom::XML_PATH_WEB_COOKIE_RESTRICTION, $storeId);
260274
}
261275

276+
/**
277+
* Retrieve true if mf cookie consent extension is enabled
278+
*
279+
* @param string|null $storeId
280+
* @return bool
281+
*/
282+
public function isMfCookieConsentExtensionEnabled(string $storeId = null)
283+
{
284+
return $this->moduleManager->isEnabled('Magefan_CookieConsent')
285+
&& $this->getConfig(self::XML_PATH_MF_COOKIE_CONSENT_EXTENSION_ENABLED , $storeId);
286+
}
287+
262288
/*
263289
* Retrieve Magento product categories
264290
*

view/frontend/templates/js_code.phtml

Lines changed: 44 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ if (!isset($escaper)) {
1717

1818
$script = '';
1919
?>
20+
21+
<?= $block->getLayout()->createBlock(\Magefan\Community\Block\JsScript::class)->setMethod('getCookie')->toHtml() ?>
22+
2023
<!-- Google Tag Manager -->
2124
<?php if ($block->isSpeedOptimizationEnabled() && $block->getRequest()->getModuleName() !== 'checkout') { ?>
2225
<?php $script .= "
@@ -39,28 +42,35 @@ $script = '';
3942
<?php } ?>
4043

4144
<?php if ($block->isProtectCustomerDataEnabled()) { ?>
42-
<?php $script .= "
43-
window.dataLayer = window.dataLayer || [];
44-
function gtag() { dataLayer.push(arguments); }
45-
gtag('consent', 'default', {
46-
'ad_user_data': 'denied',
47-
'ad_personalization': 'denied',
48-
'ad_storage': 'denied',
49-
'analytics_storage': 'denied',
50-
/* cookieyes.com start */
51-
52-
'functionality_storage': 'denied',
53-
'personalization_storage': 'denied',
54-
'security_storage': 'granted',
55-
/* cookieyes.com end */
56-
57-
'wait_for_update': 2000
58-
});
59-
/* cookieyes.com start */
60-
gtag('set', 'ads_data_redaction', true);
61-
gtag('set', 'url_passthrough', true);
62-
/* cookieyes.com end */
63-
"; ?>
45+
<?php
46+
$script .= "
47+
window.dataLayer = window.dataLayer || [];
48+
function gtag() { dataLayer.push(arguments); }
49+
";
50+
51+
if (!$block->isMfCookieConsentExtensionEnabled()) {
52+
$script .= "
53+
gtag('consent', 'default', {
54+
'ad_user_data': 'denied',
55+
'ad_personalization': 'denied',
56+
'ad_storage': 'denied',
57+
'analytics_storage': 'denied',
58+
/* cookieyes.com start */
59+
60+
'functionality_storage': 'denied',
61+
'personalization_storage': 'denied',
62+
'security_storage': 'granted',
63+
/* cookieyes.com end */
64+
65+
'wait_for_update': 2000
66+
});
67+
/* cookieyes.com start */
68+
gtag('set', 'ads_data_redaction', true);
69+
gtag('set', 'url_passthrough', true);
70+
/* cookieyes.com end */
71+
";
72+
}
73+
?>
6474

6575
<?php if ($block->isLoadBeforeConsent()) { ?>
6676
<?php $script .= "
@@ -91,6 +101,10 @@ $script = '';
91101
};
92102
93103
function customerDataAllowed() {
104+
if (MagefanJs.getCookie('mf_cookie_consent') === '1') {
105+
return true;
106+
}
107+
94108
let cookie = getCookieValue(
95109
'{$escaper->escapeHtml(\Magento\Cookie\Helper\Cookie::IS_USER_ALLOWED_SAVE_COOKIE)}'
96110
);
@@ -107,15 +121,21 @@ $script = '';
107121
function grantConsent()
108122
{
109123
window.mfGtmUserCookiesAllowed = true;
110-
gtag('consent', 'update', {
124+
";
125+
126+
if (!$block->isMfCookieConsentExtensionEnabled()) {
127+
$script .= "gtag('consent', 'update', {
111128
'ad_user_data': 'granted',
112129
'ad_personalization': 'granted',
113130
'ad_storage': 'granted',
114131
'analytics_storage': 'granted',
115132
'functionality_storage': 'granted',
116133
'personalization_storage': 'granted',
117134
'security_storage': 'granted'
118-
});
135+
});";
136+
}
137+
138+
$script .= "
119139
};
120140
121141
if (customerDataAllowed()) {

0 commit comments

Comments
 (0)