Getting Rid of Google Analytics

Let's face it. Google Analytics is bad. It's privacy invasive, it's bloated, and complex. And I will admit, I never understood the dashboard. I just want to see how many visitors visited my blog - I do not want their family history. I want something simple, something easy to understand and use, and something that doesn't track the user across the web.

This is why starting from today, my blog is switching to Plausible Analytics. I set up a self hosted Plausible instance on https://analytics.abhattacharyea.dev. You can see the public analytics here.

How to?

Let's take a look at how to set up a self hosted Plausible instance on your server.

Steps

  1. Get a server. I got myself a Debian 10 VM.
  2. Get a domain and set it up.
  3. Set up Docker and Docker compose.
  4. Install Plausible
  5. Install and configure Nginx and certbot.
  6. Configure Plausible
  7. Optional: register for geolocation data

1. Get a server

This should be easy as you can use any cloud platform like Linode, Digital Ocean etc. Or you can use a physical server if you have one. Note that Plausible supports x86_64 platforms, so you can't run it on Raspberry pi.

The following parts will assume you're using Debian or Debian based distro.

2. Get a domain and set it up

Now you'll need to get a domain. You can use GoDaddy, NameCheap etc. to buy a domain. Or if you have a domain, you can use a subdomain. I used analytics.abhattacharyea.dev . Once you have a domain, you need to point it to your server. For that, you'll need to consult your domain registrar to get the exact steps.

3. Docker and Docker compose

First, make sure the server is up to date -

sudo apt-get update && sudo apt-get upgrade

Once this is finished, we'll install Docker. For this we'll use the following script provided by the Docker team.

Warning: You should check all the scripts before running.

curl -fsSL https://get.docker.com -o get-docker.sh
sudo sh get-docker.sh

Now we will add our current user to the docker group. This will make life easier since we do not have to use sudo every time we want to use Docker.

sudo usermod -aG docker $USER

Let's also install docker-compose

sudo curl -L "https://github.com/docker/compose/releases/download/1.28.5/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose

4. Installing Plausible

First, make sure git is installed

sudo apt-get install git

Now clone the Plausible hosting repo and move to the directory-

git clone https://github.com/plausible/hosting
cd hosting

Now we need to edit the plausible-conf.env file. But first, we need to generate a random key -

openssl rand -base64 64

You'll get an output like this -

TxU9+23iGnLkwnwkB+UCP6OU7WfQEKB6Bdp6o92NgwTlFqScmpEyq3BwZJ0ohgU4
1eWnF0tF/nyxtBH00UEmsg==

Copy the string into the clipboard. Now edit the file plausible-conf.env and fill out the details

ADMIN_USER_EMAIL=your-email@example.com
ADMIN_USER_NAME=YourName
ADMIN_USER_PWD=YourPassword
BASE_URL=https://your-endpoint
SECRET_KEY_BASE=TxU9+23iGnLkwnwkB+UCP6OU7WfQEKB6Bdp6o92NgwTlFqScmpEyq3BwZJ0ohgU41eWnF0tF/nyxtBH00UEmsg==

The first 3 values are self explanatory. For the BASE_URL value,you'll put the domain you set up in step 2.

In the SECRET_KEY_BASE field, put the random string you just copied.

There are quite a lot of other configuration options too. You can check them out here.

Now we're ready to run Plausible -

docker-compose up -d

It should provide a lot of output, but at the end, you should see something like -

Creating hosting_mail_1                ... done
Creating hosting_plausible_db_1        ... done
Creating hosting_plausible_events_db_1 ... done
Creating hosting_plausible_1           ... done

Which indicates it is up and running (on port 8000). But before we can access it, we'll set up a reverse proxy with Nginx because Plausible runs in unencrypted HTTP. So we'll run Nginx to provide an HTTPS connection which will be reverse proxied to port 8000

5. Nginx

First install Nginx -

sudo apt-get install nginx

If you have some kind of firewall, be sure to allow Nginx through it.

Now we'll create a basic HTTP server on port 80 and use certbot to "upgrade" it to HTTPS.

Edit the file /etc/nginx/sites-available/default and delete the entire content and put the following lines -

server {
	# replace example.com with your domain name
	server_name example.com;
	
	listen 80;
	listen [::]:80;

	location / {
		proxy_pass http://127.0.0.1:8000;
		proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
	}
}

Remember to replace example.com with your domain name. Now just restart Nginx

sudo systemctl restart nginx

Now we'll setup certbot and obtain an SSL certificate

sudo apt-get install certbot python-certbot-nginx
sudo certbot --nginx

Follow the instructions, select your domain name from the Nginx list.
Also, select redirect to upgrade HTTP to HTTPS.

Once it is done, restart Nginx

sudo systemctl restart nginx

6. Configure Plausible

Now visit your domain on the browser. If you did everything correctly, you should see this page -

To log in, use the same email and password you used in the plausible-conf.env file. Once you click login, you'll see this page.

Once you click on Request activation code an activation code will be sent to your mail.

If you did not set up an SMTP server (I did not), or having trouble recieving mail, you can run this command -

docker exec hosting_plausible_db_1 psql -U postgres -d plausible_db -c "UPDATE users SET email_verified = true;"

After you run this command, visit the domain URL again in a new tab and log in again. Then click on Add new site.

Here you'll enter the URL of the site you want to collect Analytics about and enter the Time zone. Then click on Add snippet.

Copy the HTML snippet and paste it into the <head> tag of the target website. Check out the docs to find out the exact steps.

Once the code is added, click on Start collecting data and open the target URL. You should see the visit logged in the Analytics instance.

7. Optional: register for geolocation data

If you want to see the geographic data of your users, you'll need to perform some additional steps.

First, register here for a MaxMind account.

Then click on Manage license keys in the sidebar and then click on  generate a new license key

Provide a description, and make sure to choose the geoipupdate option.

Click on Confirm. Copy the license key and account ID. You'll not be shown the license key ever again! So copy it safely.

Now edit geoip/geoip.conf file and enter your account ID and license key in GEOIPUPDATE_ACCOUNT_ID and GEOIPUPDATE_LICENSE_KEY respectively.

Now restart Plausible with this command -

$ docker-compose -f docker-compose.yml -f geoip/docker-compose.geoip.yml up -d

And there you have it. Now you can see Geographic data.


Congratulations. You have successfully set up a self hosted Plausible instance.