Piwik Analytics Integration

Note: this article is out of date, as Piwik has become Matomo

Piwik is an open source web analytics tool developed in PHP that can be deployed to a server or run from their cloud service.

I wanted to try this tool out as an alternative to Google analytics,  with Piwik giving you full control of all accumulated data and better  privacy options for visitors to your tracked sites.

To install Piwik, it is recommended to deploy it to the root of the webserver directory which is normally /var/www .

However with ForkCMS already occupying that location and with its  .htaccess preventing subdirectory access I could not simply use that  space. So I decided to emulate the way PHPMyAdmin is accessible through a  seperate directory using an alias.

e.g. www.foobar.com/phpmyadmin ---> /var/lib/phpmyadmin

To do this I needed to setup a config on Apache.

# analytics config file allows redirection of /analytics

Alias /analytics /var/analytics

Options FollowSymLinks

# Disallow web access to directories that don't need it  Require all granted

Once created you can enable this config by using this command in the shell.

a2enconf analytics

service apache2 restart

Creating a directory called analytics in /var , then it is just a case of downloading and installing Piwik from that folder.

Once Piwik has been configured you will be provided a tracking code to  use with that site. This Javascript which is around 7-8 lines should be  added to before the closing </body> tag.

With ForkCMS this was more tricky than I anticipated as it only allows  you to include Javascript in the header of the page which won't work. So  I had to include it manually in the theme I was using's Default.tpl  file.

This can be found at /var/www/src/Themes/<themename>/Core/Layout/

Then at the bottom of the Default.tpl file I added the following lines before the </body> tag.

<script src="http://loosebruce.co.uk/src/Frontend/Core/Js/piwik.js"></script>
 <noscript><p><img  src="http://loosebruce.co.uk/analytics/piwik.php?idsite=1"  style="border:0;" alt="" /></p></noscript>

After that, all you need to do is clear the site cache and the site tracking will start working.