【问题标题】:Event tracking - need the link to open in a new window without being treated as pop-up事件跟踪 - 需要在新窗口中打开链接而不被视为弹出窗口
【发布时间】: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>

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

Google Analytics 建议的脚本运行良好,但只有一件事,我希望链接在 2 秒后在新选项卡或窗口中打开,而不会被 Chrome 和 IE 等主要浏览器上的弹出窗口阻止程序阻止。

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

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

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

感谢您的宝贵时间。

S.C.

【问题讨论】:

  • 为什么不立即打开窗口而是将焦点设置到主窗口,然后2秒后聚焦新窗口?
  • 你的意思是使用self.focus()?我试过了,但没有用。另外,我了解到,如果设置了安全选项,Firefox 只会服从引发窗口的请求,并且默认情况下没有设置。据我所知,Chrome 根本不会关注 focus() 请求。 Safari 确实服从 focus() 请求。还有什么建议吗?请提供代码,因为我不是 js-savvy。我只能粗略地猜测脚本的作用,但我不能自己制作一个。谢谢。

标签: javascript jquery google-analytics


【解决方案1】:

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

【讨论】:

  • 嗨 PingOfDeath,感谢您的建议,但它在 Chrome 和 IE 上都不起作用(我已将您的 'blank_' 更改为 '_blank'。如果您转到 jsfiddle.net/5tkAz 它没有'在结果面板中执行它时效果不佳。有任何可行的建议吗?提前致谢。
  • @S.C. mb 你应该在文件中包含你的 ga.js 吗? :) 好吧,我已经更新了我的 fork:jsfiddle.net/PingOfDeath/5tkAz/2
猜你喜欢
  • 1970-01-01
  • 2013-02-13
  • 1970-01-01
  • 1970-01-01
  • 2011-10-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-06
相关资源
最近更新 更多