Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

12327-cookie-consent-and-gtm-extension #43

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions Block/GtmCode.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,16 @@ public function isCookieRestrictionModeEnabled()
return $this->config->isCookieRestrictionModeEnabled();
}

/**
* Retrieve true if mf cookie consent extension is enabled
*
* @return bool
*/
public function isMfCookieConsentExtensionEnabled()
{
return $this->config->isMfCookieConsentExtensionEnabled();
}

/**
* Get current website ID
*
Expand Down
28 changes: 27 additions & 1 deletion Model/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Store\Model\ScopeInterface;
use Magento\Config\Model\Config\Backend\Admin\Custom;
use Magento\Framework\Module\Manager as ModuleManager;

class Config
{
Expand Down Expand Up @@ -61,20 +62,33 @@ class Config
public const XML_PATH_SPEED_OPTIMIZATION_ENABLED = 'mfgoogletagmanager/page_speed_optimization/enabled';
public const XML_PATH_THIRD_PARTY_GA = 'mfgoogletagmanager/third_party_ga/enabled';

/**
* MF cookie consent extension enabled
*/
public const XML_PATH_MF_COOKIE_CONSENT_EXTENSION_ENABLED = 'mf_cookie_consent/general/enabled';

/**
* @var ScopeConfigInterface
*/
private $scopeConfig;

/**
* @var ModuleManager
*/
protected $moduleManager;

/**
* Config constructor.
*
* @param ScopeConfigInterface $scopeConfig
* @param ModuleManager $moduleManager
*/
public function __construct(
ScopeConfigInterface $scopeConfig
ScopeConfigInterface $scopeConfig,
ModuleManager $moduleManager
) {
$this->scopeConfig = $scopeConfig;
$this->moduleManager = $moduleManager;
}

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

/**
* Retrieve true if mf cookie consent extension is enabled
*
* @param string|null $storeId
* @return bool
*/
public function isMfCookieConsentExtensionEnabled(string $storeId = null)
{
return $this->moduleManager->isEnabled('Magefan_CookieConsent')
&& $this->getConfig(self::XML_PATH_MF_COOKIE_CONSENT_EXTENSION_ENABLED , $storeId);
}

/*
* Retrieve Magento product categories
*
Expand Down
68 changes: 44 additions & 24 deletions view/frontend/templates/js_code.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ if (!isset($escaper)) {

$script = '';
?>

<?= $block->getLayout()->createBlock(\Magefan\Community\Block\JsScript::class)->setMethod('getCookie')->toHtml() ?>

<!-- Google Tag Manager -->
<?php if ($block->isSpeedOptimizationEnabled() && $block->getRequest()->getModuleName() !== 'checkout') { ?>
<?php $script .= "
Expand All @@ -39,28 +42,35 @@ $script = '';
<?php } ?>

<?php if ($block->isProtectCustomerDataEnabled()) { ?>
<?php $script .= "
window.dataLayer = window.dataLayer || [];
function gtag() { dataLayer.push(arguments); }
gtag('consent', 'default', {
'ad_user_data': 'denied',
'ad_personalization': 'denied',
'ad_storage': 'denied',
'analytics_storage': 'denied',
/* cookieyes.com start */

'functionality_storage': 'denied',
'personalization_storage': 'denied',
'security_storage': 'granted',
/* cookieyes.com end */

'wait_for_update': 2000
});
/* cookieyes.com start */
gtag('set', 'ads_data_redaction', true);
gtag('set', 'url_passthrough', true);
/* cookieyes.com end */
"; ?>
<?php
$script .= "
window.dataLayer = window.dataLayer || [];
function gtag() { dataLayer.push(arguments); }
";

if (!$block->isMfCookieConsentExtensionEnabled()) {
$script .= "
gtag('consent', 'default', {
'ad_user_data': 'denied',
'ad_personalization': 'denied',
'ad_storage': 'denied',
'analytics_storage': 'denied',
/* cookieyes.com start */

'functionality_storage': 'denied',
'personalization_storage': 'denied',
'security_storage': 'granted',
/* cookieyes.com end */

'wait_for_update': 2000
});
/* cookieyes.com start */
gtag('set', 'ads_data_redaction', true);
gtag('set', 'url_passthrough', true);
/* cookieyes.com end */
";
}
?>

<?php if ($block->isLoadBeforeConsent()) { ?>
<?php $script .= "
Expand Down Expand Up @@ -91,6 +101,10 @@ $script = '';
};

function customerDataAllowed() {
if (MagefanJs.getCookie('mf_cookie_consent') === '1') {
return true;
}

let cookie = getCookieValue(
'{$escaper->escapeHtml(\Magento\Cookie\Helper\Cookie::IS_USER_ALLOWED_SAVE_COOKIE)}'
);
Expand All @@ -107,15 +121,21 @@ $script = '';
function grantConsent()
{
window.mfGtmUserCookiesAllowed = true;
gtag('consent', 'update', {
";

if (!$block->isMfCookieConsentExtensionEnabled()) {
$script .= "gtag('consent', 'update', {
'ad_user_data': 'granted',
'ad_personalization': 'granted',
'ad_storage': 'granted',
'analytics_storage': 'granted',
'functionality_storage': 'granted',
'personalization_storage': 'granted',
'security_storage': 'granted'
});
});";
}

$script .= "
};

if (customerDataAllowed()) {
Expand Down