UNPKG

@phantomstudios/ft-lib

Version:

A collection of Javascript UI & tracking utils for FT sites

117 lines 4.8 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.consentMonitor = void 0; var debug_1 = __importDefault(require("debug")); var debug = (0, debug_1.default)("@phantomstudios/ft-lib"); var DEFAULT_DEV_HOSTS = ["localhost", "phq", ".app", "preview"]; 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()) || ""; }; this.init = function () { _this.cookieConsentTest(); setInterval(_this.cookieConsentTest, 3000); //Simulate cookie consent behaviour in non-prod environments _this._devHosts.map(function (devHost) { return _this._hostname.includes(devHost) && _this.setDevCookieHandler(); }); }; this.cookieConsentTest = function () { if (window.permutive) { if (!_this._isInitialized) { if (_this.getCookieValue("FTConsent").includes("behaviouraladsOnsite%3Aon")) { _this.permutiveConsentOn(); } else { _this.permutiveConsentOff(); } _this._isInitialized = true; } else if (_this.getCookieValue("FTConsent").includes("behaviouraladsOnsite%3Aon") && !_this.consent) { debug("setting permutive tracking consent: on"); _this.permutiveConsentOn(); } else if (!_this.getCookieValue("FTConsent").includes("behaviouraladsOnsite%3Aon") && _this.consent) { debug("setting permutive tracking consent: off"); _this.permutiveConsentOff(); } } }; this.setDevCookieHandler = function () { _this._isDevEnvironment = true; debug("setting development environment from host match"); var oCookieMessage = document.getElementsByClassName("o-cookie-message")[0]; if (oCookieMessage) { var onCookieMessageAct_1 = function () { debug("setting development FT consent cookies"); document.cookie = "FTConsent=behaviouraladsOnsite%3Aon"; document.cookie = "FTCookieConsentGDPR=true"; oCookieMessage.removeEventListener("oCookieMessage.act", onCookieMessageAct_1, false); }; oCookieMessage.addEventListener("oCookieMessage.act", onCookieMessageAct_1); } }; this.permutiveConsentOn = function () { window.permutive.consent({ opt_in: true, token: "behaviouraladsOnsite:on", }); _this._consent = true; }; this.permutiveConsentOff = function () { window.permutive.consent({ opt_in: false }); _this._consent = false; }; if (Array.isArray(devHosts)) { this._devHosts = devHosts.concat(DEFAULT_DEV_HOSTS); } else if (devHosts === undefined) { this._devHosts = DEFAULT_DEV_HOSTS; } else { this._devHosts = DEFAULT_DEV_HOSTS; this._devHosts.push(devHosts); } this._hostname = hostname || window.location.hostname; this.init(); } 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 }); return consentMonitor; }()); exports.consentMonitor = consentMonitor; //# sourceMappingURL=index-old.js.map