2015-02-23 93 views
0

我想跟踪通过身份验证的用户在我的网站上花费的时间。在网站上花费的跟踪时间

这样做,我已经把这个代码在我的布局:

function js_yyyy_mm_dd_hh_mm_ss (param) { 
     now = new Date(param); 
     year = "" + now.getFullYear(); 
     month = "" + (now.getMonth() + 1); if (month.length == 1) { month = "0" + month; } 
     day = "" + now.getDate(); if (day.length == 1) { day = "0" + day; } 
     hour = "" + now.getHours(); if (hour.length == 1) { hour = "0" + hour; } 
     minute = "" + now.getMinutes(); if (minute.length == 1) { minute = "0" + minute; } 
     second = "" + now.getSeconds(); if (second.length == 1) { second = "0" + second; } 
     return year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + second; 
    } 
    var start; 



    $(document).ready(function() { 
     var d = new Date(); 
     start = d.getTime(); 
    }); 
    <% if current_user %> 
     $(window).on('beforeunload', function() { 
     var d = new Date(); 
     end = d.getTime(); 
     timeSpent = (end-start)/1000; 
     page = window.location.toString(); 
     debut=js_yyyy_mm_dd_hh_mm_ss(start); 
     fin=js_yyyy_mm_dd_hh_mm_ss(end); 

     $.ajax('<%= users_time_path %>', { 
      async: false, 
      type: 'POST', 
      data: {log_time:{start:debut, end: fin, timespent: timeSpent, page:page}}, 
      success: function(res,status) { }, 
      error: function (res, status) { 
       console.log('Server error: '+ status); 
      } 
     }); 
     <% end %> 

     }); 

但有了这个代码,如果用户有两个标签页中打开我算两倍的时间。

Futhermore如果用户离开这个计算机与标签中打开,回来两天后,我会算48h以上......

我应该怎么做,以避免?

我该如何做到服务器端避免用户假时间?

感谢

+0

有关使用谷歌分析这些类型的跟踪功能 – Finks 2015-02-23 07:42:27

+0

你可以发送一个ping到你的服务器时,标签模糊,怎么这你可以把它当作一个标签关闭。除了最后一页之外,总时间是页面服务之间花费的时间的总和...... – dandavis 2015-02-23 08:30:25

回答