Build Your Own Web Measurement Application: Usability Data

Now that you’ve read up on usability data, it’s time to add average time spent on site and a report on the number of single-page visits to the mix in your homegrown web measurement application.

In this hack, we’ll continue our example logfile analyzer by adding statistics for the time on site [Hack #57] and the number of single-page visits [Hack #58] . With the infrastructure we’ve already built up, adding new reports is now easy. We just need to expand the Session and Data classes to include some new statistics.

The Code

The Session class needs to know how many seconds the session lasted. This is just the time between the initial and final requests. However, it’s worth noting that this doesn’t include the time the visitor spent on the final page of the session. It is possible to write a second piece of JavaScript that makes a request to the server when the visitor leaves the page, in which case you can measure the time spent on the final page. But we won’t cover that here. To add the code, you will need to append the code into the appropriate spot in each file.

Append the following lines to the Sessions class in Sessions.pm.

  package Session;
  …
 sub NumSecs {
   my $self = shift;
   if ($self->NumRequests() == 0) { return 0; }
   else { return ($self->[-1]->{time} - $self->[0]->{time}); }
}

Three new variables are added to the constructor and measured in Data:: AddSession. Append these lines to the end of the Data class in Data.pm.

package Data;
  …
  sub new {

   return bless {
    …
    total_seconds => 0,
    atomic_sessions => 0,
    atomic_pages => {},

 };
}
sub AddSession {

…
  $self->{total_seconds} += $sess->NumSecs();
  if ($reqs == 1) {

    ++$self->{atomic_sessions};
    ++$self->{atomic_pages}->{$sess->EntryPage()};

 }
}
# Data::WriteSummary will report the average time on site, and
# the number of sessions with only one page.
sub WriteSummary {

…
printf "Average time on site: %.1fs\n",
  $self->{total_sessions} == 0 ? 0:
  $self->{total_seconds} / $self->{total_sessions};

printf "Sessions with only one page: %d (%.0f%%)\n",
  $self->{atomic_sessions},
  $self->{total_sessions} == 0 ? 0:
  $self->{atomic_sessions} / $self->{total_sessions} * 100;

} # And Data::WriteReport will write a list of the pages which # people arrived at 
but d idn't progress any further through the site. 
 sub WriteReport {
  … 
  $self->WriteHash('Pages in single-page sessions', 'atomic_pages');
 }

Running the Code

Just as you learned in [Hack #53] , all you need to do to run this program from the command line (again, assuming that page.log is in the same directory as readlog.pl) is type:

  perl readlog.pl page.log

This time you will be treated to some additional summary metrics (average page views per visit, average time spent on site, and single page view visits) and a report showing the number of visits by entry page and single access pages (Figure 4-13).

Output of readlog.pl

Figure 4-13. Output of readlog.pl

The next additions to readlog.pl and your “build your own” web measurement application will be relevant technographic data, covered in Chapter 5.

—Dr. Stephen Turner and Eric T. Peterson

Get Web Site Measurement Hacks now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.