Calculating analytics for webpages

Calculating analytics for webpages

Lately, I've been diving deep into the world of my SaaS product, Reach. I've been thoroughly immersed in figuring out how to calculate basic site analytics—focusing on the usual suspects like views, unique views, clicks, and a range of other interesting metrics. It's been a bit of a rollercoaster, so I thought I'd give you a quick recap of the journey I've been on while striving to achieve these analytics goals with Reach. Buckle up for the ride! 🚀

To give you some context, the web pages we're discussing are straightforward contact pages, similar to a basic Linktree page—essentially a webpage that lists all your important links and products. When dealing with such pages, the metrics that users are most interested in are clicks, unique clicks, and similar engagement indicators.

Here's a breakdown of the key metrics we'll be tracking:

  • Views

  • Clicks

  • Unique Views

  • Unique Clicks

  • Geographical distribution of views and clicks

Basic Setup

Being a tech enthusiast, I find the setup far more exciting than the actual calculation of metrics. Ideally, the analytics system should be built on top of a messaging queue, allowing it to handle a multitude of click and view requests without overwhelming the server.

In our case, since we're just starting out and not expecting a flood of users or views, we opted for a simpler setup with a Flask server handling incoming requests. These requests are then added to a MongoDB collection. We chose MongoDB because of its excellent aggregation capabilities, which are crucial for the analytics we’re building.

Calculating Metrics

As you're probably aware, calculating basic metrics like views and clicks is relatively straightforward. When a page loads, we send an API request to log the view. Similarly, when a link is clicked, another API request logs the click.

The interesting part comes with calculating unique clicks and views. There are multiple ways to approach this, but for now, we've decided to use IP addresses and user agents. This method not only helps in distinguishing unique users but also allows us to use the IP data to calculate geographical distribution. This is how we've set up our web page analytics so far.

Did you find this article valuable?

Support Iresh Sharma by becoming a sponsor. Any amount is appreciated!