How I built an affiliate tracking system in a weekend with serverless
Tags: apigateway, appsync, aws, dynamodb
Published: 2023-10-12 22:04:21

---
title: How I built an affiliate tracking system in a weekend with serverless
published: true
date: 2023-10-12 14:56:11 UTC
tags: APIGateway,AppSync,AWS,DynamoDB
canonical_url: https://theburningmonk.com/2023/10/how-i-built-an-affiliate-tracking-system-in-a-weekend-with-serverless/
---

Having taught thousands of students to build serverless applications via my online courses and workshops, I felt it was time to kick-start an affiliate program to boost sales. Affiliates would receive 50% of the revenue and get a 15% discount code for their audience. It feels like a good deal but I would need a way to track these affiliate sales.

To my surprise, most popular affiliate tracking software options on the market turned out to be quite expensive. Starter packages typically range between $100/month to a whopping $1000/month.

I’m not confident I can generate enough affiliate sales to justify this level of outlet. More to the point, I want to fail cheaply if things don’t work out.

### Why a custom affiliate tracking system?

Currently, I host the [AppSync Masterclass](https://appsyncmasterclass.com/?utm_campaign=built-affiliate-tracking&utm_source=blog) [1] and [Testing Serverless Architectures](https://testserverlessapps.com/?utm_campaign=built-affiliate-tracking&utm_source=blog) [2] on [Thinkific](https://school.theburningmonk.com/) [3]. And I use LemonSqueezy for my [Production-Ready Serverless](https://productionreadyserverless.com/?utm_campaign=built-affiliate-tracking&utm_source=blog) [4] workshop.

Both Thinkific and LemonSqueezy support affiliates but they’re not suitable for my needs. Because:

1. Thinkific is not a merchant of record, which meant I’m responsible for collecting VATs and reporting them to the relevant countries. Digital tax on online courses is a [complex subject](https://quaderno.io/blog/tax-rules-online-courses/) [5] and I’m not equipped to deal with it.
2. Because of the above, I use [Paddle](https://paddle.com/) [6] to handle payment processing and they act as the reseller and deal with all matters related to tax.
3. Paddle does not allow instructor-led courses on their platform, so I have to use another platform for my workshop. In the end, I chose LemonSqueezy as the payment platform because they are more cost-effective than Gumroad.
4. I want to use the same affiliate tracking system for both platforms so an affiliate can see his/her earnings in one place.

Given these challenges with existing platforms, I pondered whether to switch platforms or to develop a custom solution.

1. Move from Thinkific. [Teachable](https://teachable.com/) [7] is the obvious choice. It handles tax collection and has built-in support for affiliates. I wouldn’t need Paddle anymore, but I’d have to import all my existing courses and uproot all my students. Plus, I had already paid for Thinkific for the next 12 months…
2. Built a custom affiliate tracking system and see if it’s something worth investing more effort into.

Given the context, it’s evident which path I chose.

### The anatomy of an affiliate tracking system

I’ve never built an affiliate tracking system before, so I turned to ChatGPT to find out what it would entail.

The basic ingredients are surprisingly simple:

- A unique URL for each affiliate.
- A cookie to remember which affiliate a user came from.
- A way to be notified when a referred user makes a purchase.

The commercial software would include features such as advanced reporting and fraud detection. Fraud detection would be important if you pay affiliates for clicks. But in my case, it’s a revenue share scheme, so fraudulent clicks are not a concern.

Additionally, students can request a full refund within 30 days as part of my happiness guarantee. So affiliates also cannot cash out purchases made within the last 30 days.

### Building the affiliate tracking system

Here’s a snapshot of how I built the affiliate tracking system:

![](https://theburningmonk.com/wp-content/uploads/2023/10/img_652808d30f289.png)

1. A student visits a course’s homepage through an affiliate URL, such as `productionreadyserverless.com/?affiliateId=314f53f4-9ab5-4a25-b47f-228bb1884b4e`. If an `affiliateId` cookie doesn’t exist already, then one is created with an expiration time of 30 days. This means the first affiliate that refers a student would receive the 50% commission.

2. When the student makes a purchase, a payload including the affiliate ID is passed to either Paddle or LemonSqueezy.

3. Paddle or LemonSqueezy notifies our REST API via a POST webhook. The payload from step 2. is included in the body of the webhook. The request is handled by a Lambda function, which extracts the payment details (payment platform, order ID, course name, gross and net amounts, timestamp) and saves it in a DynamoDB table. When a student requests a refund, the same webhook notifies our API of these and the previous purchase is cancelled accordingly.

4. Another Lambda function is triggered by the DynamoDB stream. This function notifies the affiliate (and me) of the sale/refund by email, using the SES service. Yes, this step can be combined into step 3. But I think this is the [best reason for using DynamoDB streams](https://lumigo.io/blog/the-best-reason-to-use-dynamodb-streams-is/?utm_campaign=built-affiliate-tracking&utm_source=yan&utm_medium=blog) [8] – to engineer a distributed transaction out of your architecture.

5. The affiliate can log into the affiliates’ portal and see his/her commissions. The portal uses the passwordless sign-in flow (with one-time passwords) described in [this post](https://dev.to/aws-heroes/passwordless-authentication-made-easy-with-cognito-a-step-by-step-guide-2pcn) [9].

![](https://theburningmonk.com/wp-content/uploads/2023/10/img_652808e992894.png)

6. Once logged in, the portal talks to an AppSync API to fetch the affiliate’s commissions and his/her unique affiliate URLs.

This system is nothing fancy, but it works and ticks all my boxes.

It took less than a day’s work in total and has no running cost as everything is under the free tier.

Moving forward, I aim to incorporate features like analytics reporting – visitors count, commissions over time, etc. and a self-service signup flow. But first, I need to prove that there is an appetite for my affiliate program. After all, that’s the whole idea behind building out this system, it’s so that I can experiment and fail cheaply.

Now, the quest is to find passionate affiliates to collaborate with. If serverless technology excites you as much as it does me, or if you know someone who fits the bill, let’s connect!

### Links

- [1] [AppSync Masterclass](https://appsyncmasterclass.com/?utm_campaign=built-affiliate-tracking&utm_source=blog) course
- [2] [Testing Serverless Architectures](https://testserverlessapps.com/?utm_campaign=built-affiliate-tracking&utm_source=blog) course
- [3] [My school on Thinkific](https://school.theburningmonk.com/)
- [4] [Production-Ready Serverless](https://productionreadyserverless.com/?utm_campaign=built-affiliate-tracking&utm_source=blog) workshop
- [5] [What’s up with digital tax for online courses](https://quaderno.io/blog/tax-rules-online-courses/)
- [6] [Paddle](https://www.paddle.com/)
- [7] [Teachable](https://teachable.com/)
- [8] [The best reason to use DynamoDB Streams](https://lumigo.io/blog/the-best-reason-to-use-dynamodb-streams-is/?utm_campaign=built-affiliate-tracking&utm_source=yan&utm_medium=blog)
- [9] [Passwordless Authentication made easy with Cognito: a step-by-step guide](https://dev.to/aws-heroes/passwordless-authentication-made-easy-with-cognito-a-step-by-step-guide-2pcn)

The post [How I built an affiliate tracking system in a weekend with serverless](https://theburningmonk.com/2023/10/how-i-built-an-affiliate-tracking-system-in-a-weekend-with-serverless/) appeared first on [theburningmonk.com](https://theburningmonk.com).