Saturday, December 22, 2012

Site tracking with Piwik

I used Piwik in a previous Web Community project. So when I was asked how to add web statistics to Alfresco, my first idea was to integrate it with Piwik. So let's start to try it out:

At first a Piwik installation is required. My target test system is an OpenSuse 12.1 and so all the dependencies are available from the software repository. They are further described here: .

So after you installed an Apache, all required PHP modules and a MySQL database (BTW: This is something which I do not like regarding Piwik. The only supported database is MySQL. I would like to see at least Postgres support. ) you can begin to install your Piwik instance. Therefore you can follow the following instructions: .

The most interesting part may be the database setup:

mysql> CREATE USER 'piwik'@'localhost' IDENTIFIED BY '${Your pwd here}';
mysql> CREATE DATABASE piwikdb;
mysql> GRANT ALL PRIVILEGES ON piwikdb.* TO 'piwik'@'localhost' WITH GRANT OPTION;

Then unzip the latest Piwik zip to your Apache2 web server and open the URL 'http://localhost/piwik'. For OpenSuse the htdocs folder is located at '/srv/www'.

If calling Piwik the first time it will prompt you to perform the following file permission changes:

chown -R www-data:www-data /srv/www/htdocs/piwik
chmod -R 0777 /srv/www/htdocs/piwik/tmp
chmod -R 0777 /srv/www/htdocs/piwik/tmp/templates_c/
chmod -R 0777 /srv/www/htdocs/piwik/tmp/cache/
chmod -R 0777 /srv/www/htdocs/piwik/tmp/assets/
chmod -R 0777 /srv/www/htdocs/piwik/tmp/tcpdf/

It's maybe required to create the user, group or directories above. You should also enable a temp. write access to '/srv/www/htdocs/piwik/config/'.

An error occurs that your php installation need zlib-suppot. You can find 'php5-zlib' in the OpenSuse software repository. Do not forget to restart Apache after installing this extension.

The next steps are quite easy. Just follow the Installation Wizard by entering your database connection details.

Also part of the installation is the generation of the tracking code. The installer says: "Here is the JavaScript Tracking code to include on all your pages, just before the </body> tag"

<!-- Piwik --> 

<script type="text/javascript">

var pkBaseURL = (("https:" == document.location.protocol) ? "https://localhost/piwik/" : "http://localhost/piwik/");

document.write(unescape("%3Cscript src='" + pkBaseURL + "piwik.js' type='text/javascript'%3E%3C/script%3E"));

</script><script type="text/javascript">

try {

var piwikTracker = Piwik.getTracker(pkBaseURL + "piwik.php", 1);



} catch( err ) {}

src="http://localhost/piwik/piwik.php?idsite=1" style="border:0" alt="" 

<!-- End Piwik Tracking Code -->

Important is the 'idsite' property. The value '1' is the id of the site which I configured as 'http://localhost:8080/share'. OK, Piwik is now up and running. Now let's investigate how to customize Alfresco in order to use it.

So to enable global tracking we can search a header or footer element which is used by every page of Alfresco. So let's check which site web scripts are available and let's see if we can put our snippet to one of the freemarker templates.

One good candidate seems to be '${WEBAPPS}/share/WEB-INF/classes/alfresco/site-webscripts/org/alfresco/components/header'.  So I just placed the script above after the first other '<script>' block.

A test showed the following result:

Piwik can do a lot more. But for now this shows exactly what we required. It answers the question which site was accessed how often.


  1. Hello David, I tried this (instead of header I choose footer to put the JS code), but there is a problem : when I navigate into the repository, the footer (or the header) is not refreshed (because repository is refreshed with AJAX), so Piwik can't see downloads or navigation.

    Did you find another solution ?

    Sorry for my bad English, I'm French.

    Best regards, Rémi.

    1. Yes, you are right. I just used it to find out which Share Site was visited how often. It does not count the visits of the folders within the Document Library as explained by you. As far as I know, the Document Library consists several components. Each of this "component" should be realized by using SURF (data web scripts + presentation web scripts). So usually such a web script uses a freemarker template. One idea could be to find a suitable presentation freemarker template and then add the tracking code to it.