From 361a54218473e72c887f0da0eae04497e86fd0e0 Mon Sep 17 00:00:00 2001 From: Jonatan Alexis Anauati Date: Tue, 11 Feb 2020 17:27:52 -0300 Subject: [PATCH] [fix] configure disableInitialLoad before service initialization (#176) --- js/manager.js | 14 +++++++++++--- lib/manager.js | 51 +++++++++++++++++++++++++++++--------------------- 2 files changed, 41 insertions(+), 24 deletions(-) diff --git a/js/manager.js b/js/manager.js index 28249b7..df54b90 100644 --- a/js/manager.js +++ b/js/manager.js @@ -193,6 +193,7 @@ const DFPManager = Object.assign(new EventEmitter().setMaxListeners(0), { gptLoadAds(slotsToInitialize) { return new Promise((resolve) => { this.getGoogletag().then((googletag) => { + this.configureInitialOptions(googletag); slotsToInitialize.forEach((currentSlotId) => { registeredSlots[currentSlotId].loading = false; @@ -246,6 +247,16 @@ const DFPManager = Object.assign(new EventEmitter().setMaxListeners(0), { }); }, + // configure those gpt parameters that need to be set before pubsads service + // initialization. + configureInitialOptions(googletag) { + googletag.cmd.push(() => { + if (this.disableInitialLoadIsEnabled()) { + googletag.pubads().disableInitialLoad(); + } + }); + }, + configureOptions(googletag) { googletag.cmd.push(() => { const pubadsService = googletag.pubads(); @@ -275,9 +286,6 @@ const DFPManager = Object.assign(new EventEmitter().setMaxListeners(0), { if (this.singleRequestIsEnabled()) { pubadsService.enableSingleRequest(); } - if (this.disableInitialLoadIsEnabled()) { - pubadsService.disableInitialLoad(); - } if (this.collapseEmptyDivs === true || this.collapseEmptyDivs === false) { pubadsService.collapseEmptyDivs(this.collapseEmptyDivs); } diff --git a/lib/manager.js b/lib/manager.js index fac4d60..05ac54d 100644 --- a/lib/manager.js +++ b/lib/manager.js @@ -216,6 +216,8 @@ var DFPManager = Object.assign(new _events.EventEmitter().setMaxListeners(0), { return new Promise(function (resolve) { _this3.getGoogletag().then(function (googletag) { + _this3.configureInitialOptions(googletag); + slotsToInitialize.forEach(function (currentSlotId) { registeredSlots[currentSlotId].loading = false; googletag.cmd.push(function () { @@ -279,14 +281,25 @@ var DFPManager = Object.assign(new _events.EventEmitter().setMaxListeners(0), { }); }); }, - configureOptions: function configureOptions(googletag) { + // configure those gpt parameters that need to be set before pubsads service + // initialization. + configureInitialOptions: function configureInitialOptions(googletag) { var _this4 = this; + googletag.cmd.push(function () { + if (_this4.disableInitialLoadIsEnabled()) { + googletag.pubads().disableInitialLoad(); + } + }); + }, + configureOptions: function configureOptions(googletag) { + var _this5 = this; + googletag.cmd.push(function () { var pubadsService = googletag.pubads(); - pubadsService.setRequestNonPersonalizedAds(_this4.personalizedAdsEnabled() ? 0 : 1); + pubadsService.setRequestNonPersonalizedAds(_this5.personalizedAdsEnabled() ? 0 : 1); - var targetingArguments = _this4.getTargetingArguments(); // set global targetting arguments + var targetingArguments = _this5.getTargetingArguments(); // set global targetting arguments Object.keys(targetingArguments).forEach(function (varName) { @@ -295,16 +308,16 @@ var DFPManager = Object.assign(new _events.EventEmitter().setMaxListeners(0), { } }); // set global adSense attributes - var adSenseAttributes = _this4.getAdSenseAttributes(); + var adSenseAttributes = _this5.getAdSenseAttributes(); Object.keys(adSenseAttributes).forEach(function (key) { pubadsService.set(key, adSenseAttributes[key]); }); - if (_this4.lazyLoadIsEnabled()) { + if (_this5.lazyLoadIsEnabled()) { var args = []; - var config = _this4.getLazyLoadConfig(); + var config = _this5.getLazyLoadConfig(); if (config !== null) { args.push(config); @@ -313,16 +326,12 @@ var DFPManager = Object.assign(new _events.EventEmitter().setMaxListeners(0), { pubadsService.enableLazyLoad.call(args); } - if (_this4.singleRequestIsEnabled()) { + if (_this5.singleRequestIsEnabled()) { pubadsService.enableSingleRequest(); } - if (_this4.disableInitialLoadIsEnabled()) { - pubadsService.disableInitialLoad(); - } - - if (_this4.collapseEmptyDivs === true || _this4.collapseEmptyDivs === false) { - pubadsService.collapseEmptyDivs(_this4.collapseEmptyDivs); + if (_this5.collapseEmptyDivs === true || _this5.collapseEmptyDivs === false) { + pubadsService.collapseEmptyDivs(_this5.collapseEmptyDivs); } }); }, @@ -364,10 +373,10 @@ var DFPManager = Object.assign(new _events.EventEmitter().setMaxListeners(0), { } }, gptRefreshAds: function gptRefreshAds(slots) { - var _this5 = this; + var _this6 = this; return this.getGoogletag().then(function (googletag) { - _this5.configureOptions(googletag); + _this6.configureOptions(googletag); googletag.cmd.push(function () { var pubadsService = googletag.pubads(); @@ -378,14 +387,14 @@ var DFPManager = Object.assign(new _events.EventEmitter().setMaxListeners(0), { }); }, reload: function reload() { - var _this6 = this; + var _this7 = this; return this.destroyGPTSlots.apply(this, arguments).then(function () { - return _this6.load(); + return _this7.load(); }); }, destroyGPTSlots: function destroyGPTSlots() { - var _this7 = this; + var _this8 = this; for (var _len4 = arguments.length, slotsToDestroy = new Array(_len4), _key4 = 0; _key4 < _len4; _key4++) { slotsToDestroy[_key4] = arguments[_key4]; @@ -405,7 +414,7 @@ var DFPManager = Object.assign(new _events.EventEmitter().setMaxListeners(0), { slots.push(slot); } - _this7.getGoogletag().then(function (googletag) { + _this8.getGoogletag().then(function (googletag) { googletag.cmd.push(function () { if (managerAlreadyInitialized === true) { if (slotsToDestroy.length > 0) { @@ -428,7 +437,7 @@ var DFPManager = Object.assign(new _events.EventEmitter().setMaxListeners(0), { }); }, registerSlot: function registerSlot(_ref) { - var _this8 = this; + var _this9 = this; var slotId = _ref.slotId, dfpNetworkId = _ref.dfpNetworkId, @@ -467,7 +476,7 @@ var DFPManager = Object.assign(new _events.EventEmitter().setMaxListeners(0), { gptSlot = slot.gptSlot; if (loading === false && !gptSlot) { - _this8.load(slotId); + _this9.load(slotId); } } });