UNPKG

@chordcommerce/analytics

Version:

Chord Commerce event tracking

142 lines (141 loc) 4.8 kB
"use strict"; /** * 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 */ Object.defineProperty(exports, "__esModule", { value: true }); exports.coerceConsentModel = exports.getNormalizedCategories = exports.getAllGroups = exports.hasActiveGroups = exports.getNormalizedActiveGroupIds = exports.getOneTrustActiveGroups = exports.getOneTrustGlobal = exports.OtConsentModel = void 0; /** * OneTrust consent model types */ // eslint-disable-next-line no-shadow var OtConsentModel; (function (OtConsentModel) { OtConsentModel["optIn"] = "opt-in"; OtConsentModel["optOut"] = "opt-out"; OtConsentModel["implied"] = "implied consent"; })(OtConsentModel || (exports.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 */ var getOneTrustGlobal = function () { var 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; }; exports.getOneTrustGlobal = getOneTrustGlobal; /** * Get the raw OnetrustActiveGroups string. * * @returns Comma-separated string of active group IDs, or undefined */ var getOneTrustActiveGroups = function () { var groups = window.OnetrustActiveGroups; if (!groups || typeof groups !== 'string') return undefined; return groups; }; exports.getOneTrustActiveGroups = getOneTrustActiveGroups; /** * 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"] */ var getNormalizedActiveGroupIds = function () { var groups = (0, exports.getOneTrustActiveGroups)(); if (!groups) return []; return groups.trim().split(',').filter(Boolean); }; exports.getNormalizedActiveGroupIds = getNormalizedActiveGroupIds; /** * 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 */ var hasActiveGroups = function () { return (0, exports.getNormalizedActiveGroupIds)().length > 0; }; exports.hasActiveGroups = hasActiveGroups; /** * Get all configured consent groups from OneTrust. * * @returns Array of group info objects */ var getAllGroups = function () { var oneTrust = (0, exports.getOneTrustGlobal)(); if (!oneTrust) return []; return oneTrust.GetDomainData().Groups.map(function (group) { return ({ groupId: group.CustomGroupId.trim(), }); }); }; exports.getAllGroups = getAllGroups; /** * 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 } */ var getNormalizedCategories = function () { var activeGroupIds = (0, exports.getNormalizedActiveGroupIds)(); return (0, exports.getAllGroups)().reduce(function (acc, group) { acc[group.groupId] = activeGroupIds.includes(group.groupId); return acc; }, {}); }; exports.getNormalizedCategories = getNormalizedCategories; /** * Coerce OneTrust consent model name to our simplified model. * * @param model - OneTrust consent model name * @returns 'opt-in' or 'opt-out' */ var coerceConsentModel = function (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'; } }; exports.coerceConsentModel = coerceConsentModel;