Proxying Plausible with Next.js
Our managed proxy lets you send analytics through your own domain name as a first-party connection. All you need to do is set up a CNAME record using the instructions we'll send you and update the snippet on your site. We'll take care of everything else. Contact us for details.
Step 1: Get your snippet
In the Site Installation area of the General section in your site settings you can see the snippet specific for your site. It will look similar to the following:
<script async src="https://plausible.io/js/pa-XXXXX.js"></script>
<script>
window.plausible=window.plausible||function(){(plausible.q=plausible.q||[]).push(arguments)},plausible.init=plausible.init||function(i){plausible.o=i||{}};
plausible.init()
</script>
Your snippet will have a different script location than the example above. Look for the https://plausible.io/js/pa-XXXXX.js part in your snippet - that's the personalized location for your site's script. Mark it down for subsequent steps.
If you use the official NPM package, you can skip this step. The tracker is already part of your own build, so you only need to proxy the event endpoint.
Step 2: Add rewrites in your Next.js config
Add the following rewrites to next.config.ts. The same rewrites function works in next.config.mjs or next.config.js.
import type { NextConfig } from 'next'
const nextConfig: NextConfig = {
async rewrites() {
return [
{
source: '/your-subdirectory/js/script.js',
destination: 'https://plausible.io/js/pa-XXXXX.js'
},
{
source: '/your-subdirectory/api/event',
destination: 'https://plausible.io/api/event'
}
]
}
}
export default nextConfig
Replace https://plausible.io/js/pa-XXXXX.js with the script location from step 1. If you use the NPM package, you can omit the script rewrite and keep only the /your-subdirectory/api/event rule.
Note that:
- The source paths identified here must be used when you configure the script tag or the
endpointoption in the next section. - You can use whatever paths you like here (for example, here prefixing with
/your-subdirectory/).
Avoid names like analytics, stats or plausible in your paths as they may get blocked.
Step 3: Point your tracker at the proxy
Option 1: NPM package
Set endpoint in init to the event path from step 2. See the Next.js integration guide for the full client component setup.
init({
domain: 'example.com',
endpoint: '/your-subdirectory/api/event'
})
Option 2: Script tag
Add your snippet to the <head> of your root layout. The src and endpoint must match the source values in your rewrites.
export default function RootLayout({ children }: { children: React.ReactNode }) {
return (
<html lang="en">
<head>
<script async src="/your-subdirectory/js/script.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({
endpoint: "/your-subdirectory/api/event"
})`
}}
/>
</head>
<body>{children}</body>
</html>
)
}
Verify your proxy
Open your deployed site and follow an internal link to a second page. Confirm that both paths appear under Top Pages in your realtime dashboard.
If a page is missing, open the Network tab in your browser’s developer tools and reload. The event request should go to the path you configured on your own domain. With the script tag, the script should also load from your domain.
Plausible returns HTTP 202 even when it drops an event. If the event response includes x-plausible-dropped: 1, check that your proxy forwards the visitor’s IP in X-Forwarded-For. See which visitor headers to forward and which credentials to strip and how to check which IP Plausible receives.
Using the community next-plausible package? It has built-in proxy support. See its repository for setup instructions.