Last active
July 16, 2022 17:19
-
-
Save eduardocereto/afec3522df75020acfe8 to your computer and use it in GitHub Desktop.
Outbound Link Tracking
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function(){ | |
// Provides a plugin name and constructor function to analytics.js. This | |
// function works even if the site has customized the ga global identifier. | |
function _providePlugin(pluginName, pluginConstructor) { | |
var ga = window[window['GoogleAnalyticsObject'] || 'ga']; | |
if (ga) ga('provide', pluginName, pluginConstructor); | |
} | |
// Creates the plugin to tag outbound link | |
function outboundConstructor(tracker, config){ | |
var d = document, | |
ownDomain = config['ownDomain'] || d.location.hostname; | |
var fnc = function(event){ | |
try{ | |
if (!event || !event.target) { | |
event = window.event; | |
event.target = event.srcElement; | |
} | |
var el = event.target; | |
if (el && el.href && el.href.indexOf(ownDomain) < 0) { | |
if (event.preventDefault) {event.preventDefault();} | |
else {event.returnValue = false;} | |
var do_redirect = function(){ | |
window.location = el.href; | |
}; | |
// Redirects anyway after 2 seconds if something goes wrong | |
setTimeout(do_redirect, 2000); | |
tracker.send('event', | |
'Outbound', | |
el.href.match('[^/]+//([^/]+)')[1], | |
{ | |
'hitCallback': do_redirect | |
} | |
); | |
} | |
} catch (e) { | |
tracker.send('event', 'Javascript Error', 'GA Outbound Link Tracking'); | |
} | |
}; | |
// W3C model | |
if (d.addEventListener) { | |
d.addEventListener('click', fnc, false); | |
return true; | |
} | |
// Microsoft model | |
else if (d.attachEvent) { | |
return d.attachEvent('onclick', fnc); | |
} | |
} | |
_providePlugin('outbound', outboundConstructor); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Should be nonInteractive