Skip to content

Commit

Permalink
PBS Bid Adapter: Fix duplicate imp.id (prebid#6933)
Browse files Browse the repository at this point in the history
* pbs adapter in case of twin codes, add suffix to imp.id

* add fix for the native assets cache

* fix for impressionId generation
  • Loading branch information
FilipStamenkovic authored and agrandes-tappx committed Sep 29, 2021
1 parent 7dd41c9 commit 30ffa6d
Showing 1 changed file with 18 additions and 7 deletions.
25 changes: 18 additions & 7 deletions modules/prebidServerBidAdapter/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -492,12 +492,24 @@ const OPEN_RTB_PROTOCOL = {
const firstBidRequest = bidRequests[0];

// transform ad unit into array of OpenRTB impression objects
let impIds = new Set();
adUnits.forEach(adUnit => {
// in case there is a duplicate imp.id, add '-2' suffix to the second imp.id.
// e.g. if there are 2 adUnits (case of twin adUnit codes) with code 'test',
// first imp will have id 'test' and second imp will have id 'test-2'
let impressionId = adUnit.code;
let i = 1;
while (impIds.has(impressionId)) {
i++;
impressionId = `${adUnit.code}-${i}`;
}
impIds.add(impressionId);

const nativeParams = processNativeAdUnitParams(utils.deepAccess(adUnit, 'mediaTypes.native'));
let nativeAssets;
if (nativeParams) {
try {
nativeAssets = nativeAssetCache[adUnit.code] = Object.keys(nativeParams).reduce((assets, type) => {
nativeAssets = nativeAssetCache[impressionId] = Object.keys(nativeParams).reduce((assets, type) => {
let params = nativeParams[type];

function newAsset(obj) {
Expand Down Expand Up @@ -563,10 +575,9 @@ const OPEN_RTB_PROTOCOL = {
const bannerParams = utils.deepAccess(adUnit, 'mediaTypes.banner');

adUnit.bids.forEach(bid => {
// OpenRTB response contains the adunit code and bidder name. These are
// OpenRTB response contains imp.id and bidder name. These are
// combined to create a unique key for each bid since an id isn't returned
bidIdMap[`${adUnit.code}${bid.bidder}`] = bid.bid_id;

bidIdMap[`${impressionId}${bid.bidder}`] = bid.bid_id;
// check for and store valid aliases to add to the request
if (adapterManager.aliasRegistry[bid.bidder]) {
const bidder = adapterManager.bidderRegistry[bid.bidder];
Expand Down Expand Up @@ -647,7 +658,7 @@ const OPEN_RTB_PROTOCOL = {
return acc;
}, {...utils.deepAccess(adUnit, 'ortb2Imp.ext')});

const imp = { id: adUnit.code, ext, secure: s2sConfig.secure };
const imp = { id: impressionId, ext, secure: s2sConfig.secure };

const ortb2 = {...utils.deepAccess(adUnit, 'ortb2Imp.ext.data')};
Object.keys(ortb2).forEach(prop => {
Expand Down Expand Up @@ -941,7 +952,7 @@ const OPEN_RTB_PROTOCOL = {
}

if (utils.isPlainObject(adm) && Array.isArray(adm.assets)) {
let origAssets = nativeAssetCache[bidRequest.adUnitCode];
let origAssets = nativeAssetCache[bid.impid];
bidObject.native = utils.cleanObj(adm.assets.reduce((native, asset) => {
let origAsset = origAssets[asset.id];
if (utils.isPlainObject(asset.img)) {
Expand Down Expand Up @@ -997,7 +1008,7 @@ const OPEN_RTB_PROTOCOL = {
bidObject.ttl = (bid.exp) ? bid.exp : configTtl;
bidObject.netRevenue = (bid.netRevenue) ? bid.netRevenue : DEFAULT_S2S_NETREVENUE;

bids.push({ adUnit: bid.impid, bid: bidObject });
bids.push({ adUnit: bidRequest.adUnitCode, bid: bidObject });
});
});
}
Expand Down

0 comments on commit 30ffa6d

Please sign in to comment.