Skip to content

Commit

Permalink
Merge pull request #361 from woocommerce/add/consent-mode-v0
Browse files Browse the repository at this point in the history
  • Loading branch information
tomalec authored Mar 1, 2024
2 parents f92124b + d8c750c commit d221ed3
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 1 deletion.
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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', {…})`).
34 changes: 34 additions & 0 deletions assets/js/src/config.js
Original file line number Diff line number Diff line change
@@ -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',
];
11 changes: 10 additions & 1 deletion assets/js/src/tracker/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { config } from '../config';
import { config, EEARegions } from '../config';
import * as formatters from './data-formatting';

let instance;
Expand Down Expand Up @@ -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, {
Expand Down

0 comments on commit d221ed3

Please sign in to comment.