?
Solved

length of time on a website

Posted on 2012-09-04
3
Medium Priority
?
489 Views
Last Modified: 2012-09-08
I do not want to use google analytics for this project

how can I discover how long (in seconds) user is on website


timestamp.php
<?php
// assume sanitation ....
$action = $_POST['action'];
$page = $_POST['page'];

if ($action == 'stop') {
   $start = $_SESSION['last_time'];
   $page = $_SESSION['last_page'];
   $visit_time = time() - $start;
   // do something with values
}
else {
   $_SESSION['last_time'] = time();
   $_SESSION['last_page'] = $page;
}


echo '<br>session last_time: '.$_SESSION['last_time'];
echo '<br>session last_page: '.$_SESSION['last_page'];
echo '<br>visit time '.$visit_time;

?>

Open in new window


timestamp2.php
<?php
?>
<html>
<head>
<script type="text/javascript">
var page = window.location; // Or you can set this with a php script
$(function() {
  $.post('timestamp.php', {page: page, action: 'start'});
  $(window).unload(function () {
      $.post('timestamp.php', {page: page}, action: 'stop');
  });
});

</script>
</head>
</html>

Open in new window




I want to see how many seconds the user is on the webpage

I visit timestamp.php
and see only session_last_time (which increases)


I visit timestamp2.php
and there is no output
0
Comment
Question by:rgb192
3 Comments
 
LVL 37

Accepted Solution

by:
Neil Russell earned 1000 total points
ID: 38362621
Personally I would discount every report that ever told me how long a user spends on a web page as meaningless.
Just because a page is open in a web browser does NOT mean that it is being read.
0
 
LVL 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 1000 total points
ID: 38367632
Agree with Neilsr, 100%.  The notion of whether a client is logged in, or how long a client is "on" a web page is simply a fantasy.  You cannot know this in any meaningful way.  In the client-server environment, clients make requests and servers make responses.  It is a stateless protocol.  There is no way to know what you're asking for, and Google Analytics gives you at best an approximation of the time from page-to-page within a given site.  Here is an example:

You visit Google.com and the Google web page appears on your computer.  Then you get up, go into the kitchen and get some coffee.  Your wife comes to the computer, visits Yahoo, then brings the Google page back up.  When you get back into the office, you make a Google search for something.

Here is what just happened on your side (the client side, in client-server).

1. Client made a request for Google
2. Client made a request for Yahoo
3. Client made a request for Google
4. Client made a request for Google

In each case, a server responded to the request with a web page, then the server disconnected.  When the server has made its response, its work is complete.  It no longer knows anything about the client.  It does not know whether you are reading the page, whether you have gone to get coffee, or whether you have turned off your computer.  The only indication the server might get would occur when the client made a subsequent request for another web page on the same server.  This information can be tracked, but not reliably.  Google Analytics is about as good as you can get.  And of course it is unable to handle the increasingly common situations in which you might have more than one browser instance open to the same site.

If you dig into the client-server protocol, you will see that there is only a request from the client.  There is never a "goodbye" from the client.  The goodbye signal comes from the server when it disconnects after it has sent the response.  So the utility of the response, and the client's reaction to it cannot be known by the server unless and until the client uses a link in the response page to visit another page.  

Given that your site (or any site) bounce rate is probably 25% or more, you're dealing with mostly inaccurate data if your application design somehow relies on this kind of information.

There are some laboratories that use computerized cameras with eye-tracking heat maps to identify how human clients use web sites.  Fidelity Investments has been a leader in this area of research, as has AARP.  They get closer to the answer by studying the client in situ where they can discern patterns of behavior that are simply invisible on the server side.
0
 

Author Closing Comment

by:rgb192
ID: 38380173
thank you for the insight and explanation
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses
Course of the Month16 days, 5 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question