Skip to main content

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 Types

import type { GrainTagConfig, GrainTagInstance, ConsentState } from '@grainql/tag';

GrainTagConfig

Configuration options passed to init().
interface GrainTagConfig {
  // Required
  tenantId: string;

  // API
  apiUrl?: string;

  // Debug
  debug?: boolean;

  // Version
  version?: string;

  // Consent
  consentMode?: 'auto' | 'opt-in' | 'opt-out';

  // Tracking features
  enablePageViews?: boolean;
  enableHeatmaps?: boolean;
  enableSnapshots?: boolean;
  enableRescue?: boolean;

  // Advanced
  batchSize?: number;
  flushInterval?: number;
  retryAttempts?: number;
}
FieldTypeDefaultDescription
tenantIdstringrequiredYour tenant identifier
apiUrlstringProduction URLOverride the API endpoint
debugbooleanfalseEnable debug logging
versionstringSDK version override
consentMode'auto' | 'opt-in' | 'opt-out''auto'Consent handling strategy
enablePageViewsbooleantrueAuto-track page views
enableHeatmapsbooleantrueTrack clicks and scroll depth
enableSnapshotsbooleantrueCapture DOM snapshots
enableRescuebooleantrueEnable rescue tracking
batchSizenumber50Max events per batch
flushIntervalnumber5000Flush interval in milliseconds
retryAttemptsnumber3Number of retry attempts

GrainTagInstance

The instance returned by init() and getInstance().
interface GrainTagInstance {
  track(eventName: string, properties?: Record<string, unknown>): void;
  identify(userId: string): void;
  consent: {
    grant(categories?: string[]): void;
    revoke(categories?: string[]): void;
    status(): ConsentState | null;
  };
  flush(): Promise<void>;
  destroy(): void;
  isReady(): boolean;
}

ConsentState

Returned by grain.consent.status().
interface ConsentState {
  granted: boolean;
  categories: string[];
  timestamp: number;
  version: string;
}
FieldTypeDescription
grantedbooleanWhether consent is currently granted
categoriesstring[]Active consent categories (e.g. ['necessary', 'analytics'])
timestampnumberUnix timestamp of the consent decision
versionstringConsent version identifier

ConsentMode

type ConsentMode = 'auto' | 'opt-in' | 'opt-out';
  • auto — Starts in cookieless mode, upgrades when consent is granted
  • opt-in — Requires explicit consent before any tracking beyond necessary
  • opt-out — Full tracking by default, respects revocation

ConsentCategory

type ConsentCategory = 'necessary' | 'analytics' | 'functional' | 'marketing';

Analytics Web Types

import type { GrainConfig, GrainEvent, AuthProvider } from '@grainql/analytics-web';

GrainConfig

Configuration options for createGrainAnalytics().
interface GrainConfig {
  // Required
  tenantId: string;

  // Authentication
  authStrategy?: 'NONE' | 'SERVER_SIDE' | 'JWT';
  secretKey?: string;
  authProvider?: AuthProvider;

  // User
  userId?: string;

  // API
  apiUrl?: string;

  // Batching
  batchSize?: number;
  flushInterval?: number;

  // Retry
  retryAttempts?: number;
  retryDelay?: number;

  // Remote Config
  defaultConfigurations?: Record<string, string>;
  configCacheKey?: string;
  configRefreshInterval?: number;
  enableConfigCache?: boolean;

  // Debug
  debug?: boolean;
}

GrainEvent

Event structure for tracking.
interface GrainEvent {
  eventName: string;
  userId?: string;
  properties?: Record<string, unknown>;
  timestamp?: Date;
}

AuthProvider

JWT authentication provider interface.
interface AuthProvider {
  getToken(): Promise<string> | string;
}
Example:
const authProvider: AuthProvider = {
  async getToken() {
    return await auth0.getAccessToken();
  }
};

Options Types

SendEventOptions

Options for the Analytics Web track() method.
interface SendEventOptions {
  flush?: boolean;  // Force immediate send
}

