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

前端 未结 2 443
灰色年华
灰色年华 2021-01-03 08:30


        
相关标签:
2条回答
  • 2021-01-03 09:01

    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/

    0 讨论(0)
  • 2021-01-03 09:08

    _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>
    
    0 讨论(0)
提交回复
热议问题