Self-hosted Web Analytics - Matomo

Posted on April 13, 2019

For the last couple of years I have been trying to reduce my dependency on Google's services, one of the harder services I found to replace was Google Analytics for this website. To be fair to Google Analytics it is very handy and easy to setup (like most of Google's services). Once you have it all working you don't really have to worry about it and you can just leave it there working away. There are also a lot of advanced features available but I never really felt the need for them since this is just a basic blog. All I was ever interested in was the amount of hits and where in the world they were coming from.

I had a couple of requirements for a self-hosted analytics solution. Firstly it would have to be fairly lightweight as it would have to share resources with this web server which runs on a small t2.micro AWS instance with one vCPU core and 1GB RAM. This blog runs in a simple docker container with Nginx. docker-compose is used for some basic orchestration so an analytics solution with a docker image available would be ideal. An open source solution would be preferred too.

Matomo was the solution that I landed on which fulfilled everything I needed. I had to setup a reverse proxy in order to deploy Matomo to the same AWS instance as the web server. Traefik is the reverse proxy I went with. As a bonus I was able to configure automated SSL certs from LetsEncrypt to secure this page over HTTPS. Matomo also requires a MariaDB instance. Luckily Bitnami have docker images available for Matomo and MariaDB. In order to deploy these all I had to do was add the following and run docker-compose up:

            image: 'bitnami/mariadb:latest'
              - default
              - MARIADB_USER=xxxxxx
              - MARIADB_DATABASE=xxxxxx
              - 'mariadb_data:/bitnami'
            image: 'bitnami/matomo:latest'
              - web
              - default
              - MARIADB_HOST=xxxxxx
              - MARIADB_PORT_NUMBER=xxxxxx
              - MATOMO_DATABASE_USER=xxxxxx
              - MATOMO_DATABASE_NAME=xxxxxx
              - MATOMO_USERNAME=xxxxxx
              - MATOMO_PASSWORD=xxxxxx
              - ""
              - "traefik.enable=true"
              - ""
              - "traefik.port=80"
              - "traefik.basic.protocol=http"
              - mariadb
              - 'matomo_data:/bitnami'

Once the frontend for Matomo becomes available, you are provided with some JavaScript that you have to add to whichever website you want to track. If you do a quick Ctrl+u on this page, you'll be able to see the Matomo JavaScript snippet that I had to add. Once this is added, you should start seeing the hits coming in. Matomo has loads of features too that I haven't even had the chance to check out. To be honest the main dashboard has more than enough information for me. All I am really interested is how many hits are there and what countries they are coming from.

Another solution I came across was called Fathom although that did not seem as mature or feature rich as Matomo.

If you are running Google Analytics to track any of your personal sites, I definitely recommend giving Matomo a go! A couple of caveats though, obviously there is a bit more overhead in managing Matomo when compared to Google Analytics & there is also some potential of downtime in the early stages until you have it setup just as you like it.

Brian Carey | DevOps Engineer