type PageClassification = 'cart' | 'checkout' | 'checkoutComplete' | 'collection' | 'customersAccount' | 'customersAuthentication' | 'customersOrders' | 'home' | 'product' | 'search' | 'other' | 'nonShopping';

interface Product {
  name: string | null;
  price: number | null;
  currencyCode: string | null;
  quantity: number | null;
  url: string | null;
  imageUrls: string[] | null;
  description: string | null;
  categoryId: string | null;
  categoryBreadcrumb: string | null;
  categoryVersion: string | null;
  brandName: string | null;
  gtin: string | null;
  mpn: string | null;
  sku: string | null;
  asin: string | null;
}

interface ProductNodeIds {
  nameNodeId: string | null;
  priceNodeId: string | null;
  descriptionNodeId: string | null;
  imagesWrapperNodeId: string | null;
}

interface ExtractProductsOptions {
  includeCategories?: boolean;
  includeProductNodeIds?: boolean;
}

type ExtractProductsResult =
  | {
      products: Product[] | null;
      productsNodeIds: ProductNodeIds[] | null;
      responseShape: 'cart';
    }
  | {
      product: Product;
      productNodeIds: ProductNodeIds | null;
      responseShape: 'product';
    };

interface Order {
  orderDate: string | null;
  orderNumber: string | null;
  orderTotal: string | null;
  orderSubtotal: string | null;
  orderTax: string | null;
  orderShipping: string | null;
  products: Product[] | null;
}

type ExtractOrderResult = {
  order: Order | null;
  pageClassification: 'cart' | 'checkout' | 'checkoutComplete';
};

interface CouponSession {
  publicId: string;
  stackingBehavior: 'unknown' | 'stack' | 'replace';
  state: 'active' | 'complete';
  codes: {
    value: string;
    orderValueBefore: number | null;
    orderValueAfter: number | null;
    entryStatus: 'success' | 'fail' | 'pending' | 'active';
  }[];
}

declare enum SdkEventType {
  COUPON_PAGE_READY = 'COUPON_PAGE_READY',
  COUPON_SESSION_COMPLETED = 'COUPON_SESSION_COMPLETED',
  COUPON_SESSION_UPDATED = 'COUPON_SESSION_UPDATED',
  COUPON_SESSION_RESUMED = 'COUPON_SESSION_RESUMED',
  COUPON_SESSION_STARTED = 'COUPON_SESSION_STARTED',
  PAGE_CLASSIFIED = 'PAGE_CLASSIFIED',
  ORDER_EXTRACTED = 'ORDER_EXTRACTED',
  USER_GENERATED_CODE = 'USER_GENERATED_CODE',
}

interface CouponSessionStartedEvent {
  type: SdkEventType.COUPON_SESSION_STARTED;
  data: {
    session: CouponSession;
  };
}

interface CouponSessionResumedEvent {
  type: SdkEventType.COUPON_SESSION_RESUMED;
  data: {
    session: CouponSession;
  };
}

interface CouponSessionCompletedEvent {
  type: SdkEventType.COUPON_SESSION_COMPLETED;
  data: {
    session: CouponSession;
  };
}

interface CouponSessionUpdatedEvent {
  type: SdkEventType.COUPON_SESSION_UPDATED;
  data: {
    session: CouponSession;
  };
}

interface CouponPageReadyEvent {
  type: SdkEventType.COUPON_PAGE_READY;
  data: {
    autoApplyAvailable: boolean;
  };
}

interface PageClassifiedEvent {
  type: SdkEventType.PAGE_CLASSIFIED;
  data: {
    classification: PageClassification;
  };
}

interface OrderExtractedEvent {
  type: SdkEventType.ORDER_EXTRACTED;
  data: {
    order: Order;
    pageClassification: 'cart' | 'checkout' | 'checkoutComplete';
  };
}

interface UserGeneratedCodeEvent {
  type: SdkEventType.USER_GENERATED_CODE;
  data: {
    code: string;
  };
}

type SdkEvent =
  | CouponPageReadyEvent
  | CouponSessionCompletedEvent
  | CouponSessionResumedEvent
  | CouponSessionStartedEvent
  | CouponSessionUpdatedEvent
  | PageClassifiedEvent
  | OrderExtractedEvent
  | UserGeneratedCodeEvent;

type CouponPageOverride = {
  /**
   * The hostname of the coupon page. Requires an exact match.
   */
  hostname: string;

  /**
   * The pathname regex of the coupon page. Any pathname that matches this regex will be considered a coupon page.
   */
  pathnameRegex: string;

  /**
   * The selector of the element that will be used to determine if the coupon page is ready.
   */
  readinessElementSelector: string;
};

/**
 * Configuration options for the Sleek SDK.
 */
export interface SdkOptions {
  /**
   * Controls which SDK features are enabled or disabled.
   */
  featureControls: {
    /**
     * Whether to emit page classification events. Default = `false`.
     */
    emitPageClassification?: boolean;
    /**
     * Whether to emit order extraction events. Default = `false`.
     */
    emitOrders?: boolean;
    /**
     * Whether to emit coupon page ready events. Default = `false`.
     */
    emitCouponPageReady?: boolean;
    /**
     * Whether to emit user generated code events. Default = `false`.
     */
    emitUserGeneratedCode?: boolean;
  };

  /**
   * Controls whether the SDK will emit console logs. Default = `false`.
   */
  enableDebug: boolean;

  /**
   * Custom coupon page overrides that will be used to emit coupon page ready events.
   */
  couponPageOverrides: CouponPageOverride[];

  /**
   * Additional properties that will be added to all client-side events tracked by the SDK.
   */
  addedAnalyticsProperties: Record<string, string | number | boolean>;

  /**
   * CSS selectors to omit from DOM captures.
   */
  cssSelectorsToSkipCapture: string[];

  /**
   * The distinct ID for the current user.
   */
  sdkUserDistinctId: string;

  envOverride: Record<string, string | undefined>;
}

interface Sdk {
    startCouponAutoApply(codes: string[]): Promise<void>;
    stopCouponAutoApply(): Promise<void>;
    classifyPage(): Promise<PageClassification>;
    extractProducts(options?: ExtractProductsOptions): Promise<ExtractProductsResult>;
    extractOrder(): Promise<ExtractOrderResult>;
}

export type SdkEventListener = (event: SdkEvent) => void;

declare function initializeSdk(apiKey: string, options?: Partial<SdkOptions>, listeners?: SdkEventListener[]): Sdk;
declare function getSdk(): Sdk;

Event information

Each event is fired per-tab when something happens in the SDK. In detail:
  • COUPON_PAGE_READY - fired when the tab navigates to a page where the SDK can run CAA.
  • COUPON_SESSION_STARTED - fired when the SDK begins automatically applying coupons on a tab.
  • COUPON_SESSION_RESUMED - fired when a tab reloads and CAA continues. Site specific, but stores like Bed Bath & Beyond reload the tab when a coupon is input. This event is fired since the UI and the couponing process continues on the tab after reload.
  • COUPON_SESSION_UPDATED - fired when the entire “session” of CAA has updated. I.e. a new coupon is being tested, or a previously tested coupon’s result is available. This provides incremental updates throughout the session lifecycle.
  • COUPON_SESSION_COMPLETED - fired when the entire “session” of CAA has completed. I.e. the entire set of coupons for a site are finished inputting.
  • USER_GENERATED_CODE - fired when the user inputs a coupon code into the tab manually after CAA has been completed.
  • PAGE_CLASSIFIED - fired when the SDK has classified the page automatically.
  • ORDER_EXTRACTED - fired when the SDK has extracted an order automatically.