Self-Hosted Site Analytics with Umami, Docker, and Traefik
/ 10 min read
why self-host analytics?
Website analytics fulfill a basic human need to feel seen and appreciated. It’s validating to know that there are people who visit your site, and it’s useful to know what they’re taking the time to read so you can focus on making your site more valuable to them.
Google Analytics is ubiquitous but invasive
Most content publishers just use Google Analytics to track website traffic. GA is free, easy to set up, and widely supported. Google makes it easy to integrate GA because they want your data. Using Google Analytics compromises your users’ privacy because Google can track them across the web, even if they’re not on your site.
GA uses cookies, which requires you to show a cookie consent banner to comply with laws in the EU, California, and elsewhere. Because it’s so invasive, Google Analytics is commonly blocked by ad-blockers and privacy-conscious browsers, making the data it collects less accurate.
I do want an occasional pat on the back, but I’d like to avoid being complicit in Google’s surveillance panopticon if at all possible.
self-hosting to the rescue
Fortunately, stroking your ego need not come at the price of selling out your users’ privacy. As an added benefit, your self-hosted analytic server is less likely to be blocked by privacy-conscious users, so your visitor counts are more likely to be accurate. If you have a server running Docker, you have a bevy of privacy-focused, open source analytics solutions to choose from.
In a previous post, I compared Umami, Plausible, and Matomo, three of the most popular self-hosted Google Analytics alternatives, deciding that Umami was the best choice for my needs. In this post I’ll walk you through the process of integrating an analytics solution, in my case Umami, into a self-hosted stack built with docker compose and fronted by a traefik reverse proxy.
prerequisites & overview
In this guide I assume that you:
- Have a domain name, with the ability to add DNS records.
- Have a server running Docker, docker compose, and Traefik, with a basic understanding of how to use them.
- Have one or more websites to track that you can add the Umami tracking script to.
There are five key configuration stages for setting up self-hosted analytics:
- Adding DNS records for your analytics endpoint.
- Configuring the docker compose stack that will run the Umami analytics service.
- Deploying the stack to your server and setting up the admin account in the Umami dashboard.
- Registering your site with Umami and getting the tracking code.
- Adding the analytics script to your website(s).
step 1: DNS configuration
You will need to add a DNS record for the subdomain or domain at which you plan to access your analytics endpoint and dashboard. Your analytics endpoint needs a public URL to receive data from the scripts that run in client browsers. Using a subdomain is common because it foregoes the expense of an additional domain. Moreover, ad blockers seem less likely to block API traffic when the endpoint is on the same root domain as your website.
If you only plan to use a single server, a wildcard DNS record that directs all subdomains to your server will work. Here’s some example documentation from namecheap on how to set up a wildcard subdomain DNS record. I won’t cover this in detail because specifics depend on your hosting and DNS providers (often the same).
step 2: docker compose stack
I used Umami’s example docker compose file as a starting point. I placed this docker-compose.yml
file in a new directory called umami-analytics
inside the git repository where I keep my docker compose stacks.
I opted to include environment variables directly in the docker compose file rather than in a separate .env
file since there are no API keys or other sensitive information that I would want to keep out of the repo. Comments in the docker compose file explain some of the choices I made.
step 3: deploy the stack
I use a bash script to deploy this stack, but the process essentially consists of SSHing into the server, navigating to the umami-analytics
directory, and running docker compose up -d
. I use git with SSH agent forwarding to allow me to remote into my machine and pull changes from the repo without keeping my GitHub private key on the server. See the section on deploying my astro blog for details and an example script.
step 4: register your site with Umami and get the tracking code
Follow these steps from the Umami documentation to add your site to your new analytics dashboard:
- Log in to the Umami dashboard (with your updated admin password).
- Click on
Settings
in the header. - Navigate to
Websites
on the left and click on theAdd Website
button. - Enter a name and domain for your site. The name doesn’t have to be the same as the domain, but it can be, or it can be something descriptive like
My Blog
. Domain should NOT include the protocol (http or https), only the domain name.
Once you’ve added your site, it’ll appear in the list of websites under Settings
. Click on the Edit
button next to your site and copy the Tracking code
to add to your site’s <head>
section.
step 5: add the analytics script to your website(s)
You’ll need to add the tracking code you copied in the previous step to the <head>
section of each page that you want to track. Details will vary depending on your site’s framework. In my Astro blog, I added the code to the BaseHead.astro
component, which generates the <head>
metadata for every page:
conclusion
Umami has a clean, privacy-focused design that doesn’t burden your users or server with heavy dependencies. By allowing you to rename the tracker script and the API endpoint, it helps evade ad blockers and ensures your visitor stats are more complete and accurate than other open source analytics solutions.
Umami’s support for Docker and docker compose makes it easy to integrate into a self-hosted stack. Traefik makes it easy to route traffic to the Umami container and automatically handles HTTPS and even compression. Integrating the tracking code into your site is as easy as adding a script tag to your root layout or page template.
Hopefully this post has shown you that the technical barriers to hosting your own analytics platform are low enough that you can do it yourself if you’re so inclined. You can own and process your own traffic data without relying on third parties to do it for you. You don’t have to settle for Google Analytics!