Hi. Web developer, JS newbie. I am setting up outbound link tracking in Google Analytics and I want to use a short jquery script to test every clicked link to see if it is outbound or not. I have the following script:
Code:
$("a").on('click',function(e){
var url = $(this).attr("href");
if (e.currentTarget.host != window.location.host) {
_gaq.push(['_trackEvent', 'Outbound Links', e.currentTarget.host, url, 0]);
if (e.metaKey || e.ctrlKey) {
var newtab = true;
}
if (!newtab) {
e.preventDefault();
setTimeout('document.location = "' + url + '"', 100);
}
}
});
The script works, and pushes outbound link events to GA, but it is also catching many local links as well, throwing off my outbound link stats. They show up in GA as myhost.com:80. Since myhost.com:80 != myhost.com, the script incorrectly pushes the outbound link event when the link is actually local.
I'm not sure why some local links have the port appended, but I need to modify the above code to strip that port number from
e.currentTarget.host before it is compared to
window.location.host. Unfortunately, I'm not sure how to do it. Very grateful for any pointers.. Sean