开发者

What is the easiest way to log how long someone is viewing a page using PHP?

What is best/cleanest method of tracking how long someone stays on a page? I know Google Analytics does this but I require the data to be stored locally within a preset database.

Any ideas of how best to go about this?开发者_JS百科


You understand that you cannot know how long the page stays opened in a browser, nor if the person is currently viewing it, or has switched to another tab in a browser or another window?

That's why, for example, forum "currently connected users" lists are so imprecise. Sometimes, they will show you that the user is connected, while this user closed the browser and shut down the PC for a few minutes.

You can have an approximation by sending regular AJAX requests to your server. This is why, for example, web based chat applications are much more precise, and can actually show more or less the actually connected users, since they are exchanging information with the server much more frequently.

But does it worth it? It will waste your bandwidth and the bandwidth of your visitors. Some visitors hate being tracked in such way and will stop using your website. Also, if you will obtain some metrics, they will still be imprecise. Finally, measuring the number of unique visitors per day and the number of requests per visitor is much more significant for a website.


I believe you can fire a JavaScript upon the event of someone leaving a page. If you have a timer/counter run in JavaScript it could send the value as an ajax to the server, saying how many seconds it have been posted.

The only thing I'm uncertain of in this solution is if all browser got the time to send this ajax before they change page.

EDIT: Here's an example on the event.

If this method is invalid in some browsers, please let me know.


For those interested, I've put some work into a small JavaScript library that times how long a user interacts with a web page. It has the added benefit of more accurately (not perfectly, though) tracking how long a user is actually interacting with the page. It ignore times that a user switches to different tabs, goes idle, minimizes the browser, etc.

Admittedly, as MainMa suggested - some users may not like this level of tracking.

https://github.com/jasonzissman/TimeMe.js

An example of its usage:

On loading your page:

document.onload = function() {
  TimeMe.setIdleDurationInSeconds(30);
  TimeMe.setCurrentPageName("my-home-page");
  TimeMe.initialize();  
}

Retrieving time spent on the page, and sending it to your server when the user leaves your page:

window.onbeforeunload = function (event) {
    xmlhttp=new XMLHttpRequest();
    xmlhttp.open("POST","ENTER_URL_HERE",false);
    xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    var timeSpentOnPage = TimeMe.getTimeOnCurrentPageInSeconds();
    xmlhttp.send(timeSpentOnPage);
};


ok heres an way to do it in PHP since How long someone viewing an page = Time between the two HTTP request .

you can create a session variable $time, set it to zero if its not already set , if already set then take that value out and reset it to $time = time();

$time = $_SESSION['time'] ;
if(!$time)
{
$_SESSION['time'] = 0;
}else {
if($time != 0)
$timeSpentByUserOnPage = time() - $time;  

$_SESSION['time'] = time();

}

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