Create a custom analytics server in 30 minutes with Python & AWS

Michael Chen
Geek Culture
Published in
4 min readMay 20, 2022

--

Source: Unsplash

Sometimes, Google Analytics isn’t the best solution.

When I was creating ProductSage, all I wanted to know was what users searched for on my website. Using Google Analytics’ lingo, I trying to track “custom events”.

But GA wasn’t suitable for my use case — I used its Custom Events feature for another project and the process was unnecessarily difficult. This time around, I wanted something quick and simple, without the baggage of dealing with GA’s unintuitive API, slow updating server, and cluttered web interface. To be fair, GA is indeed free, but it comes at the cost of my time (and mental health, it seems).

So I decided that for ProductSage, I will use AWS Lambda and DynamoDB to create a simple analytics server that costs almost $0. Here’s how you can set it up in 30 minutes or less.

How it works

DynamoDB is a non-relational (NoSQL) database service provided by AWS. Items, which have no fixed structure, are stored in tables.

AWS Lambda lets you run code online without caring about anything server-related. All you need to do is upload your code into the Lambda function, and it’ll run on the cloud!

And this is both parts come together:

  • Step 1: Client application sends data through an API to your Lambda function.
  • Step 2: Lambda function accesses your DynamoDB table and stores data into it.

And that’s it! With the explanation out of the way, let’s create the server together.

Tutorial

Create DynamoDB Table

Go to the DynamoDB service on AWS Console. After clicking on the “Tables” section in the sidebar, click on the orange “Create Table” button on the right.

Enter a table name (it’s up to you) and enter “id” as your partition key.

Leave the rest of the options as they are, then click the “Create Table” button at the bottom.

Wait for AWS to finish creating your table, and it’s ready!

Create Lambda function

Go to the AWS Lambda service on AWS Console, then click on the orange “Create Function” button on the right.

Enter a function name and choose Python 3.9 as your runtime.

Since this Lambda function will be used to insert data into our DynamoDB table, the function needs access to the DynamoDB service.

To tell AWS this, create a new execution role by entering the tiny “IAM console” link.

Select “Lambda” and click next.

Check the “AmazonDynamoDBFullAccess” policy. This will allow your Lambda function to access DynamoDB.

Write a suitable name, then click “Create role”! Note that execution roles are reusable. So if you’d like to create another similar Lambda function in the future, you can reuse this role.

After creating, type the following code into lambda_function.py using the code editor on AWS console.

Click “Deploy” and your function is ready! Except for one thing…

API Endpoint

We need to make it accessible to client applications by creating an API that links to this function. Don’t worry, this will only take a minute.

In the same Lambda interface, under “Configuration”, go to “Function URL”, then click on “Create Function URL”.

Choose “NONE” for Auth type, then check “Configure cross-origin…”. We’ll leave everything else as is, then click the “Save” button to create the URL.

Note that in production, you should change the “Allow origin” field to your website URL. This ensures that only your website can send data to the analytics server.

Finally, to make sure everything works, test your API using the following cURL command.

And to extract your analytics data using Python, here’s a sample code:

Congrats! Your analytics server is complete 🥳🥳🥳

Resources to learn more

--

--

Michael Chen
Geek Culture

I share ideas worth your time - in tech, education and society. | Co-Founder @ RoadMaple