Counting Visits and adblockers
Tags: analytics, gatsby
Published: 2019-05-05 17:40:00

---
title: Counting Visits and adblockers
published: true
tags: analytics,gatsby
canonical_url: https://jagascript.com/counting-visits-and-adblockers/
---

Nowadays a lot of users use some kind of adblocker, personally, I use [uBlock origin](https://chrome.google.com/webstore/detail/ublock-origin/cjpalhdlnbpafiamejdnhcphjbkeiagm?hl=it).

## It this just for ads right?

Well, no, most adblocker also blocks tracking events including the most famous tracking system which is the dead-simple to setup and goto tool for most companies [google analytics](https://analytics.google.com/analytics/web/)

## How does a blocker work?

Being a browser extension, a blocker sits between the internet and the browser, this can stop requests from a website you are navigating to another URL matching, for example, the domains from a blacklist.

![request-blocked](https://jagascript.com/static/f87b71417a61ff8d45afdd18174347f7/a792c/request-blocked.png)

> This look familiar?

## What is the problem

The obvious problem is that visits statistics such as numbers of pageview for an article or most used devices will no longer match the reality, given it only shows devices that don't have an adblocker, and based on your audience it can be a huge part, if not the most part.

Having data you cannot trust or make decisions upon is just useless data.

all I want for my personal projects is to know the visit count divided by page and the origin of the visit (slack, twitter, google, etc), from which country the visitor is requesting the page is a nice to have but not required.

## Solution

Use something else, something that is not in a blacklist

### First Try

(This works and I'm using it now, it was my fault for not making it work in the end, details later)

{% twitter 1124475843881861121 %}

Suggested by a friend of mine on twitter, I tried to use [simpleanalytics.io](https://simpleanalytics.io/) which offer an HTML script tag to insert in your pages and a dashboard to see the requests, it shows device type, country, origin.

Enough for my case!

The pricing is very good 9 dollars per month with unlimited websites, so I could use it for all my domains which is great.

Unfortunately, I tried it and my adblocker blocked it.

![booo](https://media2.giphy.com/media/3ornjUrHrU17Cb1JFS/giphy.gif?cid=790b76115cceb187393370332e89dd7e&rid=giphy.gif)

Well, it' a tracking service, no wonder it eventually entered in the huge blacklist.

### Second Try

So I went ahead and built my poor-man-edition visit counter in _go_ on _aws-lambda_ using _dynamodb_ that uses a great feature of lambdas being server by api-gateway receives the headers from CloudFront which have some amazing information inside

here the repo of the visit tracker [go-visits-cloudfront](https://github.com/kandros/go-visits-cloudfront)

It works, have no frontend not dashboard at the moment but It's surprisingly useful for a 2-hour Saturday project built in a bar.

### Third try

In the meantime, the twitter thread continued and the author of simpleanalytics.io step-by, to add a much-appreciated information


{% twitter 1124658593146519553 %}

adding a _cname_ redirect from one of your subdomains to simpleanalytics' api we can bypass the adblocker, given the fact that simpleanalitycs is based on privacy and "being a good internet citizen" at its core, I have no problem doing that.

The solution is as simple as it is smart.

### what about SSL?

In case you wonder how it works with https, you set the subdomain you choose into the simpleanalytics dashboard and it does SSL certificates magic to enable secure data communication for you. Using domains from different registars (Godaddy, Google, Amazon Route 53, Zeit, Netlify via their domain servers) I had to do nothing else.

### What about single page applications and gatsby?

For SPA it just works, for details there is a page in their documentation [doc](https://docs.simpleanalytics.com/trigger-custom-page-views).

For [Gatsby](https://gatsbyjs.org) I just had to add the script to the page "the gatsby way" using the SSR api
Here's how


{% gist https://gist.github.com/kandros/3a2feaea93a1c05495251e728c28b8b1 %}


## Conclusion

I moved different sites to use [simpleanalytics.io](https://simpleanalytics.io) because the pricing is small enough that it's worth buying it instead of maintaining an in-house solution even if very small, which means the greater experience for my visitors, greater experience for me because I can now have meaningful data to make decisions upon to create more content.