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

SvelteKit Polar UTM tracking with form actions and use:enhance

By Devendra Variya, co-founder of Virling. Published 2026-11-25, updated 2026-11-30.

SvelteKit hands every form action a cookies object, and that is most of what Polar UTM tracking needs. Put the Virling script in src/app.html, read cookies.get("virling_vid") in the +page.server.ts action behind your Buy button, and send it to Polar as checkout metadata. With use:enhance the same form works with and without JavaScript, and each order reaches Virling joined to the visits, and UTM tags, that led to it.

The Polar side does not depend on your framework: checkout links, the sandbox and renewals are covered in Polar UTM tracking and on the Polar integration page. Using Stripe instead? See SvelteKit Stripe UTM tracking. On Next.js, see the Next.js version of this post. On Nuxt, see Nuxt Polar UTM tracking.

Step 1: the tag in src/app.html

Place it before %sveltekit.head%. This file wraps every route, and the script counts page changes made by the SvelteKit router without extra code. Replace yourdomain.com with the site you added in Virling, and app.virling.com with your own server if you self-host.

<!-- 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>

Step 2: declare the Polar token

SvelteKit 3 expects private variables to be declared in src/env.ts and serves them from $app/env/private, which only server code may import. This token needs checkouts:write; Virling gets its own read-only one later.

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

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

On SvelteKit 2, skip this file and import the token from $env/static/private.

Step 3: a form action that opens Polar checkout

The action reads the cookie from its request event, creates the checkout with Polar's TypeScript SDK, @polar-sh/sdk, and redirects to the URL Polar returns:

// src/routes/pricing/+page.server.ts
import { redirect } from "@sveltejs/kit";
import { createPolar } from "@polar-sh/sdk/2026-10";
import { POLAR_ACCESS_TOKEN } from "$app/env/private";
import type { Actions } from "./$types";

const polar = createPolar({ accessToken: POLAR_ACCESS_TOKEN });

export const actions = {
  buy: async ({ cookies }) => {
    const vid = cookies.get("virling_vid");
    const checkout = await polar.checkouts.create({
      products: ["PRODUCT_ID"],
      success_url: "https://yourdomain.com/thanks",
      // leave the key out when there is no cookie; Polar rejects ""
      metadata: vid ? { virling_vid: vid } : {},
    });
    redirect(303, checkout.url, { external: true });
  },
} satisfies Actions;

SvelteKit 3 lets redirect() leave your origin only with { external: true }; SvelteKit 2 has no third argument, so drop it there. With a 0.x release of the Polar SDK, create the client with new Polar({ accessToken }) and write successUrl. The order in which Virling looks for the ID is in the Polar docs.

Start free trial

Form actions and progressive enhancement

The pricing page posts to the named action with action="?/buy". Before the page has hydrated, or with JavaScript off, the browser submits the form itself and follows the 303 to Polar. With use:enhance, SvelteKit submits it through fetch, and because the redirect goes to another origin it falls back to a full page load, so the buyer still lands on Polar checkout. Either way the request comes from your own page, so the cookie is sent.

<!-- src/routes/pricing/+page.svelte -->
<script lang="ts">
  import { enhance } from "$app/forms";
  let pending = $state(false);
</script>

<form method="POST" action="?/buy" use:enhance={() => {
  pending = true;
  return async ({ update }) => {
    await update();
    pending = false;
  };
}}>
  <button disabled={pending}>Buy</button>
</form>

The callback disables the button while the action runs, so a double click does not create two checkouts. Without JavaScript the form behaves the same, minus the disabled state. See SvelteKit, form actions.

Selling from a prerendered page

SvelteKit will not prerender a page that has actions: the build stops with prerender_actions. If your root layout sets prerender = true, either export prerender = false from the pricing page, or keep it prerendered and move the action to a page that is not, such as src/routes/checkout/+page.server.ts. A form on any page can then post to it with action="/checkout?/buy".

Do not read the cookie or create the checkout in a load function. On a prerendered page it ran once at build time, for no visitor, and on other pages it runs on every view, including when SvelteKit preloads a page because the pointer rests on a link.

Other things that break the Buy button

  • adapter-node behind a proxy. SvelteKit rejects form posts that do not come from the app's own origin. If it does not know its public address, set ORIGIN, or the action answers 403.
  • A second domain. The form and the action must share an origin, and the cookie belongs to your registrable domain, so keep checkout on that domain or a subdomain of it.
  • adapter-static. There is no server at run time, so no action runs. Use Polar checkout links, which the script tags in the browser.
  • Testing only with the dev server. npm run dev never prerenders. Run npm run build and npm run preview once, so a prerender_actions error shows up before you deploy.

Connect Polar to Virling

Virling reads orders with its own Polar token, so nothing in your SvelteKit app handles webhooks for it; the scopes it needs are in the Polar docs linked above. For a sandbox run, pass environment: "sandbox" to createPolar with a sandbox POLAR_ACCESS_TOKEN, and follow the sandbox steps in the general guide.

Frequently asked questions

How do I pass UTM parameters to a Polar checkout in SvelteKit?

Pass the visitor ID that the Virling script stores in the virling_vid cookie. Read it with cookies.get("virling_vid") in the form action that calls polar.checkouts.create, and set metadata: { virling_vid }. Virling joins the order to every visit recorded under that ID, with its UTM tags.

Does use:enhance work when the action redirects to Polar?

Yes. When the action calls redirect(303, checkout.url, { external: true }), use:enhance sees a redirect to another origin and does a full page load to Polar checkout. Without JavaScript, the browser follows the 303 itself.

Can the pricing page stay prerendered?

Yes, if the action lives on another page. Put it in a route that is not prerendered and point the form at it, for example action="/checkout?/buy".

Can I use Polar 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 buy.polar.sh need no server code.

See which channels bring your Polar 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