Javascript 如何跟踪在网站上花费的时间

Javascript 如何跟踪在网站上花费的时间,javascript,ruby-on-rails,ruby,coffeescript,time-tracking,Javascript,Ruby On Rails,Ruby,Coffeescript,Time Tracking,在我的rails应用程序中,我希望跟踪每个用户在站点上花费的总时间。 我对它进行了研究,但没有找到完美的解决方案。 怎么可能呢。类似的事情 var time,timeSite; window.onload=function(){ time=new Date(); } window.onbeforeunload=function(){ timeSite=new Date()-time; window.localStorage['timeSite']=timeSite; //store

在我的rails应用程序中,我希望跟踪每个用户在站点上花费的总时间。 我对它进行了研究,但没有找到完美的解决方案。
怎么可能呢。

类似的事情

var time,timeSite;
window.onload=function(){
 time=new Date();
}


window.onbeforeunload=function(){
 timeSite=new Date()-time;
 window.localStorage['timeSite']=timeSite;
 //store it with whatever serverside language.
}
如果需要更多,请添加
window.onblur

这个代码有效。。ajax也可以很好地工作

在extreme解决方案中,将其存储在localstorage中(如示例中所示),然后在下次登录时执行ajax

这是一个以秒为单位的精确时间

如果您需要大约的时间,并且对于没有本地存储的用户,请每5分钟添加一次设置超时

如果ajax和localstorage不起作用,那么更新时间就会过去


下面是一个ajax调用,用于在onbeforeunload函数中进行测试

更改
railsurl
您需要的任何内容

var x=new XMLHttpRequest;x.open('get','railsurl?time='+timeSite);x.send();

正如@Yiğitcan Uçum所提到的

为什么不使用服务器端会话来开始和结束在站点上花费的时间?

尝试以下方法:

var time,timeSite;

  window.onload = function(){
  time = new Date();
 }


   $(window).unload(function () {
     timeSite = new Date() - time;
     $.ajax({
     type: 'GET',
     async: false,
     data: {timespent: timeSite},
     url: '/url/to/rails.com'
   });
 });
jquery
unload()
事件处理程序可用于处理这种情况

示例:

var loggedInAt;
var loggedOutAt;

var userId = getUserId (); 

function getUserId () {
 return userId; // Define a mathod for getting the user if from a cookie or any local variables.
}

function getTotalTimeSpent() {
 return (loggedOutAt - loggedInAt);
}

$(document).ready(function() {
  loggedInAt = Date.getTime(); // get the start time when the user logged in

  $(window).unload(function() {
      loggedOutAt = Date.getTime();
      totalTimeSpent = getTotalTimeSpent(loggedOutAt, loggedInAt);
      $.ajax({ 
        url: "www.yoursite.com/track.php",
        method:post,
        data: {'TotalTimeSpent': totalTimeSpent, 'UserId' : userId}
      })
    });
}
创建一个服务器端脚本,比如track.php,将收集的所有跟踪详细信息保存到数据库中

/*   Assuming you are tracking authenticated users the */
在Javascript中:

var loggedInAt;
var loggedOutAt;

var userId = getUserId (); 

function getUserId () {
 return userId; // Define a mathod for getting the user if from a cookie or any local variables.
}

function getTotalTimeSpent() {
 return (loggedOutAt - loggedInAt);
}

$(document).ready(function() {
  loggedInAt = Date.getTime(); // get the start time when the user logged in

  $(window).unload(function() {
      loggedOutAt = Date.getTime();
      totalTimeSpent = getTotalTimeSpent(loggedOutAt, loggedInAt);
      $.ajax({ 
        url: "www.yoursite.com/track.php",
        method:post,
        data: {'TotalTimeSpent': totalTimeSpent, 'UserId' : userId}
      })
    });
}
在tracking.php中:

var loggedInAt;
var loggedOutAt;

var userId = getUserId (); 

function getUserId () {
 return userId; // Define a mathod for getting the user if from a cookie or any local variables.
}

function getTotalTimeSpent() {
 return (loggedOutAt - loggedInAt);
}

$(document).ready(function() {
  loggedInAt = Date.getTime(); // get the start time when the user logged in

  $(window).unload(function() {
      loggedOutAt = Date.getTime();
      totalTimeSpent = getTotalTimeSpent(loggedOutAt, loggedInAt);
      $.ajax({ 
        url: "www.yoursite.com/track.php",
        method:post,
        data: {'TotalTimeSpent': totalTimeSpent, 'UserId' : userId}
      })
    });
}
获取变量
$\u REQUEST['TotalTimeSpent']&$\u REQUEST['UserId']
,并进行插入查询以将信息中继到数据库中


谢谢。

我怎样才能知道窗户关上的时间。这一切都很好,但是你要怎么把它发送到其他地方呢?例如,您不能执行AJAX请求,因为窗口将在完成之前关闭。window.onbeforeunload函数在表单提交时调用函数。我的主要问题是AJAX调用,是否有其他方法可以执行此操作。您已经测试过AJAX吗?localstorage是最直接的解决方案,每次都可以使用。我在卸载之前使用它。。在一些脚本上,我从来没有遇到过问题。我个人没有测试过ajax。。但是一个快速的XmlHttpRequest也能很好地工作。如果你想我为你添加一个合适的ajax调用,你只需要发送一个小字符串。。。未使用jquery Obiovsli已使用:javascript:window.onbeforeunload=function()