Using Next.js? Check out the Next.js Quick Start for App Router and Pages Router examples.
Install the Package
npm
yarn
pnpm
Initialize Grain
Callinit() once at your app’s entry point. No provider or wrapper component needed:
'your-tenant-id' with the alias from your dashboard (not the UUID).
Page views, heatmap clicks, scroll depth, and DOM snapshots are tracked automatically. No manual setup required.
Track Events
Importtrack directly from @grainql/tag and call it from any component:
Identify Users
Associate events with a specific user after login:Consent Management
Grain is cookieless by default with daily rotating IDs. When you need explicit consent handling:Complete Example
Here’s a small app that puts it all together:Automatic page views: Grain Tag hooks into the History API to track navigation automatically. You do not need a
PageViewTracker component or manual page_viewed calls — this works out of the box with React Router and other History API-based routers.Cleanup on Unmount
If you need to tear down Grain (e.g., for testing or HMR), usedestroy():
What’s Next?
Core API Reference
See all available methods and options
User Identification
Learn about tracking users across sessions
Next.js Integration
App Router and Pages Router setup
Event Best Practices
Learn what to track and how to structure events
Need Remote Config or React Hooks?
If you need remote configuration, feature flags, or React hooks (useConfig, useTrack, GrainProvider), install @grainql/analytics-web instead. See the Analytics Web React SDK for details.