Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
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('.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