Skip to main content

Adding Plausible to TanStack Start

Here's how to add Plausible Analytics to your TanStack Start app. Client-side navigation with TanStack Router uses pushState, so Plausible counts page changes automatically. See single-page application support for details.

Add privacy-friendly analytics to your TanStack Start 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: Add the script tag​

Add your snippet to the scripts list returned by head() in your root route. You can copy your snippet from the Site Installation area of the General section in your site settings.

src/routes/__root.tsx
import { createRootRoute } from '@tanstack/react-router'

export const Route = createRootRoute({
head: () => ({
scripts: [
{
src: 'https://plausible.io/js/pa-XXXXX.js',
async: true
},
{
children: `window.plausible=window.plausible||function(){(plausible.q=plausible.q||[]).push(arguments)},plausible.init=plausible.init||function(i){plausible.o=i||{}};
plausible.init()`
}
]
})
})

Add these scripts next to any meta and links your route already returns. Make sure your root layout renders <HeadContent /> inside <head>, because that's where TanStack Start renders the scripts.

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.

Option 2: Install with the NPM package​

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. Add a component that initializes Plausible once when it mounts:

src/components/PlausibleAnalytics.tsx
import { useEffect } from 'react'
import { init } from '@plausible-analytics/tracker'

let initialized = false

export function PlausibleAnalytics() {
useEffect(() => {
if (initialized) return
initialized = true

init({
domain: 'example.com'
})
}, [])

return null
}

In src/routes/__root.tsx, import { PlausibleAnalytics } from ../components/PlausibleAnalytics and render <PlausibleAnalytics /> once inside your existing root layout.

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. 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