hryvinskyi / magento2-cookie-consent-google
Google consent mode for hryvinskyi/magento2-cookie-consent: tells Google tags the visitor's choice, with Google's known cookies and blocking rules
Package info
github.com/hryvinskyi/magento2-cookie-consent-google
Type:magento2-module
pkg:composer/hryvinskyi/magento2-cookie-consent-google
Requires
- php: ^8.1
- hryvinskyi/magento2-admin-setup-wizard: ^1.0
- hryvinskyi/magento2-cookie-consent: ^1.0
- magento/framework: *
- magento/module-config: *
- magento/module-store: *
Requires (Dev)
None
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
Google consent mode for hryvinskyi/magento2-cookie-consent: tells Google tags the visitor's choice, with Google's known cookies and blocking rules.
What it does for a store
- Tells Google's tags the choice. A part of the consent runtime pushes
consent defaultbefore any Google tag reads the data layer, then the stored consent, then every change. Each of the seven consent types (ad_storage,ad_user_data,ad_personalization,analytics_storage,functionality_storage,personalization_storage,security_storage) is granted exactly when the visitor allowed the store category it is mapped to. - Two implementations. Advanced (the default): Google's tags load with the page and adapt to the consent they are told. Basic: the tags are also held back until their category is allowed, by the blocking rules this module contributes (the Google tag and Google Tag Manager, Google Ads' conversion script, and a Tag Manager container's inline loader). In both the tags are told the consent once they run.
- Declares Google's first-party cookies in the consent catalogue of known trackers, ready to be declared:
_ga,_ga_*,_gac_*,_gcl_au,_gcl_aw,_gcl_gb,_gcl_gs,_gcl_ag,_gcl_dc,_gcl_ls. - A consent mode template for Google Tag Manager (below), for stores whose tags run through a container.
- A row in the consent setup wizard's Blocking step: a switch between the basic and the advanced implementation and a select for the category the tags wait for. The module's own switch only makes the row read-only.
Requirements
hryvinskyi/magento2-cookie-consentandhryvinskyi/magento2-admin-setup-wizard, and the platform's Store and Config modules (whatcomposer.jsonandetc/module.xmldeclare). PHP 8.1 or higher.
Installation
composer require hryvinskyi/magento2-cookie-consent-google bin/magento setup:upgrade bin/magento setup:di:compile bin/magento cache:flush
Configuration
Stores › Configuration › Hryvinskyi Extensions › Cookie Consent › Google Consent Mode (hryvinskyi_cookie_consent/google/*), every field per default, website and store view.
| Setting | Path | Default |
|---|---|---|
| Enabled | google/enabled |
on |
| Implementation | google/mode |
advanced (basic holds the tags back) |
| How Google tags read consent | google/command_source |
page (template) |
| Category that lets the tags load (basic only) | google/basic_category |
statistics |
| The category each consent type follows | google/ad_storage, ad_user_data, ad_personalization |
marketing |
google/analytics_storage |
statistics |
|
google/functionality_storage, personalization_storage |
preferences |
|
google/security_storage |
necessary |
|
| Wait for the visitor's consent, milliseconds | google/wait_for_update |
500 |
| Redact ads data | google/ads_data_redaction |
on |
| Pass ad click information through links | google/url_passthrough |
off |
| Data layer name | google/data_layer_name |
dataLayer |
| Consent change event | google/event_name |
cookie_consent_update |
A value that reaches the configuration without the form (a console command, a deployment file) and breaks a rule is read as its default. A category the store does not have is never allowed, so its consent types stay denied.
How Google tags read consent
How Google tags read consent (Google Consent Mode group) picks one of two ways. Use exactly one: with both, Google's tags receive two defaults.
- The page (the default). The storefront pushes
consent defaultandconsent updateinto the data layer itself, before any Google tag loads. Use it for the Google tag (gtag.js), and for a Google Tag Manager container that has no consent mode template. - The template. The consent mode template this module ships sets the consent inside the Google Tag Manager container, which is Google's recommended way for tags that run through Tag Manager. The storefront then pushes no consent command, only its consent change event (
cookie_consent_updateby default), so the container's triggers can still react to a change.
Importing the consent mode template
The template is view/base/gtm/consent-mode.tpl in this module.
- In Google Tag Manager, open Templates, then New under Tag Templates.
- Open the More actions menu, choose Import and select
consent-mode.tpl. Save the template. - Create a tag from it. Enter the code of the store category that grants each consent type (the defaults are the module's own: the three advertising types to
marketing,analytics_storagetostatistics,functionality_storageandpersonalization_storagetopreferences,security_storagetonecessary), the wait, the regions if the default should apply to some only, and the two ads settings. - Fire the tag on the Consent Initialization - All Pages trigger and publish the container.
- In the store, set How Google tags read consent to The template.
The tag reads the consent through the consent runtime's browser API, window.hryvinskyiCookieConsent, never from its cookie. It sets the default from the consent in force, then the ads settings, then updates Google's consent state after every change. On a page without the API every type stays denied.
What it adds to the consent module
All in the global etc/di.xml:
- the runtime part
signal-google(Model\Runtime\GoogleSignalPart, sort order 300) inPartPool; - the known-tracker source
google(Model\Catalog\GoogleTrackers); - the blocking rule source
google(Model\Rule\GoogleRules), which describes on the Blocking rules page what it does on each store view; - the wizard settings
google_enabled,google_mode,google_categoryand the Blocking-step rowgoogle(a virtual type of the consent module'sContributedRuleSwitch, addresseswww.googletagmanager.comandwww.googleadservices.com).
Limits
- The module creates one global, the data layer under its configured name, and only when none exists, as Google's own snippet does. It loads no Google tag itself: the store's own snippet or another module does.
- In the advanced implementation Google's tags load before a choice and receive denied until the visitor allows a category; what they send while denied is Google's to decide. Use the basic implementation to keep them off the page until then.
- The rules hold back the scripts that load Google's tags. A host that also serves scripts no consent governs (a form's challenge, a map) has no rule here.
Translations
The admin wording ships in en_US and 25 more locales: the official languages of the European Union, Norwegian Bokmål and Ukrainian. The translations are machine-made; have them read by a native speaker before a store relies on them.
Tests
- Unit:
vendor/bin/phpunit -c app/code/Hryvinskyi/CookieConsentGoogle/phpunit.xml.dist; the browser part:npm testandnpm run checkinTest/Js. - Integration: the platform's harness in
dev/tests/integration. - Browser flows:
Test/E2e/, run with the@hryvinskyi/magento2-e2epackage from the shop'se2e/directory.