Mastering the Web
Contents
Website Planning Tutorial
Website Design Tutorial
HTML Tutorial
HTML Tables Tutorial
CGI Tutorial
JavaScript Tutorial
Perl Tutorials
CSS Tutorial
Installing a Web Server
Security Tutorial
HTML Cookies Tutorial
Web Tracking Tutorial
Download Free Programs
F.A.Q.

  1. Web Tracking Concepts
  2. Web Tracking Implementation

Download FWTLogstat1

Download FWTLogstat2

Web Tracking Implementation

Web tracking, as explained before, means to produce records allowing you, the owner of a site, to get the information you need to monitor the functioning of your site, for example, how many visits received each page and where did they came from. These records are called "logs", and they may be produced by the Web server or by scripts that you made. So, "tracking" is the action of producing logs to later analyze them using programs like FWTLogstat1 and FWTLogstat2.

I provide here the scripts needed and explain their way of use, but first I will talk about the information you will get using the scripts and the program FWTLogstat1. There are three principal types of information you can get from the logs obtained using this material: the number of hits for each page, the number of site visitors, and the identity of the referrers. A page hit is the viewing of a page. When we talk of pages, we are referring to each of the Web pages of your site. You can think of the referrer as the "place" where the user came from. That place is a place in the Web, that is, another page, distinguished by an Internet address or URL, that has a link to your page. Sometimes that place does not exist, for example, when the person directly enters the address of your page in his browser. Those cases are known as "direct hits".

To record the log of your site's activity, you will need a piece of JavaScript code inserted in each page, a file of JavaScript routines, and a Perl script installed in your Web server. The Perl script does the actual writing of the log file that will contain the information for each hit. It is a CGI script; CGI scripts are explained in the "CGI Tutorial", but you need no knowledge of what CGI is because I have already made it for you.

Certain requisites are needed to use this script. The computer where your pages are stored (the Web server or host) must be running under some version of the Unix operating system. You must have permission to install executable files (scripts) in this machine. Finally, the Perl interpreter must be installed in the host. These requisites are met by any paid Unix host. In the case that you have not the necessary permissions, you can ask the support staff that they install the script for you. The script uses a Perl module, CGI.pm, which provides a number of useful functions. This module must be available at the host, but, as it is very common, this will not usually be an issue.

For the script to be called automatically each time a page is displayed so that it records the data, it will be used to display an image. The image displayed will be always the same, an "invisible" one. The tracking will be produced as a side-effect of displaying the image. The way to obtain an "invisible" image is to display an image so small that it cannot be seen. In fact, it will be only one pixel high and one pixel wide. As the file of such an image in the popular GIF format is also very small, we will not even have the need to send this file. We will use a "synthetic" image by sending the browser the bytes that would make up the GIF file.

The script will perform for each hit the logging of the visitor, the date, the time, the page, and the referrer. To supply the Perl script with the information it will write into a file, you will need a little of JavaScript. JavaScript will be used to give each unique visitor a cookie so that he is identified by the Web site in each visit. It will also provide the name of the page and the address of the referrer. The cookie will be read by the Perl script and used to get the user identifier. Some additional JavaScript routines will be needed that must be placed in the file "script.js".

JavaScript routines

var expDays = 7; // number of days the cookie should last

function setCookie(value, expire) {
  document.cookie = "userid=" + escape(value)
  + ((expire == null) ? "" : ("; expires="
  + expire.toGMTString()))
}

function getCookie() {
  var search = "userid="
  if (document.cookie.length > 0) {
    offset = document.cookie.indexOf(search)
    if (offset != -1) {
      offset += search.length
end = document.cookie.indexOf(";", offset)
if (end == -1) end = document.cookie.length
      return unescape(document.cookie.substring(offset, end))
    }
  }
}

function register(userid) {
var expires = new Date();
var today   = expires.getTime();
expires.setTime(today +  (24 * 60 * 60 * 1000 * expDays));
setCookie(userid,  expires);
}

