Setting up anayltics tracking for a web page is simple. Just copy couple of lines of code from the select analytics service and you are good to go.

While I was converting my personal WordPress blog to a static website with Hugo framework, it was good time think alternatives for Google Analytics. I wanted to get rid of Google Analytics because it is tracking way too much user information.

Honestly I’m not sure what the legislation in EU countries exactly say about the topic. Anyway, the discussion has not been favoring Google Analytics.

I had couple of big questions:

  • What is the simplest way to replace Google Analytics?
  • How much does it cost?
  • How to exclude admin traffic from the static website analytics?
  • Is it possible without cookies?

I ended up choosing a German web analytics service Pirsch .

What I need from my web analytics?

During the years I have not been using too many functionalities in Google Analytics. Here are the metrics I want from my web site analytics:

  • Most popular pages recently (2 weeks to 1 year back)
  • How the popularity of a specific page has changed
  • Which blog tags and categories are popular
  • Historical trend of monthly website visits
  • Total read time by page: [Page views ]*[Average time on page]
  • Monitoring real-time user count after publishing in social media

I have not had visibility to download times of file attachments, but that would be a nice feature. But it is not possible with the methods that typical website analytics tools use.

How much a Google Analytics alternative for a static website costs?

Privacy awareness and regulation like GDPR in the web are still hot topic. That is why many of the Google Analytics alternatives are early stage small companies or startups. That is also the reason why they often do not have extensive free subscriptions.

You might still get 500-3000 page views per month free. This should be more than enough when starting a company website or a blog.

A typical website analytics service costs around 10 $ / month for around 10 000 page visits. My blog has had just over that during the best months. So I wanted to have a bit more capacity for that 10 dollars.

Monthly sessions for mikaelahonen.com in Google Analytics between April 2015 and June 2022.
Monthly sessions for mikaelahonen.com in Google Analytics between April 2015 and June 2022.

My choice as a Google Analytics alternative: Pirsch

This page was excellent to compare European web analytics services . It would have taken forever to find all those companies from Google.

I compared most of the services and tried a few. Simplicity and 20 000 page views for less than 10 $ a month were my top priorities.

TelemetrryDeck was promising, but it was still too immature. I could not make basic statistics work.

Stormly had generous free plan but apparently it still uses cookies for tracking.

Pirsch is a German solution maintained by a small team. It offers 10 000 page views for 5 $ a month and 100 000 for 10 $ a month. Unlimited sites and pretty much everything else. Exactly what I needed but nothing extra.

Monthly sessions for mikaelahonen.com in Pirsch between August and October 2022.
Monthly sessions for mikaelahonen.com in Pirsch between August and October 2022.

Recording page views in http server of the static website

By far the best solution would have been recording the visits at the same time when the static html page is requested from the http server. It would be efficient and convenient.

But if you are using services like Netlify or Digital Ocean static websites, it is up to them whether such analytics feature exists. And so is the pricing.

This approach has one nice advantage: It can count how many times attachment files like my cv.pdf have been accessed.

It seems that Netlify would have this feature and Digital Ocean has indicated that website analytics would be coming at some point.

My old web hotel stores detailed statistics about traffic bandwith, page hits and even attachments. I found the Awstats dashboard only recently from CPanel.

Monthly statistics for mikaelahonen.com in CPanel Awstats during 2022.
Monthly statistics for mikaelahonen.com in CPanel Awstats during 2022.

Excluding admin traffic from static website analytics by

I have always had only one environment for my WordPress blog. I know pretty well what kind of changes break the site so there has been no need to have a separate development environment.

When using WordPress toghether with Google Analytics it has been also relatively easy to exclude admin traffic from the web analytics. It takes some configuration, but all the building blocks are there. Whenever I have been logged in to Wordpress, Google Analytics has ignored my page views. That keeps my visitor data clean.

Static website does not have such capability. They do not have a login mechanism because everything is static html.

As of writing the previous sentence, I realized how I can achieve the admin exclusion.

Step 1

Make it possible to save a dummy variable to local storage on any url of your site. Something like this

Public admin page to disable tracking, view deployment info and test the backend API.
Public admin page to disable tracking, view deployment info and test the backend API.

Set this page to noindex from the search engines. Do not put link to this page anywhere on your site.

Step 2

Wrap the web analytics code around an if statement that checks the variable from local storage. Here is a pseudo code:

    let role = window.localStorage.getItem('role')
    if (role=="admin"){
      console.log("Bypass analytics")
    } else {
      console.log("Submit analytics")
      //Analytics tracking code                
    }

This is not a secure method because your dummy admin login is public to anyone. But because the only implication is web analytics tracking that is anonymous anyway, there is extremely low chance that somebody would take the effort to exclude themselves from analytics.

After finding this awesome hack, I found out that Pirsch offers this mechanism out of the box…

Pirsch not counting visits on Brave browser

I switched some time ago to Brave browser as it is advertised as more secure option.

While testing my website, it seemed that Pirsch is not counting the visits when using Brave.

The reason is that Brave blocks some Javascript requests that it identifies as suspicious. As I use analytics as an indicator rather than absolute truth, that is fine for now.

One option would be to proxy the tracking request through my own backend such as api.mikaelahonen.com/visitor/. This is technically possible by Pirsch backend integration or server proxy and would look more reliable for the browsers. In fact, it is more secure, because traffic is validated by sub domain owned by me.

Excluding admin traffic from static website analytics in two environments

Another approach would be to have a separate development and production environment.

I would review and browse the site myself only in dev.mikaelahonen.com and the actual users would use mikaelahonen.com. But this workflow would generate some extra work in other parts of the process.

Ofcourse I have the local environment when I adjust the layouts and write content. It is easy to exclude local environment from website analytics. But every now and then I like to browse on live site on my phone or desktop and see what can be improved and how it looks for the user.

I do not see having just one server environment as a big risk. The failing deployments are not passed through in Vercel where I am hosting the new site.

Can I build Google Analytics alternative myself

Yes. And it would not be that difficult. Just create a REST API backend with services Google Cloud Run. Then let the static website send a request to the API at each website visit. Save the record to a file or database table. You would probably want to save:

Session id. A hash (eg. MD5 algorithm) created from client IP address, user agent, maybe the date and a secret key to make it even more difficult to decrypt. This is anonymous identification method as the IP address itself will not be saved. That is the method Pirsch and many other cookieless analytics tools use.

Timestamp. When the visit happens.

Url of the visited page.

Aand that’s it! From these bits of information you can already calculate the same basic metrics that Google Analytics shows.

I could have created something like this with a moderate effort. Maybe I still do it some day. In the end, my static website already has a backend. But it would have been still many work hours for a problem that had numerous commercial alternatives. If a paid services enables me to do 1 or 2 hours more billable work per year, it has paid back.

One head ache would have been excluding traffic from search engine crawlers such as Google bot. There are known methods of this, but again, I would repeat something that has been already solved many times before by others.

What happens to my data when switching away from Google Analytics?

I have understood that Google Analytics will switch the lights off somewhere during 2023. Personally I will export the page views and other basic data if possible.

It is natural that tech evolves and website migrations happen. There is no sense to try to save all history of all data. Just make sure that you have sufficient information to have a rough idea about what is happening on your site.