UNPKG

@chordcommerce/analytics

Version:

Chord Commerce event tracking

132 lines (131 loc) 4.05 kB
/** * 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 */ // eslint-disable-next-line no-shadow export var OtConsentModel; (function (OtConsentModel) { OtConsentModel["optIn"] = "opt-in"; OtConsentModel["optOut"] = "opt-out"; OtConsentModel["implied"] = "implied consent"; })(OtConsentModel || (OtConsentModel = {})); /** * 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 const getOneTrustGlobal = () => { const oneTrust = window.OneTrust; if (!oneTrust) return undefined; // Duck typing - check for required methods to ensure OneTrust is fully loaded if (typeof oneTrust === 'object' && 'OnConsentChanged' in oneTrust && 'IsAlertBoxClosed' in oneTrust && 'GetDomainData' in oneTrust) { return oneTrust; } return undefined; }; /** * Get the raw OnetrustActiveGroups string. * * @returns Comma-separated string of active group IDs, or undefined */ export const getOneTrustActiveGroups = () => { const groups = window.OnetrustActiveGroups; if (!groups || typeof groups !== 'string') return undefined; return groups; }; /** * 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 const getNormalizedActiveGroupIds = () => { const groups = getOneTrustActiveGroups(); if (!groups) return []; return groups.trim().split(',').filter(Boolean); }; /** * 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 const hasActiveGroups = () => { return getNormalizedActiveGroupIds().length > 0; }; /** * Get all configured consent groups from OneTrust. * * @returns Array of group info objects */ export const getAllGroups = () => { const oneTrust = getOneTrustGlobal(); if (!oneTrust) return []; return oneTrust.GetDomainData().Groups.map((group) => ({ groupId: group.CustomGroupId.trim(), })); }; /** * 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 const getNormalizedCategories = () => { const activeGroupIds = getNormalizedActiveGroupIds(); return getAllGroups().reduce((acc, group) => { acc[group.groupId] = activeGroupIds.includes(group.groupId); return acc; }, {}); }; /** * Coerce OneTrust consent model name to our simplified model. * * @param model - OneTrust consent model name * @returns 'opt-in' or 'opt-out' */ export const coerceConsentModel = (model) => { switch (model) { case OtConsentModel.optIn: case OtConsentModel.implied: case 'opt-in': case 'implied consent': return 'opt-in'; case OtConsentModel.optOut: case 'opt-out': default: // Default to opt-out for unknown models (e.g., 'custom', 'notice') return 'opt-out'; } };