Adding Plausible to Next.js
Here's how to add Plausible Analytics to your Next.js app. These steps use the App Router. There are two ways to install Plausible: the official NPM package or the script tag.
Add privacy-friendly analytics to your Next.js app. Try Plausible free for 30 days, no credit card required.
The script tag is the simplest setup and picks up changes from your Plausible site settings. Choose the NPM package if you want to bundle the tracker with your app or pin its version. Use one method to avoid counting pageviews twice.
Option 1: Install with the NPM package
The package bundles the tracker into your own build, so you can control the version that runs on your site and use track for custom events with types. Read more in the NPM package guide.
Install the package
npm install @plausible-analytics/tracker
Initialize Plausible on the client
The tracker needs browser APIs, so init has to run on the client. Create a client component that calls init once:
'use client'
import { useEffect } from 'react'
import { init } from '@plausible-analytics/tracker'
let initialized = false
export default function PlausibleAnalytics() {
useEffect(() => {
if (initialized) return
initialized = true
init({
domain: 'example.com'
})
}, [])
return null
}
Use the domain exactly as you added it to your Plausible account. init can only be called once and throws an error on a second call. The initialized flag keeps it from running twice when React Strict Mode runs effects twice in development.
Then render the component once in your root layout:
import PlausibleAnalytics from './plausible-analytics'
export default function RootLayout({ children }: { children: React.ReactNode }) {
return (
<html lang="en">
<body>
<PlausibleAnalytics />
{children}
</body>
</html>
)
}
Pageviews are tracked automatically, including navigation between pages with next/link. Optional measurements such as outbound links and file downloads are off by default. Turn them on in init. See all configuration options.
Track custom events
Import track in any client component and give it the name of a custom event goal:
'use client'
import { track } from '@plausible-analytics/tracker'
export default function SignupButton() {
return (
<button onClick={() => track('Signup button click', { props: { plan: 'business' } })}>
Sign up
</button>
)
}
See track custom events for everything the second argument accepts.
Option 2: Add the script tag
If you prefer the script tag, add your snippet to the <head> of your root layout. You can copy your snippet from the Site Installation area of the General section in your site settings. With the script tag, optional measurements are controlled in your site settings.
export default function RootLayout({ children }: { children: React.ReactNode }) {
return (
<html lang="en">
<head>
<script async src="https://plausible.io/js/pa-XXXXX.js" />
<script
dangerouslySetInnerHTML={{
__html: `window.plausible=window.plausible||function(){(plausible.q=plausible.q||[]).push(arguments)},plausible.init=plausible.init||function(i){plausible.o=i||{}};
plausible.init()`
}}
/>
</head>
<body>{children}</body>
</html>
)
}
Your snippet has a different script location than the example above. Use the https://plausible.io/js/pa-XXXXX.js URL from your own snippet.
Run Plausible as a first-party connection
To bypass adblockers, serve Plausible from your own domain. See our Next.js proxy guide and the proxy overview. If you use the NPM package, set endpoint in init to your proxy URL.
Using the Pages Router or next-plausible
On the Pages Router, call init from a useEffect in pages/_app.tsx, using the same initialized flag as above. Or add the script tag to a custom pages/_document.tsx.
next-plausible is a community package built and maintained by 4lejandrito. We don't maintain it, so check its repository for setup instructions and compatibility with your Next.js version.
Verify your installation
Open your deployed site and follow an internal link to a second page. In the realtime dashboard, look for both paths under Top Pages. Plausible ignores visits on localhost by default. If a page is missing, check your installation.
What's next?
- Create goals in your Plausible site settings to match the custom events you track. They won't show in your dashboard until you do
- Attach custom properties to your events to track additional context like plan type
- Build a funnel using your custom events as steps to see where visitors drop off