Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Track event in google analytics upon clicking form submit

I need to track an event in google analytics when someone fills out a form and clicks submit. The resulting page that comes up is a standard dashboard-type page, so in order to track the event on that page I'd have to pass in the event in the url and then read the url and output the google analytics event tracking javascript code based on it. This is a frequently bookmarked page though and page that is reloaded, clicked back to, etc. So I'd really rather not pass tracking events in the URL and screw up the analytics.

Instead, I'd much rather do something like the following jQuery code on the page with the form:

$('#form_id').submit(function() {   _gaq.push('_trackEvent', 'my category', 'my action'); }); 

The problem I fear with the above is that I'm going to miss some events being tracked because immediately after calling that javascript the browser is going to submit the form and go to another webpage. If the utm.gif tracking image isn't loaded in time, I miss the event :(.

Is my fear justified? How do I ensure I don't miss events being tracked?

like image 520
at. Avatar asked Nov 03 '10 11:11

at.


People also ask

Can Google Analytics track link clicks?

Can Google Analytics Track Link Clicks? Yes Google Analytics can track both internal and external link clicks through what is called “event tracking”. Custom code snippets or events from Google Tag Manager can help feed these events into Google Analytics based on your specific requirements.


2 Answers

Use Google Analytics hitCallback

You can specify a custom callback function on the tracker object.

_gaq.push(['_set', 'hitCallback', function(){}]); 

The callback is invoked after the "hit is sent successfully."

If you want to track a click on a submit button and send the form afterwards you can use the following code (uses jQuery) for your event:

var _this = this; // The form input element that was just clicked _gaq.push(['_set','hitCallback',function() {     $(_this).parents('form').first().submit(); // Submit underlying form }]); _gaq.push(['_trackEvent', 'My category', 'My action']); return !window._gat; // Ensure that the event is bubbled if GA is not loaded 

Or as onclickone liner for your <input type="submit"> element:

onclick="var _this=this;_gaq.push(['_set','hitCallback',function(){$(_this).parents('form').first().submit();}]);_gaq.push(['_trackEvent','My category','My action']);return !window._gat;" 

What it does it that it tracks the event My category/My action, uses jQuery to find the underlying form element of the submit button just pushed, and then submits the whole form.

See: Google Analytics - Sending Data to Google Analytics - Hit Callback (thanks supervacuo)

UPDATE If you're using modern analytics.js code with ga() function defined, you can write this as following:

var _this = this; ga('send', 'event', 'My category', 'My action', {     'hitCallback': function() {         $(_this).parents('form').first().submit();     } });  return !window.ga; 
like image 144
flu Avatar answered Sep 18 '22 07:09

flu


There are only 2 ways to ensure, 100%, that all form submissions (amongst users who have JS enabled and who don't block GA) is as follows:

  • You can do an AJAX submit, and then not have to worry about the page changing, and thus have all the time in the world for GA to process AND your new page to load in place of the old one.
  • You can force the form to open its action in a new window, thus leaving all background processes on the main page working, and preventing the race condition you're worried about.

The reason for this is that Google Analytics does not have a callback function, so you can't ever be certain you're capturing all of the submits, even if you put a 10 second lag.

Alternately, you can just pass a GET value to the submitted page and setup a check on that page for the value. If its set, you can send a trackEvent call.

like image 36
Yahel Avatar answered Sep 18 '22 07:09

Yahel