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); | |
} | |
}); | |
}); |
jQuery(function($){
$('a:not([href*="' + document.domain + '"])').mousedown(function(event){
// Just in case, be safe and don't do anything
if (typeof _gat == 'undefined') {
return;
}
var link = $(this);
var href = link.attr('href');
var noProtocol = href.replace(/http[s]?:\/\//, '');
// Track the event
_gat._getTrackerByName()._trackEvent('Outbound Links', noProtocol);
});
});
Hi,
onmousedown worked great for me
<a href="#" onmousedown="_gaq.push(['_trackEvent', 'Videos', 'Play', 'Baby\'s First Birthday']);">My Link</a>
Better the than the Google code: Outbound links https://support.google.com/analytics/answer/1136920?hl=en.
There is a bit of a setback in the last script. If the address is using relative domain it will be counted as externel. We can do a simpe check:
jQuery(function($){
$('a:not([href*="' + document.domain + '"])').mousedown(function(event){
// Just in case, be safe and don't do anything
if (typeof _gat == 'undefined') {
return;
}
var link = $(this);
var href = link.attr('href');
if (href.charAt(0) != '.' && href.charAt(0) != '#' && href.charAt(0) != '/) {
var noProtocol = href.replace(/http[s]?:\/\//, '');
// Track the event
_gat._getTrackerByName()._trackEvent('Outbound Links', noProtocol);
}
});
});
This will check if the first char is a part of a relative domain name.
mousedown
doesn't mean they clicked the link. You could get false data if they click on the element and drag away (not finishing the mouseup
on the element).
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The problem with window.open is that the referrer will be lost. A better solution is to use onmousedown instead of onclick. Having done some tests, I know this work better that using window.open or using setTimeout. You got some false positive from people clicking the right mouse button and not choosing "Open in new tab" or "Open in new window", but onclick doesn't always fire for middle and right click on all browser. It's a choice between the lesser of two evils here.