Documentation Index
Fetch the complete documentation index at: https://docs.grainql.com/llms.txt
Use this file to discover all available pages before exploring further.
Grain Tag Methods
Primary SDK for browser analytics. Import from
@grainql/tag.init()
Creates and returns the singletonGrainTagInstance. In non-browser environments (Node, SSR), returns a safe no-op stub.
config— Configuration object.tenantIdis the only required field.
GrainTagInstance
Example:
destroy() first to re-initialize with new config.
track()
Track a custom event. If called beforeinit(), the event is queued and replayed automatically once the SDK initializes.
eventName— Name of the eventproperties— Optional key-value pairs
identify()
Set user identity. If called beforeinit(), the call is queued and replayed after initialization.
userId— User identifier
getInstance()
Get the existing SDK instance, ornull if not yet initialized.
isInitialized()
Check whether the SDK has been initialized.destroy()
Tear down the SDK instance and clear the singleton. After calling this, you can callinit() again with new config.
Instance Methods
Once you have aGrainTagInstance (from init() or getInstance()), these methods are available:
grain.track()
Same as the top-leveltrack(), but called on the instance directly.
grain.identify()
Same as the top-levelidentify(), but called on the instance directly.
grain.consent.grant()
Grant consent. Upgrades from cookieless to permanent identity tracking.categories— Consent categories to grant. Defaults to['necessary', 'analytics', 'functional'].
grain.consent.revoke()
Revoke consent. Downgrades back to cookieless identity mode.categories— Categories to revoke. If omitted, revokes all.
grain.consent.status()
Get the current consent state.ConsentState object or null if no consent decision has been made.
grain.flush()
Force-send all pending events immediately.grain.destroy()
Tear down the instance. Same as the top-leveldestroy().
grain.isReady()
Returnstrue if the SDK is fully initialized in a browser environment.
Analytics Web Methods
For remote config, React hooks, and server-side tracking. Import from
@grainql/analytics-web.createGrainAnalytics()
Creates a new Grain client instance.config— Configuration object (see Types)
track()
Track an event. Two signatures available:track(eventName, properties?, options?)
eventName— Name of the eventproperties— Optional event propertiesoptions.flush— Force immediate send (default: false)
track(event, options?)
event— Complete event objectoptions.flush— Force immediate send
flush()
Manually flush all queued events.- Before page navigation
- Critical events
- Serverless functions
- Before app closes
setUserId()
Set global user ID for all subsequent events.userId— User identifier, ornullto clear
identify()
Alias forsetUserId(). Sets user ID for subsequent events.
getUserId()
Get current user ID.null if not set
Example:
setProperty()
Set user properties for analytics and personalization.properties— Up to 4 key-value pairs (values converted to strings)options.userId— Optional user ID override
- Maximum 4 properties per request
- All values converted to strings
- Subject to rate limiting
destroy()
Clean up resources and send remaining events.- Flushes queued events
- Removes event listeners
- Cleans up timers
- Stops auto-refresh
Next Steps
Config Methods
Remote configuration methods
Template Methods
Pre-built event methods
Types
TypeScript type definitions