A page on my website has become surprisingly popular recently, and I wanted a dead simple way of tracking hits. I thought I'd share what I came up with in case it's useful to anyone else. My requirements were:

  • Track hits on one page only
  • Be transparent to the visitor. That is, no annoying javascript or a readout of "you are the nth visitor" on the page itself. I pride myself in the simplicity of my web design.
  • Handle the numbers of hits I'm getting at the moment (hundreds or low thousands per day)
  • Ideally, track when the page was visited, so I can do some stats on most popular times of day, and change in popularity over time.

Here's my solution. I simply moved index.html to main.html and created an index.cgi:

date +%s >> logfile
echo "Content-Type: text/html"
cat main.html

Simplicity itself.

I also created a hitcount.cgi so I can check just by visiting the page how many hits it's had:

echo "Content-type: text/plain"
wc -l logfile | sed -e 's/ logfile//g'

I will post some MATLAB analysis later on, once I've collected enough data.

In terms of limitations, a friend suggested to me that if two people visit at the same time, the file might end up getting clobbered and having lines mixed up. But at the current rate, two people visiting at once is unlikely; and in any case I can deal with having one anomalous date in my log if this does happen.