Skip to content
This repository has been archived by the owner on Apr 23, 2024. It is now read-only.

Commit

Permalink
Basic setup for Consent V2
Browse files Browse the repository at this point in the history
  • Loading branch information
BrentRobert committed Mar 12, 2024
1 parent b3d84cb commit 3de0f2c
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 2 deletions.
63 changes: 63 additions & 0 deletions Setup/Patch/Data/PersonalizationData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php declare(strict_types=1);

namespace Phpro\CookieConsent\Setup\Patch\Data;

use Magento\Framework\Setup\Patch\DataPatchInterface;
use Magento\Store\Model\Store;
use Phpro\CookieConsent\Api\CookieGroupRepositoryInterface;
use Phpro\CookieConsent\Api\Data\CookieGroupInterface;
use Phpro\CookieConsent\Model\CookieGroupFactory;

class PersonalizationData implements DataPatchInterface
{
/**
* @var CookieGroupFactory
*/
private $cookieGroupFactory;

/**
* @var CookieGroupRepositoryInterface
*/
private $cookieGroupRepository;


public function __construct(
CookieGroupFactory $cookieGroupFactory,
CookieGroupRepositoryInterface $cookieGroupRepository
) {
$this->cookieGroupFactory = $cookieGroupFactory;
$this->cookieGroupRepository = $cookieGroupRepository;
}

public static function getDependencies()
{
return [
CookieGroupAttribute::class,
DefaultCookieData::class,
];
}

public function getAliases()
{
return [];
}

public function apply()
{
$this->createDefaultGroups();
}

private function createDefaultGroups(): void
{
/** @var CookieGroupInterface $group */
$personalization = $this->cookieGroupFactory->create();
$personalization->setStoreId(Store::DEFAULT_STORE_ID);
$personalization->setSystemName('personalization');
$personalization->setName('Personalization');
$personalization->setActive(true);
$personalization->setEssential(false);
$personalization->setDescription('Personalization cookies are used for ad personalization and remarketing.');

$this->cookieGroupRepository->save($personalization);
}
}
8 changes: 8 additions & 0 deletions etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@
<comment>The value represent the expiration of the cookie in days..</comment>
</field>
</group>
<group id="gtag" translate="label" type="text" sortOrder="20" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Google Consent</label>
<field id="enabled" translate="label" type="select" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Enabled</label>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
<comment>Enable the module to send the consent to Google using Gtag. It's important to have your Google Tag Manager initiated AFTER the 'phpro_cookie_gtag' block. This feature relies on the cookie groups with the following system names: 'essential', 'analytical', 'marketing' and 'personalization'</comment>
</field>
</group>
</section>
</system>
</config>
3 changes: 3 additions & 0 deletions etc/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
<cookie_name_preferences>cookieConsentPrefs</cookie_name_preferences>
<cookie_expiration>182</cookie_expiration>
</general>
<gtag>
<enabled>0</enabled>
</gtag>
</phpro_cookie_consent>
</default>
</config>
7 changes: 7 additions & 0 deletions view/frontend/layout/default.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
<?xml version="1.0"?>
<page layout="3columns" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceContainer name="after.body.start">
<block name="phpro_cookie_gtag"
template="Phpro_CookieConsent::gtag.phtml"
ifconfig="phpro_cookie_consent/gtag/enabled"
before="-"/>
</referenceContainer>

<referenceContainer name="before.body.end">
<block class="Magento\Framework\View\Element\Template"
name="phpro_cookie_consent_notice"
Expand Down
37 changes: 37 additions & 0 deletions view/frontend/templates/gtag.phtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<script>
window.dataLayer = window.dataLayer || [];

function gtag() {
console.log(arguments);
dataLayer.push(arguments);
}

if (localStorage.getItem('consentMode') === null) {
gtag('consent', 'default', {
'ad_storage': 'denied',
'analytics_storage': 'denied',
'personalization_storage': 'denied',
'functionality_storage': 'denied',
'security_storage': 'denied',
'ad_user_data': 'denied',
'ad_personalization': 'denied',
});
} else {
gtag('consent', 'default', JSON.parse(localStorage.getItem('consentMode')));
}

document.addEventListener('consent-changed', function (consent) {
const consentMode = {
'ad_storage': consent.marketing ? 'granted': 'denied',
'analytics_storage': consent.analytical ? 'granted': 'denied',
'personalization_storage': consent.personalization ? 'granted': 'denied',
'functionality_storage': consent.essential ? 'granted': 'denied',
'security_storage': consent.essential ? 'granted': 'denied',
'ad_user_data': consent.marketing ? 'granted': 'denied',
'ad_personalization': consent.marketing && consent.personalization ? 'granted': 'denied',
};

gtag('consent', 'update', consentMode);
localStorage.setItem('consentMode', JSON.stringify(consentMode));
})
</script>
6 changes: 4 additions & 2 deletions view/frontend/web/js/model/cookie.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ define([
return (!!$.cookie(name));
},

getConsentCookieObject: function(name) {
getConsentCookieObject: function (name) {
return $.cookie(name);
},

Expand All @@ -24,6 +24,7 @@ define([
data['expire'] = expiration;
data['secure'] = secure;
$.cookie(name, JSON.stringify(data), {expires: this.getExpiration(expiration)});
document.dispatchEvent(new Event('consent-changed', data));
},

saveSelected: function (name, expiration, secure, systemNames) {
Expand All @@ -36,6 +37,7 @@ define([
data['expire'] = expiration;
data['secure'] = secure;
$.cookie(name, JSON.stringify(data), {expires: this.getExpiration(expiration)});
document.dispatchEvent(new Event('consent-changed', data));
},

closeCookieNotice: function () {
Expand All @@ -45,7 +47,7 @@ define([
getExpiration: function (expiration) {
var today = new Date();
var expireDate = new Date(today);
expireDate.setDate(today.getDate()+expiration);
expireDate.setDate(today.getDate() + expiration);

return expireDate;
}
Expand Down

0 comments on commit 3de0f2c

Please sign in to comment.