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

Next.js Paddle UTM tracking: pass the visitor ID to Paddle.js checkout

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

A Next.js App Router app opens Paddle checkout in the browser, so that is where the visitor ID gets attached. Load the Virling script once in app/layout.tsx with next/script, then pass customData: { virling_vid: window.virling?.vid?.() } to Paddle.Checkout.open in a client component. If your server creates the transaction instead, a route handler reads the same ID with cookies().

Paddle keeps that ID in the transaction's custom_data, where Virling picks it up through a read-only API key and links the payment to each visit the script logged under it (Paddle docs). The reasons UTM tags never reach Paddle on their own are in Paddle checkout UTM parameters.

Other setups: Next.js with Stripe, Paddle with a Laravel backend, React Paddle UTM tracking for a plain React app and Django Paddle UTM tracking.

Step 1: add the script to the root layout

Use next/script with strategy="afterInteractive" in app/layout.tsx. The root layout does not re-render when the user moves between routes, so the script loads once and still counts client-side navigation. Running your own Virling server? Use its address in src.

// app/layout.tsx
import Script from "next/script";

export default function RootLayout({ children }: { children: React.ReactNode }) {
  return (
    <html lang="en">
      <body>
        {children}
        <Script src="https://app.virling.com/v.js"
          data-site="yourdomain.com" strategy="afterInteractive" />
      </body>
    </html>
  );
}

From then on the browser holds a first-party virling_vid cookie, and window.virling?.vid?.() returns its value.

Step 2: a client component for Paddle.js

Paddle.js and window.virling only exist in the browser, so the button lives in a file marked "use client", while the pricing page that renders it can stay a Server Component. The component loads Paddle.js v2, initializes it with your client-side token and passes the visitor ID as customData:

// app/buy-button.jsx
"use client";
import Script from "next/script";

export default function BuyButton() {
  function buy() {
    Paddle.Checkout.open({
      items: [{ priceId: "pri_...", quantity: 1 }],
      customData: { virling_vid: window.virling?.vid?.() },
    });
  }
  return (
    <>
      <Script src="https://cdn.paddle.com/paddle/v2/paddle.js"
        onLoad={() => Paddle.Initialize({ token: process.env.NEXT_PUBLIC_PADDLE_CLIENT_TOKEN })} />
      <button onClick={buy}>Buy</button>
    </>
  );
}

Render <BuyButton /> on your pricing page. The ID is read on click, after both scripts have loaded, so it is there even when the pricing page is the first page of the visit. The NEXT_PUBLIC_ prefix makes Next.js inline the token into the browser bundle at build time. Paddle's reference for the options is Paddle.Checkout.open.

Or create the transaction in a route handler

If your server creates the transaction through the Paddle API, read the cookie with cookies() from next/headers. Since Next.js 15 it returns a promise, so await it:

// app/api/checkout/route.ts
import { cookies } from "next/headers";

export async function POST() {
  const vid = (await cookies()).get("virling_vid")?.value;
  const res = await fetch("https://api.paddle.com/transactions", {
    method: "POST",
    headers: { authorization: `Bearer ${process.env.PADDLE_API_KEY}`, "content-type": "application/json" },
    body: JSON.stringify({
      items: [{ price_id: "pri_...", quantity: 1 }],
      custom_data: { virling_vid: vid },
    }),
  });
  if (!res.ok) return Response.json({ error: "paddle" }, { status: 502 });
  const { data } = await res.json();
  return Response.json({ transactionId: data.id });
}

Call it from the client component, where a same-origin fetch sends the cookie along, and open the result:

async function buy() {
  const res = await fetch("/api/checkout", { method: "POST" });
  const { transactionId } = await res.json();
  Paddle.Checkout.open({ transactionId });
}

Later charges on the subscription need nothing: Virling files renewals under the channel of the first payment. Credit packs a user buys later keep that channel too, as Paddle analytics for AI apps explains. More is on the Paddle integration page and the Paddle for SaaS page.

