Skip to content

Commit

Permalink
refactored the way hb_deal is handled in adserverTargeting
Browse files Browse the repository at this point in the history
  • Loading branch information
snapwich committed Dec 12, 2016
1 parent 2fac9d0 commit e3a11bb
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 25 deletions.
12 changes: 7 additions & 5 deletions src/bidmanager.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,6 @@ exports.addBidResponse = function (adUnitCode, bid) {
if (bid.bidderCode && bid.cpm !== 0) {
keyValues = getKeyValueTargetingPairs(bid.bidderCode, bid);

if (bid.dealId) {
keyValues[`hb_deal_${bid.bidderCode}`] = bid.dealId;
}

bid.adserverTargeting = keyValues;
}

Expand Down Expand Up @@ -204,7 +200,8 @@ function setKeys(keyValues, bidderSettings, custBidObj) {
}

if (
typeof bidderSettings.suppressEmptyKeys !== "undefined" && bidderSettings.suppressEmptyKeys === true &&
(typeof bidderSettings.suppressEmptyKeys !== "undefined" && bidderSettings.suppressEmptyKeys === true ||
key === "hb_deal") && // hb_deal is suppressed automatically if not set
(
utils.isEmptyStr(value) ||
value === null ||
Expand Down Expand Up @@ -400,6 +397,11 @@ function getStandardBidderSettings() {
val: function (bidResponse) {
return bidResponse.size;
}
}, {
key: 'hb_deal',
val: function (bidResponse) {
return bidResponse.dealId;
}
}
]
};
Expand Down
3 changes: 2 additions & 1 deletion src/constants.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
"hb_bidder",
"hb_adid",
"hb_pb",
"hb_size"
"hb_size",
"hb_deal"
]
}
22 changes: 4 additions & 18 deletions src/prebid.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,11 +169,6 @@ function getWinningBids(adUnitCode) {
function getWinningBidTargeting() {
let winners = getWinningBids();

// winning bids with deals need an hb_deal targeting key
winners
.filter(bid => bid.dealId)
.map(bid => bid.adserverTargeting.hb_deal = bid.dealId);

let standardKeys = getStandardKeys();
winners = winners.map(winner => {
return {
Expand All @@ -189,16 +184,6 @@ function getWinningBidTargeting() {
return winners;
}

function getDealTargeting() {
return $$PREBID_GLOBAL$$._bidsReceived.filter(bid => bid.dealId).map(bid => {
const dealKey = `hb_deal_${bid.bidderCode}`;
return {
[bid.adUnitCode]: getTargetingMap(bid, CONSTANTS.TARGETING_KEYS)
.concat({ [dealKey.substring(0, 20)]: [bid.adserverTargeting[dealKey]] })
};
});
}

/**
* Get custom targeting keys for bids that have `alwaysUseBid=true`.
*/
Expand Down Expand Up @@ -233,7 +218,9 @@ function getBidLandscapeTargeting(adUnitCodes) {
.map(bid => {
if (bid.adserverTargeting) {
return {
[bid.adUnitCode]: getTargetingMap(bid, standardKeys)
[bid.adUnitCode]: getTargetingMap(bid, standardKeys.filter(
key => typeof bid.adserverTargeting[key] !== "undefined") // mainly for possibly unset hb_deal
)
};
}
}).filter(bid => bid); // removes empty elements in array
Expand All @@ -254,8 +241,7 @@ function getAllTargeting(adUnitCode) {
// `alwaysUseBid=true`. If sending all bids is enabled, add targeting for losing bids.
var targeting = getWinningBidTargeting(adUnitCodes)
.concat(getAlwaysUseBidTargeting(adUnitCodes))
.concat($$PREBID_GLOBAL$$._sendAllBids ? getBidLandscapeTargeting(adUnitCodes) : [])
.concat(getDealTargeting(adUnitCodes));
.concat($$PREBID_GLOBAL$$._sendAllBids ? getBidLandscapeTargeting(adUnitCodes) : []);

//store a reference of the targeting keys
targeting.map(adUnitCode => {
Expand Down
5 changes: 4 additions & 1 deletion test/spec/bidmanager_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,9 @@ describe('bidmanager.js', function () {
bidmanager.adjustBids(bid)
assert.equal(bid.cpm, 0);

// reset bidderSettings so we don't mess up further tests
$$PREBID_GLOBAL$$.bidderSettings = {};

});
});

Expand Down Expand Up @@ -474,7 +477,7 @@ describe('bidmanager.js', function () {
bid.dealId = "test deal";
bidmanager.addBidResponse(bid.adUnitCode, bid);
const addedBid = $$PREBID_GLOBAL$$._bidsReceived.pop();
assert.equal(addedBid.adserverTargeting[`hb_deal_${bid.bidderCode}`], bid.dealId, 'dealId placed in adserverTargeting');
assert.equal(addedBid.adserverTargeting[`hb_deal`], bid.dealId, 'dealId placed in adserverTargeting');
});

it('should not alter bid adID', () => {
Expand Down

0 comments on commit e3a11bb

Please sign in to comment.