Install DataPocket on Next.js
Updated
App Router or Pages Router. Client-side navigations are tracked automatically. Sign in and these snippets fill in your site ID and domain. Until then, swap YOUR_SITE_ID and YOUR_DOMAIN with the values from your Install page.
- 1
Proxy through your own domain with two route handlers. They forward only what DataPocket needs (the event, User-Agent and the visitor's IP), never your visitors' cookies; a rewrite in next.config.js would pass the Cookie header along with every event. Create app/dp/js/[file]/route.ts:
tsconst PD = "https://datapocket.vo1d.studio"; export async function GET(_req: Request, { params }: { params: Promise<{ file: string }> }) { const { file } = await params; if (!/^script(\.[a-z]+)?(@\d+\.\d+\.\d+)?\.js$/.test(file)) return new Response(null, { status: 404 }); const r = await fetch(`${PD}/js/${file}`, { next: { revalidate: 3600 } }); return new Response(await r.text(), { status: r.status, headers: { "Content-Type": "application/javascript", "Cache-Control": "public, max-age=3600" }, }); } - 2
And app/dp/api/event/route.ts:
tsconst PD = "https://datapocket.vo1d.studio"; export async function POST(req: Request) { // the visitor's IP, as your host reports it (Vercel, Netlify and most proxies set X-Forwarded-For) const ip = (req.headers.get("x-real-ip") ?? req.headers.get("x-forwarded-for")?.split(",")[0] ?? "").trim(); const r = await fetch(`${PD}/api/event`, { method: "POST", body: await req.text(), headers: { "Content-Type": "text/plain", "User-Agent": req.headers.get("user-agent") ?? "", ...(ip ? { "X-Forwarded-For": ip } : {}), }, cache: "no-store", }); return new Response(await r.text(), { status: r.status, headers: { "Content-Type": "application/json" } }); } - 3
Pages Router only? Route handlers live in app/ and work next to pages/: add the two files above and keep the rest of your app as it is.
- 4
Add the script to your root layout (app/layout.tsx):
tsximport Script from "next/script"; export default function RootLayout({ children }: { children: React.ReactNode }) { return ( <html lang="en"> <head> <Script src="/dp/js/script.js" data-site-id="YOUR_SITE_ID" data-domain="YOUR_DOMAIN" strategy="afterInteractive" /> </head> <body>{children}</body> </html> ); } - 5
Pages Router: put the same <Script> in pages/_app.tsx.
Revenue attribution
- 1
Pass the visitor cookie to Stripe Checkout so each sale is attributed to the channel that brought the buyer:
jsconst session = await stripe.checkout.sessions.create({ // …your line_items, mode, success_url… metadata: { datapocket_visitor_id: req.cookies.dp_vid, datapocket_session_id: req.cookies.dp_sid, }, }); - 2
Using another payment provider? Send payments from your backend (create an API key in Settings):
bashcurl -X POST https://datapocket.vo1d.studio/api/v1/payments \ -H "Authorization: Bearer dpk_…" \ -H "Content-Type: application/json" \ -d '{"amount": 29.99, "currency": "USD", "transaction_id": "order_123", "visitor_id": "<dp_vid cookie>"}'
Related guides
React RouterFramework mode (Remix) and library mode. pushState navigations are tracked automatically.
Vue.jsVue 3 + Vue Router, Nuxt, Vite. History-mode routing is tracked automatically.
JavaScript appsReact, Vue, Svelte or Angular: the script tag with typed calls, plus server-side payments and goals over HTTP.
ExpressServe the script and forward events from your Express server.
- AI crawler trackingSee when ChatGPT, Claude and Perplexity read your pages. One server-side call.
- All install guides25 platforms, code and no-code.