Browse guides
Next.js
Add the snippet to the root layout and credit Checkout Sessions in a Route Handler or Server Action.
3 min read · For companies
For the App Router. Read Install tracking first for what the two pieces do; this page shows where they go in Next.js.
1. The snippet in the root layout
import Script from "next/script";
export default function RootLayout({ children }: { children: React.ReactNode }) {
return (
<html lang="en">
<body>
{children}
<Script src="https://rewards.acme.com/v1/br.js" strategy="afterInteractive" />
</body>
</html>
);
}Copy any data- attributes from Integrations → Tracking & links onto the Script tag, for example data-outbound="acme.app". Client-side navigation is covered without extra code.
2. Credit the checkout
import type Stripe from "stripe";
// betterrewards: copy this file into your app. It has no dependencies.
const CLICK_ID = /^clk_[0-9a-f]{32}$/;
/** A click id stored by the snippet, or null. Pass the br_click cookie's value. */
export function clickId(value: string | null | undefined): string | null {
return value && CLICK_ID.test(value) ? value : null;
}
/** Reads the click id from a raw Cookie header. */
export function clickIdFromCookieHeader(header: string | null | undefined): string | null {
const match = header?.match(/(?:^|;\s*)br_click=([^;]+)/);
return clickId(match?.[1] ? decodeURIComponent(match[1]) : null);
}
/** Credits a Checkout Session, and the subscription it starts, to the referring partner. */
export function withReferral(
params: Stripe.Checkout.SessionCreateParams,
id: string | null,
): Stripe.Checkout.SessionCreateParams {
if (!id) return params;
return {
...params,
client_reference_id: params.client_reference_id ?? id,
metadata: { ...params.metadata, br_click_id: id },
...(params.mode === "subscription" && {
subscription_data: {
...params.subscription_data,
metadata: { ...params.subscription_data?.metadata, br_click_id: id },
},
}),
};
}
/** Metadata for stripe.customers.create at signup, so later payments need no cookie. */
export function referralMetadata(id: string | null): Record<string, string> {
return id ? { br_click_id: id } : {};
}In a Route Handler
import { cookies } from "next/headers";
import { NextResponse } from "next/server";
import Stripe from "stripe";
import { clickId, withReferral } from "@/lib/betterrewards";
const stripe = new Stripe(process.env.STRIPE_SECRET_KEY!);
export async function POST() {
const id = clickId((await cookies()).get("br_click")?.value);
const session = await stripe.checkout.sessions.create(
withReferral({
mode: "subscription",
line_items: [{ price: "price_123", quantity: 1 }],
success_url: "https://app.acme.com/welcome",
cancel_url: "https://www.acme.com/pricing",
}, id),
);
return NextResponse.json({ url: session.url });
}Or in a Server Action
"use server";
import { cookies } from "next/headers";
import { redirect } from "next/navigation";
import { clickId, withReferral } from "@/lib/betterrewards";
import { stripe } from "@/lib/stripe";
export async function startCheckout() {
const id = clickId((await cookies()).get("br_click")?.value);
const session = await stripe.checkout.sessions.create(withReferral({
mode: "subscription",
line_items: [{ price: "price_123", quantity: 1 }],
success_url: "https://app.acme.com/welcome",
cancel_url: "https://www.acme.com/pricing",
}, id));
redirect(session.url!);
}At signup
const id = clickId((await cookies()).get("br_click")?.value);
await stripe.customers.create({ email, metadata: referralMetadata(id) });Pages Router: read req.cookies.br_click in an API route or getServerSideProps, and add the snippet with next/script in pages/_app.tsx.
Create Stripe customers at signup? Add metadata: referralMetadata(id) to stripe.customers.create as well. Every later payment of that customer is then credited without the cookie, even weeks later or after Safari has cleared it.
Then check it in your instance under Integrations → Tracking & links: Open test page proves the snippet runs on your site, and a test payment followed by I've paid, check now proves the click reaches Stripe. Nobody is credited for the test.