APLawrence.com -  Resources for Unix and Linux Systems, Bloggers and the self-employed

Ad Tracking and Rotation


© March 2001 Tony Lawrence

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.

By the way, I think the number is actually higher - much higher.

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="https://aplawrence.com/cgi-bin/countad.pl?https://www.netraverse.com" target-"_blank"> <img src="/NET-108button1.gif" border=0 alt="https://www.netraverse.com"></a>
<a href="https://aplawrence.com/cgi-bin/countad.pl?https://www.netraverse.com" target-"_blank"> <img src="/NET-108button2.gif" border=0 alt="https://www.netraverse.com"></a>
<a href="https://aplawrence.com/cgi-bin/countad.pl?https://www.netraverse.com" target-"_blank"> <img src="/NET-108banner.gif" border=0 alt="https://www.netraverse.com"></a>
<a href="https://aplawrence.com/cgi-bin/countad.pl?https://www.netraverse.com" target-"_blank"> <img src="/NET-108banner_glass.gif" border=0 alt="https://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:


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


Got something to add? Send me email.





(OLDER)    <- More Stuff -> (NEWER)    (NEWEST)   

Printer Friendly Version

->
-> Ad Tracking and Rotation


Inexpensive and informative Apple related e-books:

Take Control of Pages

Photos for Mac: A Take Control Crash Course

Digital Sharing Crash Course

iOS 10: A Take Control Crash Course

Sierra: A Take Control Crash Course




More Articles by © Tony Lawrence




Printer Friendly Version

Have you tried Searching this site?

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.

Contact us


Printer Friendly Version





I had a life once. Now I have a computer. (unknown)




Linux posts

Troubleshooting posts


This post tagged:

Code

Perl

Programming

SEO

Web/HTML



Unix/Linux Consultants

Skills Tests

Unix/Linux Book Reviews

My Unix/Linux Troubleshooting Book

This site runs on Linode





SCO Unix Sales, Support, & Service

Phone:  707-SCO-UNIX (707-726-8649Toll Free: 833-SCO-UNIX (833-726-8649)
www.SCOsales.com