Saturday, March 21, 2009

Tracking Perl, PHP, ASP and Other CGI-generated Pages

Introduction


If your web pages are produced dynamically, adding WunderCounter code can,
in many cases, be very easy. If all of your pages are produced by a central
script or use some sort of common header or footer, you can save yourself
a lot of work and start tracking all of your pages within minutes. Here's
how you do it.

Editing WunderCounter Code


First off, you need to generate some WunderCounter code and have a close look
at it. You'll see that the URL that creates the counter is made up of a series
of name/value pairs, like part of the code used to create invisible counters:

&page=index.html&digits=5


What we're interested in for this tutorial is the page=index.html
part of the URL. If you want to the same style of counters across your entire site,
all you need to do is change the page name in the URL for the various pages/scripts
on your site.

This is where environment variables come in. These are variables that
are passed on by the web server and are available to your scripts. The actual
environment variables that are available to your script depend on the
configuration of your web server. Let's get to the examples that should work
for your right out of the box for you.

Please note:


The counter code used in these examples is very basic -- it's only meant to
get you up and running as quickly as possible. In almost every case, you'll
want to create custom code for your site by generating your own custom counter
code (found under the Generate HTML menu). You may then use this custom
code in your scripts for the best results.

Perl (CGI)


Here's an example of a Perl script that will create some basic counter code:


#!/usr/bin/perl

use strict;
use warnings;

use CGI;
my $q = CGI->new();

# print HTML header
print $q->header;

# usually looks like "/script.cgi", so we remove the first slash to get "script.cgi"
my $page = substr($ENV{'SCRIPT_NAME'}, 1);

my $counter_code = qq[<img src="http://www.wundercounter.com/cgi-bin/stats/image.cgi?user=username&page=$page&digits=5" >];

print $counter_code;



If you're using a templating system, like HTML::Template, you can save the
counter code in its own file and then include it in all of your pages via the
TMPL_INCLUDE syntax.

Perl (SSI)


Here's an example of a Perl script that will create some basic counter code,
when used via SSI (Server Side Include)


#!/usr/bin/perl

use strict;
use warnings;

use CGI;
my $q = CGI->new();

# print HTML header
print $q->header;

# usually looks like "/index.html", so we remove the first slash to get "script.cgi"
my $page = substr($ENV{'DOCUMENT_URI'}, 1);

print qq[<img src="http://www.wundercounter.com/cgi-bin/stats/image.cgi?user=username&page=$page&digits=5" >];




Create a folder in the top of your web directory called "wunder". Save this
script in the "wunder" folder as "counter.pl". Make sure the script is
executable. On a *nix machine that would be:


chmod 755 wunder/counter.pl


If you are unfamiliar with Server Side Includes, check the Apache tutorial
Introduction to Server Side Includes
for tips on how to get started. Once you're ready to go, you can include the
counter code in all of your pages by adding the following:


<!--#include virtual="/wunder/counter.pl" -->


PHP


The following example snippet of PHP code can be included by all of your .php
pages. To do so, save the following code snippet as wundercounter.php:



<?

$page_name = substr($_SERVER['SCRIPT_NAME'], 1);
echo "<img src=\"http://www.wundercounter.com/cgi-bin/stats/image.cgi?user=username&page=$page_name&digits=5\">";

?>



Now, you can include the counter code in any page you like, using the following
syntax:


include 'wundercounter.php';



ASP


The following example snippet of ASP code can be included by all of your .asp
pages. To do so, save the following code snippet as wundercounter.asp:



<%
' get the script name from environment variable
strPageName = Request.ServerVariables("SCRIPT_NAME")

' remove the slash that begins the script name
strPageName = Mid(strPageName, 2)
%>

<img src="http://www.wundercounter.com/cgi-bin/stats/image.cgi?user=username&page=<%=strPageName%>&digits=5">



Now, you can include the counter code in any page you like, using the following
syntax:


<!--#include virtual="/wundercounter.asp" -->

No comments: