A couple of days ago a friend on mine wanted to experiment with Piwik, an open source real time web analytics software that aims to be like Google Analytics and whose use is considered appropriate to German dataprotection laws. However his Internet site consists of tons of static HTML pages that he didn't want to modify, just to test piwik. So he said:
"I wish I could configure the server to handle it."
As a proof of concept I wrote addpiwik. The first version was a little python script addpiwik.py that did the trick. However it took like 15 times longer to deliver a static HTML page (python interpreter has to be loaded every time) so I decided to also go for a C language version addpiwik.c, which is much faster, as it only takes about 3 times longer for a page to be delivered (7.0 ms vs. 2.6 ms for a 100 KB sized page). I was a bit lazy for the C version and used the GNU getline() function, which should be fine for most current Linux and FreeBSD releases.
The program queries the environment for PIWIK_URL and PIWIK_IDSITE for maximum flexibility.
The apache setup is quite easy. First you have to define a filter:
ExtFilterDefine add-piwik mode=output cmd="/path/to/addpiwik" intype=text/html \ outtype=text/html enableenv=PIWIK_IDSITE
Please note that there are no quotes allowed for the parameters, except for cmd.
Now we have an output filter called add-piwik that is only active for data with a content type of text/html (so it won't be used for eg. images) and the environment variable PIWIK_IDSITE has to be set for the filter to be active. This even works if the content was generated by a PHP or some other kind of script.
Now activate the filter for a directory:
<Directory /var/www/html/piwiktest/> SetEnv PIWIK_URL "www.example.com/piwik" SetEnv PIWIK_IDSITE "1" SetOutputFilter add-piwik </Directory>
That's all that is needed to have piwik enabled without modifying any HTML pages or CMS templates.