Created
January 10, 2013 17:46
-
-
Save iso100/4504153 to your computer and use it in GitHub Desktop.
Outbound link tracking with Google Analytics with special support for opening new tabs
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
// Track outbound links in GA via events | |
$("a").on('click',function(e){ | |
var url = $(this).attr("href"); | |
var target = $(this).attr("target"); | |
if (e.currentTarget.host !== window.location.host) { | |
var newtab; | |
try { | |
_gaq.push(['_trackEvent', 'Outbound Links', e.currentTarget.host, url, 0]); | |
} catch(err) {} | |
if (target || e.metaKey || e.ctrlKey) { // If there's a target specified or if the user is holding down a new tab key | |
newtab = true; | |
} | |
if (newtab) { | |
e.preventDefault(); | |
window.open(url, '_blank'); | |
} | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment