Skip to content

Instantly share code, notes, and snippets.

@amitramani
Created August 22, 2016 01:05
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amitramani/fa7a98073b9692fe93828d2d62c07015 to your computer and use it in GitHub Desktop.
Save amitramani/fa7a98073b9692fe93828d2d62c07015 to your computer and use it in GitHub Desktop.
How to send WooCommerce Checkout Error Messages to Google Analytics as Events
function load_ga_error_script() {
// Load the JS file (jquery is a dependency)
wp_enqueue_script( 'ga-send-error-event', get_stylesheet_directory_uri() . '/js/ga-send-error-event.js', array('jquery' ) );
}
add_action( 'wp_enqueue_scripts', 'load_ga_error_script' );
jQuery(function() {
jQuery('.woocommerce').on('DOMNodeInserted', function(e) {
if (jQuery(e.target).is('.woocommerce-error')) {
// How Many Errors
var error_count = jQuery('.woocommerce-error li').size();
// This extracts the contents of the <li></li> tags, separated by commas
var messages = jQuery(".woocommerce-error li").map(function() { return jQuery(this).text() }).get().join();
// Send the GA Event
// Event Category: WooCommerce Error Event Action: checkout-error-notice Event Label: Actual Error Message shown to user
// Event Value: How many errors were shown to this user
ga("send", "event", "WooCommerce Error","checkout-error-notice", messages, error_count, {"nonInteraction": 1});
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment