@segment/analytics-consent-wrapper-onetrust
Version:
<img src="img/onetrust-popup.jpg" width="500" />
88 lines • 3.57 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.coerceConsentModel = exports.getNormalizedCategories = exports.getAllGroups = exports.getNormalizedActiveGroupIds = exports.getOneTrustActiveGroups = exports.getOneTrustGlobal = exports.OtConsentModel = void 0;
const validation_1 = require("./validation");
/**
* @example
* ",C0001,C0002" => ["C0001", "C0002"]
*/
const normalizeActiveGroupIds = (oneTrustActiveGroups) => {
return oneTrustActiveGroups.trim().split(',').filter(Boolean);
};
var OtConsentModel;
(function (OtConsentModel) {
OtConsentModel["optIn"] = "opt-in";
OtConsentModel["optOut"] = "opt-out";
OtConsentModel["implicit"] = "implied consent";
})(OtConsentModel = exports.OtConsentModel || (exports.OtConsentModel = {}));
const getOneTrustGlobal = () => {
// window.OneTrust = {...} is used for user configuration e.g. 'Consent Rate Optimization'
// Also, if "show banner" is unchecked, window.OneTrust returns {geolocationResponse: {…}} before it actually returns the OneTrust object
// Thus, we are doing duck typing to see when this object is 'ready'
// function OptAnonWrapper() is the idiomatic way to check if OneTrust is ready, but polling for this works
const oneTrust = window.OneTrust;
if (!oneTrust)
return undefined;
if (typeof oneTrust === 'object' &&
'OnConsentChanged' in oneTrust &&
'IsAlertBoxClosed' in oneTrust &&
'GetDomainData' in oneTrust) {
return oneTrust;
}
};
exports.getOneTrustGlobal = getOneTrustGlobal;
const getOneTrustActiveGroups = () => {
const groups = window.OnetrustActiveGroups;
if (!groups)
return undefined;
if (typeof groups !== 'string') {
throw new validation_1.OneTrustApiValidationError(`window.OnetrustActiveGroups is not a string`, groups);
}
return groups;
};
exports.getOneTrustActiveGroups = getOneTrustActiveGroups;
const getNormalizedActiveGroupIds = (oneTrustActiveGroups = (0, exports.getOneTrustActiveGroups)()) => {
if (!oneTrustActiveGroups) {
return [];
}
return normalizeActiveGroupIds(oneTrustActiveGroups || '');
};
exports.getNormalizedActiveGroupIds = getNormalizedActiveGroupIds;
const normalizeGroupInfo = (groupInfo) => ({
groupId: groupInfo.CustomGroupId.trim(),
});
/**
* get *all* groups / categories, not just active ones
*/
const getAllGroups = () => {
const oneTrustGlobal = (0, exports.getOneTrustGlobal)();
if (!oneTrustGlobal)
return [];
return oneTrustGlobal.GetDomainData().Groups.map(normalizeGroupInfo);
};
exports.getAllGroups = getAllGroups;
const getNormalizedCategories = (activeGroupIds = (0, exports.getNormalizedActiveGroupIds)()) => {
return (0, exports.getAllGroups)().reduce((acc, group) => {
return {
...acc,
[group.groupId]: activeGroupIds.includes(group.groupId),
};
}, {});
};
exports.getNormalizedCategories = getNormalizedCategories;
/**
* We don't support all consent models, so we need to coerce them to the ones we do support.
*/
const coerceConsentModel = (model) => {
switch (model) {
case OtConsentModel.optIn:
case OtConsentModel.implicit:
return 'opt-in';
case OtConsentModel.optOut:
return 'opt-out';
default: // there are some others like 'custom' / 'notice' that should be treated as 'opt-out'
return 'opt-out';
}
};
exports.coerceConsentModel = coerceConsentModel;
//# sourceMappingURL=onetrust-api.js.map