如何衡量在页面上花费的时间? [英] How to measure a time spent on a page?

查看:105
本文介绍了如何衡量在页面上花费的时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想测量用户在页面上花费的时间(以浮点数表示的整数或分钟数)。我知道有一个卸载事件,我可以在他们离开页面时触发。但是如何获得他们已经在那里度过的时间?

解决方案

如果你使用谷歌分析,他们提供这个统计数据,虽然我我不确定他们是如何得到它的。



如果你想自己动手,你需要有一些AJAX请求被发送到你的服务器进行记录。 / p>

jQuery有一个.unload(...)方法,您可以使用如下:

  $(document).ready(function(){
var start = new Date();

$(window).unload(function(){
var end = new Date();
$ .ajax({
url:log.php,
data:{'timeSpent':end - start},
async :false
})
});
});

在此处查看更多信息: http://api.jquery.com/unload/



这里唯一需要注意的是它使用了javascript的beforeunload事件,并不总是有足够的时间来发出像这样的AJAX请求,所以合理地你会丢失很多数据。



另一种方法是定期轮询服务器具有某种类型的STILL HERE消息,可以更一致地处理,但显然更昂贵。


I would like to measure a time (in seconds in integers or minutes in floats) a user spends on a page. I know there is an unload event which I can trigger when they leave the page. But how to get a time they have already spent there?

解决方案

If you use Google Analytics, they provide this statistic, though I am unsure exactly how they get it.

If you want to roll your own, you'll need to have some AJAX request that gets sent to your server for logging.

jQuery has a .unload(...) method you can use like:

$(document).ready(function() {
  var start = new Date();

  $(window).unload(function() {
      var end = new Date();
      $.ajax({ 
        url: "log.php",
        data: {'timeSpent': end - start},
        async: false
      })
   });
});

See more here: http://api.jquery.com/unload/

The only caveat here is that it uses javascript's beforeunload event, which doesn't always fire with enough time to make an AJAX request like this, so reasonably you will lose alot of data.

Another method would be to periodically poll the server with some type of "STILL HERE" message that can be processed more consistently, but obviously way more costly.

这篇关于如何衡量在页面上花费的时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