Skip to content

Commit

Permalink
Making targeting keys configurable (prebid#3140)
Browse files Browse the repository at this point in the history
* moved NATIVE_KEYS to constants

* Changed TARGETING_KEYS from array to an array

- Now you can change key names
- Made changes in code at auction.js and targeting.js to handle the TARGETING_KEYS object than array

* fixed aauctionmanager test cases for dynamic targeting key names

* fixed native test cases for dynamic targeting key names

* fixed test cases of targeting for dynamic targeting key names

* in-dev changes

* replace old keys with new keys

* fixed a typo

* fixed a unit case in utils

* updated convertTargetingsFromOldToNew in fixtures

this will now replace partner specific keys as well as per config

* removed a log

* keep test case as it was

* improved convertTargetingsFromOldToNew in fixtures

* Capitalized key names in TARGETING_KEYS object; not values in object

* using computed properties

* using computed properties

* fixed eslint suggestion

* fixed eslint suggestions

* keeping unit tests more compliant with Mocha
  • Loading branch information
pm-harshad-mane authored and Pedro López Jiménez committed Mar 18, 2019
1 parent a680824 commit 42815f0
Show file tree
Hide file tree
Showing 10 changed files with 267 additions and 233 deletions.
16 changes: 8 additions & 8 deletions src/auction.js
Original file line number Diff line number Diff line change
Expand Up @@ -460,17 +460,17 @@ export function getStandardBidderSettings(mediaType) {
if (!bidderSettings[CONSTANTS.JSON_MAPPING.BD_SETTING_STANDARD][CONSTANTS.JSON_MAPPING.ADSERVER_TARGETING]) {
bidderSettings[CONSTANTS.JSON_MAPPING.BD_SETTING_STANDARD][CONSTANTS.JSON_MAPPING.ADSERVER_TARGETING] = [
{
key: 'hb_bidder',
key: CONSTANTS.TARGETING_KEYS.BIDDER,
val: function (bidResponse) {
return bidResponse.bidderCode;
}
}, {
key: 'hb_adid',
key: CONSTANTS.TARGETING_KEYS.AD_ID,
val: function (bidResponse) {
return bidResponse.adId;
}
}, {
key: 'hb_pb',
key: CONSTANTS.TARGETING_KEYS.PRICE_BUCKET,
val: function (bidResponse) {
if (granularity === CONSTANTS.GRANULARITY_OPTIONS.AUTO) {
return bidResponse.pbAg;
Expand All @@ -487,24 +487,24 @@ export function getStandardBidderSettings(mediaType) {
}
}
}, {
key: 'hb_size',
key: CONSTANTS.TARGETING_KEYS.SIZE,
val: function (bidResponse) {
return bidResponse.size;
}
}, {
key: 'hb_deal',
key: CONSTANTS.TARGETING_KEYS.DEAL,
val: function (bidResponse) {
return bidResponse.dealId;
}
},
{
key: 'hb_source',
key: CONSTANTS.TARGETING_KEYS.SOURCE,
val: function (bidResponse) {
return bidResponse.source;
}
},
{
key: 'hb_format',
key: CONSTANTS.TARGETING_KEYS.FORMAT,
val: function (bidResponse) {
return bidResponse.mediaType;
}
Expand Down Expand Up @@ -565,7 +565,7 @@ function setKeys(keyValues, bidderSettings, custBidObj) {

if (
((typeof bidderSettings.suppressEmptyKeys !== 'undefined' && bidderSettings.suppressEmptyKeys === true) ||
key === 'hb_deal') && // hb_deal is suppressed automatically if not set
key === CONSTANTS.TARGETING_KEYS.DEAL) && // hb_deal is suppressed automatically if not set
(
utils.isEmptyStr(value) ||
value === null ||
Expand Down
27 changes: 18 additions & 9 deletions src/constants.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,24 @@
"DENSE": "dense",
"CUSTOM": "custom"
},
"TARGETING_KEYS": [
"hb_bidder",
"hb_adid",
"hb_pb",
"hb_size",
"hb_deal",
"hb_source",
"hb_format"
],
"TARGETING_KEYS": {
"BIDDER": "hb_bidder",
"AD_ID": "hb_adid",
"PRICE_BUCKET": "hb_pb",
"SIZE": "hb_size",
"DEAL": "hb_deal",
"SOURCE": "hb_source",
"FORMAT": "hb_format"
},
"NATIVE_KEYS": {
"title": "hb_native_title",
"body": "hb_native_body",
"sponsoredBy": "hb_native_brand",
"image": "hb_native_image",
"icon": "hb_native_icon",
"clickUrl": "hb_native_linkurl",
"cta": "hb_native_cta"
},
"S2S" : {
"SRC" : "s2s",
"DEFAULT_ENDPOINT" : "https://prebid.adnxs.com/pbs/v1/openrtb2/auction",
Expand Down
18 changes: 5 additions & 13 deletions src/native.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,12 @@
import { deepAccess, getBidRequest, logError, triggerPixel, insertHtmlIntoIframe } from './utils';
import includes from 'core-js/library/fn/array/includes';

export const nativeAdapters = [];
const CONSTANTS = require('./constants.json');

export const NATIVE_KEYS = {
title: 'hb_native_title',
body: 'hb_native_body',
sponsoredBy: 'hb_native_brand',
image: 'hb_native_image',
icon: 'hb_native_icon',
clickUrl: 'hb_native_linkurl',
cta: 'hb_native_cta',
};
export const nativeAdapters = [];

export const NATIVE_TARGETING_KEYS = Object.keys(NATIVE_KEYS).map(
key => NATIVE_KEYS[key]
export const NATIVE_TARGETING_KEYS = Object.keys(CONSTANTS.NATIVE_KEYS).map(
key => CONSTANTS.NATIVE_KEYS[key]
);

const IMAGE = {
Expand Down Expand Up @@ -163,7 +155,7 @@ export function getNativeTargeting(bid) {
let keyValues = {};

Object.keys(bid['native']).forEach(asset => {
const key = NATIVE_KEYS[asset];
const key = CONSTANTS.NATIVE_KEYS[asset];
let value = bid['native'][asset];

// native image-type assets can be a string or an object with a url prop
Expand Down
10 changes: 7 additions & 3 deletions src/targeting.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ export const RENDERED = 'rendered';
const MAX_DFP_KEYLENGTH = 20;
const TTL_BUFFER = 1000;

export const TARGETING_KEYS = Object.keys(CONSTANTS.TARGETING_KEYS).map(
key => CONSTANTS.TARGETING_KEYS[key]
);

// return unexpired bids
export const isBidNotExpired = (bid) => (bid.responseTimestamp + bid.ttl * 1000 + TTL_BUFFER) > timestamp();

Expand Down Expand Up @@ -262,7 +266,7 @@ export function newTargeting(auctionManager) {
.reduce((acc, key) => {
const targetingValue = [winner.adserverTargeting[key]];
const targeting = { [key.substring(0, MAX_DFP_KEYLENGTH)]: targetingValue };
if (key === 'hb_deal') {
if (key === CONSTANTS.TARGETING_KEYS.DEAL) {
const bidderCodeTargetingKey = `${key}_${winner.bidderCode}`.substring(0, MAX_DFP_KEYLENGTH);
const bidderCodeTargeting = { [bidderCodeTargetingKey]: targetingValue };
return [...acc, targeting, bidderCodeTargeting];
Expand All @@ -278,7 +282,7 @@ export function newTargeting(auctionManager) {
function getStandardKeys() {
return auctionManager.getStandardBidderAdServerTargeting() // in case using a custom standard key set
.map(targeting => targeting.key)
.concat(CONSTANTS.TARGETING_KEYS).filter(uniques); // standard keys defined in the library.
.concat(TARGETING_KEYS).filter(uniques); // standard keys defined in the library.
}

/**
Expand Down Expand Up @@ -360,7 +364,7 @@ export function newTargeting(auctionManager) {
* @return {targetingArray} all non-winning bids targeting
*/
function getBidLandscapeTargeting(adUnitCodes, bidsReceived) {
const standardKeys = CONSTANTS.TARGETING_KEYS.concat(NATIVE_TARGETING_KEYS);
const standardKeys = TARGETING_KEYS.concat(NATIVE_TARGETING_KEYS);

const bids = getHighestCpmBidsFromBidPool(bidsReceived, getHighestCpm);

Expand Down
Loading

0 comments on commit 42815f0

Please sign in to comment.