开发者

is there a way to time a visit to a site, disregarding moving through pages?

开发者 https://www.devze.com 2023-03-13 20:16 出处:网络
I\'m l开发者_StackOverflowooking for a way to time a visitors stay on a website, whilst not restarting or interrupting the timer when changing pages.Store the initial visit time in a session value. An

I'm l开发者_StackOverflowooking for a way to time a visitors stay on a website, whilst not restarting or interrupting the timer when changing pages.


Store the initial visit time in a session value. Any future visits to the site use a secondary session variable to hold the last visited time and work out the time on your site from that.

Unless you use Ajax to update the session values the user could be active on your site (on the same page) for 20minutes but it would show up as a zero length visit.

Basic example:

<?php
session_start();  
if(isset($_SESSION['firstVisit']))
    $_SESSION['latestVisit'] = date();
else
    $_SESSION['firstVisit'] = date();

echo $_SESSION['firstVisit'] . " - " . $_SESSION['latestVisit']; 
?>


You can start a session the moment the user requests the first page. With the session you can track the user from page to page. so you could calculate the total time between the first and last page requst of their visit.

You should also be able to send an Ajax request on page unload, thereby you could detect the time spent on a single page. However, if they browser in multiple windows/tabs, your reading is false, unless you are somehow able to detect page focus.

Combine the two and you should be able to get a quite complete picture.


store the data in the session, and save the data when the user performs logout

(if the user does not logout it will not be saved, but that should not be a problem)

edit: if the users does not login/logout, you have to catch when the session dies and save the data then. when evaluating the data you have to compensate for how long the session lives before it dies, e.g. 5 mins or whatever, and subtract it. will give you a ballpark figure, not exact time the user looked at the site.


You can track the time for which the user was logged in, but if you want the amount of time (a non logged in) user stays, I dont think it is possible, as there are a lot of ways to move from one page to another

0

精彩评论

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

关注公众号