Matomo Analytics with WagtailCMS
March 31, 2021
Matomo is an open source alternative to Google Analytics that focuses on data ownership and privacy. Both a Cloud and an On-Premise solution exist. I opted for deploying Matomo to my own VPS. HowToForge has a great guide to help you deploy Matomo to your own server. https://www.howtoforge.com/how-to-install-matomo-piwik-on-centos-8/
WagtailCMS is an Open Source Content Management System built using Python and Django. This guide assumes that you have a website already built using Wagtail and you’re either ready to deploy or have already deployed into production.
Access to the source code of your Wagtail site that is either in production or ready to be deployed to production.
A Matomo account either in the Cloud or on your own On-Premise server.
Start by opening up the base.html page for your app. I like to separate my base.html files by apps instead of using one base.html for the whole project. In my case I have blog_base.html stored in
<script>tag pointing to an analytics.js file. You can name this file whatever you want, analytics.js was the first thing to pop into my mind.
Next we create the analytics.js file that we’ll use to import the Matomo JS code. Since I can potentially use this in other apps I created the file in the same folder that stores your settings folder.
If you’re using source control like Git, commit this file to your repository blank or with a comment indicating what the file is for.
Adding a new website to Matomo
Log into your Matomo website and navigate to the “All Websites” tab in the top navigation bar.
At the bottom of the list click on the “Add a new website” button.
Assuming you’re using this on a website, click on the “Website” button to answer the “What would you like to measure?” dialog.
Fill out the form for Managing Measurables and click Save at the bottom of the page.
In your newly created website, find the link for “View Tracking Code”.
- On your production server, paste the code into the analytics.js file that was created in the Configuring Wagtail section.
Restart your HTTP server, in my case both Gunicorn and Nginx. Make sure that tracking is allowed in your browser for your website. You should see hits in Matomo when your site is visited.
Return to blog index