A bit over two years ago I posted a description of my solution to simplify and automate the inclusion of those small tracking snippets that allow to analyse visitor impact and behavior on your web sites: Piwik tracking & snippet inclusion. Approach and solution are not specific in any way to the open source software Piwik I personally chose for this purpose. The same applies to comparable offers, namely the ever present google analytics which does exactly the same as Piwik, except that you obviously hand over all your private data to the big octopus too 🙁

Back then I described how to setup and configure an http server to automatically inject the snippet at the bottom of all delivered html documents requested from that server, regardless of which actual site (domain or host) has been requested. Now, more than two years later I wanted to apply that solution to further documents, which turned out to be a small challenge…

Some month back I made an experiment to reimplement one of my own sites. That tiny site consists of a single page, more specific an HTML container document whose sole purpose is to embed a SVG graphic which contains the actual information published by that site. It always appeared odd to me that you had to rely on such a container document only to be able to adjust and control the display of the actual document within. Since http servers are very well capable of publishing all sorts of document formats I looked and finally found a solution to get rid of that container document altogether and directly publish the SVG document as the sites content, including the required scripting and styling.

However I forgot one thing: the Piwik tracking snippet!
Since it is automatically injected by my http server into all delivered html documents that new version of my SVG based site did not get tracked any more. I took me some typical trial and error phase to finally find an approach to extend my prevision html based solution to also get applied to other document formats. Actually the approach is quite simple, not as elegant as I hoped for, but it is solid and works. That is what counts, isn’t it?

So here is my extended version of the solution presented in the article mentioned towards the beginning of this post:

As before the solution consists of two components working together: the (modified) tracking snippet itself and the configuration of its usage by the http server. I created a second variant of the existing snippet which is suitable for SVG based documents:

<?php
define('piwikBase','https://christian-reiner.info/stats/');
define('piwikSite',apache_getenv('PIWIK_ID'));
if(is_numeric(piwikSite)){
?>

<script type="text/javascript"><![CDATA[
  var _paq = _paq || [];
  _paq.push(["trackPageView"]);
  _paq.push(["enableLinkTracking"]);
  _paq.push(["setTrackerUrl", "<?= piwikBase ?>piwik.php"]);
  _paq.push(["setSiteId", "<?= piwikSite ?>"]);
]]></script>
<script type="text/javascript"
  xmlns:xlink="http://www.w3.org/1999/xlink"
  xlink:href="<?= piwikBase ?>piwik.js"
  defer="defer" async="async"></script>

 <?php } else { ?>
   <!-- invalid piwik site id: <?php echo piwikSite;?> -->
 <?php } ?>

This means that indeed I now use two separate variants of the (modified) tracking snippet: one for HTML and one for SVG based documents. I assume the solution can be extended to further document types as well… This also implies that the http server now on the fly has to decide which of the two available variants has to be picked for injection into delivered documents. This again is done by slightly extending the original configuration as demonstrated in the original article. Here is that extended variant, a straight forward solution as you probably will agree:

# internal requests to include the piwik tracking code at the bottom of every html page

FilterDeclare PIWIK_token
FilterProvider PIWIK_token SUBSTITUTE resp=Content-Type $text/html
SUBSTITUTE 's|^\s*</body>|<!--#include virtual="/piwik-html"--></body>|iq'
FilterProvider PIWIK_token SUBSTITUTE resp=Content-Type $image/svg+xml
SUBSTITUTE 's|^\s*</svg>|<!--#include virtual="/piwik-svg"--></svg>|iq'

FilterDeclare PIWIK_code
FilterProvider PIWIK_code INCLUDES resp=Content-Type $text/html
FilterProvider PIWIK_code INCLUDES resp=Content-Type $image/svg+xml

FilterChain PIWIK_token PIWIK_code

# map virtual requests to the file system
Alias /piwik-html /srv/www/internal/piwik-html.php
Alias /piwik-svg /srv/www/internal/piwik-svg.php

Note that this configuration relies on some additional setup like environment variables and configuration options which I described in the original article: Piwik tracking & snippet inclusion.

Have fun!

Tags: , ,