Skip to content

Commit

Permalink
Bug Fix: restore alwaysUseBid functionality (#389)
Browse files Browse the repository at this point in the history
* Add test for `getAdserverTargeting`.
* Add test for `getBidLandscapeTargeting`.
* Move winning bid targeting result to fixtures.
* Add test for ad server targeting when bid landscape reporting is on.
* Add function to get targeting for bids with `alwaysUseBid` set to `true`.
* Fix unsetting `enableSendAllBids`.
* Update ad server targeting to include custom keys of `alwaysUseBid` bids.
* Move `getAdserverTargeting` tests to test against public API.
* Move `alwaysUseBid` targeting test to module level.
* Add checks for ad server targeting keys.
* Remove unneeded exports.
* Remove public function to disable sendAllBids.
* style updates (@protonate)
  • Loading branch information
kmjennison authored and protonate committed Jun 15, 2016
1 parent c951eac commit 2a5b122
Show file tree
Hide file tree
Showing 3 changed files with 361 additions and 14 deletions.
43 changes: 33 additions & 10 deletions src/prebid.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ var BID_WON = CONSTANTS.EVENTS.BID_WON;
var BID_TIMEOUT = CONSTANTS.EVENTS.BID_TIMEOUT;

var pb_bidsTimedOut = false;
var pb_sendAllBids = false;
var auctionRunning = false;
var presetTargeting = [];

Expand All @@ -38,6 +37,7 @@ var eventValidators = {
pbjs._bidsRequested = [];
pbjs._bidsReceived = [];
pbjs._adsReceived = [];
pbjs._sendAllBids = false;

//default timeout for all bids
pbjs.bidderTimeout = pbjs.bidderTimeout || 2000;
Expand Down Expand Up @@ -177,7 +177,7 @@ function getWinningBidTargeting() {
}

function getDealTargeting() {
const dealTargeting = pbjs._bidsReceived.filter(bid => bid.dealId).map(bid => {
return pbjs._bidsReceived.filter(bid => bid.dealId).map(bid => {
const dealKey = `hb_deal_${bid.bidderCode}`;
return {
[bid.adUnitCode]: CONSTANTS.TARGETING_KEYS.map(key => {
Expand All @@ -188,8 +188,29 @@ function getDealTargeting() {
.concat({ [dealKey]: [bid.adserverTargeting[dealKey]] })
};
});
}

return dealTargeting;
/**
* Get custom targeting keys for bids that have `alwaysUseBid=true`.
*/
function getAlwaysUseBidTargeting() {
return pbjs._bidsReceived.map(bid => {
if (bid.alwaysUseBid) {
const standardKeys = CONSTANTS.TARGETING_KEYS;
return {
[bid.adUnitCode]: Object.keys(bid.adserverTargeting, key => key).map(key => {
// Get only the non-standard keys of the losing bids, since we
// don't want to override the standard keys of the winning bid.
if (standardKeys.indexOf(key) > -1) {
return;
}

return { [key.substring(0, 20)]: [bid.adserverTargeting[key]] };

}).filter(key => key) // remove empty elements
};
}
}).filter(bid => bid); // removes empty elements in array;
}

function getBidLandscapeTargeting() {
Expand All @@ -209,10 +230,12 @@ function getBidLandscapeTargeting() {
}

function getAllTargeting() {
let targeting = getWinningBidTargeting();
// deals are always attached to targeting
targeting = getDealTargeting().concat(targeting);
return targeting.concat(pb_sendAllBids ? getBidLandscapeTargeting() : []);
// Get targeting for the winning bid. Add targeting for any bids that have
// `alwaysUseBid=true`. If sending all bids is enabled, add targeting for losing bids.
return getDealTargeting()
.concat(getWinningBidTargeting())
.concat(getAlwaysUseBidTargeting())
.concat(pbjs._sendAllBids ? getBidLandscapeTargeting() : []);
}

//////////////////////////////////
Expand Down Expand Up @@ -240,7 +263,7 @@ pbjs.getAdserverTargetingForAdUnitCodeStr = function (adunitCode) {
};

/**
* This function returns the query string targeting parameters available at this moment for a given ad unit. Note that some bidder's response may not have been received if you call this function too quickly after the requests are sent.
* This function returns the query string targeting parameters available at this moment for a given ad unit. Note that some bidder's response may not have been received if you call this function too quickly after the requests are sent.
* @param adUnitCode {string} adUnitCode to get the bid responses for
* @returns {object} returnObj return bids
*/
Expand Down Expand Up @@ -423,7 +446,7 @@ pbjs.removeAdUnit = function (adUnitCode) {
}
};

pbjs.clearAuction = function() {
pbjs.clearAuction = function () {
auctionRunning = false;
utils.logMessage('Prebid auction cleared');
};
Expand Down Expand Up @@ -676,7 +699,7 @@ pbjs.setPriceGranularity = function (granularity) {
};

pbjs.enableSendAllBids = function () {
pb_sendAllBids = true;
pbjs._sendAllBids = true;
};

processQue();
174 changes: 174 additions & 0 deletions test/fixtures/fixtures.js
Original file line number Diff line number Diff line change
Expand Up @@ -1053,6 +1053,7 @@ export function getAdUnits() {
]
};

// Ad server targeting when `pbjs.enableSendAllBids()` is called.
export function getAdServerTargeting() {
return {
"/19968336/header-bid-tag-0": {
Expand Down Expand Up @@ -1103,3 +1104,176 @@ export function getAdServerTargeting() {
}
};
}

// Key/values used to set ad server targeting.
export function getTargetingKeys() {
return [
[
"hb_bidder",
"appnexus"
],
[
"hb_adid",
"233bcbee889d46d"
],
[
"hb_pb",
"10.00"
],
[
"hb_size",
"300x250"
],
[
"foobar",
"300x250"
],
[
"foobar",
"300x250"
]
];
}

// Key/values used to set ad server targeting when bid landscape
// targeting is on.
export function getTargetingKeysBidLandscape() {
return [
[
"hb_bidder",
"appnexus"
],
[
"hb_adid",
"233bcbee889d46d"
],
[
"hb_pb",
"10.00"
],
[
"hb_size",
"300x250"
],
[
"foobar",
"300x250"
],
[
"foobar",
"300x250"
],
[
"hb_bidder_triplelift",
"triplelift"
],
[
"hb_adid_triplelift",
"222bb26f9e8bd"
],
[
"hb_pb_triplelift",
"10.00"
],
[
"hb_size_triplelift",
"0x0"
],
[
"hb_bidder_appnexus",
"appnexus"
],
[
"hb_adid_appnexus",
"233bcbee889d46d"
],
[
"hb_pb_appnexus",
"10.00"
],
[
"hb_size_appnexus",
"300x250"
],
[
"hb_bidder_pagescienc",
"pagescience"
],
[
"hb_adid_pagescience",
"25bedd4813632d7"
],
[
"hb_pb_pagescience",
"10.00"
],
[
"hb_size_pagescience",
"300x250"
],
[
"hb_bidder_brightcom",
"brightcom"
],
[
"hb_adid_brightcom",
"26e0795ab963896"
],
[
"hb_pb_brightcom",
"10.00"
],
[
"hb_size_brightcom",
"300x250"
],
[
"hb_bidder_brealtime",
"brealtime"
],
[
"hb_adid_brealtime",
"275bd666f5a5a5d"
],
[
"hb_pb_brealtime",
"10.00"
],
[
"hb_size_brealtime",
"300x250"
],
[
"hb_bidder_pubmatic",
"pubmatic"
],
[
"hb_adid_pubmatic",
"28f4039c636b6a7"
],
[
"hb_pb_pubmatic",
"10.00"
],
[
"hb_size_pubmatic",
"300x250"
],
[
"hb_bidder_rubicon",
"rubicon"
],
[
"hb_adid_rubicon",
"29019e2ab586a5a"
],
[
"hb_pb_rubicon",
"10.00"
],
[
"hb_size_rubicon",
"300x600"
]
];
}
Loading

0 comments on commit 2a5b122

Please sign in to comment.