Last active
April 6, 2023 08:46
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Fix double analytics.js script insertion and "Same web property ID is tracked twice." problem.
More info:
https://premmerce.com/woocommerce-google-analytics-integration-plugins/
https://developers.google.com/analytics/devguides/collection/analyticsjs/enhanced-ecommerce#sending-data