gpt-mock
Version:
Test library for mocking out Google Publisher Tags
1,522 lines (1,202 loc) • 105 kB
JavaScript
/**
* @file gpt-mock
* @description Test library for mocking out Google Publisher Tags
* @version v1.0.2
* @see {@link https://github.com/krux/gpt-mock/}
* @license Apache-2.0
* @author Seth Yates
* @copyright 2016 Krux Digital, Inc
*/
(function webpackUniversalModuleDefinition(root, factory) {
if(typeof exports === 'object' && typeof module === 'object')
module.exports = factory();
else if(typeof define === 'function' && define.amd)
define([], factory);
else if(typeof exports === 'object')
exports["googletag"] = factory();
else
root["googletag"] = factory();
})(this, function() {
return /******/ (function(modules) { // webpackBootstrap
/******/ // The module cache
/******/ var installedModules = {};
/******/ // The require function
/******/ function __webpack_require__(moduleId) {
/******/ // Check if module is in cache
/******/ if(installedModules[moduleId])
/******/ return installedModules[moduleId].exports;
/******/ // Create a new module (and put it into the cache)
/******/ var module = installedModules[moduleId] = {
/******/ exports: {},
/******/ id: moduleId,
/******/ loaded: false
/******/ };
/******/ // Execute the module function
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
/******/ // Flag the module as loaded
/******/ module.loaded = true;
/******/ // Return the exports of the module
/******/ return module.exports;
/******/ }
/******/ // expose the modules object (__webpack_modules__)
/******/ __webpack_require__.m = modules;
/******/ // expose the module cache
/******/ __webpack_require__.c = installedModules;
/******/ // __webpack_public_path__
/******/ __webpack_require__.p = "";
/******/ // Load entry module and return exports
/******/ return __webpack_require__(0);
/******/ })
/************************************************************************/
/******/ ([
/* 0 */
/***/ function(module, exports, __webpack_require__) {
'use strict';
var _GPT = __webpack_require__(1);
var _GPT2 = _interopRequireDefault(_GPT);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
/** @external {DFP} https://www.google.com/dfp */
/**
* @external {Array#push} https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/push
*/
module.exports = _GPT2['default'];
/***/ },
/* 1 */
/***/ function(module, exports, __webpack_require__) {
'use strict';
exports.__esModule = true;
var _CompanionAdsService = __webpack_require__(2);
var _CompanionAdsService2 = _interopRequireDefault(_CompanionAdsService);
var _ContentService = __webpack_require__(4);
var _ContentService2 = _interopRequireDefault(_ContentService);
var _PubAdsService = __webpack_require__(5);
var _PubAdsService2 = _interopRequireDefault(_PubAdsService);
var _Slot = __webpack_require__(6);
var _Slot2 = _interopRequireDefault(_Slot);
var _SizeMappingBuilder = __webpack_require__(21);
var _SizeMappingBuilder2 = _interopRequireDefault(_SizeMappingBuilder);
var _CommandArray = __webpack_require__(22);
var _CommandArray2 = _interopRequireDefault(_CommandArray);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
/**
* This is the global namespace that the {@link GPT} uses for its API.
*
* For details on this API, see https://developers.google.com/doubleclick-gpt/reference.
*
* Note that the recommended way of handling async is to use {@link GPT#cmd} to
* queue callbacks for when {@link GPT} is ready. These callbacks do not have to check
* {@link GPT#apiReady} as they are guaranteed to execute once the API is set up.
*
* The {@link GPT#cmd} variable is initialized to an empty JavaScript array by
* the GPT tag syntax on the page, and cmd.push is the standard Array.push
* method that adds an element to the end of the array. When the {@link GPT} JavaScript
* is loaded, it looks through the array and executes all the functions in
* order. The script then replaces cmd with a {@link CommandArray} object
* whose push method is defined to execute the function argument passed to it.
* This mechanism allows {@link GPT} to reduce perceived latency by fetching the
* JavaScript asynchronously while allowing the browser to continue rendering
* the page.
*
* @example
* import GPT from 'gpt-mock';
*
* googletag = new GPT();
* googletag.cmd.push(function() {
* googletag.defineSlot('/Test/12345', [728, 90], 'gpt-div-1').addService(googletag.pubads());
* });
*
* googletag.cmd.push(function() {
* googletag.display('gpt-div-1');
* });
*
* // Vitally, you need to tell the library that it has loaded so the commands
* // will be executed.
* googletag._loaded();
*/
var GPT = function () {
/**
* Creates a new GPT instance.
*
* @param {number} version The version to emulate.
*/
function GPT() {
var version = arguments.length <= 0 || arguments[0] === undefined ? 94 : arguments[0];
_classCallCheck(this, GPT);
/**
* Flag indicating that {@link GPT} API is loaded and ready to be called.
* This property will be simply undefined until the API is ready.
*
* @type {boolean|undefined}
*/
this.apiReady = void 0;
/**
* Flag indicating that {@link PubAdsService} is enabled, loaded and fully
* operational.
*
* This property will be simply `undefined` until {@link GPT#enableServices()}
* is called and {@link PubAdsService} is loaded and initialized.
*
* @type {boolean|undefined}
*/
this.pubadsReady = void 0;
/**
* Reference to the global command queue for asynchronous execution of
* {@link GPT}-related calls.
*
* @type {!Array<function()>|!CommandArray}
*/
this.cmd = [];
this._version = version;
this._slots = [];
this._slotCounter = 0;
this._services = {};
this._addService(new _CompanionAdsService2['default'](this));
this._addService(new _ContentService2['default'](this));
this._addService(new _PubAdsService2['default'](this));
this._title = null;
}
/**
* Returns the current version of {@link GPT}.
*
* @returns {string} Version string.
*/
GPT.prototype.getVersion = function getVersion() {
return '' + this._version;
};
/**
* Returns a reference to the {@link CompanionAdsService}.
*
* @returns {CompanionAdsService} Instance of the {@link CompanionAdsService}.
*/
GPT.prototype.companionAds = function companionAds() {
return this._services[_CompanionAdsService2['default']._name];
};
/**
* Returns a reference to the {@link ContentService}.
*
* @returns {ContentService} Instance of the {@link ContentService}.
*/
GPT.prototype.content = function content() {
return this._services[_ContentService2['default']._name];
};
/**
* Returns a reference to the {@link PubAdsService}.
*
* @returns {PubAdsService} Instance of the {@link PubAdsService}.
*/
GPT.prototype.pubads = function pubads() {
return this._services[_PubAdsService2['default']._name];
};
/**
* Enables all {@link GPT} services that have been defined for ad slots on the page.
*/
GPT.prototype.enableServices = function enableServices() {
for (var service in this._services) {
if (this._services.hasOwnProperty(service)) {
this._services[service].enable();
}
}
};
/**
* Creates a new {@link SizeMappingBuilder}.
*
* @returns {SizeMappingBuilder} A new builder.
*/
GPT.prototype.sizeMapping = function sizeMapping() {
return new _SizeMappingBuilder2['default']();
};
/**
* Constructs an ad slot with a given ad unit path and size and associates it
* with the ID of a div element on the page that will contain the ad.
*
* @param {string} adUnitPath Full path of the ad unit with the network code and unit code.
* @param {GeneralSize} size Width and height of the added slot. This is the
* size that is used in the ad request if no responsive size mapping is provided
* or the size of the viewport is smaller than the smallest size provided in the mapping.
* @param {string} [optDiv] ID of the div that will contain this ad unit.
* @returns {Slot} The newly created slot.
*/
GPT.prototype.defineSlot = function defineSlot(adUnitPath, size, optDiv) {
this._slotCounter += 1;
return this._addSlot(new _Slot2['default'](adUnitPath, size, optDiv, this._slotCounter));
};
/**
* Constructs an out-of-page (interstitial) ad slot with the given ad unit path.
* optDiv is the ID of the div element that will contain the ad.
*
* @param {string} adUnitPath Full path of the ad unit with the network code and ad unit code.
* @param {string} [optDiv] ID of the div that will contain this ad unit.
* @returns {Slot} The newly created slot.
*/
GPT.prototype.defineOutOfPageSlot = function defineOutOfPageSlot(adUnitPath, optDiv) {
this._slotCounter += 1;
var slot = new _Slot2['default'](adUnitPath, [], optDiv, this._slotCounter);
slot._outOfPage = true;
return this._addSlot(slot);
};
/**
* Destroys the given slots, removes all related objects and references of
* given slots from {@link GPT}. This API does not support passback slots and
* companion slots. Calling this API clears the ad and removes the slot object
* from the internal state maintained by {@link GPT}. Calling any more functions on
* that slot object will result in undefined behaviour. Note the browser may
* still not free the memory associated with that slot if a reference to it is
* maintained by the publisher page. Calling this API makes the div associated
* with that slot available for reuse.
*
* @param {Array<!Slot>} [optSlots] The array of slots to
* destroy. Array is optional; all slots will be destroyed if it is unspecified.
* @returns {boolean} true if slots have been destroyed, false otherwise.
*/
GPT.prototype.destroySlots = function destroySlots(optSlots) {
for (var _iterator = optSlots || this._slots, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) {
var _ref;
if (_isArray) {
if (_i >= _iterator.length) break;
_ref = _iterator[_i++];
} else {
_i = _iterator.next();
if (_i.done) break;
_ref = _i.value;
}
var slot = _ref;
var i = this._slots.indexOf(slot);
if (i !== -1) {
slot._removeServices();
this._removeSlot(slot);
} else {
return false;
}
}
return true;
};
/**
* Instructs slot services to render the slot. Each ad slot should only be
* displayed once per page. All slots must be defined and have a service
* associated with them before being displayed. The display call must not
* happen until the element is present in the DOM. The usual way to achieve
* that is to place it within a script block within the div element named in
* the method call.
*
* If single request architecture (SRA) is being used, all unfetched ad slots
* at the moment display is called will be fetched in a single instance of
* {@link GPT#display}. To force an ad slot not to display, the entire div
* must be removed.
*
* @param {string} div ID of the div element containing the ad slot.
*/
GPT.prototype.display = function display(div) {
if (div) {
for (var _iterator2 = this._slots, _isArray2 = Array.isArray(_iterator2), _i2 = 0, _iterator2 = _isArray2 ? _iterator2 : _iterator2[Symbol.iterator]();;) {
var _ref2;
if (_isArray2) {
if (_i2 >= _iterator2.length) break;
_ref2 = _iterator2[_i2++];
} else {
_i2 = _iterator2.next();
if (_i2.done) break;
_ref2 = _i2.value;
}
var slot = _ref2;
if (slot.getSlotElementId() === div) {
slot.display();
return;
}
}
}
};
/**
* Opens the Console.
*/
GPT.prototype.openConsole = function openConsole() {};
/**
* Disables the Console.
*/
GPT.prototype.disablePublisherConsole = function disablePublisherConsole() {};
/**
* Sets that title for all ad container iframes created by {@link PubAdsService},
* from this point onwards.
*
* @param {string} title The title to set.
*/
GPT.prototype.setAdIframeTitle = function setAdIframeTitle(title) {
this._title = title;
};
/**
* Adds the given {@link Service}.
*
* @private
* @param {Service} service The service to add
* @returns {Service} The service added
*/
GPT.prototype._addService = function _addService(service) {
this._services[service.getName()] = service;
return service;
};
/**
* Adds the given {@link Slot}.
*
* @private
* @param {Slot} slot The {@link Slot} to add.
* @returns {Slot} The {@link Slot} added.
*/
GPT.prototype._addSlot = function _addSlot(slot) {
if (this._slots.indexOf(slot) === -1) {
this._slots.push(slot);
}
return slot;
};
/**
* Removes the given {@link Slot}.
*
* @private
* @param {Slot} slot The {@link Slot} to remove.
*/
GPT.prototype._removeSlot = function _removeSlot(slot) {
var index = this._slots.indexOf(slot);
if (index !== -1) {
this._slots.splice(index, 1);
}
};
/**
* Tells the {@link GPT} service that it has finished loading. This method
* MUST be called in order for most of the rest of the library to function.
*
* @public
*/
GPT.prototype._loaded = function _loaded() {
if (!this.apiReady) {
this.apiReady = true;
this.cmd = new _CommandArray2['default'](this.cmd);
}
};
return GPT;
}();
exports['default'] = GPT;
/***/ },
/* 2 */
/***/ function(module, exports, __webpack_require__) {
'use strict';
exports.__esModule = true;
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
var _Service2 = __webpack_require__(3);
var _Service3 = _interopRequireDefault(_Service2);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
/**
* Companion Ads service. This service is used by video ads to show companion ads.
*/
var CompanionAdsService = function (_Service) {
_inherits(CompanionAdsService, _Service);
/**
* Creates a new CompanionAdsService.
*
* @param {GPT} gpt The containing {@link GPT} instance.
*/
function CompanionAdsService(gpt) {
_classCallCheck(this, CompanionAdsService);
var _this = _possibleConstructorReturn(this, _Service.call(this, gpt, CompanionAdsService._name));
_this._options = {
syncLoading: false,
refreshUnfilledSlots: null
};
return _this;
}
/**
* The name of the service.
*
* @type {string}
* @private
*/
/**
* Enables the service implementation to be loaded synchronously. This needs
* to be called before {@link GPT#enableServices}.
*
* Note: this call can be only used if gpt.js is also loaded synchronously,
* for example, by using a script element. If called when {@link GPT} is loaded
* asynchronously, the outcome of the loading is undefined.
*/
CompanionAdsService.prototype.enableSyncLoading = function enableSyncLoading() {
this._options.syncLoading = true;
};
/**
* Sets whether companion slots that have not been filled will be automatically
* backfilled. Only slots that are also registered with the {@link PubAdsService} will
* be backfilled. This method can be called multiple times during the page's
* lifetime to turn backfill on and off.
*
* @param {boolean} value true to automatically backfill unfilled slots,
* false to leave them unchanged.
*/
CompanionAdsService.prototype.setRefreshUnfilledSlots = function setRefreshUnfilledSlots(value) {
this._options.refreshUnfilledSlots = value;
};
/* Undocumented
refreshAllSlots()
fillSlot(a, b, c, d)
notifyUnfilledSlots(a)
setClearUnfilledSlots(a)
getDisplayAdsCorrelator()
getVideoStreamCorrelator()
isRoadblockingSupported()
isSlotAPersistentRoadblock()
*/
_createClass(CompanionAdsService, null, [{
key: '_name',
get: function get() {
return 'companion_ads';
}
}]);
return CompanionAdsService;
}(_Service3['default']);
exports['default'] = CompanionAdsService;
/***/ },
/* 3 */
/***/ function(module, exports) {
'use strict';
exports.__esModule = true;
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
/**
* Base service class that contains methods common for all services.
*/
var Service = function () {
/**
* Creates a new {@link Service}.
*
* @param {GPT} gpt The containing {@link GPT} instance.
* @param {string} name The name of the service
*/
function Service(gpt, name) {
_classCallCheck(this, Service);
this._name = name;
this._gpt = gpt;
this._enabled = false;
this._used = false;
this._listeners = {};
this._attributes = {};
this._slots = [];
this._slotCounter = 0;
this._slotIdMap = {};
}
/**
* Returns the name of the service.
*
* @experimental
* @returns {string} The name of the service
*/
Service.prototype.getName = function getName() {
return this._name;
};
/**
* Returns a version string.
*
* @experimental
* @returns {string} The version string.
*/
Service.prototype.getVersion = function getVersion() {
return 'unversioned';
};
/**
* Returns the slots with this service enabled.
*
* @experimental
* @returns {Array<Slot>} The slots
*/
Service.prototype.getSlots = function getSlots() {
return this._slots.slice(0);
};
/**
* Adds the given slot to this service.
*
* @param {Slot} slot The slot to add
* @returns {Slot}
* @private
*/
Service.prototype._addSlot = function _addSlot(slot) {
if (this._slots.indexOf(slot) === -1) {
this._gpt._addSlot(slot);
this._slots.push(slot);
this._slotCounter = this._slotCounter + 1;
var id = slot.getAdUnitPath() + '_' + this._slotCounter;
this._slotIdMap[id] = slot;
}
this._used = true;
return slot;
};
/**
* Removes the given slot from the service.
*
* @param {Slot} slot The slot to remove
* @private
*/
Service.prototype._removeSlot = function _removeSlot(slot) {
var index = this._slots.indexOf(slot);
if (index !== -1) {
this._slots.splice(index, 1);
this._gpt._removeSlot(slot);
var id = slot.getAdUnitPath() + '_' + (index + 1);
delete this._slotIdMap[id];
}
};
/**
* Returns a map of IDs to slots.
*
* @experimental
* @returns {Object} The map of IDs to slots.
*/
Service.prototype.getSlotIdMap = function getSlotIdMap() {
return _extends({}, this._slotIdMap);
};
/**
* Registers a listener that allows you to set up and call a JavaScript
* function when a specific {@link GPT} event happens on the page. The following
* events are supported:
* * googletag.events.ImpressionViewableEvent
* * googletag.events.SlotOnloadEvent
* * googletag.events.SlotRenderEndedEvent
* * googletag.events.SlotVisibilityChangedEvent
*
* An object of the appropriate event type is passed to the listener when it is called.
*
* @param {string} eventType A string representing the type of event generated
* by GPT. Event types are case sensitive.
* @param {function(event: Object)} listener Function that takes a single event object argument.
* @returns {Service} The service object on which the method was called.
*/
Service.prototype.addEventListener = function addEventListener(eventType, listener) {
this._listeners[eventType] = this._listeners[eventType] || [];
this._listeners[eventType].push(listener);
return this;
};
/**
* Fires the specified eventType, calling all registered listeners with the
* specified event object.
*
* @param {string} eventType The event type to fire.
* @param {Object} event The event object to pass to listeners.
* @private
*/
Service.prototype._fireEvent = function _fireEvent(eventType, event) {
for (var _iterator = this._listeners[eventType] || [], _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) {
var _ref;
if (_isArray) {
if (_i >= _iterator.length) break;
_ref = _iterator[_i++];
} else {
_i = _iterator.next();
if (_i.done) break;
_ref = _i.value;
}
var listener = _ref;
listener(event);
}
};
/**
* Returns the value for the AdSense attribute associated with the given key.
*
* @param {string} key Name of the attribute to look for.
* @returns {?string} Current value for the attribute key, or null if the key
* is not present
*/
Service.prototype.get = function get(key) {
return this._attributes[key] || null;
};
/**
* Returns the attribute keys that have been set on this service.
*
* @returns {!Array<string>} Array of attribute keys set on this service.
* Ordering is undefined.
*/
Service.prototype.getAttributeKeys = function getAttributeKeys() {
return Object.keys(this._attributes);
};
/**
* Sets values for AdSense attributes that apply to all ad slots under the
* publisher ads service. See AdSense Attributes for a list of available keys
* and values. Calling this more than once for the same key will override
* previously set values for that key. All values must be set before the
* first display call.
*
* @param {string} key The name of the attribute.
* @param {string} value Attribute value.
* @returns {Service} The service object on which the method was called.
*/
Service.prototype.set = function set(key, value) {
this._attributes[key] = value;
return this;
};
/**
* Constructs and displays an ad slot with the given ad unit path and size.
* This method does not work with single request mode.
*
* @param {string} adUnitPath Ad unit path of slot to be rendered.
* @param {GeneralSize} size Width and height of the slot.
* @param {string} [optDiv] ID of the div containing the slot.
* @param {string} [optClickUrl] The click URL to use on this slot.
*/
Service.prototype.display = function display(adUnitPath, size, optDiv, optClickUrl) {
this._used = true;
this.enable();
this._gpt.defineSlot(adUnitPath, size, optDiv).addService(this).setClickUrl(optClickUrl);
};
/**
* Enables the service.
*
* @experimental
*/
Service.prototype.enable = function enable() {
this._enabled = true;
};
return Service;
}();
exports['default'] = Service;
/***/ },
/* 4 */
/***/ function(module, exports, __webpack_require__) {
'use strict';
exports.__esModule = true;
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
var _Service2 = __webpack_require__(3);
var _Service3 = _interopRequireDefault(_Service2);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
/**
* The content service. This service is used to set the content of a slot manually.
*/
var ContentService = function (_Service) {
_inherits(ContentService, _Service);
/**
* Creates a new {@link ContentService}.
*
* @param {GPT} gpt The containing {@link GPT} instance.
*/
function ContentService(gpt) {
_classCallCheck(this, ContentService);
var _this = _possibleConstructorReturn(this, _Service.call(this, gpt, ContentService._name));
_this._storedContent = [];
return _this;
}
/**
* The name of the service.
*
* @type {string}
* @private
*/
/**
* Enables the service.
*
* @override
*/
ContentService.prototype.enable = function enable() {
_Service.prototype.enable.call(this);
for (var _iterator = this._storedContent, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) {
var _ref;
if (_isArray) {
if (_i >= _iterator.length) break;
_ref = _iterator[_i++];
} else {
_i = _iterator.next();
if (_i.done) break;
_ref = _i.value;
}
var _ref2 = _ref;
var slot = _ref2[0];
var content = _ref2[1];
slot._setContent(content);
}
this._storedContent = null;
};
/**
* Fills a slot with the given content. If services are not yet enabled,
* stores the content and fills it in when services are enabled.
*
* @param {Slot} slot The slot to be filled.
* @param {string} content The HTML content for the slot.
*/
ContentService.prototype.setContent = function setContent(slot, content) {
if (this._enabled) {
slot._setContent(content);
} else {
this._storedContent.push([slot, content]);
}
};
_createClass(ContentService, null, [{
key: '_name',
get: function get() {
return 'content';
}
}]);
return ContentService;
}(_Service3['default']);
exports['default'] = ContentService;
/***/ },
/* 5 */
/***/ function(module, exports, __webpack_require__) {
'use strict';
exports.__esModule = true;
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
var _Service2 = __webpack_require__(3);
var _Service3 = _interopRequireDefault(_Service2);
var _Slot = __webpack_require__(6);
var _Slot2 = _interopRequireDefault(_Slot);
var _TargetingMap = __webpack_require__(20);
var _TargetingMap2 = _interopRequireDefault(_TargetingMap);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
/**
* Publisher Ads service. This service is used to fetch and show ads from your
* {@link DFP} account.
*/
var PubAdsService = function (_Service) {
_inherits(PubAdsService, _Service);
/**
* Creates a new PubAdsService.
*
* @param {GPT} gt The containing {@link GPT} instance.
*/
function PubAdsService(gt) {
_classCallCheck(this, PubAdsService);
var _this = _possibleConstructorReturn(this, _Service.call(this, gt, PubAdsService._name));
_this._categoryExclusions = [];
_this._targeting = new _TargetingMap2['default']();
_this._options = {
collapseEmptyDivs: false,
collapseBeforeAdFetch: false,
initialLoad: true,
asyncRendering: false,
singleRequest: false,
syncRendering: false,
videoAds: false,
centerAds: false,
forceSafeFrame: false,
cookieOptions: null,
ppid: null,
videoContentId: null,
videoCmsId: null,
safeFrameConfig: null,
tagForChildDirectedTreatment: null
};
_this._correlator = Math.random();
return _this;
}
/**
* The name of the service.
*
* @private
* @type {string}
*/
/**
* Enables the service.
*
* @override
*/
PubAdsService.prototype.enable = function enable() {
_Service.prototype.enable.call(this);
this._gpt.pubadsReady = true;
};
/**
* Fetches and displays new ads for specific or all slots on the page.
* Works only in asynchronous rendering mode.
*
* For proper behavior across all browsers, calling refresh must be preceded
* by a call to display the ad slot. If the call to display is omitted, refresh
* may behave unexpectedly. If desired, the {@link PubAdsService#disableInitialLoad} method can be
* used to stop display from fetching an ad.
*
* @param {Array<!Slot>} [optSlots] The slots to refresh. Array is optional;
* all slots will be refreshed if it is unspecified.
* @param {{changeCorrelator: boolean}} [optOptions] Configuration options
* associated with this refresh call. changeCorrelator specifies whether or
* not a new correlator is to be generated for fetching ads. Our ad servers
* maintain this correlator value briefly (currently for 30 seconds, but
* subject to change), such that requests with the same correlator received
* close together will be considered a single page view. By default a new
* correlator is generated for every refresh.
*/
PubAdsService.prototype.refresh = function refresh(optSlots, optOptions) {
var slots = this._slots;
if (optSlots != null) {
slots = optSlots;
}
for (var _iterator = slots, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) {
var _ref;
if (_isArray) {
if (_i >= _iterator.length) break;
_ref = _iterator[_i++];
} else {
_i = _iterator.next();
if (_i.done) break;
_ref = _i.value;
}
var slot = _ref;
slot._refresh();
}
if (optOptions && optOptions.changeCorrelator) {
this._correlator = Math.random();
}
};
/**
* Removes the ads from the given slots and replaces them with blank
* content. The slots will be marked as unfetched.
*
* @param {Array<!Slot>} [optSlots] The array of slots to clear. Array is
* optional; all slots will be cleared if it is unspecified.
* @returns {boolean} Returns true if slots have been cleared, false otherwise.
*/
PubAdsService.prototype.clear = function clear(optSlots) {
var slots = this._slots;
if (optSlots != null) {
slots = optSlots;
}
for (var _iterator2 = slots, _isArray2 = Array.isArray(_iterator2), _i2 = 0, _iterator2 = _isArray2 ? _iterator2 : _iterator2[Symbol.iterator]();;) {
var _ref2;
if (_isArray2) {
if (_i2 >= _iterator2.length) break;
_ref2 = _iterator2[_i2++];
} else {
_i2 = _iterator2.next();
if (_i2.done) break;
_ref2 = _i2.value;
}
var slot = _ref2;
if (this._slots.indexOf(slot) !== -1) {
slot._clear();
} else {
return false;
}
}
return true;
};
/**
* Constructs an out-of-page passback slot. A passback is where a {@link GPT} snippet
* is used as a creative in an ad serving system. The ad serving system could
* be {@link DFP} or a third party. In this case, the ads are always requested
* synchronously in non-single request mode.
*
* @param {string} adUnitPath The ad unit path of the slot to use as a passback.
* @returns {?Slot} The new passback object or null if the method was
* called with invalid arguments.
*/
PubAdsService.prototype.defineOutOfPagePassback = function defineOutOfPagePassback(adUnitPath) {
if (adUnitPath != null) {
var slot = new _Slot2['default'](adUnitPath).addService(this);
slot._passback = true;
slot._outOfPage = true;
return slot;
} else {
return null;
}
};
/**
* Constructs a passback slot. A passback is where a {@link GPT} snippet is used as a
* creative in an ad serving system. The ad serving system could be {@link DFP} or a
* third party. In this case, the ads are always requested synchronously in
* non-single request mode.
*
* @param {string} adUnitPath The ad unit path of the slot to use as a passback.
* @param {GeneralSize} size The size of the slot.
* @returns {?Slot} The new passback object or null if the method was
* called with invalid arguments.
*/
PubAdsService.prototype.definePassback = function definePassback(adUnitPath, size) {
if (adUnitPath != null && size != null) {
var slot = new _Slot2['default'](adUnitPath, size).addService(this);
slot._passback = true;
return slot;
} else {
return null;
}
};
/**
* Disables requests for ads on page load, but allows ads to be requested with
* a {@link PubAdsService#refresh} call. This should be set prior to enabling
* the service. Async mode must be used; otherwise it will be impossible to
* request ads using {@link PubAdsService#refresh}.
*/
PubAdsService.prototype.disableInitialLoad = function disableInitialLoad() {
var _this2 = this;
this._unlessEnabled(function () {
_this2._options.initialLoad = false;
});
};
/**
* Enables async rendering mode to enable non-blocking fetching and rendering
* of ads. Because the service uses asynchronous rendering by default, you
* only need to use this method to override a previous setting. Async mode
* must be set before the service is enabled.
*
* @returns {boolean} Returns true if async rendering mode was enabled and
* false if it is impossible to enable async rendering mode because the method
* was called after the service was enabled.
*/
PubAdsService.prototype.enableAsyncRendering = function enableAsyncRendering() {
var _this3 = this;
return this._unlessEnabled(function () {
_this3._options.asyncRendering = true;
});
};
/**
* Enables single request mode for fetching multiple ads at the same time.
* This requires all pubads slots to be defined and added to the {@link PubAdsService}
* prior to enabling the service. Single request mode must be set
* before the service is enabled.
*
* @returns {boolean} Returns true if single request mode was enabled and false
* if it is impossible to enable single request mode because the method was
* called after the service was enabled.
*/
PubAdsService.prototype.enableSingleRequest = function enableSingleRequest() {
var _this4 = this;
return this._unlessEnabled(function () {
_this4._options.singleRequest = true;
});
};
/**
* Enables sync rendering mode to enable blocking fetching and rendering of ads.
* This mode must be set before the service is enabled. Synchronous rendering
* also requires that the {@link GPT} JavaScript be loaded synchronously.
*
* @returns {boolean} Returns true if sync rendering mode was enabled and false
* if it is impossible to enable sync rendering mode because the method was
* called after the service was enabled.
*/
PubAdsService.prototype.enableSyncRendering = function enableSyncRendering() {
var _this5 = this;
return this._unlessEnabled(function () {
_this5._options.syncRendering = true;
});
};
/**
* Signals to {@link GPT} that video ads will be present on the page. This enables
* competitive exclusion constraints on display and video ads. If the video
* content is known, call setVideoContent in order to be able to use content
* exclusion for display ads.
*/
PubAdsService.prototype.enableVideoAds = function enableVideoAds() {
this._options.videoAds = true;
};
/**
* Sets custom targeting parameters for a given key that apply to all pubads
* service ad slots. Calling this multiple times for the same key will
* overwrite old values. These keys are defined in your {@link DFP} account.
*
* @param {string} key Targeting parameter key.
* @param {string|!Array<string>} value Targeting parameter value or array of values.
* @returns {PubAdsService} The service object on which the method was called.
*/
PubAdsService.prototype.setTargeting = function setTargeting(key, value) {
this._targeting.set(key, value);
return this;
};
/**
* Returns a specific custom service-level targeting parameter that has been set.
*
* @param {string} key The targeting key to look for.
* @returns {!Array<string>} The values associated with this key, or an empty
* array if there is no such key.
*/
PubAdsService.prototype.getTargeting = function getTargeting(key) {
return this._targeting.get(key);
};
/**
* Returns the list of all custom service-level targeting keys that have been set.
*
* @returns {!Array<string>} Array of targeting keys. Ordering is undefined.
*/
PubAdsService.prototype.getTargetingKeys = function getTargetingKeys() {
return this._targeting.keys();
};
/**
* Clears custom targeting parameters for a given key.
*
* @param {string} key Targeting parameter key.
* @returns {PubAdsService} The service object on which the method was called.
*/
PubAdsService.prototype.clearTargeting = function clearTargeting(key) {
this._targeting.clear(key);
return this;
};
/**
* Enables collapsing of slot divs so that they don't take up any space on the
* page when there is no ad content to display. This mode must be set before
* the service is enabled.
*
* @param {boolean} [optCollapseBeforeAdFetch=false] Whether to collapse the slots
* even before the ads are fetched. This parameter is optional; if not
* provided, false will be used as the default value.
* @returns {boolean} Returns true if div collapse mode was enabled and false
* if it is impossible to enable collapse mode because the method was called
* after the service was enabled.
*/
PubAdsService.prototype.collapseEmptyDivs = function collapseEmptyDivs() {
var _this6 = this;
var optCollapseBeforeAdFetch = arguments.length <= 0 || arguments[0] === undefined ? false : arguments[0];
return this._unlessEnabled(function () {
_this6._options.collapseEmptyDivs = true;
_this6._options.collapseBeforeAdFetch = optCollapseBeforeAdFetch;
});
};
/**
* Sets a page-level ad category exclusion for the given label name.
*
* @param {string} categoryExclusion The ad category exclusion label to add.
* @returns {PubAdsService} The service object on which the method was called.
*/
PubAdsService.prototype.setCategoryExclusion = function setCategoryExclusion(categoryExclusion) {
this._categoryExclusions.push(categoryExclusion);
return this;
};
/**
* Clears all page-level ad category exclusion labels.
* This is useful if you want to refresh the slot.
*
* @returns {PubAdsService} The service object on which the method was called.
*/
PubAdsService.prototype.clearCategoryExclusions = function clearCategoryExclusions() {
this._categoryExclusions = [];
return this;
};
/**
* Enables/disables centering of ads. This mode must be set before the service
* is enabled. Centering is disabled by default.
*
* @param {boolean} centerAds true to center ads, false to left-align them.
*/
PubAdsService.prototype.setCentering = function setCentering(centerAds) {
var _this7 = this;
this._unlessEnabled(function () {
_this7._options.centerAds = centerAds;
});
};
/**
* Sets cookie options for GPT on the page.
*
* @param {number} options The cookie options to set. Set the options parameter
* to the integer value 1 to ignore cookies, and to 0 to use cookies.
* @returns {PubAdsService} The service object on which the method was called.
*/
PubAdsService.prototype.setCookieOptions = function setCookieOptions(options) {
this._options.cookieOptions = options;
return this;
};
/**
* Configures whether all ads on the page should be forced to be rendered using
* a SafeFrame container.
*
* @param {boolean} forceSafeFrame true to force all ads on the page to be
* rendered in SafeFrames and false to change the previous setting to false.
* Setting this to false when unspecified earlier, won't change anything.
* @returns {PubAdsService} The service object on which the function was called.
*/
PubAdsService.prototype.setForceSafeFrame = function setForceSafeFrame(forceSafeFrame) {
this._options.forceSafeFrame = forceSafeFrame;
return this;
};
/**
* Passes location information from websites so you can geo-target line items
* to specific locations. {@link DFP} will not use location data unless this feature
* has been enabled for your network.
*
* @param {string|number} latitudeOrAddress Latitude or freeform address.
* @param {number} [optLongitude] The longitude (if a latitude was provided as first argument).
* @param {number} [optRadius] The radius in millimeters. Will be rounded to closest integer.
* Only used when passing the latitude and longitude.
* @returns {PubAdsService} The service object on which the method was called.
*/
PubAdsService.prototype.setLocation = function setLocation(latitudeOrAddress, optLongitude, optRadius) {
if (typeof latitudeOrAddress === 'number') {
this._options.latitude = latitudeOrAddress || null;
this._options.longitude = optLongitude || null;
this._options.radius = optRadius || null;
this._options.address = null;
} else {
this._options.address = latitudeOrAddress;
this._options.latitude = null;
this._options.longitude = null;
this._options.radius = null;
}
return this;
};
/**
* Sets the value for the publisher-provided ID.
*
* @param {string} ppid An alphanumeric ID provided by the publisher with a
* recommended maximum of 150 characters.
* @returns {PubAdsService} The service object on which the method was called.
*/
PubAdsService.prototype.setPublisherProvidedId = function setPublisherProvidedId(ppid) {
this._options.ppid = ppid;
return this;
};
/**
* Sets the page-level preferences for SafeFrame configuration. Any
* unrecognized keys in the config object will be ignored. The entire config
* will be ignored if an invalid value is passed for a recognized key. These
* page level preferences will be overriden by slot level preferences, if
* specified.
*
* @param {SafeFrameConfig} config The configuration object.
* @returns {PubAdsService} The service object on which the method was called.
*/
PubAdsService.prototype.setSafeFrameConfig = function setSafeFrameConfig(config) {
this._options.safeFrameConfig = config;
return this;
};
/**
* Configures whether the page should be treated as child-directed.
*
* @param {number} value The child-directed treatment tag status to set. Set
* the options parameter to the integer value 1 to mark the ad request as
* child-directed, and to 0 for ad requests that are not child-directed.
* @returns {PubAdsService} The service object on which the method was called.
*/
PubAdsService.prototype.setTagForChildDirectedTreatment = function setTagForChildDirectedTreatment(value) {
this._options.tagForChildDirectedTreatment = value;
return this;
};
/**
* Clears the configuration for whether the page should be treated as child-directed.
*
* @returns {PubAdsService} The service object on which the method was called.
*/
PubAdsService.prototype.clearTagForChildDirectedTreatment = function clearTagForChildDirectedTreatment() {
this._options.tagForChildDirectedTreatment = null;
return this;
};
/**
* Sets the video content information to be sent along with the ad requests for
* targeting and content exclusion purposes. Video ads will be automatically
* enabled when this method is called. For videoContentId and videoCmsId, use
* the values that are provided to the {@link DFP} content ingestion service.
*
* @param {string} videoContentId The video content ID.
* @param {string} videoCmsId The video CMS ID.
*/
PubAdsService.prototype.setVideoContent = function setVideoContent(videoContentId, videoCmsId) {
this._options.videoContentId = videoContentId;
this._options.videoCmsId = videoCmsId;
};
/**
* Changes the correlator that is sent with ad requests, effectively starting
* a new page view. The correlator is the same for all the ad requests coming
* from one page view, and unique across page views. Only applies to async mode.
*
* @returns {PubAdsService} The service object on which the function was called.
*/
PubAdsService.prototype.updateCorrelator = function updateCorrelator() {
return this;
};
/**
* Conditionally execute a function unless the service has already been
* enabled.
*
* @param {function} fn The function to call if the service is not enabled
* @returns {boolean} t