Repository URL to install this package:
|
Version:
2.1.0 ▾
|
'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
var constants = require('../constants.js');
var storage = require('../helpers/storage.js');
/**
* The URL where the OneTrust SDK/CMP integration script is located.
* @type {string}
* @private
*/
var ONETRUST_SDK_URL = 'https://cdn.cookielaw.org/scripttemplates/otSDKStub.js';
/**
* The data attribute name used by OneTrust SDK/CMP to determine which configuration/site is active.
* @type {string}
* @private
*/
var ONETRUST_SDK_SCRIPT_ID_ATTRIBUTE = 'data-domain-script';
/**
* The data attribute name used to tell OneTrust SDK to get the language for the banner/modal/prefence center
* from the document language `html lang=""`.
* @type {string}
* @private
*/
var ONETRUST_SDK_LANGUAGE_FROM_DOCUMENT_ATTRIBUTE = 'data-document-language';
/**
* Cookie set by OneTrust if a banner, alert or something else was closed by either
* accepting/rejecting or by closing the "x" button. If this is set, then we know that
* the user had interactved with the CMP prompts.
* @type {string}
* @private
*/
var CONSENT_ALERT_BOX_CLOSED_COOKIE_NAME = 'OptanonAlertBoxClosed';
/**
* Creates a HTML `<script>` element/node representing the OneTrust `<script>` include.
* @param {string} oneTrustScriptId - The OneTrust script ID.
* @return {HTMLScriptElement|null} The element or null if no OneTrust script ID was provided.
*/
var getOneTrustScriptElement = function getOneTrustScriptElement(oneTrustScriptId) {
if (!oneTrustScriptId) {
return null;
}
/**
* Construct a script node for OneTrust, based on the scripts you get from their UI:
* <script src="https://cdn.cookielaw.org/scripttemplates/otSDKStub.js" type="text/javascript" charset="UTF-8" data-domain-script="e70ae13c-f7a2-4dee-95cb-5926b1d86d7d-test" ></script>
*/
var script = document.createElement('script');
script.setAttribute('type', 'text/javascript');
script.setAttribute('charset', 'UTF-8');
script.setAttribute('src', ONETRUST_SDK_URL);
script.setAttribute(ONETRUST_SDK_SCRIPT_ID_ATTRIBUTE, oneTrustScriptId);
script.setAttribute(ONETRUST_SDK_LANGUAGE_FROM_DOCUMENT_ATTRIBUTE, 'true');
return script;
};
/**
* Installs the OneTrust CMP script into the documents head.
* @param {string} oneTrustScriptId - The OneTrust script ID.
* @param {ConsentCallback} handler - The handler called when the active consents change.
*/
var installConsentFramework = function installConsentFramework(oneTrustScriptId, handler) {
// Do not install consent framework in SSR context
if (typeof document === 'undefined') {
return;
} // Construct the <script> element
var oneTrustScript = getOneTrustScriptElement(oneTrustScriptId);
/**
* The OptanonWrapper callback function will be called automatically by the SDk.
*/
window.OptanonWrapper = function () {
handler();
}; // Insert in <head>
document.getElementsByTagName('head')[0].insertAdjacentElement('afterbegin', oneTrustScript);
};
/**
* Checks if the user interacted with the consent management (eg. dismissed the banner).
* @return {boolean} True if the consent management was interacted with, false if not.
*/
var userDidInteract = function userDidInteract() {
return Boolean(storage.getCookie(CONSENT_ALERT_BOX_CLOSED_COOKIE_NAME));
};
/**
* Checks if the consent management is initialized.
* @returns {boolean} True if initialized, false if not.
*/
var isInitialized = function isInitialized() {
return typeof window.OneTrust !== 'undefined' && typeof window.OnetrustActiveGroups !== 'undefined';
};
/**
* Parses the OneTrust "active consent groups" string into an array that contains
* an item for each active consent group.
* Example: `,C0001,C0003,C0002,STACK42,` => `['C0001', 'C0003', 'C0002', 'STACK42']`
* @param {string} rawActiveGroups - The raw string returned from `window.OnetrustActiveGroups`
* @returns {string[]} - An array of active groups.
*/
var parseActiveGroups = function parseActiveGroups(rawActiveGroups) {
return (// Parse groups from raw string, removing empty strings from the array
(rawActiveGroups || '').split(',').filter(function (group) {
return group;
})
);
};
/**
* Returns the opposite of `parseActiveGroups`: Constructs the raw consent string from parsed groups.
* It always contains the ID for the `Strictly Necessary` category as this category cannot be turned off.
* Example: `['C0001', 'C0003', 'C0002', 'STACK42']` => `,C0001,C0003,C0002,STACK42,`
* @param {string[]} activeGroups - The parsed groups.
* @returns {string} - The raw string like it would be returned from `window.OnetrustActiveGroups`
*/
var getRawStringFromParsedActiveGroups = function getRawStringFromParsedActiveGroups() {
var activeGroups = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
// Ensure that the Strictly Necessary category is always present
if (!activeGroups.includes(constants.CONSENT_CATEGORIES.STRICTLY_NECESSARY)) {
activeGroups.unshift(constants.CONSENT_CATEGORIES.STRICTLY_NECESSARY);
}
return ",".concat(activeGroups.join(','), ",");
};
/**
* Watches for consent being obtained by the Consent Management Framework. This can either be trough
* explicit prompting of the user for consent, or if they already did that, reading the existing consents.
* @param {TrackingApiOptions} options - The configuration options for API client
* @param {ConsentCallback} handler - called when consent is initialized/changed
*/
var watchForConsent = function watchForConsent(options, handler) {
var oneTrustScriptId = options.env.oneTrustScriptId;
var consentAdapter = function consentAdapter() {
var rawActiveGroups = window.OnetrustActiveGroups;
var activeGroups = parseActiveGroups(rawActiveGroups);
var context = {
isInitialized: isInitialized(),
userDidInteract: userDidInteract(),
rawActiveGroups: rawActiveGroups
};
handler(activeGroups, context);
};
if (oneTrustScriptId) {
installConsentFramework(oneTrustScriptId, consentAdapter);
} else {
// If there is no OneTrust script ID present, we cannot install the consent framework and thus not obtain actual user
// consent through OneTrust. In this case, we implicitly grant (simulate) consent to the "strictly necessary" category.
// This will cause the trackers to be blocked as they are in different categories.
// The "strictly necessary" category is simulated and not an empty array, as this behaviour aligns with the way OneTrust handles initial/default consent:
// the "strictly necessary" is always enabled whilst other consent categories are not.
console.warn('No OneTrust script ID provided for consent management framework. Trackers will not have user consent.'); // Call the listener/handler implicitly with consent given to the "scrictly necessary" category
var context = {
isInitialized: false,
userDidInteract: false,
rawActiveGroups: getRawStringFromParsedActiveGroups([constants.CONSENT_CATEGORIES.STRICTLY_NECESSARY])
};
handler([constants.CONSENT_CATEGORIES.STRICTLY_NECESSARY], context);
}
};
/**
* Opens the OneTrust consent management center.
*/
var handleShowConsentPreferences = function handleShowConsentPreferences() {
if (!window.OneTrust) {
throw new Error('OneTrust is not yet loaded or not yet initialized.');
}
window.OneTrust.ToggleInfoDisplay();
};
/**
* Changes the language/locale of the consent preferences and other prompts associated with consent management.
* @param {string} locale - The locale/language code (eg `en`, `de`) to switch to.
*/
var handleChangeConsentPreferencesLanguage = function handleChangeConsentPreferencesLanguage() {
var locale = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 'en';
var localeWithoutRegion = locale.split('-')[0];
if (!window.OneTrust) {
throw new Error('OneTrust is not yet loaded or not yet initialized.');
}
window.OneTrust.changeLanguage(localeWithoutRegion);
};
/**
* Checks if the required consent is currently active.
* @param {string} consentToCheck - The consent that should be checked
* @param {string[]} activeConsents - The currently active consents.
* @returns {boolean} - True if consent has been given.
*/
var hasConsent = function hasConsent(consentToCheck) {
var activeConsents = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
return activeConsents.includes(consentToCheck);
};
exports.getOneTrustScriptElement = getOneTrustScriptElement;
exports.getRawStringFromParsedActiveGroups = getRawStringFromParsedActiveGroups;
exports.handleChangeConsentPreferencesLanguage = handleChangeConsentPreferencesLanguage;
exports.handleShowConsentPreferences = handleShowConsentPreferences;
exports.hasConsent = hasConsent;
exports.installConsentFramework = installConsentFramework;
exports.isInitialized = isInitialized;
exports.parseActiveGroups = parseActiveGroups;
exports.userDidInteract = userDidInteract;
exports.watchForConsent = watchForConsent;
//# sourceMappingURL=consent.js.map