I made slight changes to the code described here in order to track outbound link clicks. This is my code:
function recordOutboundLink(link, label) {
try {
var myTracker=_gat._getTrackerByName();
_gaq.push(['myTracker._trackEvent', 'Outbound Links', label, link.href ]);
setTimeout('document.location = "' + link.href + '"', 100)
}catch(err){}
}
Then I added it to my href tags like so:
<a href="http://example.com/user/5" onclick="recordOutboundLink(this, 'example.com');return false"></a>
I think it’s not working, though. It’s really hard to check if it works or not. How do I check if it’s working or not?
EDIT: Using google analytics debugger for chrome I was able to see what’s going on. It displays:
Account ID : UA-XXXXX-X
It’s odd because normal page tracking is working as expected. This is how I set up google analytics code:
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'my-id']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
Look for the
__utm.giftracking pixel request. A few different ways of doing this are:The analytics code on the page probably has a _trackPageview, so as the page loads you’ll see an initial __utm.gif.
When _trackEvent fires, there should be a new __utm.gif request being made. Parameters to examine in __utm.gif URL are:
Also, (as pointed out by @Eduardo), take a look at Google Analytics Basic Debugging