tracking site visitors

There might be a delay between the first visit and the desired action (subscirbing to RSS, purchasing software, etc). An usual web site log often isn't enough to match the former and the latter. The webmaster needs cookies.

Marking the visitors

For experienced system administrator, setting up cookies isn't hard. At the moment, I use "experienced" as "with the secret knowledge how to make things simple".

Let Apache tracks the visitors using its native functionality. Therefore, there is no need to modify all the existing scripts and to write new.

First, the module "mod_usertrack" should be activated:


LoadModule usertrack_module   modules/mod_usertrack.so
AddModule mod_usertrack.c

Second, the module should be configured:


<IfModule mod_usertrack.c>
CookieExpires "365 days"
CookieTracking On
</IfModule>

Logging the marks

The format of Apache logs isn't fixed, one can configure it. (Is it surpise?) Let's take the format named "combined" and derive "combined-cookie":


LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User
-Agent}i\" \"%{Cookie}i\" \"%{Set-Cookie}o\"" combined-cookie

The explanation of these funny symbol is given in the documentation: "Module mod_log_config". Comparing to "combined", I've added two fields:

\"%{Cookie}i\" -- all the cookies sent from the browser to the server.
\"%{Set-Cookie}o\" -- the cookies which the server sets in the browser.

And we shouldn't forget to use the newly defined format:

#CustomLog /var/log/httpd/access_log combined
CustomLog /var/log/httpd/access_log combined-cookie

All together

Let's see how the setup works. Go to the site using lynx. You'll see that the server sets the cookie "Apache":


X.Y.Z.U - - [13/Aug/2007:04:52:51 +0000] "GET / HTTP/1.0" 2
008518 "-" "Lynx/2.8.6rel.2 libwww-FM/2.14 SSL-MM/1.4.1 Ope
nSSL/0.9.8d" "-" "Apache=91.6.30.36.1186980771868349; path=
/; expires=Tue, 12-Aug-08 04:52:51 GMT"

Accept the cookie in lynx, and reload the page. Now the log entry contains:


X.Y.Z.U - - [13/Aug/2007:04:54:39 +0000] "GET / HTTP/1.0" 2
00 8518 "http://uucode.com/" "Lynx/2.8.6rel.2 libwww-FM/2.1
4 SSL-MM/1.4.1 OpenSSL/0.9.8d" "Apache=91.6.30.36.118698077
1868349" "-"

Lynx sends the cookie "Apache". The server doesn't set new cookies.

Categories: blogging

Updated: