Skip to main content

Adding Plausible to React Router

Here's how to add Plausible Analytics to a React Router v7 app. The steps cover both framework mode and library mode. React Router navigates with pushState, so Plausible counts page changes automatically. See single-page application support for details.

Add privacy-friendly analytics to your React Router 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.

Framework mode​

Add your snippet to the <head> in the Layout export of app/root.tsx. You can copy your snippet from the Site Installation area of the General section in your site settings.

app/root.tsx
import { Links, Meta, Scripts, ScrollRestoration } from 'react-router'

export function Layout({ children }: { children: React.ReactNode }) {
return (
<html lang="en">
<head>
<meta charSet="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<Meta />
<Links />
<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}
<ScrollRestoration />
<Scripts />
</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. With the script tag, optional measurements are controlled in your site settings.

Library mode​

If you render your app with createBrowserRouter and RouterProvider, add your snippet to the <head> of your index.html:

index.html
<head>
<meta charset="UTF-8" />
<title>My app</title>
<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>
</head>

Use the NPM package instead​

The official NPM package bundles the tracker into your own build. Install it:

npm install @plausible-analytics/tracker

The tracker needs browser APIs, so only call init on the client. In framework mode, put it in app/entry.client.tsx, which only runs in the browser. If you don't have that file yet, run npx react-router reveal entry.client to create it.

app/entry.client.tsx
import { startTransition, StrictMode } from 'react'
import { hydrateRoot } from 'react-dom/client'
import { HydratedRouter } from 'react-router/dom'
import { init } from '@plausible-analytics/tracker'

init({
domain: 'example.com'
})

startTransition(() => {
hydrateRoot(
document,
<StrictMode>
<HydratedRouter />
</StrictMode>
)
})

In library mode, call init once in your entry file before you render your app. See the React guide.

Use the domain exactly as you added it to your Plausible account. init can only be called once. Optional measurements are off by default and are configured in init. See all configuration options.

Track custom events​

With the NPM package, import track and give it the name of a custom event goal:

import { track } from '@plausible-analytics/tracker'

<button onClick={() => track('Signup button click')}>Sign up</button>

With the script tag, follow our custom event goals guide.

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?​

Ready to ditch Google Analytics?

Start your free trial today