Quick and easy »piwik« deployment

It's a maexotic world ...


piwik 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."

This kept me thinking of a way to do it and the solution I have found is IMHO quite easy and elegant. Using the apache webserver module mod_ext_filter one can modify the HTML code on the fly, injecting the Piwik Javascript code snippet right before the closing </body> tag.

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>

The http:// or https:// part must be omitted in PIWIK_URL, as the Javascript snippet checks for the protocol used and adds the correct prefix itself.

That's all that is needed to have piwik enabled without modifying any HTML pages or CMS templates.




Comments

    • Posted byTetti
    • on
    Hallo Markus,

    erst einmal vielen Dank für den Tipp mit dem ext_filter-Modul; kannte ich noch nicht.

    Hier kurz ein paar Anmerkungen zu Deiner Anleitung.

    1.) Eventuell muss man das mod_ext_filter-Modul vor der Nutzung freigeben. Bei meiner SuSE-Distribution (11.2) muss man dazu die Datei "/etc/sysconfig/apache2" anpassen und den Apache2-Server neu starten.

    2.) In der Konfigurationszeile 'SetEnv PIWIK_URL "www.example.com/piwik"' fehlt am Ende ein'/'. Korrekt wäre 'SetEnv PIWIK_URL "www.example.com/piwik/"', sonst funktioniert das Javascript nicht. Es hängt an die URL noch den php-Dateinamen.

    3.) Das C-Programm addwipik.c schmiert bei mir mit einem Segmentation-Fault ab. Grund? In der Zeile 68 muss es statt "regmatch_t match[0];" vermutlich regmatch_t match[10]; heißen.
    Zumindest für ein Element sollte Platz auf dem Stack sein.

    Gruß aus Solingen
    Reply
    • Posted byAmos
    • on
    Hallo Markus,
    sehr interessanter Beitrag und eine bequeme Möglichkeit, den Piwik-Code in Webseiten zu integrieren. Du sprichst oben davon, dass Deine Methode die Auslieferungszeit der html-Seite verzögert. Die Zeiten von 15 mal länger auf 3 mal länger zu reduzieren mag ein guter Schritt sein aber dennoch erscheint mir die Abarbeitungszeit von 3 mal Länger immer noch ziemlich groß. Lässt sich das noch optimieren?
    Reply