APLawrence - Information and Resources for Unix and Linux Systems, Bloggers and the self-employed
RSS Feeds Get APLawrence.com by RSS














(OLDER) <- More Stuff -> (NEWER) (NEWEST)
Printer Friendly Version



Ad Tracking and Rotation





The supposed "magic point" for selling web advertising is around 15,000 unique visitors per month. If your web site attracts less than that, you won't have much luck selling banner ads, and if you expect to make any real money, you'll need a lot more than that.

But whether or not you sell ads to anyone else, you may still want to run your own ads, perhaps to direct folks to other parts of your site, to solicit contributions, or to make people aware of upcoming events. For example, while this web site does generate enough traffic to sell some banners, I also mix in my own asking for contributions.

If you have a large site (or plan to), you certainly do not want to manually insert banners into pages. That's tedious and a pain to change. Also, if you are running more than one ad, you want to "rotate" the banners- present a different ad when a page is reloaded. But, for some pages, you may want a specific ad and only that ad to appear- for example, I sell exclusive rights to certain pages to banner advertisers.

And of course you also want to track ads- you want to know how many impressions an ad has had (how many times it has been seen) and how many times someone has clicked on it.

These features obviously require software. There are commercial packages that do this sort of thing, and you can find dozens of free cgi scripts on the web, but once again, none of them did exactly what I wanted, so I wrote my own.

Ad Rotation

Let's start with the ad rotator. This is pretty simple, actually. We'll store the ads in simple files. On a larger system, it would make sense to store this sort of data in a database rather than plain text files. On the other hand, plain text files have the attraction of quick and simple editing.

Here's what an adfile from my site looks like:

NETRAVERSE
www.netraverse.com
<a href="/cgi-bin/countad.pl?http://www.netraverse.com" target="_top"> <img src="/NET-108button1.gif" border=0 alt="http://www.netraverse.com"></a>
<a href="/cgi-bin/countad.pl?http://www.netraverse.com" target="_top"> <img src="/NET-108button2.gif" border=0 alt="http://www.netraverse.com"></a>
<a href="/cgi-bin/countad.pl?http://www.netraverse.com" target="_top"> <img src="/NET-108banner.gif" border=0 alt="http://www.netraverse.com"></a>
<a href="/cgi-bin/countad.pl?http://www.netraverse.com" target="_top"> <img src="/NET-108banner_glass.gif" border=0 alt="http://www.netraverse.com"></a>
 

This is an advertiser who runs 4 different banners. All four banners are kept in one file; the ad display script will choose between them. Each ad is one line; this makes the reading and display software simpler, and HTML doesn't care.

Note that the advertiser's web site is not called directly; instead a click will cause /cgi-bin/countad.pl to be called. That will record statistics about click-throughs and then redirect the browser to the adverisers site (or to your own internal pages).

Before we get to the script that presents the ads, we also need a place to store data about impressions (number of times the ad is seen) and click-throughs (number of times someone clicks on it). Each ad we want to track will have its own sub-directory, and will contain these files:

  • imps : impressions
  • clicks : click-throughs

In my actual system, I keep more data than this, breaking it down by source (dedicated or general pages), the starting date ( to know how many days the ad has run ), and the cost (to calculate costs per thousand impressions, etc.)

The script that handles this is called from a Server Side Include, so the entry into an html page looks like this:





 

Wherever that apears, it will be replaced by the output from "newad.pl". That script is presented here:

#!/usr/bin/perl5
#
$mydata="/www/data";
chdir "$mydata/myads"; 
#
# The ads are in numbered files: 1, 2, 3 etc..  For convenience, 
# I also link these these to real names, but we only use
# the numbers here
#
# count the ads
#
$nadds=0;
while (<[0-9]*>) {
   $nadds++;
}