Start free trial

Check that it works

  1. Connect a sandbox key that starts with pdl_sdbx_.
  2. In app/buy-button.jsx, call Paddle.Environment.set("sandbox") before Paddle.Initialize, put a sandbox client-side token in NEXT_PUBLIC_PADDLE_CLIENT_TOKEN and use a sandbox price ID. Then build again. If you use the route handler, also send it to https://sandbox-api.paddle.com/transactions, with a sandbox API key in PADDLE_API_KEY.
  3. Open a private window and visit a page that runs the script through a tagged link, such as https://yourdomain.com/?utm_source=test&utm_medium=email.
  4. In the same window, go to the page with <BuyButton />, click Buy and pay with one of Paddle's test cards.
  5. Wait a minute for the next sync, then open the payments list at /app/yourdomain.com/payments. The payment shows the test source and the Email channel of your tagged visit instead of Unattributed.

Use a new email address for each test, because a repeat buyer's payment counts as a renewal. A site has one Paddle connection, and sandbox payments stay in your reports after you replace the key with a live one. If a step fails, see test your setup.

Open the payment to see its journey. If it lists no visitor ID, checkout did not carry virling_vid: check that the page with the button loads the Virling script. Such payments are kept in revenue as Unattributed.

Common mistakes

What the report showsLikely causeFix
Payments from the buy button are UnattributedPaddle.Checkout.open was called without customDataPass virling_vid in customData, as in step 2
Paddle is not defined on clickThe button was clicked before Paddle.js finished loadingKeep the button disabled until onLoad has run, for example with a state flag
Checkout fails to open with a token error in productionNEXT_PUBLIC_PADDLE_CLIENT_TOKEN was set after the build, so the bundle holds undefinedSet it in the build environment and build again (environment variables)
A warning that cookies() should be awaitedNext.js 15 or later, where cookies() returns a promiseUse (await cookies()).get("virling_vid") (cookies)
Payments from the route handler are UnattributedThe handler was called from server code, such as a Server Action or a cron job, so the request carried no browser cookieCall it from the browser, or read cookies() in the Server Action itself
Payments are Unattributed although Paddle holds the IDThe ID is in the custom data of a customer, product or price, which is not readSet it on the transaction
Only some payments are UnattributedThe script was missing on the page, blocked by an ad blocker, or the visitor had not accepted under Until consentKeep the script in the root layout, so every route runs it. Blocked and declined visits stay Unattributed
A second test payment shows the first test's channelSame customer, so it counts as a renewalPay with a new email address

Every other cause is in Unattributed revenue.

Frequently asked questions

How do I pass UTM parameters to Paddle checkout in Next.js?

Hand Paddle the Virling visitor ID, not the tags. In a client component, call Paddle.Checkout.open with customData: { virling_vid: window.virling?.vid?.() }. Every visit's UTM tags are already stored under that ID, so the transaction is linked to all of them.

Where does Virling read the visitor ID on a Paddle transaction?

From virling_vid in the transaction's custom_data. Paddle.js sets custom_data from the customData you pass when you open checkout.

Why does the checkout code need a client component?

Paddle.js and window.virling only exist in the browser, so the code that opens checkout has to run there. Mark the file with "use client" and open checkout from a click handler.

How does a route handler get the visitor ID?

Import cookies from next/headers, read (await cookies()).get("virling_vid")?.value and put it in custom_data when you create the transaction through the Paddle API. A fetch from the browser to the handler sends the cookie along.

Can I read the visitor ID in a Server Component?

You can, with await cookies(), but that renders the page dynamically on every request, and on the first page of a visit the cookie does not exist yet because the script has not run. Reading window.virling?.vid?.() in the click handler avoids both.

Do Paddle renewals need the visitor ID?

No. Virling gives renewals the channel of the subscription's first payment.

See which campaigns bring Paddle revenue

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