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

Event tracking - need the link to open in a new window without being treated as pop-up

本文关键字:窗口 跟踪 链接 在新窗口中打开 事件      更新时间:2023-09-26

这是 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 代码中的所有内容都很好,除了我需要的新选项卡/窗口。请帮忙。

谢谢并感谢您的时间。

南卡罗来纳州

好吧,您可以使用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>