Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending Google Analytics event then immediately navigating away

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?

like image 293
Alex Turpin Avatar asked Apr 02 '13 17:04

Alex Turpin


People also ask

How long does it take for events to show up in Google Analytics?

Many of your reports and explorations can take 24-48 hours to process data from your website or app.

Why are my events not showing on Google Analytics?

If you don't see any data at all in your Real-Time reports, the most likely cause is either in the Google Analytics tracking code or GA/GTM misconfiguration. You should check your tags in Google Tag Manager, Google Analytics filters, and also use browser extensions for debugging.

Can I send event data from backend to Google Analytics?

It allows businesses to send data and events from their servers back to the ads' servers. Google Analytics Measurement Protocol is one of the solutions that allow advertisers to overcome those restrictions and collect the data in the backend and send it directly from the server to Google Analytics Servers via the API.


3 Answers

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

like image 120
emma.fn2 Avatar answered Sep 20 '22 00:09

emma.fn2


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.

like image 44
mike Avatar answered Sep 18 '22 00:09

mike


@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.

like image 25
Oleg Avatar answered Sep 18 '22 00:09

Oleg