user1223427
user1223427

Reputation: 31

OnClick for google analytics and target _blank. The link does not work?

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

I have a problem with this code. Without return false; i have a new window and it's ok, but the same url is also open in the same parent window. Then i have two windows with the same content. I think the problem is the redirect of the _gaq.push Please help me! Thank you!

Upvotes: 3

Views: 6857

Answers (2)

montueron
montueron

Reputation: 198

I ran into the same problem with the target="_blank" not working when adding code for tracking file downloads.

on this website, the "Automate It" section shows a jquery code that will detect different type of links on your website (files, mailto, etc...) and automatically add the "onclick" event with _gaq.push.

in case the link above doesn't work : http://www.blastam.com/blog/index.php/2011/04/how-to-track-downloads-in-google-analytics/

Upvotes: 0

Eduardo
Eduardo

Reputation: 22834

_link will replace your current page with the link page. So you should always return false because Google Analytics will be in charge of the redirection for this link.

Also Google doesn't support the attribute _blank. So you have to find a way around.

_gaq.push(['_setAllowLinker', true]);
function _gaLink(a) {
    url = a.href
    _gaq.push(function() {
        if (a.target == '_blank') {
            window.open(_gat._getTrackers()[0]._getLinkerUrl(url));
        } else {
            _gaq.push(['_link', url]);
        }
    });
    return false;
}​

Now you can call it instead of Google Analytics _link.

<a href="http://example.com/test.html" 
   onclick="return _gaLink(this);"     
   class="noFloat" 
   target="_blank"> 
   Click Me
</a>

Upvotes: 9

Related Questions