May 22, 2008

Plotting your Web site visitors on a map

Author: Ben Martin

ApacheMap plots the location of each Web hit your site receives on a Google Map. The utility converts IP addresses from Apache logs into longitude and latitude information using hostip.info.

HostIP relies on users submitting information about what geographic location should be associated with an IP address. This can present problems, because some IP addresses cannot be resolved into geographic information. In fact, the IP address I was using when I visited hostip.info was not recognized, but sites such as geoiptool.com offered a reasonable guess as to my location based purely on my IP address. Regardless, the results of a geographic lookup from an IP address should always be taken with a grain of salt.

ApacheMap is not packaged for Ubuntu, openSUSE, or Fedora. For this article I'll install and use version 0.5b on a 64-bit Fedora 8 machine. Be warned that the apache-geo-map-0-5b.tar.gz tarball does not extract itself into a subdirectory. The apache-geo-map.pl script from apache-geo-map-0-5b.tar.gz relies on the perl-libwww-perl and perl-YAML packages, which should be installed. If perl-libwww-perl is not installed, you will get an error reporting a missing HTTP/Request/Common.pm file when you attempt to execute apache-geo-map.pl. If perl-YAML is not installed, you will see a message complaining about a missing YAML.pm file.

To install the software, as shown below, I extract the tarball into a new subdirectory in the Web document root, typically /var/www/html. I use the included Perl script to create a data file, data.xml, from the IP addresses in an Apache log file. I intend to execute the script and update this data file as my regular user, so I give permission to execute the script to myself and also allow write access to the data.xml file for my user. I move the configuration file into /etc with a descriptive name, and move the remaining miscellaneous files into /usr/local/share.

# cd /var/www/html
# mkdir apache-geo-map
# cd apache-geo-map
# tar xzvf /.../apache-geo-map-0-5b.tar.gz
# chown -R root.apache .
# mv apache-geo-map.pl /usr/local/sbin/
# chown ben.apache apache-geo-map.pl
# mv ./apache-geo-map.pl /usr/local/bin
# mv default.conf /etc/apache-geo-map.conf
# mkdir /usr/local/share/apache-geo-map
# mv README CHANGELOG gpl.txt /usr/local/share/apache-geo-map/
# chown ben.apache data.xml
# chown ben .

The /etc/apache-geo-map.conf configuration file is a simple list of key-value pairs, each specified on its own line. The default settings should work out of the box on a Debian system; presumably ApacheMap's author uses Debian and has set defaults to work for that distribution. For other Linux distributions you might have to change some paths in the configuration file. When in doubt, explicit configuration is always best. Note that the three dashes you see in the listing below are part of the configuration file and should be left intact. The slash at the end of data_dir is required for ApacheMap to concatenate data_dir and data_file properly.

# vi /etc/apache-geo-map.conf
---
apache_log: /var/log/httpd/access_log
data_dir: /var/www/html/apache-geo-map/
data_file: data.xml
default_icon_color: blue
new_icon_color: yellow

The configuration shown above assumes that the user ben has read access to the Apache log files in /var/log/httpd. The command shown below will update the data.xml file with the location of all the IP addresses from your current Apache log file. Note that apache-geo-map.pl does not work with any HTTP proxy servers; you must be able to directly fetch information from the Web for it to work.

apache-geo-map.pl -c /etc/apache-geo-map.conf

To test out ApacheMap and generate the screenshot, I used the below segment of a log file.

66.35.250.151 - - [12/May/2008:11:49:46 +1000] "GET / HTTP/1.1" 403 3918 "-" "Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.8.1.8) Gecko/20071030 Fedora/..."
18.7.22.83 - - [12/May/2008:11:49:46 +1000] "GET / HTTP/1.1" 403 3918 "-" "Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.8.1.8) Gecko/20071030 Fedora/..."

135384-1-thumb.png

I found that I could view the map by loading http://localhost/apache-geo-map/apachemap.html into Firefox.

One final step to setting up ApacheMap is to obtain a Google Maps API Key and update the src attribute at the top of apachemap.html replacing key=<your-api-key> with the API key you obtain. Setting up an API key allows you to view your ApacheMap installation from a machine other than localhost.

Wrap up

ApacheMap lets you quickly get an impression of where the viewers of your Web site are located. The value of this geographic information depends on how niche your Web site content is. If your Web site has a high hit count and is interesting to a general demographic range, then the map will likely show markers all over the place and provide little useful information. On the other hand, if your Web site targets a specific niche, being able to view a map with hit density may allow you to perform some targeted marketing to reach more users who are interested in your content.

Category:

  • Internet & WWW
Click Here!