Skip to main content

Basic Usage

Get access to the complete Grain client instance:
This hook gives you the full client API for operations not covered by specialized hooks.

When to Use

Use useGrainAnalytics when you need:
  • User identification: setUserId(), identify()
  • User properties: setProperty()
  • Manual flushing: flush()
  • Template events: trackLogin(), trackPurchase(), etc.
  • Advanced config: fetchConfig(), preloadConfig()
  • Client management: destroy()
For common operations, prefer specialized hooks:
  • useConfig for configurations
  • useTrack for event tracking
  • useAllConfigs for multiple configs

User Identification

Set user ID when users log in:
Note: If you pass userId to GrainProvider, this happens automatically. Only use this hook if you need manual control.

User Properties

Set attributes for user profiles:

Template Events

Use pre-built event methods:

Manual Flushing

Force send events immediately:

Preload Configurations

Load configs before rendering:
Now configs are available synchronously in child components.

Get Current User ID

Check who’s currently identified:

Advanced Configuration

Fetch configurations with specific options:

Configuration Listeners

Add listeners for config changes:
Note: Usually not needed with hooks. useConfig and useAllConfigs handle updates automatically.

Cleanup

The client is automatically cleaned up when the provider unmounts. Manual cleanup is rarely needed:

Stable Reference

The client reference returned by this hook is stable and won’t change between renders:

Authentication Flow Example

Complete authentication integration:

Next Steps

Core Methods API

See all available methods

User Identification

Learn about user tracking

Template Events

Pre-built event methods