Skip to content
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

Fix clear targeting #415

Merged
merged 2 commits into from
Jun 16, 2016
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
36 changes: 27 additions & 9 deletions src/prebid.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ var BID_TIMEOUT = CONSTANTS.EVENTS.BID_TIMEOUT;
var pb_bidsTimedOut = false;
var auctionRunning = false;
var presetTargeting = [];
var pbTargetingKeys = [];

var eventValidators = {
bidWon: checkDefinedPlacement
Expand Down Expand Up @@ -125,24 +126,32 @@ function setTargeting(targetingConfig) {
utils.logMessage(`Attempting to set key value for slot: ${slot.getSlotElementId()} key: ${Object.keys(key)[0]} value: ${value}`);
return value;
})
.forEach(value => slot.setTargeting(Object.keys(key)[0], value));
.forEach(value => {
slot.setTargeting(Object.keys(key)[0], value)
});
}));
});
}

function getWinningBidTargeting() {
function isNotSetByPb(key) {
return pbTargetingKeys.indexOf(key) === -1;
}

function getPresetTargeting() {
if (isGptPubadsDefined()) {
presetTargeting = (function getPresetTargeting() {
return window.googletag.pubads().getSlots().map(slot => {
return {
[slot.getAdUnitPath()]: slot.getTargetingKeys().map(key => {
[slot.getAdUnitPath()]: slot.getTargetingKeys().filter(isNotSetByPb).map(key => {
return { [key]: slot.getTargeting(key) };
})
};
});
})();
}
}

function getWinningBidTargeting() {
let winners = pbjs._bidsReceived.map(bid => bid.adUnitCode)
.filter(uniques)
.map(adUnitCode => pbjs._bidsReceived
Expand All @@ -169,10 +178,6 @@ function getWinningBidTargeting() {
};
});

if (presetTargeting) {
winners.concat(presetTargeting);
}

return winners;
}

Expand Down Expand Up @@ -232,10 +237,20 @@ function getBidLandscapeTargeting() {
function getAllTargeting() {
// 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()
var targeting = getDealTargeting()
.concat(getWinningBidTargeting())
.concat(getAlwaysUseBidTargeting())
.concat(pbjs._sendAllBids ? getBidLandscapeTargeting() : []);

//store a reference of the targeting keys
targeting.map(adUnitCode => {
Object.keys(adUnitCode).map(key => {
adUnitCode[key].map(targetKey => {
pbTargetingKeys = Object.keys(targetKey).concat(pbTargetingKeys);
});
});
});
return targeting;
}

//////////////////////////////////
Expand Down Expand Up @@ -359,6 +374,10 @@ pbjs.setTargetingForGPTAsync = function () {
return;
}

//first reset any old targeting
getPresetTargeting();
resetPresetTargeting();
//now set new targeting keys
setTargeting(getAllTargeting());
};

Expand Down Expand Up @@ -467,7 +486,6 @@ pbjs.requestBids = function ({ bidsBackHandler, timeout, adUnits, adUnitCodes })
auctionRunning = true;
pbjs._bidsRequested = [];
pbjs._bidsReceived = [];
resetPresetTargeting();
}

const cbTimeout = timeout || pbjs.bidderTimeout;
Expand Down
6 changes: 3 additions & 3 deletions test/spec/unit/pbjs_api_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,12 @@ var Slot = function Slot(elementId, pathId) {
setTargeting: function setTargeting(key, value) {
},

getTargeting: function getTargeting() {
return [{ testKey: ['a test targeting value'] }];
getTargeting: function getTargeting(key) {
return [];
},

getTargetingKeys: function getTargetingKeys() {
return ['testKey'];
return [];
},

clearTargeting: function clearTargeting() {
Expand Down