JavaScript frameworks
Add Loogika to Next.js
You add Loogika to Next.js with the Script component from next/script, rendered once at the top level of the app. In the App Router that place is the root layout, and in the Pages Router it is the custom App component.
Last updated .
What do you need before you start?
Copy your script tag from Site Settings in the Loogika dashboard, under the Tracking Script tab. You only need the address in the src attribute, which ends with your 12-character site ID:
<script src="https://app.loogika.com/api/script.js?siteId=YOUR_SITE_ID" defer></script>
Why use next/script and not a plain script tag?
The Script component of Next.js controls when a third-party script loads. Use it with strategy="afterInteractive" in place of the plain tag. You can leave out the defer attribute, because the strategy decides the load timing.
How do you add Loogika with the App Router?
Render the Script component in the root layout, app/layout.js, so that every page includes it:
import Script from "next/script";
export default function RootLayout({ children }) {
return (
<html lang="en">
<body>
{children}
<Script
src="https://app.loogika.com/api/script.js?siteId=YOUR_SITE_ID"
strategy="afterInteractive"
/>
</body>
</html>
);
}
How do you add Loogika with the Pages Router?
Render the Script component in the custom App component, pages/_app.js, next to the page component:
import Script from "next/script";
export default function MyApp({ Component, pageProps }) {
return (
<>
<Component {...pageProps} />
<Script
src="https://app.loogika.com/api/script.js?siteId=YOUR_SITE_ID"
strategy="afterInteractive"
/>
</>
);
}
Does Loogika track client-side navigation in Next.js?
Yes. Loogika records a pageview when a visitor moves between pages through the Link component or router.push(). You add no code for it.
Client-side navigation is counted while SPA Navigation is on in Site Settings, and it is on for a new site. The Loogika script waits 500 milliseconds after a route change before it records the pageview. A redirect inside the Next.js app is then counted once.
How do you track custom events in Next.js?
A client component is the place to call window.loogika.event(). Check for window and for window.loogika before the call. Next.js can render a component on the server, and the Loogika script only loads after hydration in the browser.
"use client";
export default function MyButton({ title }) {
const handleClick = () => {
if (typeof window !== "undefined" && window.loogika) {
window.loogika.event("button_click", { buttonTitle: title });
}
};
return <button onClick={handleClick}>{title}</button>;
}
A plain click needs no client component. Add data-loogika-event="button_click" to the button, and Loogika sends the event on each click. See how to track events for properties and limits.
How do you check that Loogika is installed?
Load the deployed Next.js site in a fresh browser tab and visit a few pages. Loogika's dashboard shows those pageviews within a few seconds.
If the dashboard stays empty, search the page source for script.js?siteId=. In the Network tab of the browser, script.js should return status 200 and POST requests should go to /api/track.
Can you serve the Loogika script from your own Next.js domain?
Yes. Next.js rewrites can pass the script and its requests through your own domain, so that ad blockers stop fewer visits. Follow the proxy guide for the steps and the header your proxy must send.
Rewrite the whole path to Loogika, not only script.js and track. The script also fetches your site settings through the same path when it loads. If that request fails, the script runs with default settings and ignores what you chose in Site Settings.
Why are pageviews missing after client-side navigation?
Missing pageviews after client-side navigation are rare in Next.js. Check first that SPA Navigation is on in Site Settings, under the Tracking tab. If it is on and a route change still goes uncounted, call window.loogika.pageview() yourself after that route change.