【发布时间】:2013-10-09 15:22:52
【问题描述】:
这是来自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.
【问题讨论】:
-
为什么不立即打开窗口而是将焦点设置到主窗口,然后2秒后聚焦新窗口?
-
你的意思是使用self.focus()?我试过了,但没有用。另外,我了解到,如果设置了安全选项,Firefox 只会服从引发窗口的请求,并且默认情况下没有设置。据我所知,Chrome 根本不会关注 focus() 请求。 Safari 确实服从 focus() 请求。还有什么建议吗?请提供代码,因为我不是 js-savvy。我只能粗略地猜测脚本的作用,但我不能自己制作一个。谢谢。
标签: javascript jquery google-analytics