Custom events not being tracked in Google Analytics
-
18-06-2021 - |
Question
Here is the script I'm using, copied directly from Google:
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-CODE']);
_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);
})();
function recordOutboundLink(link, category, action) {
try {
var myTracker=_gat._getTrackerByName();
_gaq.push(['myTracker._trackEvent', category , action ]);
setTimeout('document.location = "' + link.href + '"', 100);
}catch(err){}
}
</script>
And here is the link I'm trying to track:
<a href="http://www.website.com/" target="_blank" class="ad" onClick="recordOutboundLink(this, 'Outbound Links', 'Visited website.com');return false;"></a>
Yet nothing has shown up in my events report for the last 3 days. Is there something wrong with my code?
Solution
There's problems with Google's Outbound links tracking example
Assuming you're only using one tracker (which it looks like in your code), the following will work:
function recordOutboundLink(link, category, action) {
try {
_gaq.push(['_trackEvent', category , action ]);
setTimeout('document.location = "' + link.href + '"', 100);
}catch(err){}
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow