13

I wish to use event tracking to record clicks on a specific type of link to another website. I am using jQuery, and the code I currently have is:

$('a.website').click(function(event) {
    var href = $(this).attr('href');
    try {
        _gaq.push(['_trackEvent', 'website', 'click', href]);
    } catch(err) {}
});

However, after seeing referrer information from the other site, I don't belive this is accurately tracking the clicks, probably because _gaq.push is asynchronous, and the request has not been received before the browser navigates to the url, and terminates any javascript running on the current page.

Is there any way to detect that the _gaq.push function has completed successfully, so I can use event.preventDefault() and document.location to navigate to the link after the event has been recorded?

Matt Austin
  • 2,613
  • 1
  • 27
  • 32

3 Answers3

12

Use Google Analytics hitCallback

You can set a custom callback on the tracker object by using:

_gaq.push(['_set', 'hitCallback', function(){}]);

I described the code for it a little more in-depth here: Track event in Google Analytics upon clicking form submit

Community
  • 1
  • 1
flu
  • 14,307
  • 8
  • 74
  • 71
  • I don't see a `_set` method in [Google's tracker documentation](https://developers.google.com/analytics/devguides/collection/gajs/methods/). Can you provide more information on this method? – Matt Austin Sep 19 '12 at 13:09
9

How about solution from this answer?

  // Log a pageview to GA for a conversion
  _gaq.push(['_trackPageview', url]);
  // Push the redirect to make sure it happens AFTER we track the pageview
  _gaq.push(function() { document.location = url; });
Community
  • 1
  • 1
fliptheweb
  • 1,168
  • 1
  • 6
  • 14
  • Thanks for the suggestion. I'm going to attempt a test using this, and I'll report back. – Matt Austin Nov 16 '11 at 06:18
  • I'm going to go with this method. I was unable to collect enough data to prove that this method was more accurate, but it didn't result in any more missing events. – Matt Austin Nov 28 '11 at 07:41
  • 11
    This actually does not work. Watch your network panel in your Firebug or Webkit Inspector. You'll see __utm.gif gets interrupted by the form submit and never gets tracked. – Eric D. Fields Mar 22 '12 at 17:37
  • @EricD.Fields i'm wondering if the request gets executed anyway, and the tracking succeeds, even if the __utm.gif is not sent back to the client. is it possible? – caesarsol Nov 13 '14 at 09:31
  • @caesarsol is correct, it does not matter if the client receives a response as long as the server received the request. – ColinM Jan 23 '15 at 21:55
1

I've faced similar issue with the external link tracking. My salvation was using OnMouseDown event instead of OnClick.

Nikolay
  • 2,206
  • 3
  • 20
  • 25
  • 1
    Although this may get better accuracy through a 'head-start', I'd think this method still wouldn't guarantee that the event track request has been sent before the browser navigates elsewhere? Also this may cause false readings because the user may mouse-down and then move off the link. – Matt Austin Nov 18 '11 at 08:11
  • You can try testing it out with the Real-time tracking in the new interface of GA. OnMouseDown works for me, try OnMouseUp or something more complicated. – Nikolay Nov 18 '11 at 17:24
  • What about users who press the Enter button instead of using the mouse? – dotancohen Apr 21 '16 at 08:32