@chordcommerce/analytics
Version:
Chord Commerce event tracking
114 lines (113 loc) • 3.34 kB
TypeScript
/**
* OneTrust API Utilities
*
* Provides type-safe access to OneTrust's global API with proper duck typing
* to ensure the API is fully loaded before use.
*
* Modeled after analytics-next's onetrust-api.ts
*/
/**
* OneTrust consent model types
*/
export declare enum OtConsentModel {
optIn = "opt-in",
optOut = "opt-out",
implied = "implied consent"
}
/**
* OneTrust domain data structure
*/
export interface OneTrustDomainData {
ShowAlertNotice: boolean;
Groups: OneTrustGroup[];
ConsentModel: {
Name: OtConsentModel | string;
};
}
export interface OneTrustGroup {
CustomGroupId: string;
}
/**
* OneTrust consent changed event
*/
type OtConsentChangedEvent = CustomEvent<string[]>;
/**
* OneTrust global API interface
*/
export interface OneTrustGlobal {
GetDomainData: () => OneTrustDomainData;
OnConsentChanged: (cb: (event: OtConsentChangedEvent) => void) => void;
IsAlertBoxClosed: () => boolean;
}
declare global {
interface Window {
OneTrust?: OneTrustGlobal | Record<string, unknown>;
OnetrustActiveGroups?: string;
}
}
/**
* Get the OneTrust global API if it's ready.
*
* Uses duck typing to check if OneTrust is fully loaded.
* window.OneTrust may exist but not be ready (e.g., just has geolocationResponse).
*
* @returns OneTrustGlobal if ready, undefined otherwise
*/
export declare const getOneTrustGlobal: () => OneTrustGlobal | undefined;
/**
* Get the raw OnetrustActiveGroups string.
*
* @returns Comma-separated string of active group IDs, or undefined
*/
export declare const getOneTrustActiveGroups: () => string | undefined;
/**
* Get normalized active group IDs as an array.
* Reads FRESH from window.OnetrustActiveGroups each time - no caching!
*
* @returns Array of active group IDs
*
* @example
* // window.OnetrustActiveGroups = ",C0001,C0003,"
* getNormalizedActiveGroupIds() // ["C0001", "C0003"]
*/
export declare const getNormalizedActiveGroupIds: () => string[];
/**
* Check if OnetrustActiveGroups is populated with actual consent categories.
*
* OneTrust sets window.OnetrustActiveGroups after the API object is available,
* so this guards against a race condition where the API exists but consent
* categories have not yet been populated.
*
* @returns true if OnetrustActiveGroups is a non-empty string with at least one group ID
*/
export declare const hasActiveGroups: () => boolean;
/**
* Get all configured consent groups from OneTrust.
*
* @returns Array of group info objects
*/
export declare const getAllGroups: () => {
groupId: string;
}[];
/**
* Get normalized consent categories.
* Reads FRESH from OneTrust globals each time - NO CACHING!
*
* This ensures if a user changes consent between two track() calls,
* each event reflects the consent state AT THAT MOMENT.
*
* @returns Record of category ID to consent boolean
*
* @example
* getNormalizedCategories()
* // { "C0001": true, "C0002": false, "C0003": true }
*/
export declare const getNormalizedCategories: () => Record<string, boolean>;
/**
* Coerce OneTrust consent model name to our simplified model.
*
* @param model - OneTrust consent model name
* @returns 'opt-in' or 'opt-out'
*/
export declare const coerceConsentModel: (model: OtConsentModel | string) => 'opt-in' | 'opt-out';
export {};