开发者

Measuring online time on website

开发者 https://www.devze.com 2022-12-11 22:45 出处:网络
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.\"

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 timestam开发者_运维问答p 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!


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?


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.


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."\n".$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>\n");
    $lines = file($folder."/".$fn);
    foreach ($lines as $line_num => $line)
    {
      echo("  ".$line."<br>\n");
    }
    echo ("<br>\n <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.

0

精彩评论

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