Forked from bigdawggi/Outbound Link Tracking - Google Analytics.js
Created
February 16, 2012 06:43
-
-
Save kurioscreative/1842723 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 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
/** | |
* Outbound link tracking | |
* | |
* This code largely based on examples from | |
* [Google Analytics Help](http://www.google.com/support/googleanalytics/bin/answer.py?answer=55527). | |
* Modified to exclude local paths. Inspired by http://trevordavis.net/blog/use-jquery-to-open-all-external-links-in-a-new-window | |
*/ | |
*/ | |
jQuery(function($){ | |
$("a[href^='http']:not([href^='http://" + document.domain + "']):not([href^='http://www." + 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