Dev.to WebDev 🛠 Dev 👁 0 📖 2 min read

How to attribute Polar and Lemon Squeezy sales to traffic sources

Polar and Lemon Squeezy tell you who bought and how much. Neither knows which post, search or ad brought the buyer. Both accept extra data at checkout, and that is the hook: put the visitor's source there, and every orde

Polar and Lemon Squeezy tell you who bought and how much. Neither knows which post, search or ad brought the buyer. Both accept extra data at checkout, and that is the hook: put the visitor's source there, and every order can be joined back to the visit.

Step 1: remember where the visitor came from

On the first landing, save the UTM tags and referrer in a first-party cookie, and keep the first touch:

(function () {
  if (document.cookie.includes("src=")) return;
  const p = new URLSearchParams(location.search);
  const src = {
    source: p.get("utm_source") || document.referrer || "direct",
    campaign: p.get("utm_campaign") || "",
  };
  document.cookie = "src=" + encodeURIComponent(JSON.stringify(src)) +
    "; path=/; max-age=" + 60 * 60 * 24 * 90 + "; samesite=lax";
})();

Step 2: pass it into checkout

Polar, for a checkout your server creates, use metadata:

const src = JSON.parse(decodeURIComponent(req.cookies.src || "%7B%7D"));

const checkout = await polar.checkouts.create({
  products: ["PRODUCT_ID"],
  success_url: "https://yourdomain.com/thanks",
  metadata: { source: src.source, campaign: src.campaign },
});

Lemon Squeezy, on a checkout link, add custom fields to the URL:

https://yourstore.lemonsqueezy.com/checkout/buy/VARIANT_ID?checkout[custom][source]=reddit&checkout[custom][campaign]=launch

Or set checkout_data.custom when you create the checkout through the API.

Step 3: read it back

  • Polar returns the metadata on the order.
  • Lemon Squeezy returns custom data only in webhooks (meta.custom_data), not when you list orders. Store it from the order_created webhook, or you lose it.

Then group orders by source each month.

Where the hand-built version breaks

  • Visitors who arrive without tags land in one blank row. Tag every link you post.
  • Renewals need the first order's source copied forward.
  • Refunds need a second pass. Lemon Squeezy refunds do not change the order, so you have to look them up separately.
  • Orders from before you added the cookie, or before the webhook existed, have no source.

The open-source version

I build Virling, which does this for both providers. Its script sets a visitor ID and adds it to buy.polar.sh and Lemon Squeezy checkout links on its own (reference_id and checkout[custom][virling_vid]). Virling reads Polar orders with a read-only token, registers the Lemon Squeezy webhook for you, keeps renewals on the channel of the first order, and subtracts refunds. You see revenue and revenue per visitor by channel, referrer, campaign and landing page, and you can connect both providers to one site while you move from one to the other.

It is free to self-host under AGPL-3.0, or hosted. Source: github.com/virling/virling.

The full setup, including sandbox testing, is in the original post: How do I attribute Polar or Lemon Squeezy sales to traffic sources?

📰 Read the original article on Dev.to WebDev

Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.