@tranthor/sdk-node
Version:
Tranthor's node sdk for customer engagement.
187 lines (186 loc) • 5.56 kB
TypeScript
import { IdentifyData, InitParamsDataBase, PageData, ScreenData, TrackData, TranthorSdkBase } from "@tranthor/sdk-core-library";
export * from "@tranthor/sdk-core-library";
export type TimeoutHandle = ReturnType<typeof setTimeout>;
/**
* Tranthor Node.js SDK. Use it to send events to Tranthor, a
* customer engagement on autopilot.
* @class
*
* @example
* ```typescript
* import { TranthorSdk } from '@tranthor/sdk-node';
*
* // Initialize with workspace credentials
* await TranthorSdk.init({
* writeKey: 'Basic your-write-key-here',
* host: 'https://app.tranthor.com' // Optional custom endpoint
* });
*
* // Identify user with events and record attributes about them. Save user id and an identify user and add attributes about them.
* // This is how you can add optional attributes like email, name, etc.
* TranthorSdk.identify({
* userId: 'user_123',
* traits: {
* email: 'marc@legend.com',
* firstName: 'Marc',
* lastName: 'Legend',
* plan: 'premium'
* }
* });
*
* // Track custom events with properties where you record actions your users perform and any properties about the action.
* TranthorSdk.track({
* userId: 'user_123',
* event: 'purchase_completed',
* properties: {
* amount: 49.99,
* currency: 'USD'
* }
* });
*
* // Here you can record specific screen engagement on the mobile devices.
* // along with any properties about the screen.
* TranthorSdk.screen({
* userId: 'user_123',
* name: 'restaurant_screen',
* properties: {
* order_id: '1234567890',
* restaurant_name: 'The best restaurant',
* items: ['burger', 'fries', 'soda']
* }
* });
*
* // Flush pending events to Tranthor. This is important for async events to ensure they are sent synchronously.
* await TranthorSdk.flush();
* ```
*/
export declare class TranthorSdk {
private static instance;
private baseSdk;
private static createBaseSdk;
/**
* Initialize singleton SDK instance
* @param initParams - Configuration parameters
* @returns Promise resolving to initialized SDK instance
*
* @example
* ```typescript
* // Basic initialization
* const sdk = await TranthorSdk.init({
* writeKey: 'Basic your-key-here'
* });
* ```
*/
static init(initParams: InitParamsDataBase): Promise<TranthorSdk>;
/**
* Create new SDK instance (non-singleton)
* @param initParams - Configuration parameters
* @returns Promise resolving to new SDK instance
*
* @example
* ```typescript
* // Multi-instance scenario
* const adminSdk = await TranthorSdk.initNew({
* writeKey: 'admin-key'
* });
*
* const clientSdk = await TranthorSdk.initNew({
* writeKey: 'client-key'
* });
* ```
*/
static initNew(initParams: InitParamsDataBase): Promise<TranthorSdk>;
constructor(baseSdk: TranthorSdkBase<TimeoutHandle>);
/**
* Record user identity and attributes. This is how you can add optional attributes like email, name, etc.
* @param params - Identification payload
* @returns
* @example
* ```typescript
* // Basic user identification
* TranthorSdk.identify({
* userId: '123',
* traits: {
* name: 'Alice Smith',
* accountStatus: 'active'
* }
* });
* ```
*/
static identify(params: IdentifyData): void;
identify(params: IdentifyData): void;
/**
* Track custom event with properties where you record actions your users perform and any properties about the action.
* @param params - Event tracking payload
* @returns
*
* @example
* ```typescript
* // Track subscription event
* TranthorSdk.track({
* userId: '123',
* event: 'subscription_created',
* properties: {
* plan: 'pro',
* trial_days: 14
* }
* });
* ```
*/
static track(params: TrackData): void;
track(params: TrackData): void;
/**
* Record page view with metadata about the page. This is how you can record page views on the web.
* @param params - Page view payload
* @returns
* @example
* ```typescript
* // Track dashboard page view
* TranthorSdk.page({
* userId: '123',
* name: 'Dashboard',
* properties: {
* tab: 'analytics',
* referrer: 'https://search.example.com'
* }
* });
* ```
*/
static page(params: PageData): void;
page(params: PageData): void;
/**
* Record mobile screen view with context about the screen. This is how you can record screen views on the mobile devices.
* @param params - Screen view payload
* @returns
*
* @example
* ```typescript
* // Track mobile settings screen
* TranthorSdk.screen({
* userId: '123',
* name: 'Settings',
* properties: {
* section: 'notifications',
* os_version: '14.4.1'
* }
* });
* ```
*/
static screen(params: ScreenData): void;
screen(params: ScreenData): void;
/**
* Immediately send all queued events
* @returns Promise resolving when flush completes
* @returns
* @example
* ```typescript
* // Flush before application exit
* process.on('SIGTERM', async () => {
* await TranthorSdk.flush();
* process.exit(0);
* });
* ```
*/
static flush(): Promise<void> | undefined;
flush(): Promise<void>;
}