事件跟踪 - 需要链接在新窗口中打开而不被视为弹出窗口

时间:2013-10-03 04:04:22

标签: javascript jquery google-analytics

以下是来自https://support.google.com/analytics/answer/1136920?hl=en

的脚本
<script type="text/javascript">
function trackOutboundLink(link, category, action) { 

try { 
_gaq.push(['_trackEvent', category , action]); 
} catch(err){}

setTimeout(function() {
document.location.href = link.href;
}, 2000);
}
</script>

<a href="http://www.example.com" onClick="trackOutboundLink(this, 'Outbound Links', 'example.com'); return false;">Visit example</a>

Google Analytics建议的脚本运行良好,但仅举一件事,我希望链接在2秒后在新的标签页或窗口中打开,而不会被主要浏览器(如Chrome和IE等)上的弹出窗口拦截器阻止。

我尝试使用_blank使其变为<a target="_blank" href="http://www.example.com" onClick="trackOutboundLink(this, 'Outbound Links', 'example.com'); return false;">Visit example</a>,但它不起作用。

在setTimeout()中使用window.open()打开一个新窗口将被视为弹出窗口并被弹出窗口阻止程序阻止。我不希望将新窗口(或制表符)视为弹出窗口。

除了我需要的新标签/窗口外,GA代码中的所有内容都很好。请帮忙。

谢谢你,感谢你的时间。

S.C。

1 个答案:

答案 0 :(得分:0)

您可以使用jquery库触发单击。

  • 将jquery库连接到您的项目,例如:

  • 你应该隐藏一个链接然后触发它:

http://jsfiddle.net/PingOfDeath/5tkAz/2/

$( "#link" ).trigger( "click" );

然后你的html应该是这样的:

<a href="#" onClick="trackOutboundLink('http://www.example.com', 'Outbound Links', 'example.com'); return false;">Visit example</a>
<a id="link" href="http://www.example.com" target="blank_" hidden="true">Visit example</a>
相关问题