07 May 2022
About Analytics
Sahil KokamkarTable of content
Using Analytics
I didn't tend to use client side analytics(RUM) to respect user's privacy and also prior to this there wasn't any kind of client side analytics on any of my sites. But the server side analytics were not accurate enough as the traffic increased, it was hard to filter out robots, crawlers, spiders and other non-visitors from it. So I choose to add a user analytics.
Choosing Analytics
There are many client analytics out there such as Google Analytics, Hotjar, New Relic, Heap. Which offers free analytics, but at the cost of user data. And even it was declared illegal to use Google Analytics by French government. So I started to look for open source alternative, I came across with Plausible and decided to go with it. They also provide paid hosted SAAS. You can also check out Mamto(piwik), Umami. If you want other choices.
Why Plausible?
Transparent and fully open source
No cookies banner require and is fully compliant with GDPR, CCPA and PECR.
The tracking script is lightweight, less than 1 KB.
Tracks events, goal conversions and campaigns.
Can be integrated with Google Search Console
Invite team members and share or embed your dashboard
IP address is never stored
Self-hosting Plausible
A walkthrough of installing of Plausible.
You can host Plausible on any VPS or even locally as the installation is through Docker Compose OS is not a problem.
I am going to host my Plausible on AWS openSUSE Enterprise EC2 Instance in Frankfurt(eu-central-1) region to comply with GDPR.
First, make sure you have git install. And select any directory you want to clone the repo.
git clone --depth 1 https://github.com/plausible/hosting analytics
This will clone the shallow clone the repo in analytics
directory, then do cd into it by running cd hosting
After that, generate a random hash by running, echo $(openssl rand 64 | base64 -w 0)
this will generate 64 random characters and encode it in base64 format which will be required next to file in the config.
Now with the text editor of your choice, edit the plausible-conf.env
you can use additional config variables.
Here is an example of the config. [Note fill in your own credentials and make sure your password is more than 6 character long]
You can even leave this file as it without doing any changes and run sudo docker-compose up -d
it will run just fine. But it will also expose the traffic through docker container even if UFW firewall is enabled. So it's recommended to restrict your traffic through localhost by adding 127.0.0.1
in ports even there is open (PR)[https://github.com/plausible/hosting/pull/24] suggesting this. And if you don't want to use mail, bytemark/smtp
you can change it or completely remove the image if it's not needed.
Now you're all set, just make sure you have docker-compose
install. If not, run the following:
#INSTALL SCRIPT BY DOCKER
curl -fsSL https://get.docker.com -o get-docker.sh
#DRY RUN TO TEST THE SHELL SCRIPT
DRY_RUN=1 sh ./get-docker.sh
#INSTALLS docker-compose AUTOMATICALLY IF THE SUPPORTED IS SUPPORTED
sudo sh get-docker.sh
Else run sudo pip install docker-compose
to install it.
Finally, you can run
sudo docker-compose up -d
If you want to filter out city level traffic. By default, it is configured to dp-ip. You have to configure geoip.conf
for it.
First create a MaxMind account and get a free licence to use in geoip.
Then fill in your account ID and licence key.
Then you can run to run geoip/docker-compose.geoip.yml
along with docker-compose.yml
using:
sudo docker-compose -f docker-compose.yml -f geoip/docker-compose.geoip.yml up -d
Done you are all set you should be able to see the following output if you didn't set up a mail server and configured geoip
Now the setup is completed, we should now proxy the localhost proxy. For that you can use any web server of your choice such as candy, tomcat even IIS, just make sure you pass http://localhost:8000
and configure it correctly. I am going to use NGINX as a web server, so the next config will be according to NGINX.
As I am using openSUSE the installation is different, you can check out their official install guide for your distro from here
sudo zypper update
sudo zypper install ngnix
This will install NGINX for openSUSE
First, we have to open public traffic to ports 80
and 443
to allow both http and https traffic using sudo ufw allow "Nginx Full"
. “Nginx Full” is an application profile.
Next, we have to create a NGINX config for that we have to create a config file in /etc/nginx/sites-available
in that directory make a .config
file by the name of your choice. I am going to name it analytics.conf
you can directly create the file using a text editor or touch
cmd, just make sure you use sudo.
And paste the following:
server {
listen 80;
listen [::]:80;
server_name <your-domain>;
access_log /var/log/nginx/analytics.access.log;
error_log /var/log/nginx/analytics.error.log;
location / {
proxy_pass http://localhost:8000;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
Just make sure you are using the right port and proxy. You can also further configure it according to your choice.
After saving, it create a symbolic link to /etc/nginx/sites-enabled/
using the below command
sudo ln -s /etc/nginx/sites-available/analytics.conf /etc/nginx/sites-enabled/
Make sure to use your conf name if you changed it. Then test the NGINX config by running sudo nginx -t
you should get a test is successful message.
Now do a sudo systemctl reload nginx
and your Plausible should be up and running, but you need to configure your DNS record according to your domain, just make sure you don't proxy it.
Then you would be able to visit your plausible instance on http://<your-domain>
.
It will be unencrypted, so Let's Encrypt it.
We are going to use certbot which adds SSL certificate using Let’s Encrypt certificates and renews it's automatically.
sudo zypper install certbot python-certbot python-certbot-nginx
This will install cerbot for nginx server. You can also use snapd to install certbot.
Then run sudo certbot --nginx -d <your-domain>
. And you will be prompt to enter your email and read T&C.
This is how it will look like.
Certbot will auto reload nginx and switch to https
by default.
That's it, congrats you have now setup your plausible instance. You can now log in to your account using the email and password you filled in plausible-conf.env
and add your website which you want to analysis and install the snippet in the head of that site.
Updating Plausible
To update Plausible run the following commands, you can even automate it using a cron job.
docker-compose down --remove-orphans
docker-compose pull plausible
docker-compose up -d
Now that you're done, make sure to keep the instance up to date. And better secure it using Fail2Ban. And take up backup of the database, of both postgres and clickhouse.