最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin1浏览0评论

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?

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?

Share Improve this question asked Sep 13, 2011 at 8:30 ZabsZabs 14.2k50 gold badges179 silver badges311 bronze badges 1
  • You can't do this with PHP. You need some javascript that notifies your server side, when the user navigated away. – ZeissS Commented Sep 13, 2011 at 8:32
Add a ment  | 

4 Answers 4

Reset to default 3

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./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)

  1. 暂无评论