Skip to main content

Adding Plausible to React

Here's how to add Plausible Analytics to a client-side React app, for example one built with Vite. There are two ways to install Plausible: the official NPM package or the script tag.

Add privacy-friendly analytics to your React app. Try Plausible free for 30 days, no credit card required.

Using Next.js, React Router or TanStack Start? Follow our Next.js, React Router or TanStack Start guide instead.

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 the code running on your site only changes when you update the package.

npm install @plausible-analytics/tracker

Call init once in your entry file, before you render your app:

src/main.tsx
import { StrictMode } from 'react'
import { createRoot } from 'react-dom/client'
import { init } from '@plausible-analytics/tracker'
import App from './App'

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

createRoot(document.getElementById('root')!).render(
<StrictMode>
<App />
</StrictMode>
)

Use the domain exactly as you added it to your Plausible account. Calling init at the top level of your entry file means it runs once, even when React Strict Mode runs effects twice in development. init throws an error on a second call, so don't call it from a component that renders more than once.

Optional measurements such as outbound links and file downloads are off by default. Turn them on in init. See all configuration options.

Option 2: Add the script tag​

Add your snippet to the <head> of your index.html. 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.

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>

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.

Pageviews in single-page apps​

Plausible tracks a new pageview whenever your router changes the path with pushState. This works out of the box with React Router, TanStack Router and other pushState based routers. See single-page application support for details.

If your app uses the URL hash for routing, turn on hash-based routing. With the NPM package, set hashBasedRouting: true in init.

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>

See track custom events for properties, revenue and other options. 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.

React Native and other native mobile apps can't run the tracker. Use the Events API instead.

What's next?​

Ready to ditch Google Analytics?

Start your free trial today