GuidesWeb

Next.js analytics, cookieless and EU-hosted event tracking

Cookieless analytics for Next.js, with event data stored in Germany.

Privytics tracks page views and custom events in Next.js apps through the @openpanel/nextjs SDK. You add one component to your root layout, point it at the Privytics ingestion API, and client-side route changes start registering as screen views. Setup is a few lines, and nothing is written to the visitor's device.

Why Next.js teams use Privytics

App Router navigations are counted

A Next.js app server-renders once and then navigates on the client. With trackScreenViews enabled, the SDK listens for those soft navigations, so every route change registers instead of only the first page load.

One package for client and server

@openpanel/nextjs exports the OpenPanelComponent and useOpenPanel hook for the browser, plus the OpenPanel class for Route Handlers and Server Actions. Client and server events land in the same project.

Survives serverless function shutdown

On Vercel a function can return before an in-flight request resolves, which silently drops events. Wrapping the call in waitUntil keeps the function alive until the event is sent.

No consent state to thread through layouts

The default setup sets no cookies and stores nothing on the device, so there is no consent flag to pass down your layout tree or gate the component on. You remain the data controller and decide what your own lawful basis requires.

Set it up

About five minutes, on the free plan, without a credit card.

  1. Install the SDK

    pnpm install @openpanel/nextjs
  2. Add the component to your root layout

    apiUrl points the SDK at Privytics instead of its default host, and is the one prop you must set. scriptUrl is optional: the component injects the tracker script tag for you and falls back to OpenPanel's CDN when you leave it out, so set it if you want the script served from Privytics too. This prop only exists on the Next.js component.

    // app/layout.tsx
    import { OpenPanelComponent } from '@openpanel/nextjs';
    
    export default function RootLayout({ children }) {
      return (
        <>
          <OpenPanelComponent
            clientId="YOUR_CLIENT_ID"
            apiUrl="https://api.privytics.com"
            scriptUrl="https://privytics.com/op1.js"
            trackScreenViews={true}
          />
          {children}
        </>
      );
    }
  3. Track a custom event in a client component

    useOpenPanel is a hook, so the component needs the 'use client' directive.

    'use client';
    
    import { useOpenPanel } from '@openpanel/nextjs';
    
    export function UpgradeButton() {
      const op = useOpenPanel();
    
      return (
        <button
          type="button"
          onClick={() => op.track('upgrade_clicked', { plan: 'pro' })}
        >
          Upgrade
        </button>
      );
    }
  4. Track events from the server

    Server-side calls authenticate with a client secret, so keep this instance out of client components. On Vercel, waitUntil stops the function from closing before the event is sent.

    // utils/op.ts
    import { OpenPanel } from '@openpanel/nextjs';
    
    export const op = new OpenPanel({
      clientId: 'YOUR_CLIENT_ID',
      clientSecret: 'YOUR_CLIENT_SECRET',
      apiUrl: 'https://api.privytics.com',
    });
    
    // app/api/checkout/route.ts
    import { waitUntil } from '@vercel/functions';
    import { op } from '@/utils/op';
    
    export function GET() {
      waitUntil(op.track('checkout_started', { plan: 'pro' }));
      return new Response('ok');
    }

Privytics runs the open-source OpenPanel engine, so the upstream Next.js SDK reference applies here too. Point apiUrl at Privytics and the data lands in your Privytics project.

Questions

Why is the package called @openpanel/nextjs and not @privytics/nextjs?

Privytics runs on the open-source OpenPanel engine, and the SDKs are OpenPanel's packages published under the @openpanel npm scope. You install the real package and point apiUrl at https://api.privytics.com. Privytics is independent and is not affiliated with or endorsed by OpenPanel.

Does it work with the Next.js App Router?

Yes. You add OpenPanelComponent to app/layout.tsx and it tracks client-side route changes when trackScreenViews is set. The package also supports the pages directory.

Do I need a cookie banner for this?

The default setup writes no cookies and stores nothing on the visitor's device. The anonymous visitor id is a salted hash of IP, user agent and project id, and the salt rotates daily. You are still the data controller, so confirm what your own lawful basis and local rules require.

How do I track events from Server Actions or Route Handlers?

Import the OpenPanel class from @openpanel/nextjs and create an instance with your client id, client secret and apiUrl. Call op.track from any server context, and wrap it in waitUntil on serverless platforms so the function does not exit first.

Can I link events to my own user ids?

Yes, via identify with your own profileId, which gives cross-session user profiles. That is opt-in and goes beyond the cookieless default, so you need your own lawful basis for it.