Skip to main content
This page builds one React app with Analytics Web and its hooks from @grainql/analytics-web/react. Every block uses Analytics Web. For Grain Tag in React, with heatmaps and no provider, read React.

The provider

Wrap the app in GrainProvider one time, at the root. Pass config and the provider creates the client. defaultConfigurations holds the value of each configuration key until the first fetch completes. Every value is a string.
Analytics Web tracks page views by default (enableAutoPageView). Read GrainProvider.

Track an event

useTrack returns a stable function. It does not change between renders, so it is safe in an effect dependency list and as a prop.
Read useTrack.

A feature flag

useConfig returns the cached value at once and refreshes in the background. The value is a string, so compare it with 'true'.
Read Feature flags.

An A/B variant

Read the variant with useConfig, show the content for that variant, and put the variant in every event of the test. A filter on the variant property then separates the two groups.
Read A/B tests.

Identify on login

useGrainAnalytics returns the client. Call identify when a user logs in and logout when the user logs out. logout returns the client to an anonymous ID.
useAuth is your own hook. Read Identify users and User properties.

The complete app

GrainProvider sits outside AuthProvider, so AuthSync can read both contexts. React Router navigates through the History API, so each route change is a page view.