I put some work into a small JavaScript library and a service that shows how long a user has been on a web page. It has the added advantage of more accurately (not quite, though) tracking how long the user actually interacts with the page. It ignores the time when the user switches to different tabs, goes into standby mode, minimizes the browser, etc. The Google Analytics method has the drawback (as I understand it) that it only checks when a new request is processed by your domain, and this is not always accurate. It does not take into account that someone is no longer viewing your page, minimized the browser, switched tabs to 3 different web pages since the last load of your page, etc.
For reference, the solution is not ideal. But, I hope this also gives value. You can implement the Javaacript API and collect statistics yourself, or you can use a service that does all this for you.
http://timemejs.com
An example of its use:
Include on your page:
<script src="https://timemejs.com/timeme.min.js"></script> <script type="text/javascript"> TimeMe.initialize({ currentPageName: "home-page", </script>
If you want to report time about yourself to your server:
xmlhttp=new XMLHttpRequest(); xmlhttp.open("POST","ENTER_URL_HERE",true); xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); var timeSpentOnPage = TimeMe.getTimeOnCurrentPageInSeconds(); xmlhttp.send(timeSpentOnPage);
TimeMe.js also supports the transfer of synchronization data through websockets, so you do not need to try to force a full HTTP request in the document.onbeforeunload event.
source share