Self host Plausible - Google Analytics open source alternative

Ivan Radunovic

Feb 23, 202415 min read
Self host Plausible - Google Analytics open source alternative

Since Google Analytics switched to GA4 it kinda stopped working. Data reported is not accurate anymory. Majority of traffic is not captured since every adblocker blocks Google Analytics scripts.

I won't continue why Google Analytics is rubish you already know that.

Presenting Plausible analytics, ta-da!

What is Plausible?

Plausible is an open source Google analytics alternative. You can run if for free 100% or if you don't want to self-host it you can pay cloud version.

What are the benefits of using Plausible analytics?

Short list goes like this:

  • If it's self-hosted it won't be blocked by adblockers
  • You'll capture almost entire traffic
  • It is fast
  • You can do whatever you want with the data
  • You can track unlimited number of sites

Additional benefits are easy configuration for goals, WordPress plugin which can be connected to self-hosted Plausible and many more.

Why self-host Plausible?

First it'll be served from a custom domain, so it won't be blocked by adblockers.

Second, it's free (if you've a server or VPS already).

How much does it cost to host Plausible

Cheapest VPS on Hetzner is ~$4/month.

What's great thing when you own the server is that you can host anything else on it. You can host multiple applications, not only Plausible.

So your cost is going down.

Hosting Plausible on Hetzner using Coolify

Coolify is a script which allows you to host multiple applications on the same server.

You can host anything with it. I won't go into details about how Coolify works, since I want to focus on Plausible here.

First thing is to create a VPS on Hetzner and choose Ubuntu 22.04 for OS.

You may notice in the video I configured an SSH key, if you don't know how to get the key checkout SSH key setup.

Install Coolify on the server

Now we need to SSH into the server, if you're new to this see above SSH link.

As a root user, we need to run:

curl -fsSL https://cdn.coollabs.io/coolify/install.sh | bash

This command will install Coolify on our Ubuntu 22.04 server.

Running installation command

After few minutes installation will be over and you'll see success message with a link:

Coolify installed successfully

Register on Coolify

When you visit provided URL you will be presented with the registration form.

Coolify Register Page

Choose some secure password since this account will have control over every app hosted on Coolify.

Create new Coolify project for Plausible

Coolify navigation could be a bit strange for newcomers, but follow my steps here and there we'll be no problems.

In the sidebar locate projects button and click on it.

Smash +Add button to create a new project.

Creating new Coolify project

Open new Coolify project

After it's created go and click on the new project.

Open new Coolify project

Select Coolify environment

When you're inside new project you'll have 1 existing environment, that'll be production.

Coolify production environment

Go into the production environment.

Inside we need to add new Resource.

Resource is the actual application we want to run on this environment. In our case it's Plausible Analytics.

Create new Resource

Installing Plausible using Docker compose script

On new resource page we need to select Based on Docker Compose option.

In the input for Docker Compose script paste this code:

# ignore: true
# documentation: https://plausible.io/docs/self-hosting
# slogan: "Plausible Analytics is a simple, open-source, lightweight (< 1 KB) and privacy-friendly web analytics alternative to Google Analytics."
# tags: analytics, privacy, google, alternative

