Created
September 30, 2015 13:30
-
-
Save olivM/f9d15e1ea6c3a5dc1cbf to your computer and use it in GitHub Desktop.
This file contains 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
/* Track outbound links in Google Analytics */ | |
(function($) { | |
"use strict"; | |
// current page host | |
var baseURI = window.location.host; | |
// click event on body | |
$("body").on("click", function(e) { | |
// abandon if link already aborted or analytics is not available | |
if (e.isDefaultPrevented() || typeof ga !== "function") return; | |
// abandon if no active link or link within domain | |
var link = $(e.target).closest("a"); | |
if (link.length != 1 || baseURI == link[0].host) return; | |
// cancel event and record outbound link | |
e.preventDefault(); | |
var href = link[0].href; | |
ga('send', { | |
'hitType': 'event', | |
'eventCategory': 'outbound', | |
'eventAction': 'link', | |
'eventLabel': href, | |
'hitCallback': loadPage | |
}); | |
// redirect after one second if recording takes too long | |
setTimeout(loadPage, 1000); | |
// redirect to outbound page | |
function loadPage() { | |
document.location = href; | |
} | |
}); | |
})(jQuery); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment