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

穿精又带淫゛_ 提交于 2019-12-09 00:16:22

问题


<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!


回答1:


_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>



回答2:


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/



来源:https://stackoverflow.com/questions/9379615/onclick-for-google-analytics-and-target-blank-the-link-does-not-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!