English 中文(简体)
Measuring online time on website
原标题:

I would like to measure how much time a user spends on my website. It s needed for a community site where you can say: "User X has been spending 1397 minutes here."

After reading some documents about this, I know that there is no perfect way to achieve this. You can t measure the exact time. But I m looking for an approach which gives a good approximation.

How could you do this? My ideas: 1) Adding 30 seconds to the online time counter on every page view. 2) On every page view, save the current timestamp. On the next view, add the difference between the saved timestamp and the current timestamp to the online time counter.

I use PHP and MySQL if this does matter.

I hope you can help me. Thanks in advance!

最佳回答

Two factors are working against you -

  1. You can only collect point-in-time statistics (page views), and there s no reasonable way to detect what happened between those points;

  2. Even then, you d be counting browser window time, not user time; users can easily have multiple tabs open on multiple browser instances simultaneously.

I suspect your best approximation is attributing some average amount of attention time per click and then multiplying. But then you might just as well measure clicks.

问题回答

This is probably pointless.... what if the user has three tabs open and is "visiting" your site while actually working on the other two tabs? Do you want to count that?

Why not just measure what actually can be measured?: referrals, page views, click-throughs, etc.

Collecting and advertising these kinds of numbers is completely in line with the rest of the world of web metrics.

Besides—if someone were to bring up a web page and then, say, go on a two week holiday, how best to account for it?

What you could do is check if a user is active on the page and then send an ajax request to your server every X seconds (would 60 secs be fine?) that a user is active or not on the page.

Then you can use the second method you have mentioned to calculate the time difference between two active timestamps that are not separated by more than one or two intervals. Adding these would give the time spent by the user on your site.

google analytics includes a very powerful event logging/tracking mechanism you can customize and tap into get really good measurements of user behavior - I d look into that

A very simple solution is to use a hidden iframe that loads a php web page periodically. The loaded web page logs the start time (if it doesn t exist) and the stop time. When the person leaves the page you are left with the time the person first came to the site and the last time they were there. In this case, the timestamp is updated every 3 seconds.

I use files to hold the log information. The filename I use consists of month-day-year ipaddress.htm

Example iframe php code. Put this in yourwebsite/yourAnalyticsiFrameCode.php:

<?php
  // get the IP address of the sender
  $clientIpAddress=$_SERVER[ REMOTE_ADDR ];
  $folder = "yourAnalyticsDataFolder";
  // Combine the IP address with the current date.
  $clientFileRecord=$folder."/".date( d-M-Y )."  ".$clientIpAddress;

  $startTimeDate = "";

  // check to see if the folder to store analytics exists
  if (!file_exists($folder))
  {
    if (!mkdir($folder))
      return;  // error - just bail

  }


  if (file_exists($clientFileRecord) )
  {
    //read the contents of the clientFileRedord
    $lines = file($clientFileRecord);

    $count = 0;

    // Loop through our array, show HTML source as HTML source; and line numbers too.
    foreach ($lines as $line_num => $line)
    {
      echo($line);
      if ($count == 0)
        $startTimeDate = rtrim( $line );
      $count++;

    }
  }

  if ($startTimeDate == "")
    $startTimeDate = date( H:i:s d-M-Y );

  $endTimeDate = date( H:i:s d-M-Y );

  // write the start and stop times back out to the file
  $file = fopen($clientFileRecord,"w");
  fwrite($file,$startTimeDate."
".$endTimeDate);
  fclose($file);


?>

The javascript to periodically reload the iframe in the main web page.:

<!-- Javascript to reload the analytics code -->
<script>
  window.setInterval("reloadIFrame();", 3000);

  function reloadIFrame() {
    document.getElementById( AnalyticsID ).src = document.getElementById( AnalyticsID ).src
      //          document.frames["AnalyticsID"].location.reload();
  }
</script>

The iframe in the main web page looks like this:

<iframe id="AnalyticsID" name="AnalyticsID" src="http://yourwebsite/yourAnalyticsiFrameCode.php" width="1"
height="1" frameborder="0" style="visibility:hidden;display:none">
</iframe>

A very simple way to display the time stamp files:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>
  <title></title>
</head>

<body>
Analytics results
<br>
<?php
  $folder = "yourAnalyticsDataFolder";
  $files1 = scandir($folder);
  // Loop through the files
  foreach ($files1 as $fn)
  {
    echo ($fn."<br>
");
    $lines = file($folder."/".$fn);
    foreach ($lines as $line_num => $line)
    {
      echo("  ".$line."<br>
");
    }
    echo ("<br>
 <br>");

  }

?>
</body>

</html>

You get a results page like this:

22-Mar-2015 104.37.100.30

18:09:03 22-Mar-2015

19:18:53 22-Mar-2015


22-Mar-2015 142.162.20.133

18:10:06 22-Mar-2015

18:10:21 22-Mar-2015

I think client side JavaScript analytics is the solution for this.

You have the google analitycs, piwik, and there also commercials tools in JS that do exactly that.





相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签