Search Engine Optimization Techniques

Using Perl - Simple Log Script

Search Engine Optimization Techniques | Web Site Optimization Tools | Online Test Engine | Our Services | Technology and Help Resources

Using Perl: "Perl has been hailed as the single most important tool for expanding websites quickly and for minimizing development times".

Perl - Practical Extraction and Report Language, introduced in 1987 by Larry Wall, is probably the single most important reason for the explosive growth of the Internet. This free, fast and powerful language has incorporated some of the best features of several languages: C, awk, sed, and BASIC. What has started as a System administration tool has become the "glue language of the Net", owing to its popularity and sustained growth.

Almost the default language of choice for the Common Gateway Interface (CGI), Perl drives most of the successful dynamic websites. Now that we have laid down the preliminary introduction for Perl, we will explore a way of tracking our WebPages and even some of email campaigns we have labored to create.


Why do we need tracking ?
Let's consider this hypothetical situation:
You have created a string of WebPages demonstrating some of the features of your products and you want a feedback on the most popular page, where your visitors come from and what browser/OS they are using. Remember that the Internet is an evolving technology with constant change. This feedback information is vital for your business. Without this critical data, your meticulously planned and orchestrated marketing campaign will flounder. It is equally certain that your visitors are not going to spend the extra minutes of their precious time to send you their preferences. So it is imperative that we devise a way of monitoring our WebPages and extract this information with no active participation from our visitors.


Equally, our method should not be so intrusive (like opening Pop-up/Pop-behind windows to fill some questionnaire) as to deter our visitors. For the technically savvy readers, extracting such data from the server's raw access logs would spring to mind. But as in all things in life, this method is complicated and may not be practical for all. There is an easy way to track down the user information with little effort.


Simple Log Script:
With a little nifty Perl script (barely 11 lines long) and some judicious use of JavaScript, tracking our user will be easy.

#!/usr/bin/perl
my $envy=$ENV{'QUERY_STRING'};
my $file="path/log_file";
($sec,$min,$hour,$mday,$mon,$year,$wday)=(localtime(time))[0,1,2,3,4,5,6];
$time= sprintf("%02d:%02d:%02d",$hour,$min,$sec);
$year +=1900;
$mon +=1;
$dayo ="$mday/$mon/$year:-$hour:$min";

open( LOG, ">>$file" );
print LOG "$dayo\|$ENV{ 'REMOTE_ADDR' }\|$ENV{'HTTP_USER_AGENT'}\|$envy\n";
close LOG;
exit(0);

Our script above simply writes the date/ time of the request, our visitor's IP address, our visitor's Browser type and a little nice bit of customizable information at the end of our log file. The customizable bit of information is passed on to the Perl script when our target pages invoke this script. If you want to see how you can retrieve the logged information from the server, check this complete script.
You will have to use JavaScript to add part of this customizable information as given below:

//adds the title of the Page and the referrer page
var title=document.title;
var ref=document.referrer;
document.write('< img WIDTH=1 HEIGHT=1 border=0 '+ 
 'src="http://www.bestnetcraft.com/cgi-bin/log.pl?"'+title+'-'+ref+ '>');


< noscript >
< img WIDTH=1 HEIGHT=1 border=0
src="http://www.bestnetcraft.com/cgi-bin/log.pl?Page_Title">
< /noscript >

The above script adds the page title and the referrer page to the log file. You can add other information too - Visitors' screen resolution, color depth and anything else that can be accessed by the client-side JavaScript.
Consider the following scenarios where our simple logging script can enhance the available information to the Webmaster :

Scene 1 : you have an HTML page describing in some detail about your leather executive briefcase. You have promoted this merchandise in several regional websites amongst others. You wish to know which website sends you the maximum number of potential customers.
Solution: Simply add the above script to the body of the page, which will track how your visitors reach this page.

Scene 2 : You have unleashed an email campaign in HTML format so that your carefully created images about your various products and assiduously crafted copy to convince your target audience to induce them to buy. You want to make sure of the following:
a. They opened the email page.
b. Your recipients view the pages as you have created.
Solution: Add the above snippet of code to the body of your email HTML message. Even if their client does not parse the JavaScript code, it can still access the "image" from the CGI script which does its job.

Scene 3 : You have a hosting account with no access to the raw server logs. Although these days most of the Hosting companies throw in access to the log files, there are some hosting accounts, which refuse access to the access_logs.
Solution: Add the snippet of code to all the pages you want to track and you have your own log files with up-to- the minute access to your log data.

Conclusion: Perl, the ubiquitous glue language of the Web, can simplify your daily chores, enhance the user experience and achieve all these and more with little development time. Our example above of about a few lines of pithy code can bring the visitors closer to you with little effort.


More Perl/CGI Resources | More of Bestnetcraft
Free CGI/Perl scripts,ebooks and customized Web services at down-to-earth prices.
©2000-2005. Bestnetcraft.com All rights reserved !