How to calculate how long a user is on a web page?

im makes some statistic codes for my site (im php developper). I want to calculate how many seconds / minutes a web user remains on any page (for example, Google analytics), but I do not know how to do this. Thanks for any help or scripts!

+5
source share
5 answers

How do you collect data? Common parameters will be tools for the page using javascript, viewing web server log files, in a server-side request handler, or sniffing TCP / IP traffic.

Doing this, like Google Analytics, implies the first. In this case, the way to do this is to grab the timestamp as soon as possible when the page loads (rather than waiting for the page ready event / onload event) and compare this value with the previous tiestamp (so you would probably save this in a cookie). Then you need to somehow send this server server, as well as a way to record and report data.

Please note that an attempt to launch an ajax call when the user leaves the page, for example, via onunload , will not work reliably (the page that launches the request is at the end of the life cycle). The ASYNCHRONIC part is important here. And making a synchronous call will simply be the effect of slowing down the website.

, Yahoo Boomerang - , . , Graphite

+3

unload javascript, , Ajax . , , ping script ( Ajax), , (, 10-60 ).

+1

, php, , , , . , ""

script . , $_SERVER ['http_referer'], , URL .

. , $_SERVER ['http_referer'], . .

. http_referer URL- , , , .

, , - .

: , . .

0

, , .

, , script.

, , / , user-agent, ip, timestamp ..

ip , ( ).

, , ( , ).

Bounces are identified by single entries over a given ip for a certain period of time (maybe an hour is enough).

0
source

When loading the page, create a date object, and then, when the page unloads, creates another and subtracts them. After that, you can complete the AJAX request to your tracking server by sending the elapsed time.

var startTime = new Date();
var endTime;

window.onunload = function()
{
 endTime = new Date();
 var elapsedSeconds = endTime.getTime() - startTime.getTime();
 //Do the ajax request, sending elapsedSeconds
}
-1
source

All Articles