I am using the code below to track outbound links in google analytics ( found it somewhere on this site ). I have 2 issues coming up:
- Sometimes, the e.currentTarget.host part of the output shows my own domain - instead of showing the domain where the click leads to. Any idea why my domain shows up on occasion ?
Is it possible to modify this code to do the following (1) force link to open in new window and (2) track the outbound click event as shown.
$(function() { $("a").on('click',function(e){ var url = $(this).attr("href"); if (e.currentTarget.host != window.location.host) { _gat._getTrackerByName()._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); } } });
});