Created
November 22, 2011 23:02
-
-
Save bigdawggi/1387334 to your computer and use it in GitHub Desktop.
jQuery-based outbound link tracking with cases for new window as well as same window locations
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
/** | |
* Outbound link tracking | |
* | |
* This code largely based on examples from | |
* [Google Analytics Help](http://www.google.com/support/googleanalytics/bin/answer.py?answer=55527). | |
*/ | |
jQuery(function($){ | |
$('a:not([href*="' + document.domain + '"])').click(function(event){ | |
// Just in case, be safe and don't do anything | |
if (typeof _gat == 'undefined') { | |
return; | |
} | |
// Stop our browser-based redirect, we'll do that in a minute | |
event.preventDefault(); | |
var link = $(this); | |
var href = link.attr('href'); | |
var noProtocol = href.replace(/http[s]?:\/\//, ''); | |
// Track the event | |
_gat._getTrackerByName()._trackEvent('Outbound Links', noProtocol); | |
// Opening in a new window? | |
if (link.attr('target') == '_blank') { | |
/* If we are opening a new window, go ahead and open it now | |
instead of in a setTimeout callback, so that popup blockers | |
don't block it. */ | |
window.open(href); | |
} | |
else { | |
/* If we're opening in the same window, we need to delay | |
for a brief moment to ensure the _trackEvent has had time | |
to fire */ | |
setTimeout('document.location = "' + href + '";', 100); | |
} | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
mousedown
doesn't mean they clicked the link. You could get false data if they click on the element and drag away (not finishing themouseup
on the element).