Skip to content

Instantly share code, notes, and snippets.

@eduardocereto
Last active July 16, 2022 17:19
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save eduardocereto/afec3522df75020acfe8 to your computer and use it in GitHub Desktop.
Save eduardocereto/afec3522df75020acfe8 to your computer and use it in GitHub Desktop.
Outbound Link Tracking
(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);
})();
@eduardocereto
Copy link
Author

Should be nonInteractive

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment