virling Pricing Self-host Demo Changelog Agencies GitHub Start free trial
blog / 2026-11-26

SvelteKit LemonSqueezy UTM tracking: the source of each order

By Devendra Variya, co-founder of Virling. Published 2026-11-26.

For SvelteKit LemonSqueezy UTM tracking, add the Virling script to src/app.html, then read the virling_vid cookie in the form action that creates the LemonSqueezy checkout and pass it as custom data. Checkout links on STORE.lemonsqueezy.com need no code: the script adds checkout[custom][virling_vid] to them. Either way, LemonSqueezy returns the visitor ID in its webhook, and Virling joins the order to every visit behind it, with the UTM tags of each.

For what LemonSqueezy does with custom data outside SvelteKit, read the general guide to LemonSqueezy UTM tracking and the LemonSqueezy integration page. Using Stripe instead? See SvelteKit Stripe UTM tracking. On Next.js, see the Next.js version of this post.

One thing to know first: as of November 2026, LemonSqueezy's docs say it no longer accepts new sign-ups and will shut down on March 31, 2027 (their announcement). This post is for stores that already sell through it.

Why LemonSqueezy checkout loses your UTM tags

UTM tags live on the landing URL. By the time someone clicks Buy they have usually moved to another page, and checkout runs on your store's lemonsqueezy.com address, which knows only what you put in the checkout URL or request. You could copy the tags into custom data, but that records only the visit the buyer is on when they pay. A visitor ID keeps earlier visits and untagged ones, such as search and referrals, too.

A made-up example: someone finds you through a newsletter link tagged utm_source=newsletter on Monday, leaves, and comes back on Thursday by typing your address to buy a $29 license. The URL on Thursday has no tags, so copying tags at checkout stores nothing. With the visitor ID, the order is joined to both visits, Monday's newsletter click included.

Step 1: add the script to src/app.html

Paste the tag into src/app.html, before %sveltekit.head%. That file is the shell of every page, and client-side navigation is counted on its own. Use the domain you added in Virling as data-site. If you self-host, replace app.virling.com with your Virling server.

<!-- src/app.html -->
<head>
  <meta charset="utf-8" />
  <script defer src="https://app.virling.com/v.js"
    data-site="yourdomain.com"></script>
  %sveltekit.head%
</head>

The script sets the first-party virling_vid cookie on your domain, records the UTM tags of each visit against it, and window.virling?.vid?.() returns the same ID in the browser.

Step 2a: checkout links, no code

If your Buy button is a link to STORE.lemonsqueezy.com/checkout/buy/VARIANT_ID, there is no server code to write. Keep it as a normal <a href> in a Svelte component on a page that runs the script:

<!-- src/routes/pricing/+page.svelte -->
<a href="https://STORE.lemonsqueezy.com/checkout/buy/VARIANT_ID">Buy now</a>

The script adds checkout[custom][virling_vid] to every link on a lemonsqueezy.com address when the page loads, and again on click, so links rendered after a client-side navigation are covered. It never replaces a value that is already there. This is the same custom data query parameter LemonSqueezy documents for checkout links. A Lemon.js overlay opened from a link with the lemonsqueezy-button class works too, since it is still a link.

If you build the URL in code, for example to pick a variant, or open it with LemonSqueezy.Url.Open, the script never sees a link. Add the ID yourself in the click handler, since window.virling only exists in the browser:

<!-- src/lib/BuyButton.svelte -->
<script>
  function buy() {
    const url = new URL("https://STORE.lemonsqueezy.com/checkout/buy/VARIANT_ID");
    const vid = window.virling?.vid?.();
    if (vid) url.searchParams.set("checkout[custom][virling_vid]", vid);
    location.href = url.href;
  }
</script>

<button onclick={buy}>Buy now</button>

Step 2b: create the checkout in a form action

If your server creates the checkout with POST /v1/checkouts, the browser sends the virling_vid cookie with the form post, and SvelteKit hands it to you on cookies. First declare the API key in src/env.ts, as SvelteKit 3 expects for private variables:

// src/env.ts
import { defineEnvVars } from "@sveltejs/kit/env";

export const variables = defineEnvVars({
  LEMONSQUEEZY_API_KEY: {},
});

Then read the cookie in the action and set it in data.attributes.checkout_data.custom, as LemonSqueezy's reference for create a checkout describes:

// src/routes/pricing/+page.server.ts
import { error, redirect } from "@sveltejs/kit";
import { LEMONSQUEEZY_API_KEY } from "$app/env/private";
import type { Actions } from "./$types";

export const actions = {
  checkout: async ({ cookies }) => {
    const vid = cookies.get("virling_vid");
    const res = await fetch("https://api.lemonsqueezy.com/v1/checkouts", {
      method: "POST",
      headers: {
        accept: "application/vnd.api+json",
        "content-type": "application/vnd.api+json",
        authorization: `Bearer ${LEMONSQUEEZY_API_KEY}`,
      },
      body: JSON.stringify({
        data: {
          type: "checkouts",
          attributes: {
            checkout_data: { custom: vid ? { virling_vid: vid } : {} },
          },
          relationships: {
            store: { data: { type: "stores", id: "STORE_ID" } },
            variant: { data: { type: "variants", id: "VARIANT_ID" } },
          },
        },
      }),
    });
    if (!res.ok) error(502, "Could not create the checkout");
    const { data } = await res.json();
    redirect(303, data.attributes.url, { external: true });
  },
} satisfies Actions;

