Furthermore - TimeMe tracks time usage across multiple pages. This is particularly useful when running a single page web application. You can get a list of all aggregate times spent on all pages from TimeMe.js.
You can see a demo of a timer using TimeMe.js here. You can use Bower to install TimeMe (see next steps) and its dependencies. Alternatively, you can download the most recent copy at the TimeMe Github project. Notice you will also need a copy of Serkanyersen's ifvisible.js project. The ifvisible.js library is REQUIRED to allow TimeMe.js to work.First, obtain a copy of timeme.js and ifvisible.js. You can get both by installing TimeMe.js via Bower:
bower install timeme.js
Then, simply include the following lines of code in your page's head element:
<script src="ifvisible.js"></script>
<script src="timeme.js"></script">
<script type="text/javascript">
TimeMe.setIdleDurationInSeconds(30);
TimeMe.setCurrentPageName("my-home-page");
TimeMe.initialize();
</script>
This code both imports the TimeMe.js library and initializes it. Notice that this code sets the idle duration to 30 seconds, which means 30 seconds of user inactivity (no mouse or keyboard usage on the page) will stop the timer. Also, we define a page name (my-home-page) to associate with the current timer.
Once imported and initialized, we can call the various methods made available by TimeMe.js. See the API documentation below for a complete breakdown of all of the available functionality. The most basic feature is to retrieve the time spent by the user on the current page:
var timeSpentOnPage = TimeMe.getTimeOnCurrentPageInSeconds();
In most cases you will want to store the time spent on a page for analytic purposes. You will therefore need to send the time spent on a page to the server at some point! When is the best time to do this? You can hook into the window.onbeforeunload event to do so. In most browsers this method is fired during a page's shut-down routine. Notice below that we use a synchronous request (not the usual asynchronous request) to guarantee the request to our server arrives before the page closes:
window.onbeforeunload = function (event) {
xmlhttp=new XMLHttpRequest();
xmlhttp.open("POST","ENTER_URL_HERE",false);
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
var timeSpentOnPage = TimeMe.getTimeOnCurrentPageInSeconds();
xmlhttp.send(timeSpentOnPage);
};
Using 'onbeforeunload' is by no means a requirement. You can hook into any other event or logical point in your application to send the time spent information to the server.
In a traditional web design where one static page is served for each request made by the client, any call to getTimeOnCurrentPageInSeconds() will be unique and valid for each page that imports and initializes TimeMe.js. Alternatively, if using a Single Page Application (SPA) design, TimeMe.js can have its timer stopped, page name switched, and the timer resumed (for the new page) with the following calls:
TimeMe.stopTimer();
// ... Now might be a good time to upload the time spent on the page to your server!
// ... load up new page
TimeMe.setCurrentPageName("new-page-name");
TimeMe.startTimer();
All page times are tracked in TimeMe.js, so you can review total aggregate time spent on each page for a particular user's session:
var timeSpentReport = TimeMe.getTimeOnAllPagesInSeconds();
This call will return an array of objects of page names and the corresponding aggregate time spent on that page.
TimeMe.setCurrentPageName(newPageName);
TimeMe.setIdleDurationInSeconds(durationInSeconds);
TimeMe.initialize();
var timeInSeconds = TimeMe.getTimeOnCurrentPageInSeconds();
var timeInSeconds = TimeMe.getTimeOnPageInSeconds(pageName);
var timeSpentInfo = TimeMe.getTimeOnAllPagesInSeconds();
TimeMe.startTimer();
TimeMe.stopTimer();
TimeMe.resetRecordedPageTime(pageName);
TimeMe.resetAllRecordedPageTimes();