@chordcommerce/analytics
Version:
Chord Commerce event tracking
77 lines (76 loc) • 3 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.onetrustConsentAdapter = void 0;
var onetrust_api_js_1 = require("./onetrust-api.js");
/**
* Returns whether the user can currently be tracked.
* Uses duck typing to check if OneTrust is fully loaded.
*/
var userCanBeTracked = function () {
// Always loads when OneTrust is ready, a la `consentModel: 'opt-out'`
return !!(0, onetrust_api_js_1.getOneTrustGlobal)();
};
/**
* Returns the user's current consent configuration.
* Reads FRESH from OneTrust - no caching!
*/
var getCurrentConsent = function () {
var api = (0, onetrust_api_js_1.getOneTrustGlobal)();
if (!api)
return {};
return {
consentCategories: (0, onetrust_api_js_1.getNormalizedCategories)(),
};
};
/**
* Handles subsequent changes to consent configuration.
* Sets up OnConsentChanged listener with retry logic.
*/
var handleConsentUpdates = function (updateCdpConsent, options) {
var maxRetries = 100;
var retryInterval = 50;
var attempts = 0;
var setupHandler = function () {
var api = (0, onetrust_api_js_1.getOneTrustGlobal)();
if (!api) {
attempts++;
if (attempts < maxRetries) {
if (options.debug && options.enableLogging) {
console.log('[Chord Analytics DEBUG]: OneTrust API not found, retrying...');
}
setTimeout(setupHandler, retryInterval);
}
return;
}
if (options.debug && options.enableLogging) {
console.log('[Chord Analytics DEBUG]: OneTrust API found, setting up consent changed handler');
}
api.OnConsentChanged(function () {
updateCdpConsent(getCurrentConsent());
if (options.debug && options.enableLogging) {
console.log('[Chord Analytics DEBUG]: OneTrust consent changed, sending Consent Preferences Updated track event');
}
});
};
setupHandler();
};
var onetrustConsentAdapter = function (options) {
return {
// Existing methods for backwards compatibility
getCurrentConsent: getCurrentConsent,
handleConsentUpdates: function (updateCdpConsent) {
handleConsentUpdates(updateCdpConsent, options);
},
userCanBeTracked: userCanBeTracked,
getCategories: function () { return (0, onetrust_api_js_1.getNormalizedCategories)(); },
isConsentReady: function () { return !!(0, onetrust_api_js_1.getOneTrustGlobal)() && (0, onetrust_api_js_1.hasActiveGroups)(); },
getConsentModel: function () {
var api = (0, onetrust_api_js_1.getOneTrustGlobal)();
if (!api)
return 'opt-out';
return (0, onetrust_api_js_1.coerceConsentModel)(api.GetDomainData().ConsentModel.Name);
},
getApi: onetrust_api_js_1.getOneTrustGlobal,
};
};
exports.onetrustConsentAdapter = onetrustConsentAdapter;