@segment/analytics-consent-wrapper-onetrust
Version:
<img src="img/onetrust-popup.jpg" width="500" />
56 lines • 2.75 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.withOneTrust = void 0;
const analytics_consent_tools_1 = require("@segment/analytics-consent-tools");
const onetrust_api_1 = require("../lib/onetrust-api");
/**
*
* @param analyticsInstance - An analytics instance. Either `window.analytics`, or the instance returned by `new AnalyticsBrowser()` or `AnalyticsBrowser.load({...})`
* @param settings - Optional settings for configuring your OneTrust wrapper
*/
const withOneTrust = (analyticsInstance, settings = {}) => {
return (0, analytics_consent_tools_1.createWrapper)({
// wait for OneTrust global to be available before wrapper is loaded
shouldLoadWrapper: async () => {
await (0, analytics_consent_tools_1.resolveWhen)(() => (0, onetrust_api_1.getOneTrustGlobal)() !== undefined, 500);
},
shouldLoadSegment: async (ctx) => {
const OneTrust = (0, onetrust_api_1.getOneTrustGlobal)();
const consentModel = settings.consentModel?.() ||
(0, onetrust_api_1.coerceConsentModel)(OneTrust.GetDomainData().ConsentModel.Name);
if (consentModel === 'opt-out') {
return ctx.load({
consentModel: 'opt-out',
});
}
else {
await (0, analytics_consent_tools_1.resolveWhen)(() => {
return (
// if any groups at all are consented to
Boolean((0, onetrust_api_1.getNormalizedActiveGroupIds)().length) &&
// if show banner is unchecked in the UI
(OneTrust.GetDomainData().ShowAlertNotice === false ||
// if alert box is closed by end user
OneTrust.IsAlertBoxClosed()));
}, 500);
return ctx.load({ consentModel: 'opt-in' });
}
},
getCategories: () => {
const results = (0, onetrust_api_1.getNormalizedCategories)();
return results;
},
registerOnConsentChanged: settings.disableConsentChangedEvent
? undefined
: (setCategories) => {
(0, onetrust_api_1.getOneTrustGlobal)().OnConsentChanged((event) => {
const normalizedCategories = (0, onetrust_api_1.getNormalizedCategories)(event.detail);
setCategories(normalizedCategories);
});
},
integrationCategoryMappings: settings.integrationCategoryMappings,
enableDebugLogging: settings.enableDebugLogging,
})(analyticsInstance);
};
exports.withOneTrust = withOneTrust;
//# sourceMappingURL=wrapper.js.map