diff --git a/README.md b/README.md index 5bc4879c..a785ee5b 100644 --- a/README.md +++ b/README.md @@ -35,3 +35,26 @@ Alternatively, run `npm run lint:php:diff` to run coding standards checks agains ## Docs - [Hooks defined or used in Google Analytics for WooCommerce](./docs/Hooks.md) + +### Consent Mode + +The extension sets up [the default state of consent mode](https://developers.google.com/tag-platform/security/guides/consent?hl=en&consentmode=advanced#default-consent), denying all parameters for the EEA region. You can append or overwrite that configuration using the following snippet: + +```php +add_action( 'wp_enqueue_scripts', function () { + $customConsentConfig = " + gtag( 'consent', 'default', { + analytics_storage: 'granted', + ad_storage: 'granted', + ad_user_data: 'denied', + ad_personalization: 'denied', + region: 'ES', + } );"; + + wp_register_script( 'my-custom-consent-mode', '', array('woocommerce-google-analytics-integration'), null, false ); + wp_add_inline_script( 'my-custom-consent-mode', $customConsentConfig ); + wp_enqueue_script( 'my-custom-consent-mode' ); +} ); +``` + +After the page loads, the consent for particular parameters can be updated by other plugins or custom code, implementing UI for customer-facing configuration using [Google's consent API](https://developers.google.com/tag-platform/security/guides/consent?hl=en&consentmode=advanced#update-consent) (`gtag('consent', 'update', {…})`). diff --git a/assets/js/src/config.js b/assets/js/src/config.js index 0477cb8b..7c072e42 100644 --- a/assets/js/src/config.js +++ b/assets/js/src/config.js @@ -1,2 +1,36 @@ /* global wcgai */ export const config = wcgai.config; +export const EEARegions = [ + 'AT', + 'BE', + 'BG', + 'HR', + 'CY', + 'CZ', + 'DK', + 'EE', + 'FI', + 'FR', + 'DE', + 'GR', + 'HU', + 'IS', + 'IE', + 'IT', + 'LV', + 'LI', + 'LT', + 'LU', + 'MT', + 'NL', + 'NO', + 'PL', + 'PT', + 'RO', + 'SK', + 'SI', + 'ES', + 'SE', + 'GB', + 'CH', +]; diff --git a/assets/js/src/tracker/index.js b/assets/js/src/tracker/index.js index f1244f9f..25b69071 100644 --- a/assets/js/src/tracker/index.js +++ b/assets/js/src/tracker/index.js @@ -1,4 +1,4 @@ -import { config } from '../config'; +import { config, EEARegions } from '../config'; import * as formatters from './data-formatting'; let instance; @@ -39,6 +39,15 @@ class Tracker { window[ config.tracker_function_name ] = gtag; + // Set up default consent state, denying all for EEA visitors. + gtag( 'consent', 'default', { + analytics_storage: 'denied', + ad_storage: 'denied', + ad_user_data: 'denied', + ad_personalization: 'denied', + region: EEARegions, + } ); + gtag( 'js', new Date() ); gtag( 'set', `developer_id.${ config.developer_id }`, true ); gtag( 'config', config.gtag_id, {