跟踪用户在网页上停留多长时间?

Maa*_*aaz 5 javascript php jquery

如何跟踪用户在请求另一个页面之前停留在页面上的时间长度,或者只是离开网站?

基本上,我想做一个检查,如果用户在页面上停留20分钟或更长时间,然后做一些事情.

我相信这需要php和javascript,但我不确定如何实现它.

也许$_SERVER在php中使用它来获取执行时间,然后在用户点击其他地方时获取时间戳并简单地比较两者?

Dyl*_*ech 7

你可以用简单的javascript完成所有这些.
对于单页:

window.setTimeout(function(){
// Do stuff after 20 minutes of loading the page
// Then using jQuery you can call a PHP script to do stuff like so:
$.ajax({
     url: '/myScript.php',
     method: 'POST',
     success: function(result){
        //The request was successful and the output is stored in a variable: result
     },
     complete: function(){
        //Do stuff when the request is completed (Ignores if success or not)
     },
     beforeSend: function(){
        //Do something before the request is sent
     }
});
}, 20 * 60 * 1000); //This is in milliseconds that's why I use the equation 
Run Code Online (Sandbox Code Playgroud)


对于多个页面:
我建议您根据用户点击页面的时间设置一个cookie,并在每个页面上检查cookie是否存在.如果存在,则每隔x大秒运行一次查询,以查看自创建cookie以来是否已经过了20分钟.


有关完整的Ajax文档,请访问:http://api.jquery.com/jQuery.ajax/

  • 您使用Ajax请求.我会为你更新答案,所以很清楚:) (2认同)