1

I'm having a small but annoying problem with trying to open a link in a new window. I've built a site and I've got Google Analytics tracking installed. I've tracked click throughs to external sites as events and have put in the correct and it all tracks properly. For each of these links I've put in the target="_blank" but for some reason the link doesn't open in a new window! Is there something that I am doing wrong? Code below:

<a onclick="recordOutboundLink(this, 'Outbound Links', 'Link');return false;" href="http://www.mylink.com" target="_blank">Myink</a></span></li>

Is it something to do with the order it's in? I just can't seem to get it to work!

Thanks in advance.

As requested here is the code for recording the links:

<script type="text/javascript">
function recordOutboundLink(link, category, action) {
  try {
    var pageTracker=_gat._getTracker("UA-XXXXXX-XX");
    pageTracker._trackEvent(category, action);
    setTimeout('document.location = "' + link.href + '"', 100)
  }catch(err){}
}

zik
  • 3,035
  • 10
  • 41
  • 62

2 Answers2

2

The problem is that your recordOutboundLink function is using JavaScript to redirect instead of just letting the link perform it's default operation. That's the correct behavior if you want the link to open in the same tab/window because it uses a timeout to ensure the data's been sent to GA before the new page is loaded. To get the code working you need to use the following function:

    <script type="text/javascript">
        function recordOutboundLink(link, category, action) {
            try {
                var pageTracker=_gat._getTracker("UA-XXXXXX-XX");
                pageTracker._trackEvent(category, action);
            }catch(err){}
        }
    </script>

You also need to remove the return false bit of code that stops the link performing it's normal behavior:

<a onclick="recordOutboundLink(this, 'Outbound Links', 'Link')" href="http://www.mylink.com" target="_blank">Myink</a>
Ewan Heming
  • 4,628
  • 2
  • 21
  • 20
  • Awesome thanks so much. I've tested this out and it seems to be working. I'll know more tomorrow once I see some more data through GA! – zik Mar 18 '11 at 13:44
  • Worth noting that, when opening windows in a new window, you don't actually *need* to pause the execution for GA to run, since the old window stays open, and thus keeps processing the JavaScript without a problem while the user navigates to the new window. The reality is that target="_blank" makes things easier, not harder, since you don't risk interrupting the execution of the default action. – Yahel Mar 18 '11 at 13:51
0

document.location means you're changing the current page URL to another one. Plus you are using a return false statement, meaning you are preventing the default event associated wxith the link. Thus, your target="_blank" argument is not checked.

You need to remove the document.location and the return false statements. Then your link will be able to act as you wish

3rgo
  • 3,115
  • 7
  • 31
  • 44