Skip to content

Commit

Permalink
Prebid Core: use gptSlot.updateTargetingFromMap than gptSlot.setTarge…
Browse files Browse the repository at this point in the history
…ting in targeting.resetPresetTargeting (prebid#7552)

* added support for pubcommon, digitrust, id5id

* added support for IdentityLink

* changed the source for id5

* added unit test cases

* changed source param for identityLink

* import utils functions as needed and not the whole module

* import utils functions as needed and not the whole module

* import utils functions as needed and not the whole module

* Revert "import utils functions as needed and not the whole module"

This reverts commit bc6c9f6.

* Revert "import utils functions as needed and not the whole module"

This reverts commit ef500ab.

* Revert "import utils functions as needed and not the whole module"

This reverts commit 7e3fa3f.

* use updateTargetingFromMap than setTargeting in targeting.resetPresetTargeting

* indent

* using arrow functions
  • Loading branch information
pm-harshad-mane authored and Chris Pabst committed Jan 10, 2022
1 parent cacc0b2 commit eb91a53
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 11 deletions.
20 changes: 11 additions & 9 deletions src/targeting.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,17 +123,19 @@ export function newTargeting(auctionManager) {
if (isGptPubadsDefined()) {
const adUnitCodes = getAdUnitCodes(adUnitCode);
const adUnits = auctionManager.getAdUnits().filter(adUnit => includes(adUnitCodes, adUnit.code));
let unsetKeys = pbTargetingKeys.reduce((reducer, key) => {
reducer[key] = null;
return reducer;
}, {});
window.googletag.pubads().getSlots().forEach(slot => {
let customSlotMatchingFunc = isFn(customSlotMatching) && customSlotMatching(slot);
pbTargetingKeys.forEach(function(key) {
// reset only registered adunits
adUnits.forEach(function(unit) {
if (unit.code === slot.getAdUnitPath() ||
unit.code === slot.getSlotElementId() ||
(isFn(customSlotMatchingFunc) && customSlotMatchingFunc(unit.code))) {
slot.setTargeting(key, null);
}
});
// reset only registered adunits
adUnits.forEach(unit => {
if (unit.code === slot.getAdUnitPath() ||
unit.code === slot.getSlotElementId() ||
(isFn(customSlotMatchingFunc) && customSlotMatchingFunc(unit.code))) {
slot.updateTargetingFromMap(unsetKeys);
}
});
});
}
Expand Down
39 changes: 37 additions & 2 deletions test/spec/unit/pbjs_api_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ var createSlotArrayScenario2 = function createSlotArrayScenario2() {
var slot1 = new Slot(config.adUnitElementIDs[0], config.adUnitCodes[0]);
slot1.setTargeting('pos1', '750x350');
var slot2 = new Slot(config.adUnitElementIDs[1], config.adUnitCodes[0]);
slot2.setTargeting('gender', ['male', 'female']);
return [
slot1,
slot2
Expand Down Expand Up @@ -862,14 +861,17 @@ describe('Unit: Prebid Module', function () {

it('should set googletag targeting keys after calling setTargetingForGPTAsync function', function () {
var slots = createSlotArrayScenario2();

// explicitly setting some PBJS key value pairs to verify whether these are removed befor new keys are set

window.googletag.pubads().setSlots(slots);
$$PREBID_GLOBAL$$.setTargetingForGPTAsync([config.adUnitCodes[0]]);

// we need to transform the spySetTargeting into something that looks like
// googletag's targeting structure
// googletag setTargeting will override old value if invoked with same key

const targeting = [];
let targeting = [];
slots[1].getTargetingKeys().map(function (key) {
const value = slots[1].getTargeting(key);
targeting.push([key, value]);
Expand All @@ -887,6 +889,39 @@ describe('Unit: Prebid Module', function () {
invokedTargeting.push([key, value]);
});
assert.deepEqual(targeting, invokedTargeting, 'google tag targeting options not matching');

// resetPresetTargeting: initiate a new auction with no winning bids, now old targeting should be removed

resetAuction();
auction.getBidsReceived = function() { return [] };

var slots = createSlotArrayScenario2();
window.googletag.pubads().setSlots(slots);

$$PREBID_GLOBAL$$.setTargetingForGPTAsync([config.adUnitCodes[0]]);

targeting = [];
slots[1].getTargetingKeys().map(function (key) {
const value = slots[1].getTargeting(key);
targeting.push([key, value]);
});

invokedTargetingMap = {};
slots[1].spySetTargeting.args.map(function (entry) {
invokedTargetingMap[entry[0]] = entry[1];
});

var invokedTargeting = [];

Object.getOwnPropertyNames(invokedTargetingMap).map(function (key) {
const value = Array.isArray(invokedTargetingMap[key]) ? invokedTargetingMap[key] : [invokedTargetingMap[key]]; // values are always returned as array in googletag
invokedTargeting.push([key, value]);
});
assert.deepEqual(targeting, invokedTargeting, 'google tag targeting options not matching');
targeting.forEach(function(e) {
// here e[0] is key and e[1] is value in array that should be [null] as we are un-setting prebid keys in resetPresetTargeting
assert.deepEqual(e[1], [null], 'resetPresetTargeting: the value of the key ' + e[0] + ' should be [null]');
});
});

it('should set googletag targeting keys to specific slot with customSlotMatching', function () {
Expand Down

0 comments on commit eb91a53

Please sign in to comment.