Skip to main content

TanStack Router · TanStack Start · GA4

Your routes.
Your analytics. Connected.

Client-side navigation does not reload the page, so GA4 never sees it. Mount one component and every pathname change becomes a page view, then record the actions that matter with typed event helpers.

import { Outlet } from '@tanstack/react-router';
import { GoogleAnalytics } from 'tanstack-router-ga4';

// Inside your router's root component
export function RootLayout() {
return (
<>
<GoogleAnalytics measurementId="G-XXXXXXXXXX" />
<Outlet />
</>
);
}
01Install

One npm package for TanStack Router apps

02Mount

GoogleAnalytics once in your root route

03Navigate

A page_view per pathname change

04Track

Typed events from one hook

Beyond page views

From the first visit
to the next conversion.

Recommended events such as sign_up and login are typed, and custom events accept any name. The same hook sets user properties and sends consent updates from your own consent interface, so there is no separate analytics wrapper to maintain.

Browse the examples
import { useGoogleAnalytics } from 'tanstack-router-ga4';

export function SignupButton() {
const { event } = useGoogleAnalytics();

return (
<button onClick={() => event('sign_up', {
method: 'email',
})}>
Record signup
</button>
);
}

Fits your app shell

Client only.
Out of the way.

The component renders nothing during server rendering and initializes GA4 in the browser. By default it waits for browser idle time, and you can turn that off to load immediately.

Understand loading, tracking, and consent →
<GoogleAnalytics
measurementId="G-XXXXXXXXXX"
deferred={false}
consentDefaults={{ analytics_storage: 'denied' }}
/>

Documentation

Find what you need.

Guides for setup and behavior, plus an API reference generated from the source on every build.

Add it to your app

One package. One component.

npm install tanstack-router-ga4