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

Implement consent mode v0 #361

Merged
merged 5 commits into from
Mar 1, 2024
Merged
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
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
Loading