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
1npm install @viakite/sdk
1pnpm add @viakite/sdk
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:
1import { Analytics } from "@viakite/sdk/react";23export default function RootLayout({ children }: { children: React.ReactNode }) {4return (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:
1import type { AppProps } from "next/app";2import { Analytics } from "@viakite/sdk/react";34export default function App({ Component, pageProps }: AppProps) {5return (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:
1"use client";2import { track } from "@viakite/sdk";34export function SignupButton() {5return <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:
1<Analytics2project={process.env.NEXT_PUBLIC_VIAKITE_KEY!}3title4debug={process.env.NODE_ENV === "development"}5/>
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:
1import type { NextConfig } from "next";23const config: NextConfig = {4async rewrites() {5return [{ source: "/va/e", destination: "https://us.api.vk.viabio.me/va/e" }];6},7};89export default config;
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.