Google Analytics:_gaq.push() 是阻塞函数吗?

发布于 2024-10-31 05:09:18 字数 350 浏览 2 评论 0 原文

我想做的是将用户发送到 Google Analytics 后立即重定向到下一页:

_gaq.push(['_trackEvent', 'xxx' ...]);
window.location = 'some url ...';

上面的代码设法注册事件,因为我可以在 GA 报告中看到它们。不过,我怀疑一些事件丢失了,因为浏览器在跟踪像素加载之前将用户重定向到新页面。

我的问题是:

  1. _gaq.push() 是否会阻塞,直到事件成功到达 Google 服务器?
  2. 如果没有,实现我需要的最佳方法是什么?

谢谢!

What I am trying to do is to redirect the user to the next page right after sending an event to Google Analytics:

_gaq.push(['_trackEvent', 'xxx' ...]);
window.location = 'some url ...';

The above code manages to register events because I can see them in the GA report. However I suspect that some events were lost because the browser redirects the user to the new page before the track pixel loads.

My questions are:

  1. Does _gaq.push() block until the event has successfully reached Google's server?
  2. If not, what is the best way to make achieve what I need?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

梦途 2024-11-07 05:09:18

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.

First, delay the outbound click by a fraction of a second.

This delay will hardly be noticeable by the user, but it will provide the browser more time load the tracking code. Without this method, it's possible that a user can click on the outbound link before the tracking code loads, in which case the event will not be recorded.

I haven't checked @pixelfreak's claim of handling onbeforeunload, but it seems that's what they do.

你的背包 2024-11-07 05:09:18

这是我基于一些快速研究的观察。该函数是 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.

哭了丶谁疼 2024-11-07 05:09:18

也许你可以尝试这个方法,但我没有尝试过

_gaq.push(['_trackEvent', 'xxx' ...]);
_gaq.push(function(){location.reload()});

Maybe you can try this method, but I have not tried

_gaq.push(['_trackEvent', 'xxx' ...]);
_gaq.push(function(){location.reload()});
深居我梦 2024-11-07 05:09:18

只需使用 1 秒延迟即可。像这样:

_gaq.push(['_trackEvent', 'xxx' ...]);
setTimeout(function(){window.location = 'some_url'}, 1000);

Just use 1 sec delay. Like this:

_gaq.push(['_trackEvent', 'xxx' ...]);
setTimeout(function(){window.location = 'some_url'}, 1000);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文