Reputation: 667
I've just added the following 'Universal' analytics code to th[e head][1] and can't find how to set a standard, universal outbound tracking event:
<!-- Google Analytics -->
<script>
(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-xxxx-Y', 'auto');
ga('send', 'pageview');
</script>
<!-- End Google Analytics -->
This is what has been suggested:
var trackOutboundLink = function(url) {
_gaq.push(['_trackEvent', 'Outbound', 'Click', this.href]);
setTimeout('document.location = "' + this.href + '"', 100);
return false;
}
Is this for universal outbound event tracking? Does it track all events and where should it be placed - in the head or before body closing tag?
Is this the correct syntax for tracking individual universal events?
onclick="trackOutboundLink('/WEBSITE/www.something.com')
Upvotes: 1
Views: 1177
Reputation: 655
You don't want to use .on('MouseDown')
because it won't track accurately. It still misses click events on slower connections and reports fake clicks if a user mouses down and then drags away.
NikZilla provides an answer here using .on('click')
with jquery and Universal Analytics hitCallback function. I've combined it with the ability to track all outbound links as referenced in another answer.
<script>
$(document).on('click', 'a', function (e) {
if ((this.protocol === 'http:' || this.protocol === 'https:') && this.hostname.indexOf(document.location.hostname) === -1) {
var obj = $(this);
e.preventDefault();
ga('send', 'event', 'Outbound', this.hostname, this.pathname, 0, {
'hitCallback': function() {
obj.off(e);
obj[0].click();
}
});
}
});
</script>
Upvotes: 0
Reputation: 7270
$(document).on('mousedown', 'a', function () {
if ((this.protocol === 'http:' || this.protocol === 'https:') && this.hostname.indexOf(document.location.hostname) === -1) {
ga('send', 'event', 'Outbound', this.hostname, this.pathname);
}
});
Upvotes: 1
Reputation: 619
Universal Analytics Event tracking has changed.
This is the new event tracking code:
ga('send', 'event', 'category', 'action', 'opt_label', opt_value); // value is a number
So to rewrite your code it would look like this:
var trackOutboundLink = function(url) {
ga('send', 'event','Outbound', 'Click', this.href);
setTimeout('document.location = "' + this.href + '"', 100);
return false;
}
Then you can attach it to the link with onclick=
or create a javascript event listener. I usually do the event listener because I try and stay away form mixing html and JS in line.
Upvotes: 0
Reputation: 963
You need to read the documentation. The _gaq.push(['_trackEvent'])
is no longer valid. You must use the new function;
ga('send', 'event', 'button', 'click', 'nav buttons', 4);
Event Tracking - Web Tracking (analytics.js)
Upvotes: 0