I’m using the standard Google Analytics Javascript code to track the outbound links on my website:
function recordOutboundLink(link, category, action) {
_gat._getTrackerByName()._trackEvent(category, action);
setTimeout('document.location = "' + link.href + '"', 100);
}
Even when I add "target='_blank' to my links, all links are still opened in the same window/tab. I tried add 'document.location.target', but the script isn’t working yet.
document.location = newURLwill open the URL in the existing window. You can usewindow.open(newURL)to open the URL in a new window.A couple of other things:
document.locationhas been deprecated — uselocation.hrefinstead.Try the following
FYI: Why use setTimeout only for opening the URL in the existing window? Starting to open a new URL in the existing window can halt the analytics tracking pixel request before it’s completed. If you’re opening the URL in a new window, there’s no need for a delay.