如何跟踪用户在网站上的时间
我希望跟踪用户在网站上的平均时间(与谷歌分析的方式相同)以进行内部管理。
做到这一点最简单的方法是什么?
I'm looking to track users average time on a website (in the same way that Google analytics does) for internal administration.
What's the easiest way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以通过以下方式获取时间:
如果您需要,我可以写一个示例脚本。
更新:
和后端脚本:
但正如我所说,它在 Opera 浏览器上不起作用
You can get the time in next ways:
If you need, I can write an example script.
UPDATE:
And backend script:
But as I said it wouldn`t work at Opera browser
我能想到的主要方式是:
当用户第一次点击页面时,您会记录他们的 IP 地址、加载的页面和时间。然后,使用一些 Javascript 和 AJAX,当它们离开页面时,您可以使用
unload
事件发送到 AJAX 处理程序,该处理程序记录页面以及它们离开的时间。除了会话之外,您还需要使用某种 ID 来存储页面访问。假设我打开了 5 个主页实例,您需要单独记录每个实例。所以,像这样:
index.php
代码:2345
)如果他们再次访问
index.php
,您将生成另一个代码,例如36789
。最好使用生成随机 GUID 的工具,这样您就可以(本质上)忽略同一 IP/页面/代码组合上发生冲突的任何可能性。Main way I can think of:
When the user first hits a page, you log, say, their IP address, the page loaded, and the time. Then, using some Javascript and AJAX, when they leave the page, you use the
unload
event to send to an AJAX handler that records the page and when they leave.You would need to use some sort of ID, apart from a session, to store the page visit. Say I have 5 instances of the homepage open, you'd want to log each one individually. So, something like this:
index.php
code:2345
)If they visit
index.php
again, you would generate another code, say,36789
. Use something that generates a random GUID is best, so you can (essentially) ignore any possibilities of collisions on the same IP/page/code combination.对网络和移动浏览器使用 timeonsite JS。它准确地跟踪现场时间。
在页面末尾,调用
就这么简单,
即使在不支持的iPhone、iPad、移动Safari等移动浏览器中,它似乎也可以工作window.unload() 原生事件。
Use timeonsite JS for web and mobile browsers. It tracks time on site accurately.
At the page end, call
As simple as that,
It seems to work even in mobile browsers like IPhone, IPad, mobile Safari etc. that doesn't support window.unload() events natively.
实际上没有一种有效的方法可以使用 PHP 来执行此操作,因为 PHP 是服务器端的,并且无法提供确定页面何时关闭的方法。您需要使用 javascript 来确定这一点。
我要做的是使用 javascript 在
window.onload
上启动计时器,然后在window.onunload
上结束计时器。然后您可以存储数据并用它做您想做的事情。There really isn't an effective way to do this with PHP, as PHP is server-side and provides no way of determining when the page was closed. You need to use javascript to determine this.
What I would do is use javascript to start a timer on
window.onload
and then end the timer onwindow.onunload
. Then you can store the data and do what you want with it.$(window).unload 是跟踪用户离开页面的一个非常好的函数。
但有一点是,当窗口卸载发送 ajax 时,它现在可以工作,当用户离开页面时,ajax 尚未完成。所以你需要为ajax添加一个属性(async: false),代码如下:
$(window).unload is a very good function to tracking user leaving page.
but one point is when window unload to send ajax sometime it now work, the ajax not finished when user leave page. so you need add one attribute (async: false) to ajax, code is blow: