← All IntegrationsIntegration
Analytics for React
Whether you're using Vite, Create React App, or a custom bundler, BetterMeter drops into any React project with a single script tag. It tracks SPA navigations via React Router automatically — no wrapper components, no context providers, no hook dependencies.
Add the script tag to index.html
BetterMeter lives outside your React component tree. Add it to your HTML shell and it works independently of React's render cycle, router, or state management.
index.html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>My App</title>
</head>
<body>
<div id="root"></div>
<script type="module" src="/src/main.tsx"></script>
<script
defer
data-site="your-domain.com"
src="https://bettermeter.com/api/script"
></script>
</body>
</html>Custom event tracking with useEffect
Beyond automatic page views, you can fire custom events from any React component. The tracker exposes a global window.bettermeter function that you can call from effects, handlers, or callbacks.
SignupButton.tsx
export function SignupButton() {
const handleClick = () => {
// Track a custom event
window.bettermeter?.("signup_click");
};
return (
<button onClick={handleClick}>
Sign up free
</button>
);
}How SPA navigation tracking works
- 01 History API listener — BetterMeter patches
pushStateandreplaceStateto detect route changes from React Router, TanStack Router, or Wouter. - 02 Popstate events — Browser back/forward navigations are tracked automatically.
- 03 No double-counting — The tracker deduplicates navigations to the same URL within the same session.
- 04 Hash routing — If you use hash-based routing, BetterMeter tracks hash changes as page views.
Vite + CRA
Any bundler works
React Router
Auto SPA tracking
Custom Events
Track any action
No Cookies
Privacy-first
AI Detection
ChatGPT, Claude visits
Zero Config
Just a script tag