I have found this code on github to automatically track outgoing URLs with gtag.js.
<script>
var trackOutboundLink = function(url) {
gtag('event', 'click', {
'event_category': 'outbound',
'event_label': url,
'transport_type': 'beacon',
'event_callback': function(){document.location = href;}
});
}
</script>
<script>
jQuery(document).ready(function($) {
$('a[href^="http"]:not([href*="//' + location.host + '"])').on('click',
function(e) {
trackOutboundLink($(this).attr("href"));
return true;
});
});
</script>
But I get the following error:
Uncaught ReferenceError: href is not defined
When I put 'event_callback': function(){document.location = url;}
it would work, but then the link is opened twice in a new window and in the same tab.
I am a dummy with this stuff... could you put me in the right direction?