Question

Is there any easy way to monitor how long a user views a page?

Google Analytics uses this feature by allowing you to see how long your website visitors stay on a particular page. Can anything like this be reproduced with PHP or JavaScript?

Was it helpful?

Solution

Yes, it's easy. Just start timer as page loads and send it to some server script to log, when user leaves the page.

To triger page leaving event read here.

Example client-side code:

<html>
...
<script>
var visit_start_time_ms = (new Date).getTime();
window.onbeforeunload = function() {
    var visit_time = (new Date).getTime() - visit_start_time_ms;
    xmlhttp=new XMLHttpRequest();
    xmlhttp.open("GET","http://your-tracking-server.com/track.php?track_url=...&visit_time=" + visit_time,true);
    xmlhttp.send();
};
</script>
...
</html>

Your server-side script at your-tracking-server.com/track.php:

...
mysql_query('INSERT INTO track_visit_time (url, time) VALUES ("'.myescape($_GET['track_url']).'", '.float($_GET['visit_time']).')');
...

Mind, that client side is not tested and not crossbrowser at all. Server side uses deprecated function mysql_query.

OTHER TIPS

A common method for doing this is simply measuring the time between one page view and the next. There are a lot of "gotchas" with this though!

  • You don't know the time for the last page loaded
  • You don't know time for bounces
  • Issues happen with multiple tabs and what not
  • Did someone stay on your page for 4 hours, or did they close their browser and come back?

To get around these problems, another common method is to poll the server. You can do this with either long-polling (where the connection stays open with no data transfer for a minute or so, and then another connection is immediately established), or repeated hits to your server every 5 seconds or so. I recommend the long-polling method, as it is far less bandwidth intensive.

You can do this in a controller environment, where the users eyes are tracked if they are looking on a specific webpage (and where). This is often useful to remove the problems if you use in-browser tracking only for common things like a user is getting a telephone call or switching to other tabs and all the things we do each day long when the browser is opened.

I think you could try using the window.onbeforeunload method to fire off an ajax call. When the page loads, record the time, when it unloads, send the elapsed time off to your server with ajax. The page would obviously never see the response, but that doesn't matter anyway.

As Brad points out this is somewhat unreliable. An alternative is to just update the server with elapsed time every few seconds. You should add half the interval to the elapsed time as well, to account for the time between the last update and the user leaving, on average. For example, if you update every 10 seconds, and the last update said 40 seconds, assume they were there 45 seconds. That covers cases between 40.0 seconds and 49.999 seconds that don't get recorded.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top