As someone who is both security conscious and curious as to how many people visit these articles I write, I've been interested in adding some analytics tracking to my site. I've got Google Analytics working pretty well, but like all home-labbers with a healthy weariness of Google deciding to charge for previously paid for services, I'm trying to remove any dependence on them. Given that they recently announced they'll be sunsetting Universal GA in 2023, this seemed like the perfect time to look for something I can control.
Enter Plausible. This is a fully self-hosted service which uses your domain to provide tracking analytics for your sites, either as full domains or sub-domains. It provides information such as the entry, exit and top pages visited, device info regarding browser and OS, and can also show geographic location of visitors if you enable the geo-ip functionality (note that I won't be including that version below). It puts all this together in a nice, easy-to-view gui.
Let's set it up.
- You need to have Docker and docker-compose installed on your machine
- You need to be able to SSH into your machine, or use CLI/terminal
- An ability to use the
gitcommand, as we'll need to clone the repo
- You should have your own website already up and running, and you are able to access the
<head>(header) for your site, either in the site itself or through your reverse proxy
- You should have your own domain name with a fully functioning reverse proxy (check out my SWAG article, and I recommend Cloudflare as an incredibly feature-packed, not to mention free CDN)
proxydocker network (either called proxy or something else, this is the network you use for your reverse proxy/SWAG container to communicate with the services it acts as RP for)
- SMTP mail credentials, either through your own or hosted SMTP server, or through something like Gmail which supports it (I use a service from https://eforw.com which provides a single domain with unlimited aliases and accounts for $10 per year)
Setting up the file system
As we're going to clone the repo, this will be pretty straightforward.
- In your CLI/terminal, navigate to your
- Create your
plausiblefolder by typing
mkdir plausibleand hitting
Enter, then open that folder by typing
- Clone the repo by inputting the following command:
This will download the repo into your current folder, which should be plausible.
If you now type
ls you'll notice that you have one folder called
hosting. It's up to you if you keep that, I personally don't like it so I simply cut and pasted everything from
hosting directly into the
plausible folder. If you want to do that then go ahead, otherwise just
cd hosting to get yourself into the right place.
Prepping the container files
We're going to set up two files now which will not only spin up our containers, but will also create some volumes and the dedicate plausible docker network.
If you look through the repo you downloaded, you'll notice a few folders, and a few files. We're interested in
First, open up the
docker-compose.yml in a text editor, using something like
sudo vim docker-compose.yml (or replace
nano if you prefer that instead). You'll notice it's a pre-populated file, however we want to make some changes. So copy paste the following.
Save the file.
The above does a few things. It creates 4 containers required to run Plausible, it creates 2 volumes, it creates one network (plausible) and references another (proxy), it assigns the
plausible-conf.env file as the
env_file for the app (more on that in a moment) and it specifically allows for registration.
Next, open and edit the
plausible-conf.env file. Again it's prepopulated, delete that and copy in the following:
To generate the
SECRET_KEY_BASE simply input the following into your CLI/terminal, and copy the output which should be a long randomized string of characters:
Once you've completed that, save the file. You can check out additional configuration options here if you want, but I won't be covering them as they don't suit the purposes of this walkthrough.
We're now ready to create the containers.
Creating the containers
docker-compose -p "plausible" up -d and hit
Enter. This will create a stack called 'plausible' with the above containers. If you use Portainer to view/manage your containers, then your stack should look something like this:
Click through the logs to check that everything's running, and if so then you should be able to access your instance at http://localhost:8000.
- Create a new account by signing up with an email and password
- Log out
- Now go back to your
docker-compose.ymlfile and locate the variablein the
plausibleservice block which reads
- Change this variable to
trueto prevent additional user registration
- Now recreate the container by typing
You need to set up your CDN and reverse proxy NOW before going any further
The reason for this is that when you set up the sites you want to track inside Plausible, it gives you a code snippet to insert into the header of said site(s). If you do that while accessing it via localhost, then the snippet will give you an URL which uses your machine's IP address. This won't work, and so we need to access it via your domain.
Assuming you've set it up correctly, you should now be able to access your Plausible instance through https://plausible.yourdomain.com or something like it. Go ahead and do that, then log in.
You should then be able to click
+ Add a website as per the below:
On the following page, add in the URL of the site or page you'd like to track, the timezone, then click
Add snippet -->:
You'll then see your code snippet displayed, along with a copy button and an instruction to paste it into the
<head> of your website. Go ahead and do that, then click
Start collecting data:
Until you get your first visitor to your website, you'll see something like this:
But once traffic starts to flow, the data will start to be collected.
A note on CSP (Content-Security-Policy)
If you have CSP enabled on your website (generally recommended to avoid malicious script injections) you may need to modify it otherwise it could block Plausible from tracking. Dependent on how your site is set up, and what code you have running on it from other sites, you'll need to research and test what works well for you. I tend to run CSP with minimal rules, but I do use other, stricter policies to supplement it. Each use case will be different, so I encourage you to research and find out what works best for you.
Similarly, various browser adblockers or fingerprint protectors may also prevent Plausible from tracking them. There's not much you can do about this however, so be aware.