TypeScript type definitions for Grain Analytics
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;
}
interface GrainEvent {
eventName: string;
userId?: string;
properties?: Record<string, unknown>;
timestamp?: Date;
}
interface AuthProvider {
getToken(): Promise<string> | string;
}
const authProvider: AuthProvider = {
async getToken() {
return await auth0.getAccessToken();
}
};
interface SendEventOptions {
flush?: boolean; // Force immediate send
}
interface SetPropertyOptions {
userId?: string; // User ID override
}
interface RemoteConfigOptions {
immediateKeys?: string[];
properties?: Record<string, string>;
userId?: string;
forceRefresh?: boolean;
}
interface RemoteConfigResponse {
userId: string;
snapshotId: string;
configurations: Record<string, string>;
isFinal: boolean;
qualifiedSegments: string[];
qualifiedRuleSets: string[];
timestamp: string;
isFromCache: boolean;
}
interface LoginEventProperties extends Record<string, unknown> {
method?: string;
success?: boolean;
errorMessage?: string;
loginAttempt?: number;
rememberMe?: boolean;
twoFactorEnabled?: boolean;
}
interface SignupEventProperties extends Record<string, unknown> {
method?: string;
source?: string;
plan?: string;
success?: boolean;
referralCode?: string;
}
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;
}
interface PurchaseEventProperties extends Record<string, unknown> {
orderId?: string;
total?: number;
currency?: string;
paymentMethod?: string;
shipping?: number;
tax?: number;
}
interface AddToCartEventProperties extends Record<string, unknown> {
itemId?: string;
itemName?: string;
price?: number;
quantity?: number;
currency?: string;
}
interface RemoveFromCartEventProperties extends Record<string, unknown> {
itemId?: string;
itemName?: string;
price?: number;
quantity?: number;
}
interface PageViewEventProperties extends Record<string, unknown> {
page?: string;
title?: string;
url?: string;
referrer?: string;
}
interface SearchEventProperties extends Record<string, unknown> {
query?: string;
results?: number;
filters?: Record<string, unknown>;
sortBy?: string;
}
interface UseConfigResult {
value: string | undefined;
isRefreshing: boolean;
error: Error | null;
refresh: () => Promise<void>;
}
interface UseAllConfigsResult {
configs: Record<string, string>;
isRefreshing: boolean;
error: Error | null;
refresh: () => Promise<void>;
}
type ConfigChangeListener = (
configurations: Record<string, string>
) => void;
const listener: ConfigChangeListener = (configs) => {
console.log('Updated:', configs);
};
import type {
GrainConfig,
GrainEvent,
AuthProvider,
SendEventOptions,
RemoteConfigOptions,
LoginEventProperties,
CheckoutEventProperties
} from '@grainql/analytics-web';
import type {
UseConfigResult,
UseAllConfigsResult
} from '@grainql/analytics-web/react';
Was this page helpful?