向服务器报告页面花费的时间
当访问者通过关闭选项卡或浏览器离开页面时,是否可以将页面上花费的时间报告回服务器?使用 JavaScript。
并且它可以在 Firefox、Google Chrome、Safari、Opera 和 IE8 上运行(IE7 和 6 不重要)。
但无论如何它都会起作用;即使浏览器正在关闭,或者输入了另一个地址。
Is it possible to report the time spend on a page back to server when the visitor leaves the page by closing the tab or the browser? Using Javascript.
And it will work on Firefox, Google Chrome, Safari, Opera, and IE8 (IE7 and 6 are not important).
But it will work in any case; even while the browser is closing, or it is typed another address.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以尝试
windows.onbeforeunload
,但不能保证它在所有情况下都有效。无论如何,我建议您使用google Analytics,它具有许多其他功能(我怀疑您可以做得更好)比谷歌!)。它是免费的。
You can try
windows.onbeforeunload
but there is no guarantee that it will work in all cases.Anyway I suggest you use google analytics which has that feature among many others (I doubt you can do better than google!). It's free.
如果你想自己实现这一点,你需要决定何时获取开始时间——你可以在页面顶部放置一个脚本标签,该标签可能在内容呈现之前在 onload 事件上执行,该事件将等待直到所有图像和脚本加载完毕,或者发生类似于 JQuery 就绪事件 的事件当 DOM 准备好时,但不等待资源加载。此时,您需要记录开始时间:
然后您可以侦听 onunload 或 onbeforeunload 事件,并使用图像发送带有信息的 GET 请求:
您必须做一些测试来查看请求是否总是发送——我确信有时浏览器会在完成触发之前关闭或选项卡切换。
If you want to implement this yourself, you need to decide when to get the start time--you can put a script tag at the top of the page, which may execute before the content has rendered, on the onload event, which will wait until all images and scripts have loaded, or something like the JQuery ready event, which occurs when the DOM is ready, but doesn't wait for resources to load. At this point, you'd want to record a start time:
Then you can listen for the onunload or onbeforeunload event and use an image to send a GET request with the info:
You'll have to do some testing to see whether the requests are always sent--I'm sure sometimes the browser closes or the tab switches before they finish firing.
您还可以尝试 PiWik,它为您提供有关您网站访问者的类似统计信息。
You could also try PiWik which offers you similar statistics about the visitor of you website.
为什么不直接使用谷歌分析呢?
除了您想要的之外,它还为您的分析提供更多数据。
而且,它是免费的。
问候,
冻结。
Why not use Google Analytics directly?
Besides what you want, it provides much more data for your analytics.
And, it is free.
Regards,
freezea.