Skip to content

Instantly share code, notes, and snippets.

@pacotole
Last active April 6, 2023 08:46
Show Gist options
  • Save pacotole/f5bedd5b518e155f362a747a4c5b8d34 to your computer and use it in GitHub Desktop.
Save pacotole/f5bedd5b518e155f362a747a4c5b8d34 to your computer and use it in GitHub Desktop.
Fix conflict between Google Analytics Dashboard for WP (GADWP) and WooCommerce Google Analytics Integration
<?php
/**
* ============================================================================
* Analytics fixes between GADWP and WooCommerce Google Analytics Integration
* ============================================================================
*/
function fix_double_google_analytics () {
if ( ! is_admin() && class_exists( 'WC_Google_Analytics_JS' ) ) {
// Remove analytics.js script from WooCommerce Google Analytics Integration.
// Universal analytics script is already insert by GADWP
add_filter( 'woocommerce_ga_snippet_output', '__return_empty_string' );
// Always add universal_analytics_footer from WooCommerce Google Analytics Integration.
// Prevent if not is an WC page the ecommerce data is not send with ga( 'send', 'pageview' )
add_action( 'wp_footer', array( 'WC_Google_Analytics_JS', 'universal_analytics_footer' ) );
// Replace footer pageview with an event.
// Pageview is already sent from GADWP and triggers a warning "Same web property ID is tracked twice."
// Instead send an event hit to push all ecommerce data collected on footer.
function replace_pageview_to_event ($js) {
return str_replace(
"ga( 'send', 'pageview' );",
"window.ga && ga( 'send', 'event', 'ecommerce', 'loaded'); // hit to send ecommerce data",
$js
);
}
add_filter( 'woocommerce_queued_js', 'replace_pageview_to_event' );
}
}
add_action( 'init', 'fix_double_google_analytics' );
@pacotole
Copy link
Author

pacotole commented Mar 9, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment