Tracking popular posts
Let's implement a self-hosted web analytics system to track my popular posts
HEY! This is a DRAFT!
You sneaky little guy, how did you find this?
Well now that you're here... you can read whatevers here if you want, just don't share it around please.
I’ve been reading a lot of blogs lately and have noticed a bunch of my favourite sites have a “popular posts” section on their home page. All the work around this post was building up to this
Before we break things down step-by-step, let’s check out a little architecture diagram to get our bearings:
Choosing an analytics service
To have any sort of insight into what’s “popular”, you’re going to need some sort of view tracking on your site aka “web analytics”. You’re also going to need an API or some sort of interface to programmatically request those post stats.
While I’ve had analytics systems in the past, I’ve always found a functional or ethical deal breaker that’s kept me away from them in the past.
Let’s quickly go over the services I’ve used, and the issues I’ve had with them:
- Google Analytics felt like a firehose of information and I didn’t love that I was subjecting visitors to google’s ad-tech pipeline.
- Fathom was a lot closer to what I wanted. It provided just the basics data-wise, and was cookieless but didn’t have an API or an easy way to export data (both features have been added since I left).
- Umami felt like fathom but with all the features I wanted. Still, when I hosted it on Railway, I had many issues upgrading. Specifically with database migrations that wiped the database 🙃
It’s been around 6 months since I’ve had any sort of analytics on my site and since Umami was functionally exactly what I wanted, I figured I’d give that another shot. This time, I was going to host it on a server in my house, MoonTurtle.
Setting up Umami Analytics
Thanks to the operating system on the MoonTurtle server, Unraid, setting up Umami was relatively simple. All I needed to do was set up the docker image for Umami, create a database on my MySQL DB and finally update some config in my NGINX proxy manager application…
Creating the Umami database
Before we can set up an umami instance we need to create a database and database user for the umami application to use. Just log in to your MySQL DB of choice and…
-- create a new database CREATE DATABASE umami; -- create a new user + password CREATE USER 'umami_admin'@'*' IDENTIFIED WITH mysql_native_password BY '<some-password-goes-here>'; -- grant the new user all privileges on the umami database GRANT ALL PRIVILEGES ON umami.* TO 'umami_admin'@'*' WITH GRANT OPTION;
Since I already have a MySQL database setup, I just used that. If you need a database, you can spin one up on Railway with a few clicks, or set up a docker container.
I typically use TablePlus to connect to my home lab DB’s, check them out.
Creating the Umami docker image
Now that we have a database and database user, we have all the credentials required to set up the Umami docker container on our Unraid server:
- Login to the Unraid Dashboard
- Click the “Docker” tab
- Click the “Add Container” button below the list of your current containers
- Name the repository “umami” or something similar
- Use the following link to their docker repository
- Create some environment variables for the container
DATABASE_URL: a MySQL connection string for the DB
DATABASE_TYPE: this should just be
HASH_SALT: the salt used for passwords in the system
- Create a port mapping to expose the web port outside the container
- this should map the internal port 3000 -> an external port of your choosing
- Fill out all your environment variables
- Click save and watch the magic happen
After the docker image has been pulled down from the container registry and the application has (hopefully) started up, we should be able to navigate to your umami instance at
http://<your-unraid-servers-ip>:<port from step 7>.
The default username and password is admin/umami. Once you’re in I would recommend changing that ASAP.
Now that the app is up and running, let’s expose it to the rest of the world. Since I use NGINX Proxy Manager (which is just a web GUI on top of NGINX) adding a new site is pretty dang easy.
See their docs for more info on how to use NGINX proxy manager to add a site. In my case, all I had to do was put in the subdomain I wanted to use (stats.tbx.sh) and select the IP and port of the app reachable from my network.
I could have used any reverse proxy for this (Caddy, Apache, etc.), NGINX is just what I’m familiar with.
Adding Umami to the site
Adding umami to your site is the last step in actually collecting analytics.
Working with the Umami API
Creating a serverless function
Building the “popular posts” view
Keeping the site up to date
Why not make it dynamic
Netlify build hooks
Setting up a job to rebuild the site
Things to improve
Static + dynamic
Integration in more places
I'm Mykal Machon, I'm a web developer / coffee nerd from British Columbia, Canada. I'm currently working at the University of the Fraser Valley as a Systems Analyst.
Thanks for reading through this post! if you liked it, you can:
This post was liked by 0 people
Responses on this site are powered by Webmentions!