Hacker News new | ask | show | jobs
by kelseydh 3997 days 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.

3 comments

Why 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...
You 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.
And now you have a website that feels slow to respond to link clicks.
This 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

window.addEventListener('unload', function () { var now = Date.now(); while (Date.now() < now + 300) {} });