Install Linqry on Next.js
To add Linqry to a Next.js app, render two `next/script` tags in your root layout (`app/layout.tsx`): an inline queue stub and the tracker with your site ID in `data-*` attributes. Client-side navigations through `next/link` and the router are recorded as page views automatically, with no extra code.
Updated · 3 min read
Install with the App Router
- Copy your site ID
Start a free trial and add your production domain. The Install page shows your site ID and the
data-domainvalue to use. - Add both scripts to app/layout.tsx
The root layout wraps every route, so the tracker loads once and stays loaded as visitors navigate. Next.js forwards attributes it doesn't use itself, including custom
data-*attributes, to the final<script>element, which is where the tracker reads its configuration.// app/layout.tsx import Script from "next/script"; export default function RootLayout({ children }: { children: React.ReactNode }) { return ( <html lang="en"> <body> {children} <Script id="linqry-queue" strategy="beforeInteractive"> {`window.linqry=window.linqry||function(){(window.linqry.q=window.linqry.q||[]).push(arguments)}`} </Script> <Script src="https://linqry.com/t.js" data-website-id="YOUR_SITE_ID" data-domain="yourdomain.com" strategy="afterInteractive" /> </body> </html> ); } - Deploy and open Realtime
Deploy, visit the production site in a normal browser window, click through two or three pages, then open Realtime in Linqry. Each navigation appears in the live event stream as a
pageview.
Why two scripts, and why these strategies
The inline queue stub uses beforeInteractive, which Next.js injects into the initial HTML in the <head>, so window.linqry exists before any of your components run. Calls made before the tracker arrives are queued and sent once it loads. The tracker itself uses afterInteractive, the default strategy that the Next.js docs recommend for analytics: it loads early but never competes with hydration. Next.js requires an id on inline scripts, which is why the stub has one.
Avoid lazyOnload for the tracker. It waits for browser idle time, so visitors who leave quickly may never be counted. The experimental worker strategy doesn't work with the App Router.
Route changes in Next.js
Next.js navigates on the client with the History API. The tracker wraps history.pushState and replaceState and listens for popstate, so every client-side navigation is recorded as a page view, and the time spent on the previous page is sent as it is left. You don't need a usePathname effect or a router event listener. Query-string changes count as navigations too; changes to the #hash don't unless you add data-hash-routing to the tracker tag.
Using the Pages Router instead
In a Pages Router project, put the plain snippet in the <Head> of pages/_document.tsx. That file renders on the server only and its <Head> is meant for markup shared by every page, which is exactly what the tracker is.
// pages/_document.tsx
import { Html, Head, Main, NextScript } from "next/document";
export default function Document() {
return (
<Html lang="en">
<Head>
<script dangerouslySetInnerHTML={{ __html: "window.linqry=window.linqry||function(){(window.linqry.q=window.linqry.q||[]).push(arguments)}" }} />
<script defer data-website-id="YOUR_SITE_ID" data-domain="yourdomain.com" src="https://linqry.com/t.js" />
</Head>
<body>
<Main />
<NextScript />
</body>
</Html>
);
}Send custom events from components
Call window.linqry(name, props, revenueCents) from any Client Component, for example in a click handler or after a form submits. Add a small declaration file so TypeScript knows the function exists. Events can become goals and funnel steps; see custom events for naming rules and limits.
// linqry.d.ts (anywhere your tsconfig includes)
export {};
declare global {
interface Window {
linqry?: (
event: string,
props?: Record<string, string | number | boolean>,
revenueCents?: number,
) => void;
}
}"use client";
export function UpgradeButton() {
return (
<button onClick={() => window.linqry?.("upgrade-clicked", { plan: "pro" })}>
Upgrade
</button>
);
}For payments, prefer recording revenue on your server from the payment webhook, using a Route Handler and the site's write key. Browser-reported revenue can be forged by anyone who reads your page source; server-side events can't. See server-side revenue tracking and the tracking API.
Preview deployments and local development
Events from localhost are ignored, so next dev never adds to your numbers. Preview deployments usually run on a different hostname from production, and events from them are rejected unless you add that hostname under Additional hosts in site Settings. Leaving previews out is usually what you want. To exclude your own visits on production, run this once in the browser console on your site:
localStorage.setItem('linqry_ignore', 'true')Troubleshooting
- No events at all? View the page source or the Elements panel and check that the tracker's
<script>carriesdata-website-id. If it doesn't, the attribute was misspelled or set on the wrong element. - Content-Security-Policy in middleware or next.config? Allow
https://linqry.comin bothscript-srcandconnect-src. If you use a nonce-based policy, pass the nonce to both<Script>tags. - Only the first page counted? Check that the tracker is in the root layout, not inside a single page component that unmounts.
Frequently asked questions
Does Linqry work with static exports and the Edge runtime?
Yes. The tracker is a plain browser script, so it doesn't matter how the HTML was produced: static export, server rendering or the Edge runtime all serve the same script tags.
Do I need a package or an npm SDK?
No. There's no npm package to install; the two script tags are the whole integration. Custom events go through window.linqry, and server-side events are a plain HTTP request to the tracking API.
Will next/script delay the first page view?
Slightly, by design. afterInteractive loads the tracker once hydration has started, which usually takes a fraction of a second. Calls you make earlier are held by the queue stub and sent when the tracker loads.
Sources
- Next.js: How to load and optimize scripts (checked Nov 9, 2026)
- Next.js: Script component API reference (checked Nov 9, 2026)
- Next.js: Custom Document (Pages Router) (checked Nov 9, 2026)
Related
Paste the snippet into index.html in a Vite or other React SPA. React Router navigations are tracked as page views.
Send events from JavaScript or plain HTML attributes — plus the events Linqry records for you automatically.
Send events from your backend — webhooks, cron jobs, payment confirmations — with a single authenticated POST.
Server-side conversion tracking means your backend, not the visitor's browser, reports that a payment happened. You send the event from the place where money is confirmed, usually a payment webhook, so the number in your analytics matches your billing system and can't be faked from the browser console.
Start measuring your Next.js site
No card required · 30-day trial · No cookies