UNPKG

@phantomstudios/ft-lib

Version:

A collection of Javascript UI & tracking utils for FT sites

173 lines 7.48 kB
"use strict"; var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) { if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) { if (ar || !(i in from)) { if (!ar) ar = Array.prototype.slice.call(from, 0, i); ar[i] = from[i]; } } return to.concat(ar || Array.prototype.slice.call(from)); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.consentMonitor = exports.ConsentMonitor = void 0; var cmp_client_1 = require("@financial-times/cmp-client"); var debug_1 = __importDefault(require("debug")); var loadFtCmp_1 = require("../cmp/loadFtCmp"); var debug = (0, debug_1.default)("@phantomstudios/ft-lib/consentMonitor"); var DEFAULT_DEV_HOSTS = ["localhost", "phq", ".app", "preview"]; var CMP_CHOICE_ACCEPT_ALL = 11; var CMP_CHOICE_REJECT_ALL = 13; var ConsentMonitor = /** @class */ (function () { function ConsentMonitor(hostname, devHosts) { var _this = this; this._consent = false; this._isDevEnvironment = false; this._isInitialized = false; this.getCookieValue = function (name) { var _a; return ((_a = document.cookie.match("(^|;)\\s*" + name + "\\s*=\\s*([^;]+)")) === null || _a === void 0 ? void 0 : _a.pop()) || ""; }; //check for FTConsent - cookiesOnSite to trigger custom consent_update event for GTM tags (banner updated) this.cookieConsentTest = function () { if (!_this._isInitialized || !window || !window.dataLayer) return; if (_this.getCookieValue("FTConsent").includes("cookiesOnsite%3Aon")) { //send consent_update event window.dataLayer.push({ event: "consent_update", consent: true, }); } else { window.dataLayer.push({ event: "consent_update", consent: false, }); } }; this.setDevConsentCookies = function () { _this._isDevEnvironment = true; debug("setting development FT consent cookies"); document.cookie = "FTConsent=behaviouraladsOnsite%3Aon%2CcookiesOnsite%3Aon%2CpermutiveadsOnsite%3Aon"; document.cookie = "FTCookieConsentGDPR=true"; }; if (Array.isArray(devHosts)) { this._devHosts = __spreadArray(__spreadArray([], devHosts, true), DEFAULT_DEV_HOSTS, true); } else if (devHosts === undefined) { this._devHosts = __spreadArray([], DEFAULT_DEV_HOSTS, true); } else { this._devHosts = __spreadArray(__spreadArray([], DEFAULT_DEV_HOSTS, true), [devHosts], false); } this._hostname = hostname || window.location.hostname; this._isDevEnvironment = this._devHosts.some(function (h) { return _this._hostname.includes(h); }); (0, loadFtCmp_1.loadFtCmpScript)() .then(function () { _this.attachCmpListeners(); var propertyConfig = window.location.hostname.endsWith(".ft.com") ? cmp_client_1.properties["FT_DOTCOM_PROD"] : cmp_client_1.properties["FT_DOTCOM_TEST"]; // initialize CMP (0, cmp_client_1.initSourcepointCmp)({ propertyConfig: propertyConfig }); // use cmp client lib to intercept footer 'Manage Cookies' links (opens privacy modal) // Note, function requires very specific link: text = 'Manage Cookies' and href = 'https://ft.com/preferences/manage-cookies' (0, cmp_client_1.interceptManageCookiesLinks)(); _this._isInitialized = true; }) .catch(function (err) { return console.error(err); }); } Object.defineProperty(ConsentMonitor.prototype, "consent", { get: function () { return this._consent; }, enumerable: false, configurable: true }); Object.defineProperty(ConsentMonitor.prototype, "devHosts", { get: function () { return this._devHosts; }, enumerable: false, configurable: true }); Object.defineProperty(ConsentMonitor.prototype, "isDevEnvironment", { get: function () { return this._isDevEnvironment; }, enumerable: false, configurable: true }); Object.defineProperty(ConsentMonitor.prototype, "isInitialized", { get: function () { return this._isInitialized; }, enumerable: false, configurable: true }); Object.defineProperty(ConsentMonitor.prototype, "userHasConsented", { get: function () { return this._consent; }, enumerable: false, configurable: true }); ConsentMonitor.prototype.attachCmpListeners = function () { var _this = this; (0, loadFtCmp_1.enqueueCmpCallback)(function () { var _a, _b, _d, _e; var onReady = function (_l, _u, _t, info) { debug("onConsentReady:", info); if (info.consentedToAll) { _this.enablePermutive(); } else { _this.disablePermutive(); } }; var onChoice = function (_l, _c, typeId) { debug("onMessageChoiceSelect:", typeId); if (typeId === CMP_CHOICE_ACCEPT_ALL) _this.enablePermutive(); else if (typeId === CMP_CHOICE_REJECT_ALL) _this.disablePermutive(); //Simulate cookie consent behaviour in non-prod environments as banner does not set cookies in non .ft.com domains _this._devHosts.map(function (devHost) { return _this._hostname.includes(devHost) && typeId === CMP_CHOICE_ACCEPT_ALL && _this.setDevConsentCookies(); }); // banner updated - check new cookie value to fire consent_update event setTimeout(_this.cookieConsentTest, 3000); }; (_b = (_a = window._sp_).addEventListener) === null || _b === void 0 ? void 0 : _b.call(_a, "onConsentReady", onReady); (_e = (_d = window._sp_).addEventListener) === null || _e === void 0 ? void 0 : _e.call(_d, "onMessageChoiceSelect", onChoice); }); }; ConsentMonitor.prototype.enablePermutive = function () { var _a; if (this._consent) return; debug("Permutive consent: ON"); (_a = window.permutive) === null || _a === void 0 ? void 0 : _a.consent({ opt_in: true, token: "behaviouraladsOnsite:on", }); this._consent = true; }; ConsentMonitor.prototype.disablePermutive = function () { var _a; if (!this._consent) return; debug("Permutive consent: OFF"); (_a = window.permutive) === null || _a === void 0 ? void 0 : _a.consent({ opt_in: false }); this._consent = false; }; return ConsentMonitor; }()); exports.ConsentMonitor = ConsentMonitor; exports.consentMonitor = ConsentMonitor; //# sourceMappingURL=index.js.map