Google Analytics Events Canceled - javascript

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.

Related

NS Binding Aborted call in the Sitecatalyst call during a button click action

I am getting an NS Binding aborted call while passing data to Sitecatalyst upon a button click. An scAdd event is getting called but after that the tag is not able to access all the data. Some data is being sent while some is not. I tried to add a timeout after firing the event but it did not work. I've seen the s.tl() being suggested as the cause of this error but what is the change that needs to be made in that function to avoid this error? Thanks!
In short, it's nothing to worry about. Adobe's servers respond with a 1x1 transparent pixel, so the user's browser does not need to wait for that to render.
You can refer to this KB article for more details on NS_BINDING_ABORTED and how it relates to Adobe Analytics:
https://marketing.adobe.com/resources/help/en_US/sc/implement/debugger_ns_binding.html
You can also check the Analytics Community for any follow up questions.

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);

Google Analytics Events - Triggering a user exit

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.

AJAX GET race condition?

I am attempting to track events when links are clicked on my site in a method similar to the following.
Example
<script type="text/javascript">
jQuery(function($) {
// track clicks on all anchor tags that require it
$('a.track').live('click', function(e) {
// send an AJAX request to our event tracking URL for the server to track it
$.get('/events/track', {
url: $(this).attr('href'),
text: $(this).text()
});
});
});
</script>
The problem that I'm having is that a new page load interrupts the AJAX request, and so sometimes these events aren't being tracked. I know Google Analytics has a _trackPageview function that can be attached to onclick events though, and this doesn't seem to be an issue for that. I'm wondering what's different about their call vs. mine that I'm seeing this race condition, and GA isn't. e.g.:
Example
Note that I'm not worried about the result of the AJAX request...I simply want it to ping the server with the fact that an event happened.
(Also, I expect I'll get at least one answer that says to simply track the new page load from the server side, not the client side. This is not an acceptable answer for this question. I'm looking for something like how Google Analytics' trackPageview function works on the click event of anchor tags regardless of a new page being loaded.)
Running Google's trackPageview method through a proxy like Charles shows that calls to trackPageview( ) request a pixel from Google's servers with various parameters set, which is how most analytics packages wind up implementing such pieces of functionality (Omniture does the same).
Basically, to get around ansynchronous requests not completing, they have the client request an image and crunch the parameters passed in those requests on the server side.
For your end, you'd need to implement the same thing: write a utility method that requests an image from your server, passing along the information you're interested in via URL parameters (something like /track.gif?page=foo.html&link=Click%20Me&bar=baz); the server would then log those parameters in the database and send back the gif.
After that, it's merely slicing and dicing the data you've collected to generate reports.
Matt,
If you just want to make sure that the tracking pixel request is made and you don't depend upon response then just doing document.write for the tracking pixel image will do the work.
And you can do the document.write in your onclick handler.
AFA race condition between href and onclick handler of anchor element is concerned the order is well defined.
the event handler script is executed first
the default action takes place afterwards (in this case the default handler is href)
(Source : Href and onclick issue in anchor link)
But yes, if you depend upon the response of the tracking request to the server then you will have to make it synchronous.
Suggested option would be to call some javascript function to wrap the already defined onclick handlers and then in the order make the calls. Make sure that your tracking request is not asynchronous.
Though it is suggested that you should not be dependent upon the response of the tracking pixel request.

Categories