2

Im currently playing around with Google Analytics event tracking and can see a few plugins floating around, but im trying to see if i can put something together that would be very generic and simple to use.

<script>
$("form").submit(function(){
    _gaq.push(['_trackEvent', $(this).attr('name'), 'Form Submission', 'The-form-name',, false]);
});
</script>

I figure if i have a few of these simple snippets of code lingering around on my page this way i can track when a form is submitted and by leveraging form names and form id's i can see when they are submitted.

I figure i can probably use this simple method to track link clicks etc but was just wondering whether anyone has done anything like this before and would have any suggestions as to whether this is a reliable method for tracking events with Google Analytics.

The reason why im looking at doing it like this is because I will have everything generated dynamically on the site and the majority of tags will have id's associated to them, so this way i wont need to add them directly into the html by using the onclick event handlers and stuff like that.

thanks for any advice

hexalys
  • 5,177
  • 3
  • 31
  • 56
user125264
  • 1,809
  • 2
  • 27
  • 54

1 Answers1

4

You can do it this way but you will have to delay the form submit in order to leave time for the trackEvent to proceed. Or else the form will be submitted right away, preventing the trackEvent from completing and being sent out. The same applies for tracking clicks.

This should work:

<script>
$("form").submit(function(e){
    e.preventDefault();//prevent the form from being submitted
    _gaq.push(['_trackEvent', $(this).attr('name'), 'Form Submission', 'The-form-name',, false]);
    $(this).off('submit');//prevent 'this' jquery submit event recursion
    setTimeout(function(){//delay the form submit
        e.currentTarget.submit();//submit form after the track event had time to complete
    }, 350);//350 ms
});
</script>

NB: While this guarantees that your form will be submitted, it does not guarantee that the submit event will be tracked 100% of the time. But the 200ms 350ms should be good enough to get over 90% properly tracked. (Edit: I bumped the delay with considerations for high latency cellular networks).

In this particular example of a form onsubmit on a low-latency connection. The most a full Google Ad tracker script can make is 4 requests, each in the 30-40 ms range (below the 350ms delay indicated). And technically, you only have to account for the latency of the first request being sent to reach Google, for the event to be saved in Google Analytics. Network tab in Chromium developer tools

The only other way to insure 100% that the event got tracked, is to use the Hit Callback as described at: https://stackoverflow.com/a/12461558/1647538 However, the caveat of the Hit Callback is that it could prevent the form from being submitted, thus the Hit Callback method is not really advisable here.

Community
  • 1
  • 1
hexalys
  • 5,177
  • 3
  • 31
  • 56
  • 3
    You can always combine the two. Use hitCallback with a timeout as a fallback - if it hasn't returned anything within 1 - 2 seconds, force a redirect. – MisterPhilip Oct 19 '13 at 20:00
  • @MisterPhilip Yes, I am aware of that. But the purpose of this question is something simple and straight forward to use. So I didn't want to bring that unnecessary added complication. – hexalys Oct 19 '13 at 21:50
  • 1
    @nemo I am not completely on board with your edit. A tracking event is technically only one request as far as GA alone is concerned. The others you show, seem to be generated from other Google Ad script(s) in parallel. i.e. 4 requests is not the norm in regards to this question. Also the tracking request only need to be sent. It's doesn't really matter if you get the tracking image or not in response. So the latency is only one way. I made re-edits to address this, while keeping your example. – hexalys Sep 26 '15 at 06:48
  • @hexalys thank you! I mostly just wanted to add the screenshot to give an idea, sorry for imprecisions in describing it. – Nemo Sep 26 '15 at 09:33
  • For some reason, the above won't work for me unless I substitute all the occurrences of `$(this)` with `$("form")`. – coccoinomane May 22 '16 at 12:05
  • 1
    Thanks @GuidoWalterPettinari I was used the wrong scope in setTimeout :( Now corrected! You can use the more proper updated code instead of 'form'. – hexalys May 22 '16 at 23:22