Skip to content

Commit e0a8c58

Browse files
permutiveRtd : transform integers to strings (prebid#11910)
* fix(permutiveRtd): transform integers to strings * docs(permutiveRtd): update jsdoc to match function signatures * docs(permutiveRtd): fix ordering of jsdoc comments
1 parent 9529ae3 commit e0a8c58

File tree

2 files changed

+114
-30
lines changed

2 files changed

+114
-30
lines changed

modules/permutiveRtdProvider.js

Lines changed: 72 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,8 @@ export function getModuleConfig(customModuleConfig) {
9595
/**
9696
* Sets ortb2 config for ac bidders
9797
* @param {Object} bidderOrtb2 - The ortb2 object for the all bidders
98-
* @param {Object} customModuleConfig - Publisher config for module
98+
* @param {Object} moduleConfig - Publisher config for module
99+
* @param {Object} segmentData - Segment data grouped by bidder or type
99100
*/
100101
export function setBidderRtb (bidderOrtb2, moduleConfig, segmentData) {
101102
const acBidders = deepAccess(moduleConfig, 'params.acBidders')
@@ -129,13 +130,13 @@ export function setBidderRtb (bidderOrtb2, moduleConfig, segmentData) {
129130

130131
/**
131132
* Updates `user.data` object in existing bidder config with Permutive segments
132-
* @param string bidder - The bidder
133+
* @param {string} bidder - The bidder identifier
133134
* @param {Object} currConfig - Current bidder config
134-
* @param {Object[]} transformationConfigs - array of objects with `id` and `config` properties, used to determine
135-
* the transformations on user data to include the ORTB2 object
136135
* @param {string[]} segmentIDs - Permutive segment IDs
137136
* @param {string[]} sspSegmentIDs - Permutive SSP segment IDs
138137
* @param {Object} topics - Privacy Sandbox Topics, keyed by IAB taxonomy version (600, 601, etc.)
138+
* @param {Object[]} transformationConfigs - array of objects with `id` and `config` properties, used to determine
139+
* the transformations on user data to include the ORTB2 object
139140
* @param {Object} segmentData - The segments available for targeting
140141
* @return {Object} Merged ortb2 object
141142
*/
@@ -270,7 +271,7 @@ function setSegments (reqBidsConfigObj, moduleConfig, segmentData) {
270271
*/
271272
function makeSafe (fn) {
272273
try {
273-
fn()
274+
return fn()
274275
} catch (e) {
275276
logError(e)
276277
}
@@ -310,23 +311,71 @@ export function isPermutiveOnPage () {
310311
* @param {number} maxSegs - Maximum number of segments to be included
311312
* @return {Object}
312313
*/
313-
export function getSegments (maxSegs) {
314-
const legacySegs = readSegments('_psegs', []).map(Number).filter(seg => seg >= 1000000).map(String)
315-
const _ppam = readSegments('_ppam', [])
316-
const _pcrprs = readSegments('_pcrprs', [])
317-
314+
export function getSegments(maxSegs) {
318315
const segments = {
319-
ac: [..._pcrprs, ..._ppam, ...legacySegs],
320-
ix: readSegments('_pindexs', []),
321-
rubicon: readSegments('_prubicons', []),
322-
appnexus: readSegments('_papns', []),
323-
gam: readSegments('_pdfps', []),
324-
ssp: readSegments('_pssps', {
325-
cohorts: [],
326-
ssps: []
316+
ac:
317+
makeSafe(() => {
318+
const legacySegs =
319+
makeSafe(() =>
320+
readSegments('_psegs', [])
321+
.map(Number)
322+
.filter((seg) => seg >= 1000000)
323+
.map(String),
324+
) || [];
325+
const _ppam = makeSafe(() => readSegments('_ppam', []).map(String)) || [];
326+
const _pcrprs = makeSafe(() => readSegments('_pcrprs', []).map(String)) || [];
327+
328+
return [..._pcrprs, ..._ppam, ...legacySegs];
329+
}) || [],
330+
331+
ix:
332+
makeSafe(() => {
333+
const _pindexs = readSegments('_pindexs', []);
334+
return _pindexs.map(String);
335+
}) || [],
336+
337+
rubicon:
338+
makeSafe(() => {
339+
const _prubicons = readSegments('_prubicons', []);
340+
return _prubicons.map(String);
341+
}) || [],
342+
343+
appnexus:
344+
makeSafe(() => {
345+
const _papns = readSegments('_papns', []);
346+
return _papns.map(String);
347+
}) || [],
348+
349+
gam:
350+
makeSafe(() => {
351+
const _pdfps = readSegments('_pdfps', []);
352+
return _pdfps.map(String);
353+
}) || [],
354+
355+
ssp: makeSafe(() => {
356+
const _pssps = readSegments('_pssps', {
357+
cohorts: [],
358+
ssps: [],
359+
});
360+
361+
return {
362+
cohorts: makeSafe(() => _pssps.cohorts.map(String)) || [],
363+
ssps: makeSafe(() => _pssps.ssps.map(String)) || [],
364+
};
327365
}),
328-
topics: readSegments('_ppsts', {}),
329-
}
366+
367+
topics:
368+
makeSafe(() => {
369+
const _ppsts = readSegments('_ppsts', {});
370+
371+
const topics = {};
372+
for (const [k, value] of Object.entries(_ppsts)) {
373+
topics[k] = makeSafe(() => value.map(String)) || [];
374+
}
375+
376+
return topics;
377+
}) || {},
378+
};
330379

331380
for (const bidder in segments) {
332381
if (bidder === 'ssp') {
@@ -342,7 +391,8 @@ export function getSegments (maxSegs) {
342391
}
343392
}
344393

345-
return segments
394+
logger.logInfo(`Read segments`, segments)
395+
return segments;
346396
}
347397

348398
/**
@@ -393,7 +443,7 @@ function iabSegmentId(permutiveSegmentId, iabIds) {
393443
* Pull the latest configuration and cohort information and update accordingly.
394444
*
395445
* @param reqBidsConfigObj - Bidder provided config for request
396-
* @param customModuleConfig - Publisher provide config
446+
* @param moduleConfig - Publisher provided config
397447
*/
398448
export function readAndSetCohorts(reqBidsConfigObj, moduleConfig) {
399449
const segmentData = getSegments(deepAccess(moduleConfig, 'params.maxSegs'))

test/spec/modules/permutiveRtdProvider_spec.js

Lines changed: 42 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -484,7 +484,9 @@ describe('permutiveRtdProvider', function () {
484484
_psegs: [],
485485
_ppam: [],
486486
_pcrprs: [],
487-
_pssps: { ssps: [], cohorts: [] }
487+
_pindexs: [],
488+
_pssps: { ssps: [], cohorts: [] },
489+
_ppsts: {},
488490
})
489491

490492
setBidderRtb(bidderConfig, moduleConfig, segmentsData)
@@ -568,6 +570,7 @@ describe('permutiveRtdProvider', function () {
568570
const data = transformedTargeting()
569571
expect(getSegments(250)).to.deep.equal(data)
570572
})
573+
571574
it('should enforce max segments', function () {
572575
const max = 1
573576
const segments = getSegments(max)
@@ -584,6 +587,26 @@ describe('permutiveRtdProvider', function () {
584587
}
585588
}
586589
})
590+
591+
it('should coerce numbers to strings', function () {
592+
setLocalStorage({ _prubicons: [1, 2, 3], _pssps: { ssps: ['foo', 'bar'], cohorts: [4, 5, 6] } })
593+
594+
const segments = getSegments(200)
595+
596+
expect(segments.rubicon).to.deep.equal(['1', '2', '3'])
597+
expect(segments.ssp.ssps).to.deep.equal(['foo', 'bar'])
598+
expect(segments.ssp.cohorts).to.deep.equal(['4', '5', '6'])
599+
})
600+
601+
it('should return empty values on unexpected format', function () {
602+
setLocalStorage({ _prubicons: 'a string instead?', _pssps: 123 })
603+
604+
const segments = getSegments(200)
605+
606+
expect(segments.rubicon).to.deep.equal([])
607+
expect(segments.ssp.ssps).to.deep.equal([])
608+
expect(segments.ssp.cohorts).to.deep.equal([])
609+
})
587610
})
588611

589612
describe('Existing key-value targeting', function () {
@@ -703,14 +726,25 @@ function getConfig () {
703726
}
704727

705728
function transformedTargeting (data = getTargetingData()) {
729+
const topics = (() => {
730+
const topics = {}
731+
for (const topic in data._ppsts) {
732+
topics[topic] = data._ppsts[topic].map(String)
733+
}
734+
return topics
735+
})()
736+
706737
return {
707-
ac: [...data._pcrprs, ...data._ppam, ...data._psegs.filter(seg => seg >= 1000000)],
708-
appnexus: data._papns,
709-
ix: data._pindexs,
710-
rubicon: data._prubicons,
711-
gam: data._pdfps,
712-
ssp: data._pssps,
713-
topics: data._ppsts,
738+
ac: [...data._pcrprs, ...data._ppam, ...data._psegs.filter(seg => seg >= 1000000)].map(String),
739+
appnexus: data._papns.map(String),
740+
ix: data._pindexs.map(String),
741+
rubicon: data._prubicons.map(String),
742+
gam: data._pdfps.map(String),
743+
ssp: {
744+
ssps: data._pssps.ssps.map(String),
745+
cohorts: data._pssps.cohorts.map(String)
746+
},
747+
topics,
714748
}
715749
}
716750

0 commit comments

Comments
 (0)