Skip to content

Commit

Permalink
PBjs Core: use GPT's slot.updateTargetingFromMap instead of slot.setT…
Browse files Browse the repository at this point in the history
…argeting (Issue #7416) (#7453)

* added support for pubcommon, digitrust, id5id

* added support for IdentityLink

* changed the source for id5

* added unit test cases

* changed source param for identityLink

* using GPT's slot.updateTargetingFromMap instead of slot.setTargeting

tests are failing; need to fix tests

* now tests are passing

* tests passsing now

* modified the check for splitting the string

* added some explanation in comment

* code review suggestions
  • Loading branch information
pm-harshad-mane authored Sep 28, 2021
1 parent a5141af commit f0be881
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 13 deletions.
23 changes: 10 additions & 13 deletions src/targeting.js
Original file line number Diff line number Diff line change
Expand Up @@ -379,21 +379,18 @@ export function newTargeting(auctionManager) {
targeting.setTargetingForGPT = function(targetingConfig, customSlotMatching) {
window.googletag.pubads().getSlots().forEach(slot => {
Object.keys(targetingConfig).filter(customSlotMatching ? customSlotMatching(slot) : isAdUnitCodeMatchingSlot(slot))
.forEach(targetId =>
.forEach(targetId => {
Object.keys(targetingConfig[targetId]).forEach(key => {
let valueArr = targetingConfig[targetId][key];
if (typeof valueArr === 'string') {
valueArr = valueArr.split(',');
let value = targetingConfig[targetId][key];
if (typeof value === 'string' && value.indexOf(',') !== -1) {
// due to the check the array will be formed only if string has ',' else plain string will be assigned as value
value = value.split(',');
}
valueArr = (valueArr.length > 1) ? [valueArr] : valueArr;
valueArr.map((value) => {
utils.logMessage(`Attempting to set key value for slot: ${slot.getSlotElementId()} key: ${key} value: ${value}`);
return value;
}).forEach(value => {
slot.setTargeting(key, value);
});
})
)
targetingConfig[targetId][key] = value;
});
utils.logMessage(`Attempting to set targeting-map for slot: ${slot.getSlotElementId()} with targeting-map:`, targetingConfig[targetId]);
slot.updateTargetingFromMap(targetingConfig[targetId])
})
})
};

Expand Down
4 changes: 4 additions & 0 deletions test/spec/unit/pbjs_api_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ var Slot = function Slot(elementId, pathId) {
clearTargeting: function clearTargeting() {
this.targeting = {};
return this;
},

updateTargetingFromMap: function updateTargetingFromMap(targetingMap) {
Object.keys(targetingMap).forEach(key => this.setTargeting(key, targetingMap[key]))
}
};
slot.spySetTargeting = sinon.spy(slot, 'setTargeting');
Expand Down

0 comments on commit f0be881

Please sign in to comment.