How to create your simple version of google analytics real-time using Nun-db

One of the features I like the most about Google Analytics is the real-time user track. Randomly during my day, I visit the real-time analytics of all products I run to see how many people my work is impacting at that time. It gives me the energy to see my work affecting other people. On the other hand, there are some drawn backs of using google analytics, to name a few:

  • Technical user may block google analytics to avoid been tracked (and you will not see that traffic in your tool)
  • You are giving data to google for free just to use a simple feature that may not be worth paying for at all. Or maybe you won’t even use most of it’s features.

To skip all that drawn backs, how can you create an online users count using Nun-db?

Crete a database for your site

How to count connected browsers?

Nun-db has a native function to count the number of open connections to a database, so here you don’t need to write any piece of code to push the number of users on your site, all you need to do is to connect to Nun-db, and that is it.

Here is how you do it,

Analytics script

Add the element to receive the users count that you just created

You can see it in action in this very blog (Going to the about link at the end of the page). Check out the next gif showing it working!


Good parts

  • Little code (simplicity).
  • 0 User data (privacy) you will need 0 data from the user reading your site, and this is respectful.
  • You can self-host it and avoid the tracker been blocked by the tracking blocker (all data is yours).

Problems with this implementation

  • If the user opens multiple browser windows, it will count as various users online.
  • The user may leave the browser open, and it will still count as online.
Written on February 28, 2021