Viewing 10 posts - 1 through 10 (of 10 total)
  • Author
    Posts
  • #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

    #398692

    Hey Ralph!

    Set the contact form to redirect to another page on submission and put a code block containing the tracking code there.

    Cheers!
    Josue

    #399085

    Hey 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

    #399237

    Hey 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,
    Josue

    #399599

    Hey 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

    #399849

    Hey Ralph!

    As far as i know is not possible to affect a JS file from functions.php without directly modifying it.

    Regards,
    Josue

    #399987

    Hey 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

    #400141

    Hey 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,
    Josue

    #401441

    Hey 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

    #401656

    You are welcome Ralph, always glad to help :)

    Regards,
    Josue

Viewing 10 posts - 1 through 10 (of 10 total)
  • The topic ‘How to track only validated form submissions with Google Analytics goals?’ is closed to new replies.