如何跟踪用户在请求另一个页面或离开网站之前在页面上停留的时间?
基本上,我想做一个检查,如果用户在页面上停留 20 分钟或更长时间,然后做一些事情。
我相信这需要 php 和 javascript,但我不确定如何完成它。
也许在php中使用这个$_SERVER
来获取执行时间,然后在用户点击其他地方时获取时间戳并简单地比较两者?
最佳答案
您可以使用简单的 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
对于多个页面:
我建议您在用户点击页面时设置一个 cookie,并在每个页面上检查 cookie 是否存在。如果存在,则每隔 x 秒运行一次查询,以查看自创建 cookie 以来是否已过去 20 分钟。
要获得完整的 Ajax 文档,请访问:http://api.jquery.com/jQuery.ajax/
关于php - 跟踪用户在页面上停留的时间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15558475/