Skip to main content
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

Call init() once at your app’s entry point. No provider or wrapper component needed:
Replace 'your-tenant-id' with the alias from your dashboard (not the UUID).
Pro tip: Store your tenant ID in an environment variable like VITE_GRAIN_TENANT_ID or REACT_APP_GRAIN_TENANT_ID.
Page views, heatmap clicks, scroll depth, and DOM snapshots are tracked automatically. No manual setup required.

Track Events

Import track directly from @grainql/tag and call it from any component:
Events are automatically batched and sent every few seconds. No manual flushing needed.

Identify Users

Associate events with a specific user after login:
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), use destroy():

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
TypeScript users: @grainql/tag ships with full TypeScript declarations. You’ll get autocomplete and type checking out of the box.

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.