Add FastStats Web Analytics to a React or Next.js app
The @faststats/react package wraps the browser SDK in a single component and a
set of hooks. It works with any React app and with the Next.js App Router.
Render the Analytics component once, near the root of your app. It starts
tracking when it mounts and cleans up when it unmounts. You never have to create
an instance by hand.
The component accepts every option from the SDK. Here is a fuller example that
turns on a few extra features.
import { Analytics } from "@faststats/react";
import { errorTracking } from "@faststats/react/error";
import { sessionReplay } from "@faststats/react/replay";
import { webVitals } from "@faststats/react/web-vitals";
<Analytics
siteKey={process.env.NEXT_PUBLIC_FASTSTATS_SITE_KEY!}
extensions={[errorTracking(), webVitals(), sessionReplay()]}
/>;
Options are read when the component mounts. Only changes to the consent prop
are applied to the running client afterwards. See
Configuration for the full list.
useIdentify links the visitor to your user after sign-in. useSetTraits
updates traits on that user later without a full identify, for example after a
plan upgrade.
Pass { mode: "setOnce" } to only set traits the user does not have yet, such
as where they first came from. Called before useIdentify, traits are kept in
memory for the current page and sent right after the next identify on that
page.
useSetTraits needs @faststats/react 0.14.0 or later. See
Identify for details.
The React package only runs in the browser. If you also want to capture errors
thrown in Next.js route handlers or server functions, read the server section in
Error Tracking.