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

Next.js Creem UTM tracking: pass the visit into checkout

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

Creem only tells Virling which visitor paid if the checkout carries that visitor in its metadata, and it only hands that metadata over in a webhook. So a Next.js setup has two halves: your code puts virling_vid into the checkout, from a route handler, a server action or a hosted product link, and Virling's own checkout.completed webhook reads it back when the buyer pays.

The reason for the ID: the buyer pays on Creem's site, so without it the sale shows up as Direct. The Virling script stores the UTM tags of each visit against a visitor ID, and Virling credits the payment to the visits behind that ID. The full reference is in the Creem docs; the framework-free version is Creem UTM tracking, and the Dodo equivalent of this post is Next.js Dodo Payments UTM tracking.

The script tag

Add v.js once with next/script in the root layout (or pages/_app.tsx on the Pages Router). It sets a first-party virling_vid cookie, and since the cookie belongs to your domain, your route handlers and server actions receive it on every request from the browser.

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

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

Checkout metadata from a route handler

No SDK is needed: one POST /v1/checkouts with the x-api-key header returns a checkout_url. Keep the API base in an environment variable, because test mode lives on a separate host:

// app/api/checkout/route.ts
import { NextRequest, NextResponse } from "next/server";

const CREEM_API = process.env.CREEM_API_URL ?? "https://api.creem.io";

export async function POST(req: NextRequest) {
  const vid = req.cookies.get("virling_vid")?.value;
  const res = await fetch(`${CREEM_API}/v1/checkouts`, {
    method: "POST",
    headers: { "x-api-key": process.env.CREEM_API_KEY!, "content-type": "application/json" },
    body: JSON.stringify({
      product_id: "prod_...",
      success_url: "https://yourdomain.com/thanks",
      metadata: vid ? { virling_vid: vid } : {},
    }),
  });
  if (!res.ok) return NextResponse.json(await res.json(), { status: 502 });
  const { checkout_url } = await res.json();
  return NextResponse.redirect(checkout_url, 303);
}

Point a <form action="/api/checkout" method="POST"> at it. The res.ok check matters with Creem: an error response has no checkout_url, and redirecting to undefined hides the real message. On the Pages Router, the cookie is req.cookies.virling_vid in an API route.

Or from a server action

If the Buy button lives in a server component, a server action saves you the API route. Read the cookie with cookies(), which is async in current Next.js, and call redirect last:

// app/actions.ts
"use server";
import { cookies } from "next/headers";
import { redirect } from "next/navigation";

const CREEM_API = process.env.CREEM_API_URL ?? "https://api.creem.io";

export async function buy(formData: FormData) {
  const vid = (await cookies()).get("virling_vid")?.value;
  const res = await fetch(`${CREEM_API}/v1/checkouts`, {
    method: "POST",
    headers: { "x-api-key": process.env.CREEM_API_KEY!, "content-type": "application/json" },
    body: JSON.stringify({
      product_id: formData.get("product_id"),
      success_url: "https://yourdomain.com/thanks",
      metadata: vid ? { virling_vid: vid } : {},
    }),
  });
  if (!res.ok) throw new Error(`Creem ${res.status}`);
  redirect((await res.json()).checkout_url);
}

Use it as <form action={buy}> with a hidden product_id input, so one action serves every plan. redirect throws to do its job, so keep it out of any try block. If the app sells AI credits, the same action can serve every credit pack, and Creem attribution for AI apps explains how packs and top-ups are credited to the channel that brought the user.

Hosted product links

The Share button on a Creem product gives a link like https://creem.io/payment/prod_..., and Creem accepts metadata[key] query parameters on it, passing them on to webhooks. Unlike some providers' links, the Virling script does not rewrite Creem links on its own, so add the ID in a client component when the buyer clicks:

// app/creem-link.jsx
"use client";

export default function CreemLink({ href, children }) {
  function go(e) {
    const vid = window.virling?.vid?.();
    if (!vid) return;
    e.preventDefault();
    const url = new URL(href);
    url.searchParams.set("metadata[virling_vid]", vid);
    window.location.assign(url.href);
  }
  return <a href={href} onClick={go}>{children}</a>;
}

A bare product link, without this component, reaches Creem with no visitor ID and its payment is Unattributed. If you prefer to keep the API key out of the picture entirely, this is the route; otherwise the route handler gives you more control over the checkout.

The Creem webhook Virling needs, and why

