2015-02-23 50 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

回答