Visitor Count

Distinguish robots from humans

by Read Write Tools
Abstract
This plugin is triggered each time a web browser requests your website's favicon.ico. Generally this happens the first time a visitor requests a page. Robots ignore this file, allowing this simple technique to serve as an easy way to count human visitors. Subsequent browser requests don't need to fetch this file again, so only unique new visitors are counted.

Motivation

Monitoring website traffic is important for planning and evaluation purposes. Is the website continuing to attract new visitors or is it declining in popularity? Has a recent campaign been successful in attracting new visitors? Is it time to contemplate adjustments or redesign?

Heavyweight solutions such as Google Analytics go far beyond this plugin's capabilities, but at the risk of ceding control to a third party, which may not be acceptable to your needs.

Customization

This plugin is open source and can be modified or enhanced to perform tasks such as these:

  • Counting requests to two or more pages for A/B testing.
  • Summarizing activity over different periods, such as hourly or weekly.
  • Monitoring visitor click-paths based on the HTTP referer header.

Download

The plugin module is available from NPM. Before proceeding, you should already have Node.js and RWSERVE configured and tested.

This module should be installed on your web server in a well-defined place, so that it can be discovered by RWSERVE. The standard place for public domain plugins is /srv/rwserve-plugins.

cd /srv/rwserve-plugins
npm install rwserve-visitor-count

Configuration is Everything

Make the software available by declaring it in the plugins section of your configuration file. For detailed instructions on how to do this, refer to the plugins documentation on the Read Write Tools HTTP/2 Server website.

TL;DR

plugins {
rwserve-visitor-count {
location `/srv/rwserve-plugins/node_modules/rwserve-visitor-count/dist/index.js`
config {
save-as `/var/log/rwserve/localhost.daily-counts.txt`
}
}
router {
`/favicon.ico` *methods=GET *plugin=rwserve-visitor-count
}
}

The save-as value is an absolute path to the file to be used to save the visitor counts. Be sure to enclose this value in GRAVE-ACCENTS. The directory and file will be created if they don't already exist. Both read and write permissions must be allowed for the system user "rwserve".

The router only sends requests to the plugin when the HTTP method is GET and the resource path is `/favicon.ico`.

Cookbook

A full configuration file with typical settings for a server running on localhost port 7443, is included in this NPM module at etc/visitor-count-config. To use this configuration file, adjust these variables if they don't match your server setup:

$PLUGIN-PATH='/srv/rwserve-plugins/node_modules/rwserve-visitor-count/dist/index.js'
$PRIVATE-KEY='/etc/pki/tls/private/localhost.key'
$CERTIFICATE='/etc/pki/tls/certs/localhost.crt'
$DOCUMENTS-PATH='/srv/rwserve/configuration-docs'
$SAVE-AS='/var/log/rwserve/localhost.daily-counts.txt'

Usage

Server

Start the server using the configuration file just prepared. Use Bash to start the server in the background, like this:

[user@host ~]# rwserve /srv/rwserve-plugins/node_modules/rwserve-visitor-count/etc/visitor-count-config &

Browser

Use your browser to navigate to the website's homepage, which will automatically trigger a request for favicon.ico:

https://localhost:7443/        

Shutdown and Save

Finally, shut down the server using the process ID of the server. The visitor count file will be saved during the shutdown process.

[user@host ~]# kill PID

Deployment

Once you've tested the plugin and are ready to go live, adjust your production web server's configuration in /etc/rwserve/rwserve.conf and restart it using systemd. . .

[user@host ~]# systemctl restart rwserve

. . . then monitor its request/response activity with journald.

[user@host ~]# journalctl -u rwserve -ef

Prerequisites

This is a plugin for the Read Write Tools HTTP/2 Server, which works on Linux platforms.

Software Minimum Version Most Recent Version
Ubuntu 16 Xenial Xerus 16 Xenial Xerus
Debian 9 Stretch 10 Buster
openSUSE openSUSE 15.1 openSUSE 15.1
Fedora Fedora 27 Fedora 32
CentOS CentOS 7.4 CentOS 8.1
RHEL RHEL 7.8 RHEL 8.2
RWSERVE RWSERVE 1.0.1 RWSERVE 1.0.47
Node.js Node.js 10.3 Node.js 12.17

Review

Lessons
This plugin demonstrates these concepts:
  • Basic plugin and configuration patterns.
  • How to use the shutdown() method to save state.
Find other plugins for the Read Write Tools HTTP/2 Server using npm with these keywords: rwserve, http2, plugins.

License

The rwserve-visitor-count plugin is licensed under the MIT License.

MIT License

Copyright © 2020 Read Write Tools.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Availability

Source code github
Package installation NPM
Documentation Read Write Hub

Visitor Count — Distinguish robots from humans

🔎