使用 Google Analytics 跟踪出站链接时,为什么要延迟出站点击而不是将函数推入队列?

发布于 2024-11-12 08:15:40 字数 687 浏览 3 评论 0原文

官方建议用于跟踪出站链接(异步的版本)Google Analytics 是将跟踪事件推送到队列中,例如:

gaq.push(['_trackEvent', 'Outbound', 'http://foo.bar/']);
setTimeout('document.location = "http://foo.bar"', 100);

将匿名函数推入 GA 队列,例如:

gaq.push(['_trackEvent', 'Outbound', 'http://foo.bar/']);
gaq.push(function() { document.location = 'http://foo.bar/'; });

setTimeout 版本中,无法保证事件会在重定向发生之前得到处理,而在第二个版本中,它只会在事件处理后重定向,对吧?

The official suggestion for tracking outbound links with (the asynchronous version of) Google Analytics is to push an tracking event into the queue, like:

gaq.push(['_trackEvent', 'Outbound', 'http://foo.bar/']);
setTimeout('document.location = "http://foo.bar"', 100);

Would it not be better to push an anonymous function into the GA queue, like:

gaq.push(['_trackEvent', 'Outbound', 'http://foo.bar/']);
gaq.push(function() { document.location = 'http://foo.bar/'; });

In the setTimeout version, there's no guarantee that the event will be processed before the redirect occurs, whereas in the second version, it would only redirect after the event is processed—right?

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

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

发布评论

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

评论(2

冰之心 2024-11-19 08:15:40

执行您的建议的问题是,在页面更改之前没有时间执行请求。

浏览器不会等待这两个事件完成后再引导用户前进。如果您熟悉 jQuery,这类似于向链接添加 click 事件处理程序,向该处理程序添加 ajax 请求,但不放置 event.preventDefault() 在那里。换句话说,由于用户已经转到下一页,因此不会处理 ajax 请求。

编辑 正如您在评论中提到的,如果您应用 ,则这是无关紧要的也向链接返回 false

如果您实际上可以推送像示例中所示的函数,我真的不明白为什么它不能更好地工作,例外,第一个请求由于某种原因超时,让用户等待的时间远远超过了通常的 100 毫秒。

被谷歌屏蔽的用户怎么办?有很多插件/程序等可以完全阻止谷歌分析、Adsense 等。这些用户会有正常的用户体验吗?

The problem with doing your suggestion is that it wouldn't have time to do the request before the page changes.

The browser won't wait for those 2 events to be complete before navigating the user onwards. If you are familiar with jQuery, it would be similar to adding a click event handler to a link, adding an ajax request to that handler, but not putting an event.preventDefault() in there. In other words, the ajax request would not be handled as the user has already gone to the next page.

edit as you mentioned in the comments, this is irrelevant if you apply return false to the links as well.

If you can actually push a function like you illustrated in your example, I really don't see why it wouldn't work better then, with the exception that the first request is timing out for some reason, making the user wait far beyond the 100ms they usually would.

What about the users that have google blocked? There are lot of addons/programs etc which can completely block out google analytics, adsense etc. will those users have normal user experience?

薄凉少年不暖心 2024-11-19 08:15:40

最好的方法是使用 GA 支持的 hitCallback 函数。 hitCallback 是一个在成功发送命中后立即调用的函数。

在你的情况下,你可以这样做:

     // if after 300 ms, we still didn't get any action from hitCallback,
     // redirect manually
    setTimeout(function() {
      document.location = 'http://foo.bar/';
    }, 300);

    _gaq.push(['_set', 'hitCallback', function() {
      document.location = 'http://foo.bar/';
    }]);

    _gaq.push(['_trackEvent', 'outbound link ','click', 'http://foo.bar/']);

我在这里做了一个要点: https://gist.github.com/jonasva/aa20811003e7077360fcb1b297f0311d

The best way is to work with the hitCallback function supported by GA. hitCallback is a function that gets called as soon as the hit has been successfully sent.

In your case you could do something like this:

     // if after 300 ms, we still didn't get any action from hitCallback,
     // redirect manually
    setTimeout(function() {
      document.location = 'http://foo.bar/';
    }, 300);

    _gaq.push(['_set', 'hitCallback', function() {
      document.location = 'http://foo.bar/';
    }]);

    _gaq.push(['_trackEvent', 'outbound link ','click', 'http://foo.bar/']);

I made a gist about it here: https://gist.github.com/jonasva/aa20811003e7077360fcb1b297f0311d

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文