Slava Fomin II
Slava Fomin II

Reputation: 28621

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?

Upvotes: 6

Views: 4335

Answers (1)

mike
mike

Reputation: 7177

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.

Upvotes: 9

Related Questions