Skip to main content

Grain Tag Methods

Primary SDK for browser analytics. Import from @grainql/tag.

init()

Creates and returns the singleton GrainTagInstance. In non-browser environments (Node, SSR), returns a safe no-op stub.
Parameters:
  • config — Configuration object. tenantId is the only required field.
Returns: GrainTagInstance Example:
If called more than once, returns the existing instance and logs a warning. Use destroy() first to re-initialize with new config.

track()

Track a custom event. If called before init(), the event is queued and replayed automatically once the SDK initializes.
Parameters:
  • eventName — Name of the event
  • properties — Optional key-value pairs
Example:

identify()

Set user identity. If called before init(), the call is queued and replayed after initialization.
Parameters:
  • userId — User identifier
Example:

getInstance()

Get the existing SDK instance, or null if not yet initialized.
Example:

isInitialized()

Check whether the SDK has been initialized.
Example:

destroy()

Tear down the SDK instance and clear the singleton. After calling this, you can call init() again with new config.
Example:

Instance Methods

Once you have a GrainTagInstance (from init() or getInstance()), these methods are available:

grain.track()

Same as the top-level track(), but called on the instance directly.

grain.identify()

Same as the top-level identify(), but called on the instance directly.
Grant consent. Upgrades from cookieless to permanent identity tracking.
Parameters:
  • categories — Consent categories to grant. Defaults to ['necessary', 'analytics', 'functional'].
Example:
Revoke consent. Downgrades back to cookieless identity mode.
Parameters:
  • categories — Categories to revoke. If omitted, revokes all.
Example:
Get the current consent state.
Returns: ConsentState object or null if no consent decision has been made.
Example:

grain.flush()

Force-send all pending events immediately.
Example:

grain.destroy()

Tear down the instance. Same as the top-level destroy().

grain.isReady()

Returns true 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.
Parameters:
  • config — Configuration object (see Types)
Returns: GrainAnalytics client instance Example:

track()

Track an event. Two signatures available:

track(eventName, properties?, options?)

Parameters:
  • eventName — Name of the event
  • properties — Optional event properties
  • options.flush — Force immediate send (default: false)
Example:

track(event, options?)

Parameters:
  • event — Complete event object
  • options.flush — Force immediate send
Example:

flush()

Manually flush all queued events.
Returns: Promise that resolves when events are sent Example:
When to use:
  • Before page navigation
  • Critical events
  • Serverless functions
  • Before app closes

setUserId()

Set global user ID for all subsequent events.
Parameters:
  • userId — User identifier, or null to clear
Example:

identify()

Alias for setUserId(). Sets user ID for subsequent events.
Example:

getUserId()

Get current user ID.
Returns: Current user ID or null if not set Example:

setProperty()

Set user properties for analytics and personalization.
Parameters:
  • properties — Up to 4 key-value pairs (values converted to strings)
  • options.userId — Optional user ID override
Returns: Promise that resolves when properties are set Example:
Limits:
  • Maximum 4 properties per request
  • All values converted to strings
  • Subject to rate limiting

destroy()

Clean up resources and send remaining events.
Example:
What it does:
  • 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