Google Analytics Events - Triggering a user exit - javascript

I am looking for a way to simulate with Google Analytics _gaq.push that the user has left the page (so GA stops counting on Average time on site).
This is some code I use to track AJAX pages in Google Analytics (it counts a page view):
_gaq.push(['_trackPageview', '/ajax/save']);
What I am looking for is something such as:
_gaq.push(['_exitCurrentPage', 'http://example.com/']);
Does anything with that functionality exist?
Thanks in advance,
Xavi

I don't think that there's any specific functionality in GA for this but you could just set a cookie called something like dont_track then set it to simulate the page exit. You could then check for the cookie's existence before calling your normal tracking code and stop execution if it exists.

Related

Google Analytics Events Canceled

There is a serious issue with attaching Google Analytics event tracking to a hyperlink on a page (as demonstrated in the documentation).
Example:
<a
href="http://www.example.com/"
onclick="_gaq.push(['_trackEvent', 'Links', 'Click', 'Example Link']);"
>
Example!
</a>
When user clicks on such a link in the Google Chrome browser, GA is starting to send the request to the server (event tracking), but browser is also starting to load another page (following the link) and the GA request is getting "canceled", so the event is not reaching the GA server and is not shown in the statistics.
How can we counter it?
For example we can delay the navigation to a linked page while event is reaching the GA server, but how can we do this if GA requests are asynchronous and we can't find any callback functionality in the documentation.
Is there a way to get notified when pushed command gets executed?
With analytics.js (part of the new Universal Analytics), you can specify a hitCallback function that runs as soon as the data has been sent. See Setting the Hit Callback
With ga.js (standard Asynchronous Analytics), thee's no way to know for sure that the tracking gif request has been made (without re-writing the analytics code and sending the gif yourself). I've had good results using a short delay (150 ms) before following the link.

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?
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
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 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.
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".

Google Analytics: Is _gaq.push() a blocking function?

What I am trying to do is to redirect the user to the next page right after sending an event to Google Analytics:
_gaq.push(['_trackEvent', 'xxx' ...]);
window.location = 'some url ...';
The above code manages to register events because I can see them in the GA report. However I suspect that some events were lost because the browser redirects the user to the new page before the track pixel loads.
My questions are:
Does _gaq.push() block until the event has successfully reached Google's server?
If not, what is the best way to make achieve what I need?
Thanks!
Google has a support page that says you might want to add a delay after calling _gac.push, however they claim that this is to give the Google Analytics JavaScript a chance to load. They don't say that it's to ensure the request is sent.
First, delay the outbound click by a fraction of a second.
This delay will hardly be noticeable by the user, but it will provide the browser more time load the tracking code. Without this method, it's possible that a user can click on the outbound link before the tracking code loads, in which case the event will not be recorded.
I haven't checked #pixelfreak's claim of handling onbeforeunload, but it seems that's what they do.
This is my observation based on some quick research. That function is part of Google Analytic's asynchronous API, so it should not block anything, otherwise, it's not async. :)
Digging through the obfuscated ga.js, you can kinda see that the tracking is probably called onbeforeunload, which fires when you leave a page.
It really doesn't matter if the tracking pixel loads completely, it's a fire & forget. As long as the request is initiated and reaches Google's server, it'll get tracked. The response (in this case, the pixel image) is a by-product.
Maybe you can try this method, but I have not tried
_gaq.push(['_trackEvent', 'xxx' ...]);
_gaq.push(function(){location.reload()});
Just use 1 sec delay. Like this:
_gaq.push(['_trackEvent', 'xxx' ...]);
setTimeout(function(){window.location = 'some_url'}, 1000);

Virtual pageview in place of real pageview in Google Analytics

I'd like to have better error page reporting in Google Analytics. Currently, if someone does something on my site which causes a problem, they see an error page instead of the content they expected. The URL remains the same. So if they went to www.example.com/view_my_profile and there was a problem with their profile, they would see an error page at that URL.
What I'd like to do is send Google Analytics a virtual pageview of something like www.example.com/error/view_my_profile/ (maybe an event captures the extra parameters better?). That's easy enough. But I want this virtal pageview to happen instead of the /view_my_profile real pageview. Because that real page wasn't actually viewed and it would be registering an extra pageview on my site.
Is this as simple as leaving out the _trackPageView call in the google analytics snippet below or am I asking for trouble?
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-${gaAccount}-1']);
_gaq.push(['_trackPageview']);
Way over-complicating things..just use _trackPageView like normal but pass a value (virtual URL) to it for the URL you want it to be. It will count as a page view for the URL you pass it instead of the current URL.
You can do this without complication. As long as you load ga.js, instantiate var gaq, and set the account, you can make any calls you want, including _trackPageview with only a virtual pageview value.
You don't even need to call the organic _trackPageview -- you could just do event tracking. In fact, you might want to consider doing an organic pageview, coupled with an Event Tracking that passes some detailed error tracking info to you. Because there can be up to 4 parameters, you can log more and better structured data.
So, on your 404 page, you could call something like:
_gaq.push(['_trackEvent', '404 Error', location.pathname, document.referrer, time_stamp_value]);
(where you've previously defined time_stamp_value as a non-float Number.)
Something that simple will allow you to create hierarchies for your errors, count them more easily, and even do things like the referring page and a timestamp value, without cluttering your pageview information.

Is it possible to gracefully handle a Google Maps AJAX request timeout?

My site uses the Google Maps API. In situations where the connection to Google is slow and the map can't be rendered in a reasonable time, I'd like a Javascript callback method to be called such that I can display a useful message to the user rather than have a 'loading...' message constantly displayed.
Is this achievable?
Maybe you could have a sleep function that would check if the page has loaded yet, and after a certain time you take some sort of action.
See this posting for a situation similar to yours
setTimeout might be useful too.
so, you would have:
setTimeout((function()
{ /* test if the page is loaded,
if so, call another function
or set a flag to get out*/
}),2000); //set for 2 seconds

Categories