Next.js

Install Partnersify tracking on Next.js, pixel setup and reading the affiliate cookie in server-side Checkout Sessions.

Next.js merchants typically create Stripe Checkout Sessions on the server, which means you need to read the affiliate cookie and forward it to Stripe. Here's the full setup.

Step 1: Add the pixel

Add the pixel to your root layout so it loads on every page.

App Router (app/layout.tsx)

import Script from 'next/script';

export default function RootLayout({ children }) {
  return (
    <html>
      <head>
        <Script id="partnersify-init" strategy="beforeInteractive">{`
          window.partnersify = window.partnersify || function() { (window.partnersify.q = window.partnersify.q || []).push(arguments); };
        `}</Script>
        <Script
          src="https://partnersify.com/partner.js"
          data-partnersify="YOUR_MERCHANT_ID"
          strategy="afterInteractive"
        />
      </head>
      <body>{children}</body>
    </html>
  );
}

Replace YOUR_MERCHANT_ID with the ID from Setup → Install tracking.

Pages Router (pages/_app.tsx)

import Script from 'next/script';

export default function App({ Component, pageProps }) {
  return (
    <>
      <Script id="partnersify-init" strategy="beforeInteractive">{`
        window.partnersify = window.partnersify || function() { (window.partnersify.q = window.partnersify.q || []).push(arguments); };
      `}</Script>
      <Script
        src="https://partnersify.com/partner.js"
        data-partnersify="YOUR_MERCHANT_ID"
        strategy="afterInteractive"
      />
      <Component {...pageProps} />
    </>
  );
}

When creating a Checkout Session in a Route Handler or API route, read the psfy_partner cookie and attach it to metadata.

App Router Route Handler

import { cookies } from 'next/headers';
import Stripe from 'stripe';

const stripe = new Stripe(process.env.STRIPE_SECRET_KEY!);

export async function POST() {
  const cookieStore = await cookies();
  const partner = cookieStore.get('psfy_partner')?.value ?? null;

  const session = await stripe.checkout.sessions.create({
    // ... your line items, success_url, etc.
    metadata: {
      psfy_partner: partner,
    },
  });

  return Response.json({ url: session.url });
}

Pages Router API Route

import type { NextApiRequest, NextApiResponse } from 'next';
import Stripe from 'stripe';

const stripe = new Stripe(process.env.STRIPE_SECRET_KEY!);

export default async function handler(req: NextApiRequest, res: NextApiResponse) {
  const partner = req.cookies['psfy_partner'] ?? null;

  const session = await stripe.checkout.sessions.create({
    // ... your line items, success_url, etc.
    metadata: {
      psfy_partner: partner,
    },
  });

  res.json({ url: session.url });
}

That's it. Partnersify reads metadata.psfy_partner from the webhook when the customer pays and attributes the sale to the right affiliate.

Step 3: Test it

  1. Sign up as an affiliate on your own portal
  2. Visit your site via the affiliate referral link (this sets the cookie)
  3. Run the Test Pixel check at Setup → Install tracking and confirm it's green
  4. Complete a test Stripe checkout using a test card
  5. Check Sales. The sale should appear with the affiliate attributed within 30 seconds

If your Next.js site links to Stripe Payment Links rather than server-created Checkout Sessions, the pixel handles everything automatically. No cookie forwarding code is needed.

Updated