Install on Next.js
View as MarkdownAdd Reveata to a Next.js app with the script tag or the @reveata/next component.
On this page
There are two ways to add Reveata to a Next.js app. Both count every page view, client-side navigation included; pick one.
Option 1: the script tag
No package to install. Add the snippet to the root layout with next/script:
// 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://reveata.com/js/script.js"
data-website-id="ws_your_tracking_id"
strategy="afterInteractive"
/>
</body>
</html>
);
}Goals then use window.reveata('goal', 'signup') or an HTML attribute: see custom goals. The snippet options are in Install the script.
Option 2: the @reveata/next package
A typed component and helpers for goals, identify calls and payments. Install the package:
npm install @reveata/nextAdd the component once, in the root layout:
// app/layout.tsx
import { Reveata } from '@reveata/next';
export default function RootLayout({ children }: { children: React.ReactNode }) {
return (
<html lang="en">
<body>
{children}
<Reveata websiteId="ws_your_tracking_id" host="https://reveata.com" />
</body>
</html>
);
}Client-side navigation is tracked by the script, so every page view is counted. Options: cookieless, domains, excludePaths, hash, api, respectDnt, and enabled (for example enabled={process.env.NODE_ENV === 'production'}).
With the Pages Router, put <Reveata /> in pages/_app.tsx.
Goals and identify
'use client';
import { identify, trackGoal } from '@reveata/next/client';
<button onClick={() => trackGoal('signup', { plan: 'pro' })}>Start free trial</button>;
identify({ userId: user.id, email: user.email });The same module has trackPayment({ email }) for a thank-you page (thank-you page payments), resetVisitor() for log out, and decorateUrl(url) for checkout and cross-domain links you open from code. See the JavaScript API.
Broken links (404)
Render <ReveataNotFound /> in app/not-found.tsx so missing pages appear under Broken links (404) with the page that linked to them:
import { ReveataNotFound } from '@reveata/next';
export default function NotFound() {
return (
<>
<ReveataNotFound />
<h1>Page not found</h1>
</>
);
}File downloads, form submissions and web vitals are opt-in: <Reveata … extras={{ downloads: true, forms: true, vitals: true }} />. See custom goals.
Link payments to visits
On the server, read the visitor id from the Reveata cookie and pass it to your checkout:
import { cookies } from 'next/headers';
import { clientReference, readTrackerIds } from '@reveata/next/server';
const ids = readTrackerIds(await cookies());
await stripe.checkout.sessions.create({
// …
client_reference_id: clientReference(ids) ?? undefined,
});ReveataApi from @reveata/next/server sends events, identify calls and payments with a website API key. See Payments API.