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

Allow selection of supported default targeting keys at configuration time. #5763

Merged
merged 2 commits into from
Sep 30, 2020
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
47 changes: 47 additions & 0 deletions src/targeting.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,48 @@ export function newTargeting(auctionManager) {
return [];
};

/**
* Returns filtered ad server targeting for custom and allowed keys.
* @param {targetingArray} targeting
* @param {string[]} allowedKeys
* @return {targetingArray} filtered targeting
*/
function getAllowedTargetingKeyValues(targeting, allowedKeys) {
const defaultKeyring = Object.assign({}, CONSTANTS.TARGETING_KEYS, CONSTANTS.NATIVE_KEYS);
const defaultKeys = Object.keys(defaultKeyring);
const keyDispositions = {};
logInfo(`allowTargetingKeys - allowed keys [ ${allowedKeys.map(k => defaultKeyring[k]).join(', ')} ]`);
targeting.map(adUnit => {
const adUnitCode = Object.keys(adUnit)[0];
const keyring = adUnit[adUnitCode];
const keys = keyring.filter(kvPair => {
const key = Object.keys(kvPair)[0];
// check if key is in default keys, if not, it's custom, we won't remove it.
const isCustom = defaultKeys.filter(defaultKey => key.indexOf(defaultKeyring[defaultKey]) === 0).length === 0;
// check if key explicitly allowed, if not, we'll remove it.
const found = isCustom || allowedKeys.find(allowedKey => {
const allowedKeyName = defaultKeyring[allowedKey];
// we're looking to see if the key exactly starts with one of our default keys.
// (which hopefully means it's not custom)
mimenet marked this conversation as resolved.
Show resolved Hide resolved
const found = key.indexOf(allowedKeyName) === 0;
return found;
});
keyDispositions[key] = !found;
return found;
});
adUnit[adUnitCode] = keys;
});
const removedKeys = Object.keys(keyDispositions).filter(d => keyDispositions[d]);
logInfo(`allowTargetingKeys - removed keys [ ${removedKeys.join(', ')} ]`);
// remove any empty targeting objects, as they're unnecessary.
const filteredTargeting = targeting.filter(adUnit => {
const adUnitCode = Object.keys(adUnit)[0];
const keyring = adUnit[adUnitCode];
return keyring.length > 0;
});
return filteredTargeting
}

/**
* Returns all ad server targeting for all ad units.
* @param {string=} adUnitCode
Expand All @@ -206,6 +248,11 @@ export function newTargeting(auctionManager) {
});
});

const allowedKeys = config.getConfig('targetingControls.allowTargetingKeys');
if (Array.isArray(allowedKeys) && allowedKeys.length > 0) {
targeting = getAllowedTargetingKeyValues(targeting, allowedKeys);
}

targeting = flattenTargeting(targeting);

const auctionKeysThreshold = config.getConfig('targetingControls.auctionKeyMaxChars');
Expand Down
40 changes: 40 additions & 0 deletions test/spec/unit/core/targeting_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,46 @@ describe('targeting tests', function () {
});
});

describe('targetingControls.allowTargetingKeys', function () {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we are going to use "begins with" matching then embody that in a test as a form of explicit documentation, this will trigger a test failure if someone tries to "improve" the code and change the behavior among other advantages like documenting the reality in the test:

it('should allow hn_pb and hb_pb_anything in any variation')

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense. The behavior is being tested, but it's not documented clearly. Thanks!

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This has been updated.

let bid4;

beforeEach(function() {
bid4 = utils.deepClone(bid1);
bid4.adserverTargeting = {
hb_deal: '4321',
hb_pb: '0.1',
hb_adid: '567891011',
hb_bidder: 'appnexus',
};
bid4.bidder = bid4.bidderCode = 'appnexus';
bid4.cpm = 0.1; // losing bid so not included if enableSendAllBids === false
bid4.dealId = '4321';
enableSendAllBids = true;
config.setConfig({
targetingControls: {
allowTargetingKeys: ['BIDDER', 'AD_ID', 'PRICE_BUCKET']
}
});
bidsReceived.push(bid4);
});

it('targeting should include custom keys', function () {
const targeting = targetingInstance.getAllTargeting(['/123456/header-bid-tag-0']);
expect(targeting['/123456/header-bid-tag-0']).to.include.all.keys('foobar');
});

it('targeting should include keys prefixed by allowed default targeting keys', function () {
const targeting = targetingInstance.getAllTargeting(['/123456/header-bid-tag-0']);
expect(targeting['/123456/header-bid-tag-0']).to.include.all.keys('hb_bidder_rubicon', 'hb_adid_rubicon', 'hb_pb_rubicon');
expect(targeting['/123456/header-bid-tag-0']).to.include.all.keys('hb_bidder_appnexus', 'hb_adid_appnexus', 'hb_pb_appnexus');
});

it('targeting should not include keys prefixed by disallowed default targeting keys', function () {
const targeting = targetingInstance.getAllTargeting(['/123456/header-bid-tag-0']);
expect(targeting['/123456/header-bid-tag-0']).to.not.have.all.keys(['hb_deal_appnexus', 'hb_deal_rubicon']);
});
});

describe('targetingControls.alwaysIncludeDeals', function () {
let bid4;

Expand Down