#
# seed the random number generator
#
srand;
$prefad=shift @ARGV;
#
# We may want to force a particular ad to run.  
# We'd do that by adding an argument to the ssi call:
#
# would cause $prefad to be set to 3
#
# make sure we didn't ask for something we don't have
#
$prefad ="" if $prefadd > $nadds;
#
# now pick an ad
#
$nextad=int(rand($nadds)) + 1;
$nextad=$prefad if $prefad;
#
# and read the data
#
open(MYAD,$nextad);
$acct=<MYAD>;   # the account 
chomp $acct;
$match=<MYAD>;  # the web site (we use this in another script)
@adline=<MYAD>;  # now all the ads into an array
#
# how many ads?
#
$numlines=@adline;
#
# pick one
#
$thisadd=int(rand($numlines));
#
# output the ad
#
print "Content-TYPE: text/plain\n\n";
print "$adline[$thisadd]";
#
# just in case we screwed up the text file
#
$acct="MISC" if not $acct;
chdir "$mydata/adstats/$acct" or chdir "$mydata/adstats/MISC";
#
# record the impression
#
open(CNT, "+<imps");
flock CNT,2;
$cnt=<CNT>;
$cnt++;
seek(CNT,0,0);
print CNT "$cnt";
flock CNT,8;
close CNT;
 

As mentioned above, when someone clicks on the banner, the "countad.pl" script gets called, with the actual advertiser's site as an argument. That script has to figure out who the advertiser actually is. I could have passed that as an additional argument, but instead we look it up. The "countad.pl" script looks like this:

#!/usr/bin/perl5
$mydata="/www/data";
#
# $goto holds the place we actually want to get to after recording the click
#
$goto=shift @ARGV;
chomp($goto);
exit if not $goto;
#
# in case we screw up, have a fall-back to MISC
#
$acct="MISC";
chdir $mydata/myads;
#
# which advertiser is this?
#
while (<[0-9]*>) {
  open(FOO,$_);
  $tacct=<FOO>;
  chomp $tacct;
  $macct=<FOO>;
  chomp $macct;
  $acct = $tacct if $goto =~ /$macct/;
  
}
#
# if we screwed up the text file somehow
#
$acct="MISC" if not $acct;
chdir "$mydata/adstats/$acct" or chdir "$mydata/adstats/MISC";
#
# record the click
#
open(CNT, "+<clicks");
flock CNT,2;
$cnt=<CNT>;
$cnt++;
seek(CNT,0,0);
print CNT "$cnt";
flock CNT,8;
close CNT;
#
# redirect to the desired page
#
print "Location: $goto\n\n";
exit 0;
 

After recording the data, the broswer gets redirected. Note that this is not 100% accurate: a user could hit "Stop" and never get to the page; this actually measures intentions, not actual visits to the other page.

With the data in place in the impressions and clickthrough files, it's easy to write scripts to generate reports. I've been using this system for some time now; it is easy to maintain (just text files) and self-healing if you remove ads.

Publish your articles, comments, book reviews or opinions here!

© March 2001 A.P. Lawrence. All rights reserved





Click here to add your comments



Don't miss responses! Subscribe to Comments by RSS or by Email

Click here to add your comments


If you want a picture to show with your comment, go get a Gravatar



Have you tried Searching this site?

Unix/Linux/Mac OS X support by phone, email or on-site: Support Rates

This is a Unix/Linux resource website. It contains technical articles about Unix, Linux and general computing related subjects, opinion, news, help files, how-to's, tutorials and more. We appreciate comments and article submissions.

Publishing your articles here

Jump to Comments



Many of the products and books I review are things I purchased for my own use. Some were given to me specifically for the purpose of reviewing them. I resell or can earn commissions from the sale of some of these items. Links within these pages may be affiliate links that pay me for referring you to them. That's mostly insignificant amounts of money; whenever it is not I have made my relationship plain. I also may own stock in companies mentioned here. If you have any question, please do feel free to contact me.

Specific links that take you to pages that allow you to purchase the item I reviewed are very likely to pay me a commission. Many of the books I review were given to me by the publishers specifically for the purpose of writing a review. These gifts and referral fees do not affect my opinions; I often give bad reviews anyway.

We use Google third-party advertising companies to serve ads when you visit our website. These companies may use information (not including your name, address, email address, or telephone number) about your visits to this and other websites in order to provide advertisements about goods and services of interest to you. If you would like more information about this practice and to know your choices about not having this information used by these companies, click here.


book graphic unix and linux troubleshooting guide

My Troubleshooting E-Book will show you how to solve tough problems on Linux and Unix systems!



 I sell and support
 Kerio Mail server




pavatar.jpg
More:
       - Programming
       - Code
       - Perl
       - Web/HTML


Unix/Linux Consultants

Skills Tests

Guest Post Here











My Favorites

Change Congress