Save the "script.js" file in the same directory where you stored your pages. In each of your pages include the following line in the HEAD section.

<SCRIPT language="JavaScript" src="script.js"></SCRIPT>

Just after the BODY tag in each of your pages, include the following:

<SCRIPT LANGUAGE="JavaScript">
  var userid;
  userid = getCookie();
  if (userid == null) {
    userid = (Math.floor(Math.random() * 100000)).toString();
    register(userid);
  }
  document.write("<IMG src=\"cgi-bin/trax.pl?url=" +
  document.URL + "&ref=");
  document.write(document.referrer + 
  "\" width=1 height=1 border=0>");
</SCRIPT>

Enabling page tracking

You will need to upload to the "cgi-bin" sub-directory of your site two files: a Perl script, "trax.pl", and a text file, "trax-log.txt".

By clicking the next link, you will open a new window that will display the Perl script. Right click this window and choose "Select all". Copy and paste into a text editor that lets you save the script as a Unix file with the name "trax.pl".

Download the Perl script

"Trax-log.txt" is a UNIX empty text file that will contain the log. You can create it with a text editor by opening a new document and, without writing anything, saving it in Unix format with the name "trax-log.txt".

If you do not want to bother yourself creating these files, you can download them and the program FWTLogstat1 by clicking the next link.

Download FWTLogstat1 installation package

Upload both files to the sub-directory "cgi-bin" in the Web server by the same means that you use to upload your pages. The "cgi-bin" directory needs to be just under your root directory, where I will assume that you have stored your HTML pages.

Once the files are in "cgi-bin" there is nothing else to do but to check that they have the right permissions. Depending on the type of host, you may have to do something or not. Some hosts assume that everything that is in "cgi- bin", or that has a "pl" or "cgi" ending, is executable. If this is not the case, you must give "trax.pl" the attribute "rwxr- xr-x", which expressed in the octal numbering system is "755". The file "trax-log.txt" must have the attribute "rw- rw-rw" or "666".

When all this is done you can start your browser, and in the address bar type the following, assuming that your domain is "www.yourdomain.com":

www.yourdomain.com/cgi-bin/trax.pl

If everything is right, you will obtain the response:

Trax.pl - ready to work.

Now you may begin to visit some or all the pages of you site. After a while, take a look at the file "trax-log.txt" in the "cgi- bin" directory. You will see something like the following lines. I have used the "$" character to mark a wrapped lined.

50879,30-04-05,22:23:23, http://www.yourdomain.com/page00.html,$ http://www.yourdomain.com/page10.html 50879,30-04-05,22:23:26, http://www.yourdomain.com/page10.html,$ http://www.yourdomain.com/page20.html 50879,30-04-05,22:23:30, http://www.yourdomain.com/page20.html,$ http://www.yourdomain.com/page30.html 50879,30-04-05,22:23:34, http://www.yourdomain.com/page30.html,$ http://www.yourdomain.com/page40.html 50879,30-04-05,22:23:37, http://www.yourdomain.com/page40.html,$ http://www.yourdomain.com/page50.html

The first number in the line is the visitor identifier, and next follows the date, the time, the referrer page, and the page you visited. After some time of activity, you can download the file to your computer, and analyze it using FWTLogstat1. The directions for installing and using FWTLogstat1 are in the Free Programs section.

If you have any difficulty in following these steps, please let me know using the feedback page.

Previous | Contents | Next

| HOME | FEEDBACK | BOOKMARK |
Build your Website
© 1999-2008 Hector Castro -- All rights reserved

If your doubt is not answered in this site, please use the
contact form .
I'll answer as soon as posible.
I can help you using instant messaging. To schedule a meeting, please use the
meeting form.
You will find the late news about the free programs offered here on my blog
Free Webmaster Tools
You can get news about updates to my free programs through this
RSS feed.

www.great-web-info.com