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

Add integration with WP Consent API #425

Merged
merged 14 commits into from
May 29, 2024
44 changes: 44 additions & 0 deletions assets/js/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,33 @@ if ( window.ga4w ) {
window.addEventListener( 'load', warnIfDataMissing );
}
}

const consentMap = {
statistics: [ 'analytics_storage' ],
marketing: [ 'ad_storage', 'ad_user_data', 'ad_personalization' ],
};

function initializeTracking() {
// eslint-disable-next-line camelcase
tomalec marked this conversation as resolved.
Show resolved Hide resolved
if ( typeof wp_has_consent === 'function' ) {
tomalec marked this conversation as resolved.
Show resolved Hide resolved
window.wp_consent_type = 'optin';

const consentState = {};

for ( const [ category, types ] of Object.entries( consentMap ) ) {
// eslint-disable-next-line camelcase, no-undef
martynmjones marked this conversation as resolved.
Show resolved Hide resolved
if ( wp_has_consent( category ) ) {
types.forEach( ( type ) => {
consentState[ type ] = 'granted';
} );
}
}

if ( Object.keys( consentState ).length > 0 ) {
gtag( 'consent', 'update', consentState ); // eslint-disable-line no-undef
tomalec marked this conversation as resolved.
Show resolved Hide resolved
}
tomalec marked this conversation as resolved.
Show resolved Hide resolved
}

const getEventHandler = setupEventHandlers( window.ga4w.settings );

classicTracking( getEventHandler, window.ga4w.data );
Expand All @@ -30,3 +56,21 @@ function warnIfDataMissing() {
);
}
}

document.addEventListener( 'wp_listen_for_consent_change', function ( event ) {
const consentUpdate = {};

const types = consentMap[ Object.keys( event.detail )[ 0 ] ];
const state =
Object.values( event.detail )[ 0 ] === 'allow' ? 'granted' : 'deny';

if ( types !== undefined ) {
types.forEach( ( type ) => {
consentUpdate[ type ] = state;
} );

if ( Object.keys( consentUpdate ).length > 0 ) {
gtag( 'consent', 'update', consentUpdate ); // eslint-disable-line no-undef
martynmjones marked this conversation as resolved.
Show resolved Hide resolved
}
}
} );
3 changes: 3 additions & 0 deletions includes/class-wc-google-analytics.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ public function __construct() {
// utm_nooverride parameter for Google AdWords
add_filter( 'woocommerce_get_return_url', array( $this, 'utm_nooverride' ) );

// Mark extension as compatible with WP Consent API
add_filter( 'wp_consent_api_registered_' . plugin_basename( __FILE__ ), '__return_true' );

// Dequeue the WooCommerce Blocks Google Analytics integration,
// not to let it register its `gtag` function so that we could provide a more detailed configuration.
add_action(
Expand Down
2 changes: 1 addition & 1 deletion includes/class-wc-google-gtag-js.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ private function register_scripts(): void {
function %2$s(){dataLayer.push(arguments);}
// Set up default consent state.
for ( const mode of %4$s || [] ) {
%2$s( "consent", "default", mode );
%2$s( "consent", "default", { ...mode, "wait_for_update": 500 } );
tomalec marked this conversation as resolved.
Show resolved Hide resolved
}
%2$s("js", new Date());
%2$s("set", "developer_id.%3$s", true);
Expand Down