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

Nuxt Polar UTM tracking: a server route, SSR and $fetch

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

In a Nuxt app, the visitor ID that ties a Polar order to its traffic source is a cookie, and the place to read it is a Nitro route under server/api. Load the Virling script from nuxt.config.ts, call getCookie(event, "virling_vid") in the POST handler that creates the checkout, and put the value in metadata. Most of this post is about where that read goes wrong in Nuxt: server rendering, cached routes and $fetch.

How Polar stores the ID, checkout links, the sandbox and how renewals are credited work the same in every framework, so they live in Polar UTM tracking. This post covers Nuxt 4. Using Stripe instead? See Nuxt Stripe UTM tracking. On Svelte, see SvelteKit Polar UTM tracking.

Step 1: add the script in nuxt.config.ts

An entry in app.head is rendered into the HTML of every page, and later route changes are picked up through history.pushState. Set data-site to the domain you added in Virling; self-hosters point src at their own server.

// nuxt.config.ts
export default defineNuxtConfig({
  app: {
    head: {
      script: [
        { src: "https://app.virling.com/v.js", defer: true,
          "data-site": "yourdomain.com" },
      ],
    },
  },
  runtimeConfig: {
    polarAccessToken: "", // set NUXT_POLAR_ACCESS_TOKEN
  },
});

The runtimeConfig key keeps the Polar token on the server, and Nuxt fills it from NUXT_POLAR_ACCESS_TOKEN at run time (source: Nuxt, runtime config). A built server does not read .env, so set the variable on your host.

Step 2: a Nitro route that creates the checkout

Install @polar-sh/sdk and give the route a token with the checkouts:write scope. Naming the file checkout.post.ts makes Nitro answer only POST, which matters below:

// server/api/checkout.post.ts
import { createPolar } from "@polar-sh/sdk/2026-10";

export default defineEventHandler(async (event) => {
  const { polarAccessToken } = useRuntimeConfig(event);
  const polar = createPolar({ accessToken: polarAccessToken });
  const vid = getCookie(event, "virling_vid");
  const checkout = await polar.checkouts.create({
    products: ["PRODUCT_ID"],
    success_url: "https://yourdomain.com/thanks",
    metadata: vid ? { virling_vid: vid } : {},
  });
  return sendRedirect(event, checkout.url, 303);
});

Submit a form to it from any page. The request goes to your own origin, so it carries the cookie, and the 303 takes the buyer on to Polar:

<!-- app/pages/pricing.vue -->
<template>
  <form method="POST" action="/api/checkout">
    <button type="submit">Buy</button>
  </form>
</template>

The 2026-10 import path pins the Polar API version (source: Polar, TypeScript SDK). On 0.x releases, use new Polar({ accessToken }) from @polar-sh/sdk and successUrl. When the cookie is missing the route sends no key at all, because Polar rejects a metadata value of "". If you sell through buy.polar.sh checkout links, you can skip this step: a plain <a href> in a page component is tagged by the script in the browser.

Polar also ships a Nuxt module. Its checkout handler takes metadata from the query string of a GET request, so with it you would have to build that URL with the ID yourself.

Start free trial

SSR, prerendering and client navigation

Nuxt can render a page on the server, in the browser, at build time or from a cache. The script always runs in the browser, so tracking is the same in each mode. What changes is whether code that runs while a page renders can see this visitor's cookie, and often it cannot. See Nuxt, rendering modes.

PageWhat happensHow to pass the ID
Server rendered, the defaultThe first visit is rendered before the script has set the cookieRead it in the POST route
ssr: falseThe browser renders the page, and server routes still runRead it in the POST route
Prerendered with routeRulesThe HTML is made once, at build time, with no visitorPost a form from the page to the route
Cached with swr or isrOne copy of the HTML serves many visitorsPost a form to the route, and keep the route out of the cached patterns
Whole app built with nuxt generateNo server is included in the outputUse checkout links

So read the cookie in the request that creates the checkout, never while a page is rendered.

Buying from a button with $fetch

If the Buy button should stay inside your Vue component instead of submitting a form, call the route with $fetch from a click handler. In the browser, $fetch to your own origin sends the cookie. A fetch follows a 303 by itself, though, so the page would never leave your site. Change the last line of the route to return { url: checkout.url }; and navigate from the component:

<!-- app/pages/pricing.vue -->
<script setup lang="ts">
async function buy() {
  const { url } = await $fetch("/api/checkout", { method: "POST" });
  await navigateTo(url, { external: true });
}
</script>

<template>
  <button type="button" @click="buy">Buy</button>
</template>

Keep the call in the handler. Placed directly in setup or wrapped in useFetch, it also runs during server rendering on every page view, creates checkouts nobody asked for, and on a first visit there is no cookie yet. navigateTo needs external: true to leave for another site (source: Nuxt, navigateTo).

Connect Polar and check one order

Virling needs a second Polar token with read scopes only, listed in the Polar docs; your Nuxt app needs no webhook for it. For a sandbox test, set NUXT_POLAR_ACCESS_TOKEN to a sandbox token, pass environment: "sandbox" to createPolar, and follow the steps in the general guide on a deployed build.

Symptoms, causes and fixes

Open an Unattributed payment and look for Visitor under Details. If there is none, checkout did not carry the ID.

SymptomCauseFix
No visitor ID, though the browser has the cookieCheckout is a GET route under a swr or cache rule, and Nitro strips cookies before a cached handler runsUse checkout.post.ts. Only GET and HEAD are cached
No visitor ID when $fetch runs in setupDuring server rendering, $fetch does not pass on the browser's cookiesUse the form, or call it from a click handler
The button does nothing, and the console shows a CORS error$fetch followed the route's redirect to PolarReturn { url } from the route and call navigateTo
404 on /api/checkout after deployThe site was built with nuxt generateDeploy with nuxt build, or use checkout links
404 on /api/checkout from a linkA link sends GET, but .post.ts answers only POSTSubmit a form to it

Some orders stay Unattributed with correct code, when the script was blocked or the site is in cookieless mode or waiting for consent. See Unattributed revenue.

Frequently asked questions

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

Send the Virling visitor ID, not the tags. In a server/api route that calls polar.checkouts.create, read it with getCookie(event, "virling_vid") and set it in metadata as virling_vid. Virling has the UTM tags of every visit stored against that ID.

Why is the visitor ID missing when I call $fetch in setup?

Code in setup also runs during server rendering, where $fetch does not forward the browser's cookies, and a first visit has no cookie yet. Call the route from a click handler or a form instead.

Does this work with nuxt generate?

Only with checkout links. A site built with nuxt generate includes no server, so a server/api route cannot run there. To create checkouts in a route, deploy with nuxt build.

Can I use Polar's Nuxt module?

Yes, but its checkout handler reads metadata from the query string of a GET request, so you have to add the visitor ID to that URL yourself. A POST route of your own is simpler and cannot be cached by a route rule.

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