How to track user time on site

Viewed 21712

I'm looking to track users average time on a website (in the same way that Google analytics does) for internal administration.

What's the easiest way to do this?

5 Answers

Use timeonsite JS for web and mobile browsers. It tracks time on site accurately.

<script type="text/javascript">
        var Tos;
        (function(d, s, id, file) {
            var js, fjs = d.getElementsByTagName(s)[0];
            if (d.getElementById(id)) return;
            js = d.createElement(s);
            js.id = id;
            js.onload = function() {
                var config = {
                    trackBy: 'seconds' 
                };
                if (TimeOnSiteTracker) {
                    Tos = new TimeOnSiteTracker(config);
                }
            };
            js.src = file;fjs.parentNode.insertBefore(js, fjs);
        } (document, 'script', 'TimeOnSiteTracker', 'https://cdnjs.cloudflare.com/ajax/libs/timeonsite/1.2.0/timeonsitetracker.min.js'));
    </script> 

At the page end, call

<script>
    Tos.getTimeOnPage();
        //Response ->
        {
            TOSId: 1129620185532,
            TOSSessionKey: "14802525481391382263",
            TOSUserId: "anonymous",
            title: "Test application - TimeOnSiteTracker",
            URL: "http://tos-localdata.chennai/home.php"
            entryTime: "2016-11-27 13:15:48.663",
            currentTime: "2016-11-27 13:17:31.663",
            timeOnPage: 103,
            timeOnSite: 0,
            timeOnPageTrackedBy: "second",
            timeOnPageByDuration: "0d 00h 01m 43s",
            timeOnSiteByDuration: "0d 00h 00m 00s",
            trackingType: "tos",
        }
</script>

As simple as that,

It seems to work even in mobile browsers like IPhone, IPad, mobile Safari etc. that doesn't support window.unload() events natively.

Related