I am trying to track a link being clicked using event tracking and hitcallback. My analytics code looks like this...
<script type="text/javascript">//<![CDATA[
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXXXXX-X']);
_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);
})();
//]]></script>
And the link I am trying to track looks like this....
<a href='http://cool.com/' onclick="var href = this.href; ga('send','event','outbound','click', this.href, {'hitCallback': function(){document.location = href;}}); return false;">Cool</a>
I followed the hitcallback example from http://www.swellpath.com/2013/12/universal-analytics-using-hitcallback/ but its still not working.
Am I missing something?
If you don't see any data at all in your Real-Time reports, the most likely cause is either in the Google Analytics tracking code or GA/GTM misconfiguration. You should check your tags in Google Tag Manager, Google Analytics filters, and also use browser extensions for debugging.
To see if Google Analytics is firing on your page, go to any page on your site in your Chrome Browser and right-click. Click on Inspect. Then go to the Network tab. Hit refresh on your browser and watch to see the different content and scripts loading on the page.
Another reason why your Google Analytics is not working is that it might be conflicting with another script on your webpage. If you have other scripts running on your website, make sure they don't use the same variables as Google Analytics.
The problem is you are attempting to use Universal Analytics syntax (analytics.js) for your click tracking, but the GA library you are using is the older _gaq
(ga.js) syntax.
_gaq
style does let you specify a callback function, though it's undocumented. Basically you have to _set
the callback function before you make the _trackEvent
call.
Your link should look like this:
<a href='http://cool.com' onclick="var _this=this;_gaq.push(['_set','hitCallback',function(){document.location=_this.href;}]);_gaq.push(['_trackEvent','outbound','click',_this.href]);return false;">cool</a>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With