---
title: "Add privacy-friendly analytics to Next.js"
description: "Add Plausible or Umami to the Next.js App Router: next/script, custom events, route changes, a first-party proxy, and how to skip localhost."
canonical: https://datalighthouse.dev/guides/add-privacy-friendly-analytics-to-nextjs
last-updated: 2026-11-24
---

# Add privacy-friendly analytics to Next.js
Add Plausible or Umami to the Next.js App Router: next/script, custom events, route changes, a first-party proxy, and how to skip localhost.
### 1. Add the Plausible snippet

In app/layout.tsx, load the per-site script at https://plausible.io/js/pa-XXXXX.js with next/script and call plausible.init() from the inline queue. Copy the pa- URL from Site installation. The October 2025 snippet does not use data-domain.

### 2. Add the Umami snippet

In app/layout.tsx, load script.js with next/script and set data-website-id to the UUID from the website tracking code. Cloud's script URL in the bypass guide is https://cloud.umami.is/script.js. A self-hosted instance uses its own script.js.

### 3. Send custom events

Call plausible('Signup') or plausible('Download', { props: { method: 'HTTP' } }). Create a Plausible goal whose name matches, including capitals. Call umami.track('signup-button') or umami.track('signup-button', { plan: 'pro' }). Leave emails and tokens out of the payload.

### 4. Track App Router route changes

Plausible listens for history.pushState when autoCapturePageviews is left at its default true. Umami's collect-data page says single-page navigations are tracked with no extra code. If a client click does not send a second pageview, turn automatic capture off and call plausible('pageview') or umami.track() when usePathname() changes. Leaving both on counts twice.

### 5. Proxy the script with Next.js rewrites

Rewrite /js/script.js to https://plausible.io/js/pa-XXXXX.js and /api/event to https://plausible.io/api/event, then set plausible.init({ endpoint: '/api/event' }). For Umami Cloud, rewrite /stats.js to https://cloud.umami.is/script.js and /api/send to https://gateway.umami.is/api/send, and set data-host-url to your origin. A route handler on the same path wins over the rewrite.

### 6. Exclude localhost and verify

Leave Plausible captureOnLocalhost at the default false so localhost is ignored. Set Umami data-domains to the production hostnames. Confirm the script returns 200 and the event POST succeeds. Plausible's proxy guide expects HTTP 202 on /api/event. The header x-plausible-dropped: 1 means Plausible received the hit and dropped it.

## FAQ
### How do I add Plausible to a Next.js App Router app?

Add next/script in app/layout.tsx. Load the per-site file https://plausible.io/js/pa-XXXXX.js and run the inline queue that calls plausible.init(). Copy that URL from Site installation in the site settings. The October 2025 snippet does not use a data-domain attribute. Set NEXT_PUBLIC_PLAUSIBLE_SCRIPT_URL to the pa- URL. It is public, because it ships in the HTML.

### How do I add Umami to Next.js?

Add next/script with defer, the script URL, and data-website-id. Umami's collect-data page says to use next/script instead of a plain script tag. Cloud's bypass guide uses https://cloud.umami.is/script.js. The website id is the UUID on the tracking code screen. Put it in NEXT_PUBLIC_UMAMI_WEBSITE_ID. Set data-domains to your production hostnames so localhost does not match.

### Do cookieless analytics need a consent banner?

This page is not legal advice. Plausible's self-hosted page says the product uses no cookies, collects no personal data, and does not require a consent banner. A banner is a legal control, not a script feature. The request can still carry the URL, referrer, language, and screen size. If you later send an email or a user id, read the notice again and get advice for your jurisdiction.

### How do I proxy Plausible or Umami so ad blockers miss the script?

Add rewrites in next.config. Plausible's proxy guides map /js/script.js to https://plausible.io/js/pa-XXXXX.js and /api/event to https://plausible.io/api/event. Then call plausible.init({ endpoint: '/api/event' }). Umami's bypass page points Next.js at rewrites. Their server example maps /stats.js to https://cloud.umami.is/script.js and /api/send to https://gateway.umami.is/api/send, with data-host-url set to your origin.

### Why does localhost not show up in Plausible or Umami?

Plausible's captureOnLocalhost option defaults to false, so dev traffic is ignored until you set it true. Umami matches data-domains against window.location.hostname. localhost fails that match. Plausible received the hit and then dropped it when the response includes x-plausible-dropped: 1. The custom-event docs name localhost, and a hostname that is not on the site, as common causes.

### When should I use a dashboard template instead of Plausible or Umami?

Pageviews answer which URL was opened. They do not join that visit to a Stripe charge or to your MRR rules. Data Lighthouse is a Next.js template you host. The license is $59 once. Optional Stripe cards show succeeded-charge revenue, MRR from active subscriptions, ARR as that MRR times 12, and revenue per visitor. Refunds are not subtracted. The overview does not compute churn. The tracker stores a visitor id in localStorage, which is a different choice than a cookieless script. The sample dashboard is at https://datalighthouse.dev/demo and pricing is at https://datalighthouse.dev/pricing.


## Sources
- [Plausible: add the tracking script](https://plausible.io/docs/plausible-script)
- [Plausible: script update guide](https://plausible.io/docs/script-update-guide)
- [Plausible: init options and manual pageviews](https://plausible.io/docs/script-extensions)
- [Plausible: custom events](https://plausible.io/docs/custom-event-goals)
- [Plausible: Next.js integration](https://plausible.io/docs/nextjs-integration)
- [Plausible: proxy snippet and rewrites](https://plausible.io/docs/proxy/guides/netlify)
- [Plausible: exclude internal traffic](https://plausible.io/docs/excluding)
- [Plausible: self-hosted Community Edition](https://plausible.io/self-hosted-web-analytics)
- [Plausible Community Edition README (v3.2.2)](https://github.com/plausible/community-edition)
- [Umami: collect data](https://docs.umami.is/docs/collect-data)
- [Umami: tracker functions](https://docs.umami.is/docs/tracker-functions)
- [Umami: tracker configuration](https://docs.umami.is/docs/tracker-configuration)
- [Umami: bypass ad blockers](https://docs.umami.is/docs/bypass-ad-blockers)
- [Umami: installation](https://docs.umami.is/docs/install)
