简体   繁体   English

计算花费在网站上的时间而不是单个页面

[英]Calculate time spent on website not single page

I want to calculate total time spent on my website not single page in jQuery. 我想计算在我的网站上花费的总时间,而不是jQuery中的单个页面。

How can i do that? 我怎样才能做到这一点? Please share your views. 请分享您的看法。

I can get time spent on single page via handling jQuery unload event but it reset on new page load. 通过处理jQuery卸载事件,我可以花时间在单个页面上,但是在新页面加载时会重置它。

When user enter on any page, you have to know if he navigate between pages or enter on your website. 当用户在任何页面上输入时,您必须知道他是在页面之间导航还是在您的网站上输入。 This can be done by look at the last lastLeave data in the local storage (you have to set it when user leave a page). 这可以通过查看本地存储中的最后一个lastLeave数据来完成(您必须在用户离开页面时进行设置)。 You can do something like that: 您可以执行以下操作:

window.onload = function onload() {
  const now = new Date();
  const lastLeave = localStorage.getItem('lastLeave');
  if (!lastLeave || now - lastLeave >= MAXTIME_BETWEEN_PAGE) {
    localStorage.setItem('lastEnter', now);
  }
}

window.onunload = function onunload() {
  localStorage.setItem('lastLeave', new Date());
}

function getTimeSpend() {
  return new Date() - localStorage.getItem('lastEnter');
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM