2

I have the following link to track cross site travelling for google analytics:

<a href="http://example.com/test.html" 
   onclick="_gaq.push(['_link', 'http://example.com/test.html']); 
   return false;"     
   class="noFloat" 
   target="_blank"> 
   Click Me
</a>

I am trying to debug why this link but it does not work, I click it and no new window opens. Just wanted to confirm that it's a problem with the code above or will it be something else on my page breaking this? If so I will get back to debugging javascript.

Anicho
  • 2,647
  • 11
  • 48
  • 76
  • Most browser have developer tools which will tell you the error if there is one. In Chrome and IE press F12. Also, `JScript` and `JavaScript` are to different languages, though similar they do have their differences. – Ash Burlaczenko Jan 10 '12 at 10:16
  • good spot thanks for the Jscript vs Javascript tip didnt realise that. I have been looking but no cigar will dive deeper. Just wanted to make sure and get a second set of eyes on the link to confirm that, the syntax is fine. Differece between jscript and javascript: http://stackoverflow.com/questions/135203/whats-the-difference-between-javascript-and-jscript – Anicho Jan 10 '12 at 10:23

6 Answers6

4

The link behaviour is being prevented by the return false;. To let the link complete as usual, simply remove it:

<a href="http://example.com/test.html" 
   onclick="_gaq.push(['_link', 'http://example.com/test.html']);"     
   class="noFloat" 
   target="_blank"> 
   Click Me
</a>
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
  • Mate your absolutely right, don't know why I didnt look at that before I have ran into similar problems in the past. I must also add that we needed to define `_gaq.push(['_setDomainName', 'example-petstore.com']);` the example.petstore.com should be the domain your currently on. Without that definition the _.gaq.push methods fail or shall I say get stuck in a loop which I noticed when debugging. – Anicho Jan 10 '12 at 10:57
  • 1
    defined target="_blank" before the onlick event, works like a charm and can keep return false is well this stop _gaq.push from loading the link in the same window. – Anicho Jan 10 '12 at 11:55
  • Rory's answer is correct but incomplete. Do NOT remove "return false" without first understanding what the "_link" method for Google Analytics does (it causes a redirect when done). See my reply below for some details. – Plaudit Design May 03 '14 at 16:08
2

The inline way of passing cookie information that you are using will not work if the link is opening in a new window (target="_blank"), the return-false is basicly overwriting the functions of that a-tag.

You have to change the href="" of the <a>-tag:

<a href="http://example.com/test.html" 
   id="ext-link"     
   class="noFloat" 
   target="_blank"> 
   Click Me
</a>

And then have the following javascript:

_gaq.push(function() {
  var pt = _gat._getTrackerByName(),
      link = document.getElementById('ext-link');
  link.href = pt._getLinkerUrl(link.href);
});
bskard
  • 1,040
  • 1
  • 9
  • 13
  • I ran into this issue and realized that the JS that I was using to trackOutboundLink called document.location = url which did not specify a target. It worked after changing it to window.open(url, '_blank'); – fseminario Oct 02 '15 at 21:10
0

Rory McCrossan is correct that "return false" will stop the default behavior of the link but that is an incomplete answer for this situation. The "_link" method pushed to Google Analytics (GA) will cause a redirect when done processing. GA needs time to complete. For details see: http://bit.ly/1iasHQh

The reason the GA redirect is failing is due to another issue. Two theories: (1) I have experienced this situation once before and realized it was because I am using Chrome and have the "Google Analytics Opt-out Add-on" installed. (2) The "_link" method may be incompatible with using target="_blank". If using target to open a new window/tab you can safely remove the "return false;" because the current page processing the Javascript will not be closed which will still allow GA to do its thing.

Hope that helps others.

Plaudit Design
  • 1,156
  • 8
  • 16
0

After going through the answers, none of them worked entirely. In my case, I had a link structured as so:

<a href="http://www.google.com/" target="_blank" onclick="trackOutboundLink('http://www.google.com'); return false;" >
Outbound Link</a>

Broken JS in footer

var trackOutboundLink = function(url) {
   ga('send', 'event', 'outbound', 'click', url, {'hitCallback':
     function () {
     document.location = url;
     }
   });
}

Fixed JS in footer:

var trackOutboundLink = function(url) {    
 ga('send', 'event', 'outbound', 'click', url, {'hitCallback':
   function () {
    window.open(url, '_blank');
   }    
 }); 
}

Originally the JS did not specify a target so it opened in the same window. Removing "return false" allowed both the a href and the onclick to trigger, which resulted in two windows being opened.

fseminario
  • 801
  • 1
  • 9
  • 13
0

what about :

<a href="#" onclick="Clicked();" class="noFloat" target="_blank"> 
   Click Me
</a>

function Clicked()
{
_gaq.push(['_link', 'http://example.com/test.html']); 
window.location = "http://example.com/test.html";
return false;
}
Arrabi
  • 3,718
  • 4
  • 26
  • 38
0

I've found the default analytics link code breaks if your script tag is not in the head of the document.

Tim Rodham
  • 283
  • 4
  • 14