The official suggestion for tracking outbound links with (the asynchronous version of) Google Analytics is to push an tracking event into the queue, like:
gaq.push(['_trackEvent', 'Outbound', 'http://foo.bar/']);
setTimeout('document.location = "http://foo.bar"', 100);
Would it not be better to push an anonymous function into the GA queue, like:
gaq.push(['_trackEvent', 'Outbound', 'http://foo.bar/']);
gaq.push(function() { document.location = 'http://foo.bar/'; });
In the setTimeout
version, there's no guarantee that the event will be processed before the redirect occurs, whereas in the second version, it would only redirect after the event is processed—right?
Outbound clicks are classifieds as "Enhanced Measurement" in Google Analytics 4. In the previous version of Google Analytics i.e. Universal Analytics, tracking outbound clicks required you to set up tracking by placed a small JavaScript call on your links or set them up via Google Tag Manager.
In the Google Tag Manager workspace, go to Variables → New. We'll create an Auto-Event Variable and select the variable type Element URL. The Component Type for this variable will be Is Outbound. This setting will check whether or not the clicked link is from the same domain on which Google Tag Manager is installed.
Outbound clicks provide a measure of the amount of traffic your ads help send to your website or app.
The problem with doing your suggestion is that it wouldn't have time to do the request before the page changes.
The browser won't wait for those 2 events to be complete before navigating the user onwards. If you are familiar with jQuery, it would be similar to adding a click
event handler to a link, adding an ajax
request to that handler, but not putting an event.preventDefault()
in there. In other words, the ajax request would not be handled as the user has already gone to the next page.
edit as you mentioned in the comments, this is irrelevant if you apply return false
to the links as well.
If you can actually push a function like you illustrated in your example, I really don't see why it wouldn't work better then, with the exception that the first request is timing out for some reason, making the user wait far beyond the 100ms they usually would.
What about the users that have google blocked? There are lot of addons/programs etc which can completely block out google analytics, adsense etc. will those users have normal user experience?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With