Building a UTM Attribution Engine with Supabase Edge Functions (Open Source)

Published: (March 16, 2026 at 02:40 AM EDT)
2 min read
Source: Dev.to

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:

  1. A coach runs Meta ads.
  2. A visitor lands on the landing page.
  3. The visitor eventually pays via Razorpay.
  4. 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:

  1. Validates the webhook signature.
  2. Extracts email, phone, and amount.
  3. Matches the payment to the most recent unmatched UTM visitor (within 24 hours).
  4. Updates the contact record with attribution data.

Matching Logic Example

  • Payment arrives (email: user@example.com).
  • Query utm_visitors table 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.

0 views
Back to Blog

Related posts

Read more »