@financial-times/o-ads
Version:
This package contains the core functionality used by the FT in providing ads across all of its sites. This includes ft.com, howtospendit.com, ftadviser.com and other specialist titles.
170 lines (133 loc) • 5.05 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _config = _interopRequireWildcard(require("./dist-esm/js/config.js"));
var _slots = _interopRequireDefault(require("./dist-esm/js/slots.js"));
var _gpt = _interopRequireDefault(require("./dist-esm/js/ad-servers/gpt.js"));
var _targeting = _interopRequireDefault(require("./dist-esm/js/targeting.js"));
var _index = _interopRequireDefault(require("./dist-esm/js/utils/index.js"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function _getRequireWildcardCache() { return cache; }; return cache; }
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
/* eslint valid-jsdoc: 0 */
function Ads() {
addDOMEventListener();
if (window && !window.oAds) {
window.oAds = this;
}
}
_config.default.init = _config.init;
_config.default.clear = _config.clear; // bung all our modules on the protoype
Ads.prototype.config = _config.default;
Ads.prototype.slots = _slots.default;
Ads.prototype.gpt = _gpt.default;
Ads.prototype.targeting = _targeting.default;
Ads.prototype.utils = _index.default;
/**
* Initialises the ads library and all sub modules
* @param options {object} a JSON object containing configuration for the current page
*/
Ads.prototype.init = function (options) {
options = options || {};
this.config.init();
var configOptions = Object.assign(options, {
nonPersonalized: !options.disableConsentCookie
});
this.config(configOptions);
if (options.disableConsentCookie) {
this.consents = {
behavioral: true
};
} else {
this.consents = getConsents();
}
if (this.consents.behavioral) {
this.utils.broadcast('consentBehavioral');
}
if (this.consents.programmatic) {
this.utils.broadcast('consentProgrammatic');
}
this.utils.broadcast('initialising');
this.targeting.add(this.config().targeting);
return Promise.resolve(this.initLibrary());
};
/**
* Update page level targeting data in o-ads and GPT
*/
Ads.prototype.updateTargeting = function (data) {
this.targeting.add(data);
this.gpt.updatePageTargeting(this.targeting.get());
};
Ads.prototype.initLibrary = function () {
this.slots.init();
if (this.consents.programmatic) {
this.config({
'nonPersonalized': false
});
this.targeting.add({
"cc": "y"
});
}
this.gpt.init();
this.utils.on('debug', this.debug.bind(this));
this.isInitialised = true;
this.utils.broadcast('initialised', this);
removeDOMEventListener();
return this;
};
Ads.prototype.debug = function () {
var remove = true;
if (localStorage.getItem('oAds')) {
remove = false;
} else {
localStorage.setItem('oAds', true);
}
this.gpt.debug();
this.slots.debug();
this.targeting.debug();
if (remove) {
localStorage.removeItem('oAds');
}
};
Ads.prototype.version = function () {
this.utils.log.warn('DEPRECATION NOTICE: Ads.version() will be deprecated in favour of Ads.getVersion()');
this.utils.log("o-ads version: ".concat(this.utils.getVersion()));
};
Ads.prototype.getVersion = function () {
return this.utils.getVersion();
};
var initAll = function initAll() {
return ads.init().then(() => {
var slots = Array.from(document.querySelectorAll('.o-ads, [data-o-ads-name]'));
slots.forEach(ads.slots.initSlot.bind(ads.slots));
});
};
Ads.prototype.initAll = initAll;
function getConsents() {
// derive consent options from ft consent cookie
var re = /FTConsent=([^;]+)/;
var match = document.cookie.match(re);
if (!match) {
// cookie stasis or no consent cookie found
return {
behavioral: false,
programmatic: false
};
}
var consentCookie = decodeURIComponent(match[1]);
return {
behavioral: consentCookie.indexOf('behaviouraladsOnsite:on') !== -1,
programmatic: consentCookie.indexOf('programmaticadsOnsite:on') !== -1
};
}
function addDOMEventListener() {
document.addEventListener('o.DOMContentLoaded', initAll);
}
function removeDOMEventListener() {
document.removeEventListener('o.DOMContentLoaded', initAll);
}
var ads = new Ads();
var _default = ads;
exports.default = _default;