Integration guide
Analytics in React
For single-page React apps (Vite, Create React App). One SDK instance in a module and a component that reports each route change.
Before you start
- A React app with React Router v6 or newer (for other routers, the idea is the same).
- Your
site_key, and your domain (plus the dev server address if you want to measure locally) under Allowed origins.
Installation
1Install the SDK
In Vite, variables exposed to the browser start with
VITE_.npm install @racoelho/analytics.env VITE_ANALISA_SITE_KEY=YOUR_SITE_KEY2Create the instance in a module
Importing the module creates the SDK exactly once; any component can import
analyticsafterwards.src/analytics.ts import { Analytics } from '@racoelho/analytics' // one instance for the whole app; the constructor sends the first pageview export const analytics = new Analytics({ siteKey: import.meta.env.VITE_ANALISA_SITE_KEY, })3Count route changes
A component with no UI that calls
pageview()whenever the path changes.src/route-tracker.tsx import { useEffect } from 'react' import { useLocation } from 'react-router-dom' import { analytics } from './analytics' export function RouteTracker() { const { pathname } = useLocation() useEffect(() => { // on every route change; the SDK drops the repeat on first load analytics.pageview() }, [pathname]) return null }src/App.tsx import { BrowserRouter, Routes, Route } from 'react-router-dom' import { RouteTracker } from './route-tracker' export default function App() { return ( <BrowserRouter> <RouteTracker /> <Routes> <Route path="/" element={<Home />} /> <Route path="/pricing" element={<Pricing />} /> </Routes> </BrowserRouter> ) }
Events and goals
Import analytics and call track(name, properties), or use the built-in helpers (signup, addToCart, purchase, formSubmit…).
import { analytics } from './analytics'
export function PricingCard({ plan }: { plan: string }) {
return (
<button onClick={() => analytics.track('plan_selected', { plan })}>
Choose plan
</button>
)
}
// built-in SDK helpers
analytics.signup('email')
analytics.purchase('order-123', 99.9, 'BRL')In the dashboard, create an event goal with the same name (plan_selected, purchase…) and build funnels from those steps.
Page navigation
The SDK sends the first-load pageview but does not detect navigation without a reload — hence the RouteTracker above. The SDK records the path (pathname), so changing only the query string isn’t a new page, and a repeated pageview for the same path within 2.5 seconds is dropped.
Check that it works
- Open the published site in a normal window, with no ad blocker.
- In your browser devtools, Network tab, filter by
v1/i: you should see aPOSTwith status200within 3 seconds (or when you leave the page). - In the dashboard, events show up in about 30 seconds.
- Click an internal link: a new
page_viewshows up in the nextPOST.
Troubleshooting
siteKey is requiredin the console- The variable wasn’t read. Check the
VITE_prefix and restart the dev server after creating.env. - The request returns 403 (
origin_not_allowed) - Your domain isn’t in Allowed origins in the site settings. Add the exact origin — with
https://, with and withoutwww, plus test addresses likehttp://localhost:3000if you want to measure in development. - No request shows up in the Network tab
- The SDK honors Do Not Track by default: if your browser sends DNT, nothing is sent. Test in another browser or profile. Ad blockers can also block the script.
- Pageviews show up twice
- The SDK was initialized twice (for example, pasted in the theme and also added by a plugin). Keep a single instance per page.
Create your account and grab the site_key
Free up to 10K events a month, no credit card.
Create a free account