@phantomstudios/ft-lib
Version:
A collection of Javascript UI & tracking utils for FT sites
130 lines • 5.04 kB
JavaScript
;
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 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;
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);
}
var hostToCheck = hostname !== null && hostname !== void 0 ? hostname : window.location.hostname;
this._isDevEnvironment = this._devHosts.some(function (h) {
return hostToCheck.includes(h);
});
(0, loadFtCmp_1.loadFtCmpScript)()
.then(function () {
_this.attachCmpListeners();
_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();
};
(_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