@grainql/tag package. Grain Tag needs no
provider and no wrapper component. For Next.js, read Next.js.
Before you start
You need three things:- Your tenant alias. The dashboard at
https://grainql.com/dashboardshows it. It is not the UUID. - A React app with a build tool that reads environment variables, for example Vite
(
VITE_GRAIN_TENANT_ID) or Create React App (REACT_APP_GRAIN_TENANT_ID). - A browser with developer tools, to make sure that events arrive.
For remote configuration, feature flags, and the hooks
useConfig, useTrack, and
GrainProvider, use Analytics Web instead of Grain Tag. Read React hooks.1. Install the package
2. Initialize Grain Tag
Callinit one time, in a useEffect at the root of the app. The isInitialized guard keeps
a second render from a second initialization. The cleanup calls destroy, so hot module
replacement and tests start clean.
init, Grain Tag tracks page views, heatmap clicks, scroll depth, and DOM snapshots. React
Router and every other router that uses the History API needs no page view code.
3. Make sure that events arrive
Open the app in a browser. In the developer console, run:installation_test event within 30 seconds. Pass debug: true to init
to see the batching and sending activity in the console. If the event does not appear, read
Troubleshooting.
4. Track an event
Importtrack and call it in an event handler.
track and
identify called before init are queued and replayed after initialization. Read
Track an event.
5. Identify a user
After a login, pass the user ID toidentify. Every later event holds this user ID.
6. Grant consent
Grain Tag is cookieless by default and uses daily rotating IDs. When your app asks for consent, call theconsent methods on the instance. getInstance returns null before init.
grant enables persistent tracking. Read
Privacy and consent.
Next steps
Core methods
Every method of both SDKs, with signatures.
Identify users
Anonymous IDs, daily rotating IDs, and
identify.Next.js
App Router, Pages Router, and server-side events.
React hooks
Analytics Web, the provider, and remote configuration in React.