Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to calculate how much time the user is on a web page?

Tags:

javascript

php

im making some statistic codes for my website (im a php developper). I want to calculate how many seconds/minutes the web user stay on any page (like google analytics do) but i have no idea of how to make this. Thanks for any help or scripts!

like image 596
DomingoSL Avatar asked Sep 13 '11 07:09

DomingoSL


People also ask

How do you calculate time on a website?

Also known as Session Duration in Google Analytics, it is calculated by dividing the total duration of all sessions (in seconds) by the number of sessions.

How much time do users spend on a website?

This statistic is also known as “dwell time”. By looking at these analytics, we can determine that the average amount of time spent on a website can be anywhere between 15 and 45 seconds.

What is the average time on page?

Average Time on Page is a web analytics metric that measures the average amount of time spent on a single page by all users of a website. This metric does not consider exit pages or bounces, and only measures the average time spent by users on non-exit pages.


2 Answers

How are you gathering the data? The common options would be instrumenting the page using javascript, looking at webserver log files, in the server-side request handler or sniffing the TCP/IP traffic.

Doing it "like Google Analytics" implies the former. In which case the way to do it would be to grab a timestamp as soon as possible when the page loads (rather than waiting for page ready / onload event) and compare that value with the previous tiestamp (so you'd probably store that in a cookie). Then you need some way to send this back serverside, and a way of recording and reporting on the data.

Note that trying to fire an ajax call as the user leaves the page, e.g. via onunload, will not work reliably (the page launching the request is at the end of its lifecycle). The important thing here is the ASYNCHRONOUS part. And making a synchronous call will just have the effect of slowing down the website.

You might want to have a look at Yahoo Boomerang - although it doesn't support dwell time measurements out of the box, it's easy to extend. For a backend, you could do a lot worse than Graphite

like image 80
symcbean Avatar answered Sep 28 '22 09:09

symcbean


You can fire an unload event in javascript when the user leaves the page, which sends an Ajax request to your server. Since this may not work in all browsers, especially if the network latency is high, also have a ping script (also with Ajax) which calls your statistics system once in a while as long as the user stays on the page (for example, every 10-60 seconds depending on the resolution you want).

like image 36
Emil Vikström Avatar answered Sep 28 '22 08:09

Emil Vikström