SetPropertyOptions

Options for the Analytics Web setProperty() method.
interface SetPropertyOptions {
  userId?: string;  // User ID override
}

RemoteConfigOptions

Options for Analytics Web configuration methods.
interface RemoteConfigOptions {
  immediateKeys?: string[];
  properties?: Record<string, string>;
  userId?: string;
  forceRefresh?: boolean;
}

Response Types

RemoteConfigResponse

API response for configuration fetch.
interface RemoteConfigResponse {
  userId: string;
  snapshotId: string;
  configurations: Record<string, string>;
  isFinal: boolean;
  qualifiedSegments: string[];
  qualifiedRuleSets: string[];
  timestamp: string;
  isFromCache: boolean;
}

Template Event Types

LoginEventProperties

interface LoginEventProperties extends Record<string, unknown> {
  method?: string;
  success?: boolean;
  errorMessage?: string;
  loginAttempt?: number;
  rememberMe?: boolean;
  twoFactorEnabled?: boolean;
}

SignupEventProperties

interface SignupEventProperties extends Record<string, unknown> {
  method?: string;
  source?: string;
  plan?: string;
  success?: boolean;
  referralCode?: string;
}

CheckoutEventProperties

interface CheckoutEventProperties extends Record<string, unknown> {
  orderId?: string;
  total?: number;
  currency?: string;
  items?: Array<{
    id: string;
    name: string;
    price: number;
    quantity: number;
  }>;
  paymentMethod?: string;
  success?: boolean;
  errorMessage?: string;
  couponCode?: string;
  discount?: number;
}

PurchaseEventProperties

interface PurchaseEventProperties extends Record<string, unknown> {
  orderId?: string;
  total?: number;
  currency?: string;
  paymentMethod?: string;
  shipping?: number;
  tax?: number;
}

AddToCartEventProperties

interface AddToCartEventProperties extends Record<string, unknown> {
  itemId?: string;
  itemName?: string;
  price?: number;
  quantity?: number;
  currency?: string;
}

RemoveFromCartEventProperties

interface RemoveFromCartEventProperties extends Record<string, unknown> {
  itemId?: string;
  itemName?: string;
  price?: number;
  quantity?: number;
}

PageViewEventProperties

interface PageViewEventProperties extends Record<string, unknown> {
  page?: string;
  title?: string;
  url?: string;
  referrer?: string;
}

SearchEventProperties

interface SearchEventProperties extends Record<string, unknown> {
  query?: string;
  results?: number;
  filters?: Record<string, unknown>;
  sortBy?: string;
}

React Hook Types

import type { UseConfigResult, UseAllConfigsResult } from '@grainql/analytics-web/react';

UseConfigResult

interface UseConfigResult {
  value: string | undefined;
  isRefreshing: boolean;
  error: Error | null;
  refresh: () => Promise<void>;
}

UseAllConfigsResult

interface UseAllConfigsResult {
  configs: Record<string, string>;
  isRefreshing: boolean;
  error: Error | null;
  refresh: () => Promise<void>;
}

Listener Types

ConfigChangeListener

type ConfigChangeListener = (
  configurations: Record<string, string>
) => void;
Example:
const listener: ConfigChangeListener = (configs) => {
  console.log('Updated:', configs);
};

Importing Types

Grain Tag

import type {
  GrainTagConfig,
  GrainTagInstance,
  ConsentState,
  ConsentMode,
  GrainEvent
} from '@grainql/tag';

Analytics Web

import type {
  GrainConfig,
  GrainEvent,
  AuthProvider,
  SendEventOptions,
  RemoteConfigOptions,
  LoginEventProperties,
  CheckoutEventProperties
} from '@grainql/analytics-web';

React Hook Types

import type {
  UseConfigResult,
  UseAllConfigsResult
} from '@grainql/analytics-web/react';

Next Steps

Core Methods

Event tracking and user management

API Overview

Complete API reference