Skip to content

Commit 9af0357

Browse files
authored
prebid 8 - read ortb segment data in appnexus keywords library functions (#10082)
* prebid8 - read ortb segment data in appnexus keywords library functions * remove null condition from segtax map
1 parent e563a3c commit 9af0357

File tree

2 files changed

+100
-3
lines changed

2 files changed

+100
-3
lines changed

libraries/appnexusKeywords/anKeywords.js

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
1-
import {_each, getValueString, isArray, isStr, mergeDeep, isNumber} from '../../src/utils.js';
1+
import {_each, deepAccess, getValueString, isArray, isStr, mergeDeep, isNumber} from '../../src/utils.js';
22
import {getAllOrtbKeywords} from '../keywords/keywords.js';
3+
import {CLIENT_SECTIONS} from '../../src/fpd/oneClient.js';
4+
5+
const ORTB_SEGTAX_KEY_MAP = {
6+
526: '1plusx',
7+
527: '1plusx',
8+
541: 'captify_segments',
9+
540: 'perid'
10+
};
11+
const ORTB_SEG_PATHS = ['user.data'].concat(
12+
CLIENT_SECTIONS.map((prefix) => `${prefix}.content.data`)
13+
);
314

415
/**
516
* Converts an object of arrays (either strings or numbers) into an array of objects containing key and value properties
@@ -85,7 +96,7 @@ function convertKeywordsToANMap(kwarray) {
8596
* @param ortb2
8697
* @return {{}} appnexus-style keyword map using all keywords contained in ortb2
8798
*/
88-
export function getANMapFromOrtb(ortb2) {
99+
export function getANMapFromOrtbKeywords(ortb2) {
89100
return convertKeywordsToANMap(getAllOrtbKeywords(ortb2));
90101
}
91102

@@ -100,7 +111,30 @@ export function getANKewyordParamFromMaps(...anKeywordMaps) {
100111

101112
export function getANKeywordParam(ortb2, ...anKeywordsMaps) {
102113
return getANKewyordParamFromMaps(
103-
getANMapFromOrtb(ortb2),
114+
getANMapFromOrtbKeywords(ortb2),
115+
getANMapFromOrtbSegments(ortb2),
104116
...anKeywordsMaps
105117
)
106118
}
119+
120+
export function getANMapFromOrtbSegments(ortb2) {
121+
let ortbSegData = {};
122+
ORTB_SEG_PATHS.forEach(path => {
123+
let ortbSegsArrObj = deepAccess(ortb2, path) || [];
124+
ortbSegsArrObj.forEach(segObj => {
125+
// only read segment data from known sources
126+
const segtax = ORTB_SEGTAX_KEY_MAP[deepAccess(segObj, 'ext.segtax')];
127+
if (segtax) {
128+
segObj.segment.forEach(seg => {
129+
// if source was in multiple locations of ortb or had multiple segments in same area, stack them together into an array
130+
if (ortbSegData[segtax]) {
131+
ortbSegData[segtax].push(seg.id);
132+
} else {
133+
ortbSegData[segtax] = [seg.id]
134+
}
135+
});
136+
}
137+
});
138+
});
139+
return ortbSegData;
140+
}

test/spec/modules/appnexusBidAdapter_spec.js

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -784,6 +784,69 @@ describe('AppNexusAdapter', function () {
784784
config.getConfig.restore();
785785
});
786786

787+
it('adds ortb2 segments to auction request as keywords', function() {
788+
let bidRequest = Object.assign({}, bidRequests[0]);
789+
const bidderRequest = {
790+
ortb2: {
791+
site: {
792+
keywords: 'drill',
793+
content: {
794+
data: [{
795+
name: 'siteseg1',
796+
ext: {
797+
segtax: 540
798+
},
799+
segment: [{
800+
id: 's123',
801+
}, {
802+
id: 's234'
803+
}]
804+
}, {
805+
name: 'sitseg2',
806+
ext: {
807+
segtax: 1
808+
},
809+
segment: [{
810+
id: 'unknown'
811+
}]
812+
}, {
813+
name: 'siteseg3',
814+
ext: {
815+
segtax: 526
816+
},
817+
segment: [{
818+
id: 'dog'
819+
}]
820+
}]
821+
}
822+
},
823+
user: {
824+
data: [{
825+
name: 'userseg1',
826+
ext: {
827+
segtax: 526
828+
},
829+
segment: [{
830+
id: 'cat'
831+
}]
832+
}]
833+
}
834+
}
835+
};
836+
const request = spec.buildRequests([bidRequest], bidderRequest);
837+
const payload = JSON.parse(request.data);
838+
839+
expectKeywords(payload.keywords, [{
840+
'key': 'drill'
841+
}, {
842+
'key': '1plusx',
843+
'value': ['cat', 'dog']
844+
}, {
845+
'key': 'perid',
846+
'value': ['s123', 's234']
847+
}]);
848+
});
849+
787850
if (FEATURES.NATIVE) {
788851
it('should attach native params to the request', function () {
789852
let bidRequest = Object.assign({},

0 commit comments

Comments
 (0)