Building a UTM Attribution Engine with Supabase Edge Functions (Open Source)
Source: Dev.to
The Attribution Challenge
One of the hardest problems in marketing automation is attribution — knowing which ad drove which payment.
Solution Overview
I built an open‑source UTM attribution engine using Supabase Edge Functions. The flow works like this:
- A coach runs Meta ads.
- A visitor lands on the landing page.
- The visitor eventually pays via Razorpay.
- The engine connects the ad click to the payment.
Capturing UTM Parameters (Client‑side)
When someone lands on your page, a JavaScript snippet captures the UTM parameters and sends them to a Supabase Edge Function.
// Captures utm_source, utm_medium, utm_campaign, utm_content
fetch(TRACK_URL, {
method: "POST",
body: JSON.stringify({
visitor_id: visitorId,
utm_source: getCookie("utm_source"),
utm_campaign: getCookie("utm_campaign"),
page_url: window.location.href
})
});The Edge Function also detects the visitor’s city using Cloudflare headers—free, no API key needed.
Processing Payments (Server‑side)
When a payment is captured, Razorpay sends a webhook to the Edge Function, which:
- Validates the webhook signature.
- Extracts email, phone, and amount.
- Matches the payment to the most recent unmatched UTM visitor (within 24 hours).
- Updates the contact record with attribution data.
Matching Logic Example
- Payment arrives (email:
user@example.com). - Query
utm_visitorstable for recent unmatched visitors. - Match by timing (closest visitor to payment time).
- Copy UTM data to
automation_contacts.
Now we know: this payment came from Facebook, Campaign X, Creative Y.
Dashboard Insights
In your dashboard you can see results such as:
- Ad Creative A drove 5 payments totaling $500.
- Ad Creative B drove 1 payment of $100.
Use these insights to scale the winning creative and pause the under‑performing one.
Open‑Source Project
This engine is part of Claude Coach Kit, a free, open‑source marketing automation toolkit.
- GitHub:
- License: MIT
Built with Claude.