I have used the following link to record outbound links through google analytics. Is it possible to open the link in a new window?
<script type="text/javascript">
function recordOutboundLink(link, category, action)
{
try {
var myTracker=_gat._getTrackerByName();
_gaq.push(['myTracker._trackEvent', category , action ]);
setTimeout('document.location = "' + link.href + '"', 100)
}
catch(err)
{
}
}
</script>
<a href="http://www.example.com" onClick="recordOutboundLink(this, 'Outbound Links', 'example.com');return false;">
In the general case
In the more general case of
target
being anything, like<a>
elements. same meaning as_self
._blank
- URL is loaded into a new window/tab_self
- URL replaces the current page._parent
- URL is loaded into the parent frame_top
- URL replaces any framesets that may be loadedyou can modify the line
to this:
The odd thing is that providing
""
towindow.open
is the same as providing_blank
, therefore the extra?:
operator is necessary.By the way: Your provided code (and the code provided by official google examples) shall be modified slightly to work correct, see How do I set up Google Analytics to track outbound links? The official example doesn't work
edit, I forgot to add target="_blank":
I would do it this way tracking outbound links:
Replace the
document.location = url;
withwindow.open(url)
, that opens in a new window and tracks at the same time. You don't need to modify the link, just call the JS function on the onclick.Here's a modified snippet from the link you provided that should work both for links that open in the current window and links that open in a new window without any modification:
Here are the two link types. Note that the onclick handler is the same in both:
Limitations:
Jeff's answer was very close to solving this problem for me. The recommended code from Google breaks target = _blank, and window.open strategies get caught by popup blockers. I modified the test for target like so: