Skip to content

Commit 4473d54

Browse files
robertrmartinezstsepelin
authored andcommitted
Rubicon Analytics: Fire event once gptSlots render (prebid#6241)
* Once all gam slots are back fire event * push to cmd queue
1 parent 7f91119 commit 4473d54

File tree

2 files changed

+33
-65
lines changed

2 files changed

+33
-65
lines changed

modules/rubiconAnalyticsAdapter.js

+33-38
Original file line numberDiff line numberDiff line change
@@ -458,47 +458,35 @@ function updateRpaCookie() {
458458
return decodedRpaCookie;
459459
}
460460

461-
const gamEventFunctions = {
462-
'slotOnload': (auctionId, bid) => {
463-
cache.auctions[auctionId].gamHasRendered[bid.adUnit.adUnitCode] = true;
464-
},
465-
'slotRenderEnded': (auctionId, bid, event) => {
466-
if (event.isEmpty) {
467-
cache.auctions[auctionId].gamHasRendered[bid.adUnit.adUnitCode] = true;
468-
}
469-
bid.adUnit.gam = utils.pick(event, [
470-
// these come in as `null` from Gpt, which when stringified does not get removed
471-
// so set explicitly to undefined when not a number
472-
'advertiserId', advertiserId => utils.isNumber(advertiserId) ? advertiserId : undefined,
473-
'creativeId', creativeId => utils.isNumber(creativeId) ? creativeId : undefined,
474-
'lineItemId', lineItemId => utils.isNumber(lineItemId) ? lineItemId : undefined,
475-
'adSlot', () => event.slot.getAdUnitPath(),
476-
'isSlotEmpty', () => event.isEmpty || undefined
477-
]);
478-
}
479-
}
480-
481461
function subscribeToGamSlots() {
482-
['slotOnload', 'slotRenderEnded'].forEach(eventName => {
483-
window.googletag.pubads().addEventListener(eventName, event => {
484-
const isMatchingAdSlot = utils.isAdUnitCodeMatchingSlot(event.slot);
485-
// loop through auctions and adUnits and mark the info
486-
Object.keys(cache.auctions).forEach(auctionId => {
487-
(Object.keys(cache.auctions[auctionId].bids) || []).forEach(bidId => {
488-
let bid = cache.auctions[auctionId].bids[bidId];
489-
// if this slot matches this bids adUnit, add the adUnit info
490-
if (isMatchingAdSlot(bid.adUnit.adUnitCode)) {
491-
// mark this adUnit as having been rendered by gam
492-
gamEventFunctions[eventName](auctionId, bid, event);
493-
}
494-
});
495-
// Now if all adUnits have gam rendered, send the payload
496-
if (rubiConf.waitForGamSlots && !cache.auctions[auctionId].sent && Object.keys(cache.auctions[auctionId].gamHasRendered).every(adUnitCode => cache.auctions[auctionId].gamHasRendered[adUnitCode])) {
497-
clearTimeout(cache.timeouts[auctionId]);
498-
delete cache.timeouts[auctionId];
499-
sendMessage.call(rubiconAdapter, auctionId);
462+
window.googletag.pubads().addEventListener('slotRenderEnded', event => {
463+
const isMatchingAdSlot = utils.isAdUnitCodeMatchingSlot(event.slot);
464+
// loop through auctions and adUnits and mark the info
465+
Object.keys(cache.auctions).forEach(auctionId => {
466+
(Object.keys(cache.auctions[auctionId].bids) || []).forEach(bidId => {
467+
let bid = cache.auctions[auctionId].bids[bidId];
468+
// if this slot matches this bids adUnit, add the adUnit info
469+
if (isMatchingAdSlot(bid.adUnit.adUnitCode)) {
470+
// mark this adUnit as having been rendered by gam
471+
cache.auctions[auctionId].gamHasRendered[bid.adUnit.adUnitCode] = true;
472+
473+
bid.adUnit.gam = utils.pick(event, [
474+
// these come in as `null` from Gpt, which when stringified does not get removed
475+
// so set explicitly to undefined when not a number
476+
'advertiserId', advertiserId => utils.isNumber(advertiserId) ? advertiserId : undefined,
477+
'creativeId', creativeId => utils.isNumber(creativeId) ? creativeId : undefined,
478+
'lineItemId', lineItemId => utils.isNumber(lineItemId) ? lineItemId : undefined,
479+
'adSlot', () => event.slot.getAdUnitPath(),
480+
'isSlotEmpty', () => event.isEmpty || undefined
481+
]);
500482
}
501483
});
484+
// Now if all adUnits have gam rendered, send the payload
485+
if (rubiConf.waitForGamSlots && !cache.auctions[auctionId].sent && Object.keys(cache.auctions[auctionId].gamHasRendered).every(adUnitCode => cache.auctions[auctionId].gamHasRendered[adUnitCode])) {
486+
clearTimeout(cache.timeouts[auctionId]);
487+
delete cache.timeouts[auctionId];
488+
sendMessage.call(rubiconAdapter, auctionId);
489+
}
502490
});
503491
});
504492
}
@@ -576,6 +564,13 @@ let rubiconAdapter = Object.assign({}, baseAdapter, {
576564
if (!cache.gpt.registered && utils.isGptPubadsDefined()) {
577565
subscribeToGamSlots();
578566
cache.gpt.registered = true;
567+
} else if (!cache.gpt.registered) {
568+
cache.gpt.registered = true;
569+
let googletag = window.googletag || {};
570+
googletag.cmd = googletag.cmd || [];
571+
googletag.cmd.push(function() {
572+
subscribeToGamSlots();
573+
});
579574
}
580575
break;
581576
case BID_REQUESTED:

test/spec/modules/rubiconAnalyticsAdapter_spec.js

-27
Original file line numberDiff line numberDiff line change
@@ -1361,7 +1361,6 @@ describe('rubicon analytics adapter', function () {
13611361
describe('with googletag enabled', function () {
13621362
let gptSlot0, gptSlot1;
13631363
let gptSlotRenderEnded0, gptSlotRenderEnded1;
1364-
let gptslotOnload0, gptslotOnload1;
13651364
beforeEach(function () {
13661365
mockGpt.enable();
13671366
gptSlot0 = mockGpt.makeSlot({code: '/19968336/header-bid-tag-0'});
@@ -1375,16 +1374,6 @@ describe('rubicon analytics adapter', function () {
13751374
lineItemId: 3333
13761375
}
13771376
};
1378-
gptslotOnload0 = {
1379-
eventName: 'slotOnload',
1380-
params: {
1381-
slot: gptSlot0,
1382-
isEmpty: false,
1383-
advertiserId: 1111,
1384-
creativeId: 2222,
1385-
lineItemId: 3333
1386-
}
1387-
};
13881377

13891378
gptSlot1 = mockGpt.makeSlot({code: '/19968336/header-bid-tag1'});
13901379
gptSlotRenderEnded1 = {
@@ -1397,16 +1386,6 @@ describe('rubicon analytics adapter', function () {
13971386
lineItemId: 6666
13981387
}
13991388
};
1400-
gptslotOnload1 = {
1401-
eventName: 'slotOnload',
1402-
params: {
1403-
slot: gptSlot1,
1404-
isEmpty: false,
1405-
advertiserId: 1111,
1406-
creativeId: 2222,
1407-
lineItemId: 3333
1408-
}
1409-
};
14101389
});
14111390

14121391
afterEach(function () {
@@ -1536,12 +1515,6 @@ describe('rubicon analytics adapter', function () {
15361515
mockGpt.emitEvent(gptSlotRenderEnded0.eventName, gptSlotRenderEnded0.params);
15371516
mockGpt.emitEvent(gptSlotRenderEnded1.eventName, gptSlotRenderEnded1.params);
15381517

1539-
expect(server.requests.length).to.equal(0);
1540-
1541-
// now emit slotOnload and it should send
1542-
mockGpt.emitEvent(gptslotOnload0.eventName, gptslotOnload0.params);
1543-
mockGpt.emitEvent(gptslotOnload1.eventName, gptslotOnload1.params);
1544-
15451518
expect(server.requests.length).to.equal(1);
15461519
let request = server.requests[0];
15471520
let message = JSON.parse(request.requestBody);

0 commit comments

Comments
 (0)