Tagged: event tracking, form, google analytics
-
AuthorPosts
-
February 19, 2015 at 3:29 am #398611
Hey!
Sorry to bother you again, but I can’t get the following out of my head :) Re tracking form submissions via Google Analytics: What would it take to only track valid form submissions? As the code I’m currently using tracks every submit button click, even when the form is invalid / empty.
Will changing .on(‘click’ to .submit( do the trick? Or how about adding a check to the event tracking code, which checks whether the form validation script outputs errors? Just my 2 cents :)
Kind regards,
Ralph
February 19, 2015 at 8:01 am #398692Hey Ralph!
Set the contact form to redirect to another page on submission and put a code block containing the tracking code there.
Cheers!
JosueFebruary 19, 2015 at 6:02 pm #399085Hey Josue,
Thank you for clearing things up!
Re Enfold forms I’d like to suggest a feature similar to the Contact Form 7 feature described here: http://www.astralwebinc.com/contact-form-7-wp-how-to-add-event-tracking-on-new-universal-google-analytics/
The described CF7 feature only triggers the tracking code when a form submissions is valid; pretty neat :) Perhaps such a feature could also work for Enfold buttons.
Kind regards,
Ralph
February 19, 2015 at 8:48 pm #399237Hey Ralph!
That’s actually possible with the Enfold form but requires you to edit js/shortcodes.js, just put the GA code below line 2869:
send_ajax_form(); // ga code here
It will affect all forms though, if you are using multiple forms i’d recommend using the CF7-approach instead, let me know if you need help applying the Enfold form styles to it.
Regards,
JosueFebruary 20, 2015 at 1:37 pm #399599Hey Josue,
Because of theme updates I prefer not to modify too much of the core code (even though I’m using a child theme). So your input got me thinking: How about hooking the tracking code to function checkElements()? So, basically rewrite the tracking code, in functions.php, and turn it into a hook. Would that be possible?
If not, I’m going to leave things as they are, as I don’t want to make things overly complex :)
Kind regards,
Ralph
February 20, 2015 at 8:00 pm #399849Hey Ralph!
As far as i know is not possible to affect a JS file from functions.php without directly modifying it.
Regards,
JosueFebruary 21, 2015 at 5:03 am #399987Hey Josue,
Because of your post, I dropped the hook idea. Then I tried to call the validation function within js/shortcodes.js via functions.php… this resulted in an epic fail. Finally I had a really simple idea, which actually seems to be doing the trick (only track valid form submissions). I simply added the following check:
if (!$('p').hasClass('error')) {
So the final tracking code looks as follows:
$('.avia_ajax_form button[type="submit"]').on('click', function() { if (!$('p').hasClass('error')) { ga('send', 'event', 'button', 'click', 'button-3'); } });
So far I’ve only tested it with an ‘alert’ and it seems to be working. Could you please let me know what you think of this approach / the check?
Kind regards,
Ralph
February 21, 2015 at 11:50 pm #400141Hey Ralph!
Yes, that should work, i’d suggest targeting only form paragraph elements:
$('.avia_ajax_form button[type="submit"]').on('click', function() { if (!$('.avia_ajax_form p').hasClass('error')) { ga('send', 'event', 'button', 'click', 'button-3'); } });
Best regards,
JosueFebruary 24, 2015 at 2:59 pm #401441Hey Josue,
As per your suggestion, I’ve added the form class to the check. I’ve tested the forms real-time via Google Analytics and the check works like a charm; the tracking code only tracks valid submissions now.
Thank you very much for your input and patience!
Kind regards,
Ralph
February 24, 2015 at 7:51 pm #401656You are welcome Ralph, always glad to help :)
Regards,
Josue -
AuthorPosts
- The topic ‘How to track only validated form submissions with Google Analytics goals?’ is closed to new replies.