viakite
Dashboard

SDKs/Frameworks/Next.js

Next.js

App Router and Pages Router

In Next.js, add the <Analytics /> component from @viakite/sdk/react once, in your root layout or custom App. It starts tracking in the browser and counts every route change as a page view. It works with the App Router and the Pages Router, in development and in a production build.

Install

terminal
1npm install @viakite/sdk
terminal
1pnpm add @viakite/sdk
terminal
1yarn add @viakite/sdk

React 18 or later is needed; your Next.js app already has it.

App Router

Add it to the root layout, app/layout.tsx:

app/layout.tsx
1import { Analytics } from "@viakite/sdk/react";
2
3export default function RootLayout({ children }: { children: React.ReactNode }) {
4 return (
5 <html lang="en">
6 <body>
7 {children}
8 <Analytics project="pk_live_us_…" />
9 </body>
10 </html>
11 );
12}

<Analytics /> is a client component (the package marks it "use client"), so your layout can stay a server component. Don't add it to nested layouts or pages: once in the root layout covers every route.

Pages Router

Add it to the custom App, pages/_app.tsx:

pages/_app.tsx
1import type { AppProps } from "next/app";
2import { Analytics } from "@viakite/sdk/react";
3
4export default function App({ Component, pageProps }: AppProps) {
5 return (
6 <>
7 <Component {...pageProps} />
8 <Analytics project="pk_live_us_…" />
9 </>
10 );
11}

An app that uses both routers needs it in both files. Each page load starts the SDK once, so they never count twice.

Route changes

Next.js changes pages through the browser's history, and the SDK follows it: one page view for each new path, none when only the query changes (a filter or a sort). <Link>, router.push() and the back button all count. The engaged time of the page being left is sent before the next page view.

Send events

Import track, identify and reset from @viakite/sdk in any client component:

app/pricing/SignupButton.tsx
1"use client";
2import { track } from "@viakite/sdk";
3
4export function SignupButton() {
5 return <button onClick={() => track("Signup", { plan: "pro" })}>Start free</button>;
6}

A page holds one copy of the SDK, whether you import from @viakite/sdk, @viakite/sdk/react or both. Calls made before <Analytics /> has mounted are kept and sent once it does.

Server components and server actions

The SDK runs in the browser only. In a server component, a route handler or a server action, every function does nothing, so importing it there is safe but sends nothing. Call track() from a client component, for example after a server action returns.

To send events from your server, such as a payment webhook, use a secret key: see Server-side events.

Options and environment variables

Every option of init() is a prop. Keep the key in an environment variable so preview and production can use different projects:

app/layout.tsx
1<Analytics
2 project={process.env.NEXT_PUBLIC_VIAKITE_KEY!}
3 title
4 debug={process.env.NODE_ENV === "development"}
5/>
.env.local
1NEXT_PUBLIC_VIAKITE_KEY=pk_live_us_…

Nothing is sent from localhost. To check your setup in next dev, add debug to see events in the console, or localhost to send them. All props: Options.

Through your own domain

Ad blockers sometimes block requests to analytics hosts. You can send events through your own domain with a rewrite, and point api at it:

next.config.ts
1import type { NextConfig } from "next";
2
3const config: NextConfig = {
4 async rewrites() {
5 return [{ source: "/va/e", destination: "https://us.api.vk.viabio.me/va/e" }];
6 },
7};
8
9export default config;
app/layout.tsx
1<Analytics project="pk_live_us_…" api="/va/e" />

Use your region's address from Analytics → Install (turn on Use a proxy) in place of us.api.vk.viabio.me. More hosts and what a proxy must pass on: Proxy.

Next steps