UNPKG

@advertol/service-google-dfp

Version:
249 lines (234 loc) 7.41 kB
'use strict'; function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; } var core = require('@advertol/core'); var manageSideEffects = _interopDefault(require('manage-side-effects')); function _inheritsLoose(t, o) { t.prototype = Object.create(o.prototype), t.prototype.constructor = t, _setPrototypeOf(t, o); } function _setPrototypeOf(t, e) { return _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function (t, e) { return t.__proto__ = e, t; }, _setPrototypeOf(t, e); } var GoogleDfpService = /*#__PURE__*/function (_Service) { function GoogleDfpService(options) { var _this; if (options === void 0) { options = {}; } _this = _Service.call(this) || this; _this.slots = {}; _this.adUnitPaths = {}; _this.filledZoneCallbacks = {}; _this.emptyZoneCallbacks = {}; _this.resolvedSlots = {}; _this.slotResolvedZones = []; _this.refreshedZones = []; _this.displayedZones = []; _this.sideEffects = manageSideEffects(); var _options = options, _options$zones = _options.zones, zones = _options$zones === void 0 ? [] : _options$zones, _options$onSetup = _options.onSetup, onSetup = _options$onSetup === void 0 ? function () {} : _options$onSetup, _options$refreshZones = _options.refreshZones, refreshZones = _options$refreshZones === void 0 ? _this.refreshZones : _options$refreshZones, _options$displayZone = _options.displayZone, displayZone = _options$displayZone === void 0 ? _this.displayZone : _options$displayZone; _this.refreshZones = refreshZones.bind(_this); _this.displayZone = displayZone.bind(_this); zones.forEach(function (_ref) { var id = _ref.id, adUnitPath = _ref.adUnitPath, slot = _ref.slot; _this.addZone({ id: id, adUnitPath: adUnitPath, slot: slot }); }); /* istanbul ignore next */ _this.cmd(function () { _this.setupService(); onSetup.call(_this); _this.setupEvents(); }); return _this; } /** * @param {string} id * @param {string} adUnitPath * @param {Function} callback */ _inheritsLoose(GoogleDfpService, _Service); var _proto = GoogleDfpService.prototype; _proto.addZone = function addZone(_ref2) { var id = _ref2.id, adUnitPath = _ref2.adUnitPath, slot = _ref2.slot; if (id in this.slots && id in this.adUnitPaths) { return; } this.slots[id] = slot; this.adUnitPaths[id] = adUnitPath; } /** * @param {Function} callback [description] */; _proto.cmd /* istanbul ignore next */ = function cmd(callback) { if (typeof window.googletag === 'undefined') { return; } window.googletag.cmd.push(callback); }; _proto.setupService /* istanbul ignore next */ = function setupService() { if (typeof window.googletag === 'undefined') { return; } if (window.googletag.pubadsReady) { return; } window.googletag.pubads().enableSingleRequest(); window.googletag.pubads().collapseEmptyDivs(true); window.googletag.pubads().disableInitialLoad(); window.googletag.enableServices(); }; _proto.setupEvents /* istanbul ignore next */ = function setupEvents() { var _this2 = this; if (typeof window.googletag === 'undefined') { return; } this.sideEffects.add(function () { var handler = function handler(data) { var adUnitPath = data.slot.getAdUnitPath(); var response = data.slot.getResponseInformation(); var callback = _this2.isZoneEmpty(response) ? _this2.emptyZoneCallbacks[adUnitPath] : _this2.filledZoneCallbacks[adUnitPath]; if (typeof callback === 'function') { callback(); } delete _this2.filledZoneCallbacks[adUnitPath]; delete _this2.emptyZoneCallbacks[adUnitPath]; }; window.googletag.pubads().addEventListener('slotRenderEnded', handler); return function () { window.googletag.pubads().removeEventListener('slotRenderEnded', handler); }; }); } /** * @param {HTMLElement} zone.element * @param {string} zone.id */; _proto.displayZone /* istanbul ignore next */ = function displayZone(_ref3) { var element = _ref3.element, id = _ref3.id; if (typeof window.googletag === 'undefined') { return; } window.googletag.display(element.id); } /** * @param {googletag.Slot[]} slots */; _proto.refreshZones /* istanbul ignore next */ = function refreshZones(slots) { if (typeof window.googletag === 'undefined') { return; } window.googletag.pubads().refresh(slots); } /** * @param {Object[]} zones */; _proto.beforeWriteZones = function beforeWriteZones(zones) { var _this3 = this; this.cmd(function () { if (zones.length === 0) { return; } zones.filter(function (_ref4) { var id = _ref4.id; return _this3.slotResolvedZones.indexOf(id) === -1 && id in _this3.slots === true; }).forEach(function (_ref5) { var id = _ref5.id; _this3.slotResolvedZones.push(id); _this3.resolvedSlots[id] = _this3.slots[id](); }); }); } /** * @param {Object[]} zones */; _proto.afterWriteZones = function afterWriteZones(zones) { var _this4 = this; this.cmd(function () { if (zones.length === 0) { return; } var slots = zones.filter(function (_ref6) { var id = _ref6.id; return _this4.refreshedZones.indexOf(id) === -1 && id in _this4.slots === true; }).map(function (_ref7) { var id = _ref7.id; _this4.refreshedZones.push(id); return _this4.resolvedSlots[id]; }); if (slots.length === 0) { return; } _this4.refreshZones(slots); }); } /** * @param {HTMLElement} zone.element * @param {string} zone.id * * @return {Promise<boolean>} */; _proto.writeZone = function writeZone(_ref8) { var _this5 = this; var element = _ref8.element, id = _ref8.id; return new Promise(function (resolve) { var adUnitPath = _this5.adUnitPaths[id]; _this5.filledZoneCallbacks[adUnitPath] = function () { return resolve(true); }; _this5.emptyZoneCallbacks[adUnitPath] = function () { return resolve(false); }; _this5.cmd(function () { if (_this5.displayedZones.indexOf(id) === -1 && id in _this5.slots === true) { _this5.displayedZones.push(id); _this5.displayZone({ element: element, id: id }); } }); }); } /** * @param {*} response * * @return {boolean} */; _proto.isZoneEmpty = function isZoneEmpty(response) { return response === null; }; _proto.afterZoneRegistered = function afterZoneRegistered(_ref9) { var element = _ref9.element; this.sideEffects.add(function () { return function () { element.style.display = ''; element.innerHTML = ''; delete element.dataset.googleQueryId; }; }); }; _proto.destroy = function destroy() { this.sideEffects.removeAll(); }; return GoogleDfpService; }(core.Service); module.exports = GoogleDfpService; //# sourceMappingURL=index.cjs.js.map