Skip to content

Commit 5e3b878

Browse files
pm-azhar-mullapm-azhar-mulla
andauthored
BidViewability: Refactored the init function (#13141)
* Refactored the init function * Added new line for linting error * removed the listener before firing the handler * Removed event listener when module is disabled --------- Co-authored-by: pm-azhar-mulla <[email protected]>
1 parent 6f4c3b1 commit 5e3b878

File tree

2 files changed

+28
-19
lines changed

2 files changed

+28
-19
lines changed

modules/bidViewability.js

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ export let logWinningBidNotFound = (slot) => {
5858
logWarn(`bid details could not be found for ${slot.getSlotElementId()}, probable reasons: a non-prebid bid is served OR check the prebid.AdUnit.code to GPT.AdSlot relation.`);
5959
};
6060

61-
export let impressionViewableHandler = (globalModuleConfig, slot, event) => {
61+
export let impressionViewableHandler = (globalModuleConfig, event) => {
62+
const slot = event.slot;
6263
let respectiveBid = getMatchingWinningBidForGPTSlot(globalModuleConfig, slot);
6364

6465
if (respectiveBid === null) {
@@ -78,24 +79,28 @@ export let impressionViewableHandler = (globalModuleConfig, slot, event) => {
7879
}
7980
};
8081

81-
export let init = () => {
82-
events.on(EVENTS.AUCTION_INIT, () => {
83-
// read the config for the module
84-
const globalModuleConfig = config.getConfig(MODULE_NAME) || {};
85-
// do nothing if module-config.enabled is not set to true
86-
// this way we are adding a way for bidders to know (using pbjs.getConfig('bidViewability').enabled === true) whether this module is added in build and is enabled
87-
if (globalModuleConfig[CONFIG_ENABLED] !== true) {
88-
return;
89-
}
90-
// add the GPT event listener
91-
window.googletag = window.googletag || {};
92-
window.googletag.cmd = window.googletag.cmd || [];
82+
const handleSetConfig = (config) => {
83+
const globalModuleConfig = config || {};
84+
window.googletag = window.googletag || {};
85+
window.googletag.cmd = window.googletag.cmd || [];
86+
87+
// do nothing if module-config.enabled is not set to true
88+
// this way we are adding a way for bidders to know (using pbjs.getConfig('bidViewability').enabled === true) whether this module is added in build and is enabled
89+
const impressionViewableHandlerWrapper = (event) => {
90+
window.googletag.pubads().removeEventListener(GPT_IMPRESSION_VIEWABLE_EVENT, impressionViewableHandlerWrapper);
91+
impressionViewableHandler(globalModuleConfig, event);
92+
};
93+
94+
if (globalModuleConfig[CONFIG_ENABLED] !== true) {
9395
window.googletag.cmd.push(() => {
94-
window.googletag.pubads().addEventListener(GPT_IMPRESSION_VIEWABLE_EVENT, function(event) {
95-
impressionViewableHandler(globalModuleConfig, event.slot, event);
96-
});
96+
window.googletag.pubads().removeEventListener(GPT_IMPRESSION_VIEWABLE_EVENT, impressionViewableHandlerWrapper);
9797
});
98+
return;
99+
}
100+
// add the GPT event listener
101+
window.googletag.cmd.push(() => {
102+
window.googletag.pubads().addEventListener(GPT_IMPRESSION_VIEWABLE_EVENT, impressionViewableHandlerWrapper);
98103
});
99104
}
100105

101-
init()
106+
config.getConfig(MODULE_NAME, config => handleSetConfig(config[MODULE_NAME]));

test/spec/modules/bidViewability_spec.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ const GPT_SLOT = {
1919
}
2020
};
2121

22+
const EVENT_OBJ = {
23+
slot: GPT_SLOT
24+
}
25+
2226
const PBJS_WINNING_BID = {
2327
'adUnitCode': '/harshad/Jan/2021/',
2428
'bidderCode': 'pubmatic',
@@ -282,7 +286,7 @@ describe('#bidViewability', function() {
282286
firePixels: true
283287
};
284288
winningBidsArray.push(PBJS_WINNING_BID);
285-
bidViewability.impressionViewableHandler(moduleConfig, GPT_SLOT, null);
289+
bidViewability.impressionViewableHandler(moduleConfig, EVENT_OBJ);
286290
// fire pixels should be called
287291
PBJS_WINNING_BID.vurls.forEach((url, i) => {
288292
let call = triggerPixelSpy.getCall(i);
@@ -314,7 +318,7 @@ describe('#bidViewability', function() {
314318
deferBilling: true
315319
}
316320
winningBidsArray.push(bid);
317-
bidViewability.impressionViewableHandler(moduleConfig, GPT_SLOT, null);
321+
bidViewability.impressionViewableHandler(moduleConfig, EVENT_OBJ);
318322
expect(triggerBillingSpy.callCount).to.equal(1);
319323
sinon.assert.calledWith(triggerBillingSpy, bid);
320324
});

0 commit comments

Comments
 (0)