Google Analytics:_gaq.push() 是阻塞函数吗?
我想做的是将用户发送到 Google Analytics 后立即重定向到下一页:
_gaq.push(['_trackEvent', 'xxx' ...]);
window.location = 'some url ...';
上面的代码设法注册事件,因为我可以在 GA 报告中看到它们。不过,我怀疑一些事件丢失了,因为浏览器在跟踪像素加载之前将用户重定向到新页面。
我的问题是:
- _gaq.push() 是否会阻塞,直到事件成功到达 Google 服务器?
- 如果没有,实现我需要的最佳方法是什么?
谢谢!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
Google 有 支持页面显示您可能希望在通话后添加延迟
_gac.push
,但他们声称这是为了让 Google Analytics JavaScript 有机会加载。他们没有说这是为了确保发送请求。我还没有检查 @pixelfreak 的处理
onbeforeunload
的声明,但似乎他们就是这样做的。Google has a support page that says you might want to add a delay after calling
_gac.push
, however they claim that this is to give the Google Analytics JavaScript a chance to load. They don't say that it's to ensure the request is sent.I haven't checked @pixelfreak's claim of handling
onbeforeunload
, but it seems that's what they do.这是我基于一些快速研究的观察。该函数是 Google Analytic 异步 API 的一部分,因此它不应该阻止任何内容,否则它就不是异步的。 :)
深入研究混淆的 ga.js,您可以看到跟踪可能称为 onbeforeunload,当您离开页面时会触发该跟踪。
跟踪像素是否完全加载并不重要,这是一场火灾。忘记。只要请求发起并到达 Google 的服务器,就会被跟踪。响应(在本例中为像素图像)是副产品。
This is my observation based on some quick research. That function is part of Google Analytic's asynchronous API, so it should not block anything, otherwise, it's not async. :)
Digging through the obfuscated ga.js, you can kinda see that the tracking is probably called onbeforeunload, which fires when you leave a page.
It really doesn't matter if the tracking pixel loads completely, it's a fire & forget. As long as the request is initiated and reaches Google's server, it'll get tracked. The response (in this case, the pixel image) is a by-product.
也许你可以尝试这个方法,但我没有尝试过
Maybe you can try this method, but I have not tried
只需使用 1 秒延迟即可。像这样:
Just use 1 sec delay. Like this: