</ Amal>

</ Amal>

Amalytics: A tiny analytics powered by HarperDB

Amalytics: A tiny analytics powered by HarperDB

And named after me!!

Amalytics is a tiny analytics service built using Flask for the application and HarperDB for storing data. This post explains how to use the application, the design approach, metrics calculated, and the future scope.

You can go ahead and try out the application. The demo is currently running on Google Cloud Platform with free credits.

TLDR version

  1. Sign Up
  2. Add a new website
  3. The the following script to just before the closing </body> tag

    <script src="https://amalytics.amalshaji.com/amalytics.js"></script>
    

    The script grabs the page URL, referrer, and windows.innerWidth and sends it back to the analytics endpoint.

  4. Visit your website(the one you added)

  5. Check the stats on Amalytics

If you're not comfortable creating a new account, use the following credentials to check out the stats.

username: amalshaji
password: amalshaji

The demo account provided already tracks the following websites, https://amalytics.amalshaji.com and https://amalytics-demo.vercel.app. The latter is a dummy page set up to test the script.

Source Code

Tools & Technologies Used

  • Flask - For building the application
  • HarperDB - Storing data
  • Docker - Containerize Flask application
  • Traefik - Easy deployment and automatic SSL certificates
  • GCP - Virtual Machine to serve the application demo

Usage

  1. Step 1 - Amalytics

    Getting Started

  2. Step 2 - Sites

    markuphero-9iOke8C5ECy3f5kXqE8L-1.png

  3. Step 3 - Click on any registered website to get the stats

    Screenshot 2021-06-13 233920.png

Design Approach

The application is divided into multiple packages(api, auth, sites), each of which defines the following files.

  • forms.py: Contains all the forms generated using Flask-WTF
  • routes.py: Contains all the routing and core functionalities
  • models.py: Models for each package that defines operations on HarperDB
  • utils.py: Helper functions

Following is an example of a Model:

from db import db, schema
from harperdb.exceptions import HarperDBError


class Click:
    @staticmethod
    def create(payload):
        return db.insert(schema, "clicks", [payload])

    @staticmethod
    def get_click_data(website: str):
        try:
            data = db.search_by_value(schema, "clicks", "domain", website)
        except HarperDBError:
            data = None
        return data

This helps simplify the database operations. For example, one can create a new Click entry by passing the payload dictionary to Click.create(payload).

Metrics Calculated

Device: The device used to access the page. The device(either Mobile or Desktop is parsed from the window.outerWidth)

Browser: The browser used to access the page.

Referrer: The referrer, i.e, which page/website referrer the URL.

Page Visited: The pages visited within the website that you're tracking

Country: Country from which the user viewed the page/website

Clicks: Clicks, aka Views, is the number of unique views on each website page. A user is unique if the hash(page URL + IP + User-Agent) is unique for x hours. Redis is used to store this unique value. If the value is not present, a view is counted, else discarded.

Future scope

I've always wanted to build something like Amalytics. The hashnode-harperdb hackathon gave enough juice to complete the application. Currently, I'm looking for bugs and fixing them as I go. If you really like the application, give it a try(use amalshaji for username and password). I plan to keep the server alive until the end of the hackathon. Feedbacks and PRs are always welcome.

Demo

#flask#tailwind-css#hackathon#harperdbhackathon
 
Share this