I am currently using analytics.js (a newer version of GA), and I'm trying to track all types of events from my site, including when a user clicks on an anchor tag that points to an external URL. I am currently using this setting:
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-KEY-HERE', { 'alwaysSendReferrer': true, 'allowAnchor': true });
and I send events when I hit the link:
$(document).on("click", ".anchor-class", function (event) {
label =
ga('send', 'event', 'Link Clicked', 'Click Details', label);;
return;
}
});
and it does not send anything to GA (although the event handler calls the ga (send ...) method ). However, if I use this exact technique, but with event.preventDefault (); at the beginning of the function, the event is dispatched and appears on the GA toolbar.
Are there any settings that I skipped to do this correctly?