Nandolytics - My Own Visits Analysis Tool

Reading time ~4 minutes

Want to learn more about AWS, Serverless and Projects? Sign up for my mailing list!

I’d like to introduce to you a new advanced analytics tool for site traffic - Nandolytics.

Nandolytics is a highly-advanced one-of-a-kind analytics service that’s going to change the world through uniquely variable analysis in a market of established and reliable vendors for analytics services.

A few lucky visitors can have access to a demo of how I built this service. Find out if you’re one of them by clicking the button below.

I was using my website analytics recently to review my site traffic and I was curious how difficult it would be to build my own web analytics service that could count website hits on my blog. So I did - sort of: I present to you Nandolytics:

An image of the word Nandolytics

Now before I continue, let me without question let you know that this entire thing is super unreliable/easily exploitable and suggest that you never use it in a real production environment. With that said… Let’s have some fun!

While working on a course that touches on AWS DynamoDB I wrote about DynamoDB atomic counters. Atomic counters essentially allow you to increment numeric values in a single write operation rather than doing a read and a subsequent write. This avoids issues like accidentally overwriting previous data and generally adds some fault-tolerance. You can technically increment or decrement the values you’re evaluating so I figured what the heck - I’ll try to make a website ‘hits’ counter that keeps track of website hits.

Here’s how you can implement it on your own site.

The first thing you need to know is that there are two endpoints for Nandolytics:

  1. The nandolytics/record endpoint - this is used to tabulate visit counts internally. You POST JSON data to the endpoint and it is tabulated in your DynamoDB table.
  2. The nandolytics/{id} endpoint - this is used to check the internal site hits numbers.

The first thing you’ll need to do is to copy the Serverless Framework project I created for this. The code and configuration can be found on Github.

You can also do this with git clone https://github.com/fernando-mc/nandolytics.git.

Once you have the repo locally then you can deploy it with sls deploy. Copy down the API endpoints it outputs from the deployment. You’ll use these later on.

This project uses a DynamoDB table that has a simple primary key consisting of a siteUrl attribute as a partition key. In addition to the siteUrl all these items have a number type attribute - siteHits. Now siteHits needs to be a number type because that’s required for using an atomic counter with DynamoDB. So, a sample item in the table might initialize to this for my blog’s homepage.

{
  "siteUrl": "https://www.fernandomc.com/",
  "siteHits": "1"
}

Fortunately the Serverless Framework project will spin all this up and manage it for you along with the API endpoints and Lambda functions you need.

In order to implement this on your own site you’ll need to include some JavaScript to send a POST request to your API each time you load the page. You can also optionally use the GET endpoint to get the siteHits information without adding to the site hits.

After deploying your Serverless Framework project replace the API_ENDPOINT variable below with your own and copy the code into whatever page you’d like to count hits for.

var currentEncodedURL = encodeURIComponent(window.location.href)
var API_ENDPOINT = "https://some12url34.execute-api.us-east-1.amazonaws.com/dev/nandolytics/"
var getEndpoint = API_ENDPOINT + currentEncodedURL
var recordEndpoint = API_ENDPOINT + "record"

// Record a visit to this site
fetch(record_endpoint, {
  method: 'post',
  body: JSON.stringify({"siteUrl":currentEncodedURL})
}).then(r => r.json())
  .then(data => console.log(data))
  .catch(e => console.log("The request for Nandolytics failed"))

// Console log current site visits
fetch(get_endpoint).then(r => r.json())
  .then(data => {
    console.log(data['siteHits'])
  })
  .catch(e => console.log("The request for Nandolytics failed"))
}

After the JavaScript is copied onto the page it should run on your site whenever the page is loaded - And boom! You have your very own analytics service to count site hits. Have fun playing around with it and Happy April 1st!