You're
planning on advertising in a number of magazines, web sites, and
newspapers, and you realize that you'd like to gauge
how much traffic is coming in from each
advertisement -- it'll help you better spend your
money in the future. Unfortunately, you have no time to implement
something complicated, so you're looking for a
low-tech solution, as well as something that will make it easy to
analyze the results.
Apache happily logs all environment variables passed to the pages it
serves to the outside world. Thus, the hack is simple: pass an
advertisement-based QUERY_STRING to the main page of your site. The
QUERY_STRING will be ignored if you don't
specifically act on it (with SSI, PHP, CGI, etc.), but Apache will
still log the information to it's
access_log.
For example, say you're advertising in the New York
Times. Instead of saying "Come visit us at
http://www.GatesMcFaddenCo.com,"
change the advertisement to specifically match the readership:
http://www.GatesMcFaddenCo.com/?nyt
In the above example, anytime someone types in that address, Apache
will serve the main page normally but silently log that a
QUERY_STRING of "nyt" has been
passed. Using a log analyzer (like analog, or the simple one below),
you can then find out how many people visited your site from seeing
your ad in the New York Times.
Since you're using QUERY_STRINGs, you can also act
upon them with Server Side Includes, PHP, or any other server-side
language. For instance, the example HTML page below would show
different greetings based on the web site address that the user typed
in.
<html>
<head>
<title>Apache Hack #12396 - Simplistic Ad Referrel Tracker</title>
</head>
<body>
<!--#if expr="\"$QUERY_STRING\" = \"nyt\"" -->
<h1>Welcome New York Times Reader!</h1>
<!--#elif expr="\"$QUERY_STRING\" = \"xml\"" -->
<h1>Welcome XML.com Reader!</h1>
<!--#else -->
<h1>Welcome To Our Site!</h1>
<!--#endif -->
</body>
</html>
The above will show special greetings if the user types in a
QUERY_STRING that corresponds to advertisements at XML.com or at the
New York Times. If the user didn't type in either,
then a generic welcome is shown. Some sites have been known to change
more than just the greeting, customizing the color, logos, and
internal ads served.
Be careful when you're choosing your
QUERY_STRING -- if it's too long, hard to
remember, or suitably threatening, then the user may mistype the
address (creating bad statistics) or else not type the QUERY_STRING
at all. In a worst case scenario, they may not even make the attempt
to visit. These are all bad choices:
# this one is simply too long.
http://www.gamegrene.com/?newyorktimes-04-21
# this one would be hard to remember or type.
http://www.gamegrene.com/?04xlmfo3d
# and this may worry people.
http://www.gamegrene.com/?track=nyt
If you are advertising online, encoding query strings in anchor tags
works just fine (and can be as long as you like, as
it's all just one click as far as the user knows).
To analyze your stats upon request, try the following Perl script.
Listing: referral-report.pl
#!/usr/bin/perl -w
use strict; my %ads;
# define each of your QUERY_STRINGS,
# and the matching "real name" here.
# this script will only look for
# QUERY_STRINGS that are letters and
# numbers only (no dashes, etc.)
$ads{"xml"} = "XML.com";
$ads{"nyt"} = "New York Times";
$ads{"ora"} = "O'Reilly and Associates";
# your Apache access log location.
my $logfile = "/var/log/httpd/access_log";
# define some counters.
my %ads_count; my $total;
# open the logfile.
open(LOG, "<$logfile") or die $!;
# and loop through each line.
while (<LOG>) {
# skip over lines we're not interested in.
next unless /GET/; next unless /\?(\w+)\s/;
# save the query_string.
my $query_string = $1;
# move on if not defined, else increment.
next unless exists $ads{$query_string};
$total++; $ads_count{$query_string}++;
}
# and print out the data.
print "There were a total of $total ad referrals.\n";
foreach ( sort keys %ads_count ) {
print "$ads{$_} had $ads_count{$_} ad referrals.\n";
}
# close logfile and
# exit the program.
close(LOG); exit;
The results of this script would look like:
There were a total of 17 ad referrals.
XML.com had 6 ad referrals.
New York Times had 7 ad referrals.
O'Reilly and Associates had 4 ad referrals.
Being simplistic, this code does not track multiple hits from the
same IP address, nor does it create percentages based on the total
amount of referrals received. Both should be easy for any Perl hacker
to implement.