So I got a lot of traffic on my site after my previous post got featured on Hacker news home page for 20mins.The good thing was that I enabled caddy logging. So I had some data to work with.
So on normal days, I used to get some hits amounting to 30-40MB bandwidth usage. But on the HN day, I got 8k hits and 1.25GB bandwidth usage. The problem with this data was that unique users are calculated using IP addresses. But those addresses are refreshed every 24 hours for normal devices. So I decided to check injecting javascript solutions available in public.
My criteria for selecting a solution were straightforward:-
- Lightweight implementation
- Minimal invasion of privacy
I came across two promising options:-
So I decided to go with GoatCounter. It was simple to setup and had a free tier. I just had to add a script tag to my site and it started collecting data on google chrome. Then I switched to Brave browser to my surprise,Brave was blocking the script.
That was a bummer. I had to find a solution. And I gave piratepx a try. It was also simple to setup. But the same problem persisted. Brave was blocking the gif download.
This experience taught me that any analytics service with moderate popularity would likely be blocked by Brave or uBlockOrigin. So I decided to create my analytics service.
So I started coding, I had a simple post route that would get UUID from local storage and the current URL after the user has been on any link for 2 secs.
Triggering a POST call after two seconds helps in 2 ways:-
- It checks if the user is spending some time on the page and not just bouncing off.
- It helped reduce the number of calls to the server if a user repeatedly opened and closed the page.
I also added a simple server-rendered HTML which shows analytics of each page for both domains I have registered with it. I am using charts.css for styling the charts.
The first bar of each pair shows unique users and the second bar shows total hits. So the first bar can be smaller than the second bar if the user has visited the page multiple times.
I named this small project “Lialytics” (Light Analytics), and it written in Golang and SQLite.
EDIT
[2023-07-01]: Turns out caddy logging is enough, I dont need this level of analytics anyway so i removed this