I'm having trouble tracking outbound links from my page. I can see the visits to the page in Google Analytics, but still I can't see what I'm doing wrong (of course I have replaced the UA-XXXX code with my own code).

Can anyone spot anything wrong with the below code?

One interesting thing is that when I include the tracking function in the <head> section, my page doesn't load correctly in Firefox. So, I checked the syntax and all seems ok.

I've added this just before the closing </body> tag:

<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
<script type="text/javascript">
var pageTracker = _gat._getTracker("UA-XXXX-1");
} catch(err) {} 

I have this just before the closing </head> tag:

<script type="text/javascript">
function recordOutboundLink(link, category, action) {
        var pageTracker = _gat._getTracker("UA-XXXX-1");
        pageTracker._trackEvent(category, action);
        setTimeout('document.location = "' + link.href + '"', 100);
    } catch(err)

This is an example of an outbound link:

<a href="http://www.blahblah.com" title="blah_site" target="_blank" onClick="recordOutboundLink(this, 'My Outbound Links', 'BLAH');return false;">blah site</a>
  • 1
    Just this code works just fine. You might want to think about giving the setTimeout a little longer than 100 milliseconds (I prefer 400), but I ran this code and inspected the HTTP headers, and at least in my browser, it caught the event before the page redirected.
    – Yahel
    Commented Dec 22, 2010 at 17:21
  • @yc01: Thanks for the info. I didn't think to even look at the timing. I'll try that. Did your event register with GA? Can you see the click in the data? I'm also concerned that there may be something else broken in the chain, but first let me try the timing before I make any other hypotheses.
    – milesmeow
    Commented Dec 24, 2010 at 15:02
  • Does your website creates other GA events?
    – Osvaldo
    Commented Jul 29, 2011 at 18:04

1 Answer 1


As the comments suggest, you might want to set the timeout higher. Here is a similar script that uses 400 milliseconds: http://www.blastam.com/blog/index.php/2013/03/how-to-track-downloads-in-google-analytics-v2/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.