Build Your Own Web Measurement Application: Marketing Data

At this point, we’re sure you’re itching to generate some real, useful data with your “build your own” application. In this hack, we attack common marketing measurements, including number of visits, page views per visit, referrers, search terms, and entry pages.

In this hack, we shall continue writing our miniature web analytics program. In [Hack #12] , we parsed a logfile and collated the individual lines into visitor sessions. Now we shall report some actual results.

The Code

Previously, we used a class called Data to hold the statistics, but we didn’t define that class. It’s time to do that now. Save this code into a file called Data.pm.

    package Data;
    use strict;
    The number of items to list in each report
    my $top_n = 100;

At this stage, we will report the total number of sessions, the total number of requests, the list of referrers [Hack #1] , and the list of search terms [Hack #43] . We shall also report the list of entry pages; assuming you have set up your ad campaigns to have different entry pages [Hack #58] , this also tells you the number of visits from each campaign.

This constructor initializes all the variables we will need at this stage. We will add more variables in subsequent hacks.

     sub new {
       return bless {
         total_sessions => 0,
         total_requests => 0,
         referrers => {},
         search_terms => {},
         entry_pages => {},

      };
    }
    # Just before deleting an old session, add its data to the totals. sub AddSession { my ($self, $sess) ...

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.