admin管理员组

文章数量:1326317

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

<a href=".html" 
   onclick="_gaq.push(['_link', '.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.

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

<a href="http://example./test.html" 
   onclick="_gaq.push(['_link', 'http://example./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.

Share Improve this question edited Jan 10, 2012 at 11:04 Anicho asked Jan 10, 2012 at 10:13 AnichoAnicho 2,66711 gold badges48 silver badges76 bronze badges 2
  • 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 Commented Jan 10, 2012 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: stackoverflow./questions/135203/… – Anicho Commented Jan 10, 2012 at 10:23
Add a ment  | 

6 Answers 6

Reset to default 4

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

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

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./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);
});

what about :

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

function Clicked()
{
_gaq.push(['_link', 'http://example./test.html']); 
window.location = "http://example./test.html";
return false;
}

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

Rory McCrossan is correct that "return false" will stop the default behavior of the link but that is an inplete answer for this situation. The "_link" method pushed to Google Analytics (GA) will cause a redirect when done processing. GA needs time to plete. 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 inpatible 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.

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./" target="_blank" onclick="trackOutboundLink('http://www.google.'); 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.

本文标签: javascriptLink with OnClick event any reason it will not fireworkStack Overflow