Skip to content

Commit

Permalink
Merge pull request #2290 from woocommerce/add/gtag-consent
Browse files Browse the repository at this point in the history
  • Loading branch information
tomalec authored Mar 4, 2024
2 parents 439ff3c + de77d2b commit 119ed3b
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 6 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ npm run -- wp-env run tests-cli -- wp wc update

- [Usage Tracking](./src/Tracking/README.md)
- [Hooks defined or used in GLA](./src/Hooks/README.md)
- [gtag consent mode](./docs/gtag-consent-mode.md)

<p align="center">
<br/><br/>
Expand Down
9 changes: 9 additions & 0 deletions docs/gtag-consent-mode.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
## Googla Analytics (gtag) Consent Mode

Unless you're running the [Google Analytics for WooCommerce](https://woo.com/products/woocommerce-google-analytics/) extension for a more sophisticated configuration, Google Listings and Ads will add Google's `gtag` to help you track some customer behavior.

To respect your customers' privacy, we set up the default state of [consent mode](https://support.google.com/analytics/answer/9976101). We set it to deny all the parameters for visitors from the EEA region. You can add an extension or CMP that delivers a banner or any other UI to let visitors update their consent in runtime.

You can also customize your own default state configuration using the `woocommerce_gla_gtag_consent` filter providing any snippet that uses [Google's `gtag('consent', 'default', {…})` API ](https://developers.google.com/tag-platform/security/guides/consent?consentmode=advanced).

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', {…})`).
25 changes: 24 additions & 1 deletion src/Google/GlobalSiteTag.php
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ function () use ( $gtag_events ) {
/**
* Activate the Global Site Tag framework:
* - Insert GST code, or
* - Include the Google Ads conversion ID in WooCommerce Google Analytics Integration output, if available
* - Include the Google Ads conversion ID in WooCommerce Google Analytics for WooCommerce output, if available
*
* @param string $ads_conversion_id Google Ads account conversion ID.
*/
Expand Down Expand Up @@ -254,6 +254,10 @@ protected function display_global_site_tag( string $ads_conversion_id ) {
<script>
window.dataLayer = window.dataLayer || [];
function gtag() { dataLayer.push(arguments); }
<?php
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
echo $this->get_consent_mode_config();
?>

gtag('js', new Date());
gtag('set', 'developer_id.<?php echo esc_js( self::DEVELOPER_ID ); ?>', true);
Expand All @@ -279,6 +283,25 @@ protected function get_gtag_config( string $ads_conversion_id ) {
);
}

/**
* Get the default consent mode configuration.
*/
protected function get_consent_mode_config() {
$consent_mode_snippet = "gtag( 'consent', 'default', {
analytics_storage: 'denied',
ad_storage: 'denied',
ad_user_data: 'denied',
ad_personalization: 'denied',
region: ['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'],
} );";
/**
* Filters the default gtag consent mode configuration.
*
* @param string $consent_mode_snippet Default configuration with all the parameters `denied` for the EEA region.
*/
return apply_filters( 'woocommerce_gla_gtag_consent', $consent_mode_snippet );
}

/**
* Add inline JavaScript to the page either as a standalone script or
* attach it to Google Analytics for WooCommerce if it's installed
Expand Down
10 changes: 5 additions & 5 deletions src/Proxies/GoogleGtagJs.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class GoogleGtagJs {
/**
* GoogleGtagJs constructor.
*
* Load the WooCommerce Google Analytics Integration extension settings.
* Load the WooCommerce Google Analytics for WooCommerce extension settings.
*/
public function __construct() {
$this->wcga_settings = get_option( 'woocommerce_google_analytics_settings', [] );
Expand All @@ -36,20 +36,20 @@ public function __construct() {
}

/**
* Determine whether WooCommerce Google Analytics Integration is already
* Determine whether WooCommerce Google Analytics for WooCommerce is already
* injecting the gtag <script> code.
*
* @return bool True if the <script> code is present.
*/
public function is_adding_framework() {
// WooCommerce Google Analytics Integration is disabled for admin users.
// WooCommerce Google Analytics for WooCommerce is disabled for admin users.
$is_admin = is_admin() || current_user_can( 'manage_options' );

return ! $is_admin && class_exists( '\WC_Google_Gtag_JS' ) && $this->is_gtag_page() && $this->has_required_settings();
}

/**
* Determine whether the current page has WooCommerce Google Analytics Integration enabled.
* Determine whether the current page has WooCommerce Google Analytics for WooCommerce enabled.
*
* @return bool If the page is a Analytics-enabled page.
*/
Expand All @@ -61,7 +61,7 @@ private function is_gtag_page(): bool {
}

/**
* In order for WooCommerce Google Analytics Integration to include the Global Site Tag
* In order for WooCommerce Google Analytics for WooCommerce to include the Global Site Tag
* framework, it needs to be enabled in the settings and a Measurement ID must be provided.
*
* @return bool True if Global Site Tag is enabled and a Measurement ID is provided.
Expand Down

0 comments on commit 119ed3b

Please sign in to comment.