jasonzissman / TimeMe.js

A JavaScript library to accurately time how long a user views a web page, disregarding idle time and time when the tab or window is minimized.
MIT License
592 stars 124 forks source link

How to get total time spent on multiple pages? #62

Open niitprachi opened 5 years ago

niitprachi commented 5 years ago

Hi,

Thanks for this great plugin. as I can see when page is reloaded, time counter is set to '0'. I have below query

1) lets say if user spent 2 min on home page and then goto about page, is there a way on about page counter should continue i..e start from 2 min and go ahead

i.e. instead of tracking time spent on page I would like to see total time spent on website when user visited different pages

Thanks Pravin

ghost commented 3 years ago

I have the same question

carlgrundberg commented 1 year ago

I have solved this by storing the time when page unloads and then combining the stored time with the current time.

function combineTimers(storedTime) {
  return TimeMe.getTimeOnAllPagesInSeconds().reduce((obj, p) => {
    obj[p.pageName] = Math.round((obj[p.pageName] || 0) + p.timeOnPage);
    return obj;
  }, storedTime);
}