I just want to support some of the guys in Winnipeg. I wonβt let me comment, but his solution works!
Recommended Google code (doesn't work to open the link in a new tab):
var trackOutboundLink = function(url) { ga('send', 'event', 'outbound', 'click', url, { 'transport': 'beacon', 'hitCallback': function(){document.location = url;} }); }
:
<a href="http://www.example.com" onclick="trackOutboundLink('http://www.example.com'); return false;">Check out example.com</a>
However, if you change "document.location = url;" to "document.location = href;" and in the link tag change "return false"; "return the truth"; and add "target =" _ blank ", the link will open in a new tab and will track the outgoing link.
So, the code that works:
var trackOutboundLink = function(url) { ga('send', 'event', 'outbound', 'click', url, { 'transport': 'beacon', 'hitCallback': function(){document.location = href;} }); }
:
<a href="http://www.example.com" onclick="trackOutboundLink('http://www.example.com'); return true;" target="_blank;">Check out example.com</a>
source share