version: "3.3"
services:
  plausible:
    image: plausible/analytics:v2.0
    command: sh -c "sleep 10 && /entrypoint.sh db createdb && /entrypoint.sh db migrate && /entrypoint.sh run"
    environment:
      - DATABASE_URL=postgres://postgres:$SERVICE_PASSWORD_POSTGRES@plausible_db/plausible
      - BASE_URL=$SERVICE_FQDN_PLAUSIBLE
      - SECRET_KEY_BASE=$SERVICE_BASE64_64_PLAUSIBLE
    depends_on:
      - plausible_db
      - plausible_events_db
      - mail

  mail:
    image: bytemark/smtp

  plausible_db:
    image: postgres:14-alpine
    volumes:
      - db-data:/var/lib/postgresql/data
    environment:
      - POSTGRES_DB=plausible
      - POSTGRES_PASSWORD=$SERVICE_PASSWORD_POSTGRES

  plausible_events_db:
    image: clickhouse/clickhouse-server:23.3.7.5-alpine
    volumes:
      - type: volume
        source: event-data
        target: /var/lib/clickhouse
      - type: bind
        source: ./clickhouse/clickhouse-config.xml
        target: /etc/clickhouse-server/config.d/logging.xml
        read_only: true
        content: >-
          <clickhouse><profiles><default><log_queries>0</log_queries><log_query_threads>0</log_query_threads></default></profiles></clickhouse>
      - type: bind
        source: ./clickhouse/clickhouse-user-config.xml
        target: /etc/clickhouse-server/users.d/logging.xml
        read_only: true
        content: >-
          <clickhouse><logger><level>warning</level><console>true</console></logger><query_thread_log
          remove="remove"/><query_log remove="remove"/><text_log
          remove="remove"/><trace_log remove="remove"/><metric_log
          remove="remove"/><asynchronous_metric_log
          remove="remove"/><session_log remove="remove"/><part_log
          remove="remove"/></clickhouse>
    ulimits:
      nofile:
        soft: 262144
        hard: 262144

And hit Save.

Configure custom domain for self-hosted Plausible

Before deploying this application we need to configure custom domain.

In my case I am using Cloudflare as a DNS and I'll configure subdomain plausible.corpcodingo.com for Plausible analytics.

Configuring DNS record on Cloudflare for Plausible

Now I need to update Coolify resource with this new domain.

After modifications hit Deploy.

It'll take 2-3 minutes and it should be deployed. You'll need to manually reload page to see a fresh status.

Plausible deployed

Registering on a self-hosted Plausible

You should visit custom url you specified and create Plausible account there.

Registering on self-hosted Plausible

On the next step it'll ask you which domain you want to track.

Adding sites to track with Plausible

One thing I noticed with Plausible is that it's able only to track exact domain. I am not able to add root domain and track all of its subdomains.

Tracking Tuts Sites

After inputing domain to track Plausible will display JavaScript snippet.

Plausible JavaScript Snippet

This code should be pasted in the head section of the site.

Plausible and WordPress

Plausible posses official WordPress plugin. You just need to search for Plausible inside Add new plugin page on WordPress.

Locating Plausible WordPress plugin

Click the Install Now button and after it's installed click Activate.

Go into Settings -> Plausible Analytics and open Self-Hosted tab.

There you have an input for the custom domain you created earlier.

Setup domain for self-hosted Plausible in WordPress

Click Save Changes and after few reloads your self hosted Plausible should get events from the WordPress site.

Final thought about Plausible

This Plausible installation may seem like very simple and limited but metrics I receive everyday from it are very valuable.

When you take into account that GA4 is only tracking small portion of users without adblockers, you'll understand that you will get a better picture with Plausible analytics.

Better analytics will guide you to create better content and gain higher positions in Google results. This will lead to more sales.

Happy tracking!

Share
Author Photo
Ivan Radunovic is a Senior Developer with over 11 years of experience in web development. His expertise are Laravel SaaS solutions. So far he developed or took part in 300+ Laravel projects.

More Infrastructure tutorials

Get visitors real IP address when using Cloudflare Proxy

When Cloudflare Proxy is active our webserver will receive it's IP as a visitor IP address. This quick fix will help you get true IP.

Mar 03, 2024 Ivan Radunovic

Most used Linux commands - focus on Ubuntu

During development and in production you'll have to use many Linux/Unix commands. Working in terminal will speed-up your workflow.

Mar 01, 2024 Ivan Radunovic

Block unwanted GA4 referrals - Google analytics spam

If you're wondering what is that huge spike in Google Analytics traffic, and is it legit traffic. This is the latest GA4 spam campaign

Feb 26, 2024 Ivan Radunovic