2 ms·
We just ran into a problem with Google Analytics trying to track opening clicks by sending an event to GA. Turns out when you click a link to open it, the brow
by kelseydh 11y ago
We just ran into a problem with Google Analytics trying to track opening clicks by sending an event to GA. Turns out when you click a link to open it, the browser page would load before the event to GA could be sent.
Screwed up a huge amount of our click tracking data on GA.
- gprasanth 11y agowindow.addEventListener('unload', function () { var now = Date.now(); while (Date.now() < now + 300) {} });
- lpsz 11y agoWhy not just use an event handler which sends the event to GA, and after that, follows your link? They provide a callback and everything... var outboundLink = function(url) { ga('send', 'event', 'button', 'click', {'hitCallback': function () { document.location = url; } }); } <a href="#" onclick="outboundLink('https://www.foo.com'); return false;">Foo</a> See: https://developers.google.com/analytics/devguides/collection/analyticsjs/events https://developers.google.com/analytics/devguides/collection...
- NathanKP 11y agoAnd now you have a website that feels slow to respond to link clicks.
- dudus 11y agoYou can also do ga('send', 'event', 'button', 'click', {transport: 'beacon'}); This will work even after you redirect to another page but only if the browser supports navigator.sendBeacon(). Currently not IE or Safari.
- simonw 11y agoThis isn't a problem with GA, it's a general problem with trying to track outbound link clicks - when your browser navigates away it kills execution of scripts, often preventing an event from being fired back to the server. There are solutions, but they're all pretty unpleasant. The most common is to use an onclick handler on your outbound link that only navigates after the tracking event has been fired. Google Analytics cover this in their own documentation here: https://support.google.com/analytics/answer/1136920?hl=en https://support.google.com/analytics/answer/1136920?hl=en