Skip to content

BidViewability: Refactored the init function #13141

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Jun 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 22 additions & 17 deletions modules/bidViewability.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ export let logWinningBidNotFound = (slot) => {
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.`);
};

export let impressionViewableHandler = (globalModuleConfig, slot, event) => {
export let impressionViewableHandler = (globalModuleConfig, event) => {
const slot = event.slot;
let respectiveBid = getMatchingWinningBidForGPTSlot(globalModuleConfig, slot);

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

export let init = () => {
events.on(EVENTS.AUCTION_INIT, () => {
// read the config for the module
const globalModuleConfig = config.getConfig(MODULE_NAME) || {};
// do nothing if module-config.enabled is not set to true
// 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
if (globalModuleConfig[CONFIG_ENABLED] !== true) {
return;
}
// add the GPT event listener
window.googletag = window.googletag || {};
window.googletag.cmd = window.googletag.cmd || [];
const handleSetConfig = (config) => {
const globalModuleConfig = config || {};
window.googletag = window.googletag || {};
window.googletag.cmd = window.googletag.cmd || [];

// do nothing if module-config.enabled is not set to true
// 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
const impressionViewableHandlerWrapper = (event) => {
window.googletag.pubads().removeEventListener(GPT_IMPRESSION_VIEWABLE_EVENT, impressionViewableHandlerWrapper);
impressionViewableHandler(globalModuleConfig, event);
};

if (globalModuleConfig[CONFIG_ENABLED] !== true) {
window.googletag.cmd.push(() => {
window.googletag.pubads().addEventListener(GPT_IMPRESSION_VIEWABLE_EVENT, function(event) {
impressionViewableHandler(globalModuleConfig, event.slot, event);
});
window.googletag.pubads().removeEventListener(GPT_IMPRESSION_VIEWABLE_EVENT, impressionViewableHandlerWrapper);
});
return;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The OP does not call this a bugfix, but I think the previous version would attach duplicate GPT event listeners on every auction. This fix, to be complete, should detach it here (when the module is turned off) IMO.

https://developers.google.com/publisher-tag/reference#googletag.Service.removeEventListener

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Made the changes as per the GPT document. where we are removing the listener before firing the handler.
Also consumed the event object to extract the slot to handle Co-pilot's suggestion.

}
// add the GPT event listener
window.googletag.cmd.push(() => {
window.googletag.pubads().addEventListener(GPT_IMPRESSION_VIEWABLE_EVENT, impressionViewableHandlerWrapper);
});
}

init()
config.getConfig(MODULE_NAME, config => handleSetConfig(config[MODULE_NAME]));
8 changes: 6 additions & 2 deletions test/spec/modules/bidViewability_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ const GPT_SLOT = {
}
};

const EVENT_OBJ = {
slot: GPT_SLOT
}

const PBJS_WINNING_BID = {
'adUnitCode': '/harshad/Jan/2021/',
'bidderCode': 'pubmatic',
Expand Down Expand Up @@ -282,7 +286,7 @@ describe('#bidViewability', function() {
firePixels: true
};
winningBidsArray.push(PBJS_WINNING_BID);
bidViewability.impressionViewableHandler(moduleConfig, GPT_SLOT, null);
bidViewability.impressionViewableHandler(moduleConfig, EVENT_OBJ);
// fire pixels should be called
PBJS_WINNING_BID.vurls.forEach((url, i) => {
let call = triggerPixelSpy.getCall(i);
Expand Down Expand Up @@ -314,7 +318,7 @@ describe('#bidViewability', function() {
deferBilling: true
}
winningBidsArray.push(bid);
bidViewability.impressionViewableHandler(moduleConfig, GPT_SLOT, null);
bidViewability.impressionViewableHandler(moduleConfig, EVENT_OBJ);
expect(triggerBillingSpy.callCount).to.equal(1);
sinon.assert.calledWith(triggerBillingSpy, bid);
});
Expand Down