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>
<script type="text/javascript">
try{
var pageTracker = _gat._getTracker("UA-XXXX-1");
pageTracker._trackPageview();
} catch(err) {}
</script>
I have this just before the closing </head>
tag:
<script type="text/javascript">
function recordOutboundLink(link, category, action) {
try{
var pageTracker = _gat._getTracker("UA-XXXX-1");
pageTracker._trackEvent(category, action);
setTimeout('document.location = "' + link.href + '"', 100);
} catch(err)
{}
}
</script>
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>