Friso Kluitenberg
Friso Kluitenberg

Reputation: 1179

Google Universal Analytics Form Submit

I'm trying to code a javascript snippet tracking form submits, with the least amount of inconvenience to the visitor. The flow I'm trying to program is this.

  1. The user clicks on submit or presses enter to submit a form (leave a comment for example).
  2. The attached form submit handler catches this event and prevent the form from submitting.
  3. The script send an google analytics event, upon hitting the callback (i don't want to use the timeout method), the handler is removed and the form is submitted

However, the problem I'm facing is that the form is not (re)submitted upon hitting the google analytics callback. The handler is removed, because submitting the form manually a second time results in normal behaviour.

The code I have is:

var formSubmitHandler = function(event){
    event.preventDefault();

    ga('send', 'event', 'Form Submit', 'Form Submit', 'Form Submit', 
        {'hitCallback':
            function () {
                jQuery(event.target).submit();
            }
        });

    return false;
}

jQuery( document ).ready(function( $ ) {

    if (typeof(ga)==='undefined')
        return;
    jQuery('form').one('submit',formSubmitHandler);
});

Any help in this matter would be greatly appreciated.

Upvotes: 0

Views: 209

Answers (1)

PeterKA
PeterKA

Reputation: 24638

You want to submit the form, not trigger the submit event again.

Please change:

jQuery(event.target).submit();

To:

event.target.submit();

Upvotes: 1

Related Questions