JagaScript

Counting Visits and adblockers

May 05, 2019 | 5 min read
tags: [analytics]

Nowadays a lot of users use some kind of adblocker, personally, I use uBlock origin.

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

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

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)

Suggested by a friend of mine on twitter, I tried to use 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

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

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

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.

For Gatsby I just had to add the script to the page “the gatsby way” using the SSR api Here’s how

const React = require("react")
exports.onRenderBody = ({ setPostBodyComponents }) => {
if (process.env.NODE_ENV !== `production`) {
return null
}
return setPostBodyComponents([
<script
key="simpleanalytics"
async
defer
src="https://visits.yourdomain.com/app.js"
/>,
<noscript key="simpleanalytics-no-script">
<img src="https://visits.yourdomain.com/image.gif" alt="" />
</noscript>,
])
}
view raw gatsby-ssr.js hosted with ❤ by GitHub
view raw instruction.md hosted with ❤ by GitHub

Conclusion

I moved different sites to use 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.


Jaga Santagostino

Blog by Jaga Santagostino.
Software consultant, polyglot developer, maker of things, lifelong learner.