Skip to main content
This page lists the core methods of Grain Tag and Analytics Web. Every signature is copied from the .d.ts files of the published packages. Read SDK reference for the import forms.

Grain Tag

The instance also has track, identify, and destroy with the same signatures as the module functions. The script build exposes init, getInstance, track, identify, and the GrainTag class on window.GrainTag.

init

In a browser, init creates the instance and starts automatic tracking. Outside a browser, init returns a no-op stub. If init runs a second time, it returns the existing instance and logs a warning. To start again with a new configuration, call destroy first.

getInstance

This function has no parameters. It returns null before init runs.

isInitialized

This function has no parameters.

track

If track runs before init, the event goes into a queue. The SDK replays the queue after init.

identify

If identify runs before init, the call goes into the same queue as track. Read Identify users.

destroy

This function has no parameters. It removes the instance and clears the singleton. After destroy, init can run again with a new configuration.
A grant switches the identity from a daily rotating ID to a permanent ID. Read Privacy and consent.
A revoke switches the identity back to a daily rotating ID.
This method has no parameters. It returns null when the user made no consent decision.

flush

This method has no parameters. It sends every queued event now and resolves when the request is done.

isReady

This method has no parameters. It returns true when the instance is a real instance in a browser, and false for the no-op stub.

updateDataLayerConnectors

Analytics Web

Every method is a member of a GrainAnalytics instance. Create the instance with new GrainAnalytics(config) or createGrainAnalytics(config). Every example below starts from this instance.

track

Read Track an event for batching and delivery.

identify

setUserId

getUserId

This method has no parameters. It returns null when no user ID is set.

getEffectiveUserId

This method has no parameters. It returns the user ID that you set, or the anonymous ID that the client generated. The anonymous ID is a daily rotating ID in cookieless mode and a permanent ID after consent. Read Identify users.

getSessionId

This method has no parameters. The session ID is in memory only before consent and persistent after consent.

login

logout

This method has no parameters. It clears the user ID and the token. Later events use the anonymous ID again.

setProperty

Read User properties.

flush

This method has no parameters. It sends every queued event now. Call it before a page navigation, before a serverless function returns, and before the application closes.

grantConsent

A grant switches the identity from a daily rotating ID to a permanent ID. Read Privacy and consent.

revokeConsent

getConsentState

This method has no parameters. It returns null when the user made no consent decision.

hasConsent

onConsentChange

offConsentChange

destroy

This method has no parameters. It sends the queued events, removes the event listeners, stops the timers, and stops the configuration refresh. When the application unmounts, call it.