A plain form on the same page posts to it:

<!-- src/routes/pricing/+page.svelte -->
<form method="POST" action="?/checkout">
  <button type="submit">Buy</button>
</form>

Four details matter here:

  • External redirect. SvelteKit 3 only lets redirect() go to another site when you pass { external: true }. On SvelteKit 2, leave it out, skip src/env.ts and import the key from $env/static/private.
  • No use:enhance. Leave it off this form, so the browser itself follows the redirect to LemonSqueezy.
  • An action, not load. A load function runs on every page view, and in a prerendered or cached page it has no cookie of this visitor. Create the checkout when the buyer presses Buy.
  • Same origin. In production SvelteKit rejects form posts from another origin, so keep the form and the action on one domain.

cookies.get returns undefined when there is no cookie, for example when the script was blocked or the site is in cookieless mode. Never block the sale for it: the action sends empty custom data, the customer pays as usual, and the order counts toward revenue as Unattributed. Do not send your own user ID as virling_vid, since no visit carries it.

Start free trial

Step 3: connect LemonSqueezy to Virling

Create an API key in LemonSqueezy under Settings, API, and paste it into Virling. LemonSqueezy only returns checkout custom data in webhooks, so Virling registers a webhook on each of your stores for order_created and subscription_payment_success and checks each request's X-Signature. There is no webhook to add to your SvelteKit app. The webhook needs VIRLING_BASE_URL to be a public https:// address; without one, LemonSqueezy payments show as Unattributed. The full setup is in the LemonSqueezy docs.

How renewals keep the first source

You pass the visitor ID once, at the first checkout. Subscription invoices with billing reason renewal or updated, and any later order by the same customer, keep the channel of that customer's first order. So a subscriber who first came from a tagged newsletter link stays credited to it every month, without any code on renewal. If the first order is older than Virling's first import, which reads the last 90 days, its renewals are usually Unattributed. The details are in renewals.

Check that it works in test mode

One visit with a UTM tag and one test order run the whole path:

  1. Switch your LemonSqueezy store to test mode, create an API key there and connect it to Virling, so Virling reads test orders and registers its webhook in test mode. Virling must be on a public https:// address.
  2. In your app, use a test mode checkout link. For the form action, put a test mode key in LEMONSQUEEZY_API_KEY too: test and live keys only work on their own side of the store. Deploy it to your domain or a subdomain such as staging.yourdomain.com, because the script does nothing on localhost.
  3. Open a private window and visit a page that runs the script through a tagged link, such as https://staging.yourdomain.com/?utm_source=test&utm_medium=email. Run window.virling.vid() in the browser console and note the ID.
  4. In the same window, press Buy and pay with one of the test cards LemonSqueezy lists.
  5. Wait a minute for the next sync, then open the payments list at /app/yourdomain.com/payments.

The order shows the test source and the Email channel of your tagged visit instead of Unattributed. Open it: Journey starts at the tagged visit and ends with the LemonSqueezy order, and Details lists the ID from your console as Visitor. If there is no visitor, checkout did not carry virling_vid: check that the page with the button loads the script.

Use a new email address for each test, because a later payment by the same customer is a renewal and keeps the first payment's channel. A site has one LemonSqueezy connection, and test orders stay in your reports after you replace the key with a live one. A product copied to live mode gets a new ID and checkout URL, so change your links and VARIANT_ID when you go live. The full checklist is in test your setup, and how the join works from visit to order is in how to track LemonSqueezy revenue by traffic source.

Frequently asked questions

How do I pass UTM parameters to LemonSqueezy checkout in SvelteKit?

Pass the Virling visitor ID instead. In the form action that calls POST /v1/checkouts, read it with cookies.get("virling_vid") and set checkout_data.custom to { virling_vid }. The Virling script records the UTM tags of every visit against that ID, so the order is joined to all of them.

Do LemonSqueezy checkout links in a SvelteKit app need code?

No. Keep the link to STORE.lemonsqueezy.com as a normal link on a page that runs the script. The script adds checkout[custom][virling_vid] to it, unless it is already there, and LemonSqueezy returns it in the order webhook.

Do I need a LemonSqueezy webhook in my SvelteKit app?

Not for Virling. When you connect your API key, Virling registers its own webhook for order_created and subscription_payment_success on each store, which needs a public https address. Your app may still want webhooks for its own work, such as granting access.

Are LemonSqueezy renewals credited to the first source?

Yes. Subscription invoices with billing reason renewal or updated, and any later order by the same customer, keep the channel of that customer's first order. You pass the visitor ID only at the first checkout.

Can I use LemonSqueezy with a static SvelteKit site?

Yes, with checkout links. A site built with adapter-static has no server to run a form action, but links to STORE.lemonsqueezy.com need no server code.

See which channels bring your LemonSqueezy orders

Virling is open source under AGPL. Self-host it free, or use the hosted version with a 14-day free trial. It connects to Stripe, Paddle, Shopify, WooCommerce, Polar, LemonSqueezy, Dodo, Creem and RevenueCat.

Start free trial See the demo