How to setup Google Analytics event tracking in Divi?

Hi Guys,

This tutorial can give you quick easy fix for setting up Google Analytics event tracking in Divi Contact form. Before going forward, I assumed your website is well  set up with ga code and it’s working fine.

Here logic is to use jQuery(Javascript library) to track form submit an event and keeping validation to ensure there is no error. This solution may not cover all the cases but cover almost 99% use cases.

Kindly add this js script in your custom .js file or can use WP add_inline_script().

jQuery(document).ready(function($){
	$('.et_pb_contact_form').submit(function(event){
		let dcHasError = true;
		setTimeout(function() { // now wait 1000 milliseconds...
          $('.et_pb_contact_form input').each( function() {
          	console.log($(this).hasClass('.et_contact_error'))
          	if( false === $(this).hasClass('.et_contact_error') ) {
          		dcHasError = false;
          		return false;
          	}
          })
          if( true === dcHasError) {
          	ga('send', {
    		  hitType: 'event',
    		  eventCategory: 'event category name',
    		  eventAction: 'Form Submit',
    		  eventLabel: 'event category name'
    		});
          }
        },1000);
	})
});

Hope this helps to anyone struggling to tracking Divi contact form.

Thanks

Join the Conversation

4 Comments

      1. Thanks for the code. I can do the form validation just fine with it, but for some reason the event is not registering in Google Analytics. I tried another kind of Jquery code for contact forms in divi so i know the events track just fine. That code however doesnt do validation and registers a submission regardless of whether form was submitted or not.

        Any help would be much appreciated

        Like

Leave a comment