Skip to content
All articles

Product guides

Adding social proof to a Next.js app

How to install Provedly purchase notifications and a live visitor counter in a Next.js app, handle route changes, and pass the visitor id to Stripe.

Provedly team··6 min read

Provedly runs in a Next.js app with one script tag, loaded once from your root layout. The widget renders inside a Shadow DOM, so it does not touch your Tailwind or CSS Modules, and the loader is idempotent across client-side route changes. The only Next-specific work is deciding where to put the tag and, if you want attribution, forwarding the visitor id to your checkout.

The one-line install

Create a widget in the dashboard, copy the token, and add the script to your root layout. With the App Router, next/script is the clean way to do it.

// 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://provedly.app/w/widget.js"
                    data-token="pp_your_token"
                    strategy="afterInteractive"
                />
            </body>
        </html>
    );
}

afterInteractive loads the script after hydration, which is the right trade-off for a non-critical element. Do not use beforeInteractive; nothing about a purchase toast needs to block the page. The bundle is about 6 KB gzipped and the loader fetches the widget config from GET /api/w/{token}/config, which returns settings and up to 30 recent records.

If you still use the Pages Router, put the same <Script> in pages/_app.tsx. The behaviour is identical.

Route changes and the SPA problem

Toasts are governed by per-session caps: by default a 3-second start delay, 6 seconds on screen, a 30-second gap, and at most 5 notifications per session. Those caps are tracked by the widget, not by the page, so a visitor who clicks through five product pages does not get five fresh cycles. That is intentional. A notification that fires again on every navigation is the fastest way to be dismissed as spam.

The loader checks for an existing instance before it initialises, so even if your layout remounts the script tag it will not double-render. If you prefer to mount it from a client component, the documented pattern looks like this:

'use client';
import { useEffect } from 'react';

export function ProofWidget() {
    useEffect(() => {
        if (document.querySelector('script[data-token="pp_your_token"]'))
            return;
        const s = document.createElement('script');
        s.src = 'https://provedly.app/w/widget.js';
        s.dataset.token = 'pp_your_token';
        s.async = true;
        document.body.appendChild(s);
    }, []);
    return null;
}

Render <ProofWidget /> once, in the root layout. URL targeting (all pages, only these, all except) is configured in the dashboard, so you do not need conditional rendering in React to keep the widget off, say, /account.

Styling: what you do and do not control

Because the widget lives in a Shadow DOM, your global CSS cannot reach it and its CSS cannot reach you. That rules out the usual class-name collisions and z-index surprises. You choose the look in the dashboard instead: one of five themes (Clean white, Deep ocean, Midnight, Electric violet, Sunset blaze), the name format, the location format and the position. Read Shadow DOM: why the widget cannot break your CSS if you want the details.

If you run a strict Content Security Policy, allow https://provedly.app for script-src and connect-src. The widget makes requests to the config, track and presence endpoints on that origin and nowhere else.

Making purchases "Verified"

A toast that says "Verified purchase" is a toast that arrived through a signed webhook. For a Next.js app that charges through Stripe Checkout, that means registering https://provedly.app/api/hooks/stripe/{token} as a webhook endpoint for checkout.session.completed and pasting the signing secret into the widget's Data tab. The full walkthrough is in Stripe webhooks for verified purchases.

If you charge through your own backend, or through a provider without a Provedly webhook, post records from a Route Handler or server action:

// app/api/orders/route.ts, after payment is confirmed
await fetch('https://provedly.app/api/v1/widgets/pp_your_token/records', {
    method: 'POST',
    headers: {
        Authorization: 'Bearer ' + process.env.PROVEDLY_API_KEY,
        'Content-Type': 'application/json',
    },
    body: JSON.stringify({
        name: order.customerName,
        city: order.shipping.city,
        country: order.shipping.countryCode,
        product_name: order.lines[0].title,
        price: order.totalFormatted,
        external_id: order.id,
        visitor: order.provedlyVisitor,
    }),
});

Records posted this way are shown but labelled "Imported", never "Verified". That is the honesty rule: only a cryptographically signed event from the payment provider earns the mark. See Using the Records API for purchases from your own backend.

Attribution: forward the visitor id

The dashboard reports proof-assisted orders, meaning a buyer who saw a notification before buying, matched by visitor id inside a 30-day window. For the match to happen, the purchase has to carry the id. On the client, read it with window.Provedly?.visitorId() and send it to your checkout code. With Stripe, pass it as metadata.provedly_visitor when you create the Checkout Session. With the Records API, send it in the visitor field.

Step Where What to pass
Read id client component at checkout window.Provedly?.visitorId()
Stripe Checkout server, session creation metadata: { provedly_visitor: id }
Records API server, after payment "visitor": id

No id, no attribution. The dashboard never estimates.

Checklist before you ship

  • Script in the root layout with strategy="afterInteractive".
  • Production domain added under allowed domains on the site; add preview and staging hosts explicitly, because *.vercel.app previews will not match your production domain.
  • CSP updated if you have one.
  • Webhook registered (Stripe) or Records API wired (custom backend).
  • Visitor id forwarded at checkout.

Frequently asked questions

Does the widget work with React Server Components?

Yes. The script tag is plain HTML output from the layout; it does not need a client boundary. The optional ProofWidget component is a client component only because it uses useEffect.

Will the toast cause layout shift?

No. The widget is positioned fixed inside its own shadow root and does not occupy space in your document flow, so it does not contribute to CLS.

Can I show it only on marketing pages and not inside the app?

Yes. Use URL targeting in the dashboard ("all except" with /app/*, or "only these" with your marketing paths) instead of conditional rendering.

Start on the Free plan, which includes verified purchases and 2,000 impressions a month: create an account.

next.jsinstallationreact

Get new articles by email

Honest social proof, verified purchases and what actually moves conversion. A few emails a month at most; leave with one click.

We email you once to confirm. Your address is used only for this newsletter; see the privacy policy.

Show proof you can stand behind.

Free plan, one site, 2,000 impressions a month, verified purchases included. The badge on the free plan links to your verification page, which is the point.