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 do you measure how long the person has been on the page? #60

Closed slidenerd closed 5 years ago

slidenerd commented 5 years ago

First of all, fantastic work on the library. I am looking to measure 2 things

  1. How long the person has been on the page regardless of whether they interacted or not?
  2. How long the person has interacted on the page?
  3. Going through your issues I found that mentioned if you idleTimeout to -1 it will stop tracking immediately the moment person goes idle
  4. How do I not have an idle timeout at all meaning even if the person is just reading the page and not doing anything indefinitely, the timer should keep running

Suggestions are super appreciated

slidenerd commented 5 years ago

UPDATE I tried something but this has a drawback

TimeMe.initialize({
            currentPageName: "my-home-page", // current page
            // idleTimeoutInSeconds: 5, // stop recording time due to inactivity
            websocketOptions: { // optional
                websocketHost: "ws://your_host:your_port",
                appId: "insert-your-made-up-app-id"
            }
        });

I commented out idleTimeoutInSeconds but this has a drawback If I try to type something on the address bar, the timer stops running but unless I click on the page again it does not start running

slidenerd commented 5 years ago

Sorry my bad, modified your code slightly and does the trick


        var start = Date.now();

        window.onload = function () {
            TimeMe.trackTimeOnElement('area-of-interest-1');
            TimeMe.trackTimeOnElement('area-of-interest-2');
            setInterval(function () {
                var timeSpentOnPage = TimeMe.getTimeOnCurrentPageInSeconds();
                document.getElementById('timeInSeconds').textContent = timeSpentOnPage.toFixed(2);

                var timeSpentOnElement = TimeMe.getTimeOnElementInSeconds('area-of-interest-1');
                document.getElementById('area-of-interest-time-1').textContent = timeSpentOnElement.toFixed(2);

                var timeSpentOnElement = TimeMe.getTimeOnElementInSeconds('area-of-interest-2');
                document.getElementById('area-of-interest-time-2').textContent = timeSpentOnElement.toFixed(2);

                var totalTime = (Date.now() - start) / 1000;
                document.getElementById("totalTime").textContent = totalTime.toFixed(2)
            }, 25);