向服务器报告页面花费的时间

发布于 2024-08-15 20:40:22 字数 178 浏览 8 评论 0原文

当访问者通过关闭选项卡或浏览器离开页面时,是否可以将页面上花费的时间报告回服务器?使用 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 技术交流群。

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

发布评论

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

评论(4

绅士风度i 2024-08-22 20:40:22

您可以尝试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.

违心° 2024-08-22 20:40:22

如果你想自己实现这一点,你需要决定何时获取开始时间——你可以在页面顶部放置一个脚本标签,该标签可能在内容呈现之前在 onload 事件上执行,该事件将等待直到所有图像和脚本加载完毕,或者发生类似于 JQuery 就绪事件 的事件当 DOM 准备好时,但不等待资源加载。此时,您需要记录开始时间:

<script>var startTime = new Date().getTime();</script>

然后您可以侦听 onunload 或 onbeforeunload 事件,并使用图像发送带有信息的 GET 请求:

<script>
  window.onunload = function() {
    var i = new Image();
    var timeSpentMilliseconds = new Date().getTime() - startTime;
    i.src = '/pagetime?timespent=' + timeSpentMilliseconds;
  }
</script>

您必须做一些测试来查看请求是否总是发送——我确信有时浏览器会在完成触发之前关闭或选项卡切换。

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:

<script>var startTime = new Date().getTime();</script>

Then you can listen for the onunload or onbeforeunload event and use an image to send a GET request with the info:

<script>
  window.onunload = function() {
    var i = new Image();
    var timeSpentMilliseconds = new Date().getTime() - startTime;
    i.src = '/pagetime?timespent=' + timeSpentMilliseconds;
  }
</script>

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.

三寸金莲 2024-08-22 20:40:22

您还可以尝试 PiWik,它为您提供有关您网站访问者的类似统计信息。

You could also try PiWik which offers you similar statistics about the visitor of you website.

满天都是小星星 2024-08-22 20:40:22

为什么不直接使用谷歌分析呢?
除了您想要的之外,它还为您的分析提供更多数据。
而且,它是免费的。
问候,
冻结。

Why not use Google Analytics directly?
Besides what you want, it provides much more data for your analytics.
And, it is free.
Regards,
freezea.

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