使用 Google Analytics 跟踪出站链接时,为什么要延迟出站点击而不是将函数推入队列?
官方建议用于跟踪出站链接(异步的版本)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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
执行您的建议的问题是,在页面更改之前没有时间执行请求。浏览器不会等待这两个事件完成后再引导用户前进。如果您熟悉 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 aclick
event handler to a link, adding anajax
request to that handler, but not putting anevent.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?
最好的方法是使用 GA 支持的
hitCallback
函数。hitCallback
是一个在成功发送命中后立即调用的函数。在你的情况下,你可以这样做:
我在这里做了一个要点: 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:
I made a gist about it here: https://gist.github.com/jonasva/aa20811003e7077360fcb1b297f0311d