10

Can I send a Google Analytics event and immediately navigate away, like so?

_gaq.push(['_trackEvent', 'foobar']);
window.location = "/";

If Google Analytics does some kind of AJAX request when this is called then it should work whether we stay on the page or not. My concern is that it seems it may sometimes just be putting stuff in an array for later processing. I'm thinking this only happens initially, when Google Analytics hasn't had time to be initialized yet, but I'd like to be certain of this.

I did a test with GA debug, and it seemed to have worked, but I'm not sure if that means it always will depending on loading speed and what not.

Can I do this and never lose any events?

Alex Turpin
  • 46,743
  • 23
  • 113
  • 145

4 Answers4

8

The way I've done it is like so:

_gaq.push(['_trackEvent', '...', '...', '...']);
_gaq.push(function(){
    // do stuff here
});

$('#logo').on('click', function(){
    var curPage = window.location.href;
    _gaq.push(['_trackEvent', curPage, '#logo']);
    _gaq.push(function(){
        window.location.href = '/';
    });
});

The second push call will always run after the first one, because Google queues all push calls, so that the first one will run and complete, then the second one will run and complete. Google lets you put functions in the push method so you can queue them.

Source: https://developers.google.com/analytics/devguides/collection/gajs/#PushingFunctions

emma.fn2
  • 229
  • 1
  • 10
  • Be sure to do lot's of testing when trying this. Just because the _trackEvent _gaq.push call has returned doesn't mean that the data has been recorded. Internally, there's an asynchronous image request being used to send the data, and you're dependent on what stage the request is in when the page is torn down for the new page. – mike Apr 03 '13 at 14:45
  • I thought that if _gaq.push() has actually sent the request off, it wouldn't matter if I proceeded immediately after -- because once the request is sent, the browser doesn't need to do anything else, does it? Unless there was an error with sending the tracking, but that would likely be broken anyway, regardless of whether I redirected or not. – emma.fn2 Apr 03 '13 at 19:16
  • Once the image request actually goes out, everything's good -- the issue is when the browser actually makes the request. What I think is going on is that _trackEvent makes an image request, which the browser puts on a network request queue, which is handled by a different thread. – mike Apr 04 '13 at 14:44
6

I add a slight delay (via setTimeout) if the new page isn't being opened in a new window.

I haven't had a chance to try this yet, but Google's new Universal Analytics has a hitCallback function that is executed after the data has been sent.

mike
  • 7,137
  • 2
  • 23
  • 27
1

@mike mentions hitCallback method which is described in analytics.js documentation:

In some cases, like when you track outbound links, you might want to know when the tracker is done sending data. That way you can send a user to their destination only after their click has been reported to Google Analytics. To solve this, the send command allows you to specify a hitCallback function in the field name object that will execute as soon as analytics.js has completed sending data.

Which is fantastic, except the snippet is still in public beta. If for some reason (ahem technophobic policies ahem) you're limited to ga.js functionality, you can use this workaround:

_gaq.push(['_set', 'hitCallback', function(){
  document.location='someOtherPage.html';   
}]);
_gaq.push(['_trackEvent', 'category', 'event', 'value']);

Another sensible suggestion is to have a fallback to have the callback executed immediately if _gaq is not defined.

Community
  • 1
  • 1
Oleg
  • 24,465
  • 8
  • 61
  • 91
0

It looks like your concern about losing the event is legitimate, see this question. One answer there seems a little fragile with regards to Google changing their code, but would let you confirm the event tracking before navigating to the link. That would probably be "near-immediate".

Community
  • 1
  • 1
Dan Wich
  • 4,923
  • 1
  • 26
  • 22