Creem returns checkout metadata only in webhooks; the transactions Virling lists every minute do not include it. So when you connect Creem, Virling registers one webhook for checkout.completed at https://<your Virling>/api/webhooks/creem/<site ID>, keeps its signing secret encrypted, and checks each delivery's creem-signature header. It looks for virling_vid in the checkout's metadata, then the subscription's, then the customer's.

Your Next.js app does not handle this webhook, and any webhook you already run for fulfilment stays as it is. Two things are needed on Virling's side: the key must have the webhooks:write scope next to transactions:read (and subscriptions:read for Retention), and Virling must be reachable over https. On plain http, payments still import but stay Unattributed. The setup is on the Creem integration page.

Start free

Creem test mode

Creem's Test Mode toggle sits at the bottom of the dashboard sidebar, and test data is kept apart from production, with its own keys and products. Test requests go to https://test-api.creem.io, so set CREEM_API_URL to that in your preview or staging environment along with a test key and a test product ID.

On the Virling side, a key that starts with creem_test_ is tried against the test API first, and the webhook is registered in test mode. Connect it, then:

  1. Deploy to a host other than localhost, where the script does not run, such as staging.yourdomain.com.
  2. Open https://staging.yourdomain.com/?utm_source=test&utm_medium=email in a private window and note window.virling.vid().
  3. Buy through the route handler or server action and pay with Creem's test card 4111 1111 1111 1111, any future expiry and any CVC.
  4. After about a minute, the payment at /app/yourdomain.com/payments shows the test source, the Email channel, and your ID as Visitor.

A second payment from the same customer counts as a renewal with the first one's channel, so change the email between tests. Test payments stay in the reports after you switch to a live key, since a site has one Creem connection. More checks: test your setup and how to track Creem revenue by traffic source.

Common mistakes

An Unattributed Creem payment with no Visitor under Details means the ID never arrived. Usually it is one of these.

SymptomCauseFix
Every Creem payment is Unattributed, though checkouts carry the IDNo webhook: Virling was on plain http when you connected, or the key lacked webhooks:writeSet VIRLING_BASE_URL to an https address, give the key the scope, then use Replace key
Creem answers 403A test key is sent to api.creem.io, or a live key to the test API; each key works in its own environmentSet CREEM_API_URL per environment
The redirect goes to /undefinedCreem returned an error and the code read checkout_url anywayCheck res.ok and log the error body
Payments from a product link are UnattributedThe bare Share link has no metadata[virling_vid]Wrap it in the client component above, or use the route handler
Payments through <CreemCheckout> are UnattributedThe component builds its link from props and never reads the cookieReplace it with the route handler or server action
cookies(...).get is not a function in the actioncookies() must be awaited since Next.js 16Write (await cookies()).get("virling_vid")
The action runs but the buyer never leavesredirect sits in a try whose catch eats itCall it after the try block
Refunds show up hours lateVirling rereads Creem transactions from the last 60 days every 6 hoursNothing to fix; wait for the next pass

Anything else is covered in Unattributed revenue. A fix does not change older payments, so make a new one to confirm it.

Frequently asked questions

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

Pass the Virling visitor ID instead. Read the virling_vid cookie in the route handler or server action that creates the checkout and set metadata: { virling_vid } in POST /v1/checkouts. The script records the UTM tags of every visit against that ID, so the payment is joined to all of them.

Do hosted Creem product links get the visitor ID by themselves?

No. The Virling script does not rewrite Creem links. Add metadata[virling_vid] to the link in a client component on click, or send the Buy button to a route handler or server action that creates the checkout.

Why can I not see virling_vid on my Creem transactions?

Creem returns checkout metadata only in webhooks, not on transactions. Virling adds one checkout.completed webhook when you connect, so it needs to be reachable over https.

Does my Next.js app need to handle the Creem webhook for Virling?

No. Virling registers and verifies its own checkout.completed webhook, pointed at your Virling server. Webhooks your app already handles are not affected.

Which scopes does the Creem API key need?

transactions:read to import payments, webhooks:write so Virling can add its webhook, and subscriptions:read so Retention can tell who still pays. No other scopes are needed.

Does Creem test mode work with Virling?

Yes. Connect a key that starts with creem_test_ and point your Next.js code at https://test-api.creem.io. Virling reads test transactions and adds its webhook in test mode. Test payments stay in your reports after you replace the key with a live one.

See which campaigns bring Creem payments

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 See the demo