Skip to content

Commit

Permalink
userId: group UIDs by source when possible (#10488)
Browse files Browse the repository at this point in the history
  • Loading branch information
dgirardi authored Sep 14, 2023
1 parent 7764e53 commit 425cf20
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 24 deletions.
37 changes: 13 additions & 24 deletions modules/userId/eids.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,34 +32,23 @@ function createEidObject(userIdData, subModuleKey) {
return null;
}

// this function will generate eids array for all available IDs in bidRequest.userId
// this function will be called by userId module
// if any adapter does not want any particular userId to be passed then adapter can use Array.filter(e => e.source != 'tdid')
export function createEidsArray(bidRequestUserId) {
let eids = [];

for (const subModuleKey in bidRequestUserId) {
if (bidRequestUserId.hasOwnProperty(subModuleKey)) {
if (subModuleKey === 'pubProvidedId') {
eids = eids.concat(bidRequestUserId['pubProvidedId']);
} else if (Array.isArray(bidRequestUserId[subModuleKey])) {
bidRequestUserId[subModuleKey].forEach((config, index, arr) => {
const eid = createEidObject(config, subModuleKey);

if (eid) {
eids.push(eid);
}
})
} else {
const eid = createEidObject(bidRequestUserId[subModuleKey], subModuleKey);
if (eid) {
eids.push(eid);
}
}
const allEids = {};
function collect(eid) {
const key = JSON.stringify([eid.source?.toLowerCase(), eid.ext]);
if (allEids.hasOwnProperty(key)) {
allEids[key].uids.push(...eid.uids);
} else {
allEids[key] = eid;
}
}

return eids;
Object.entries(bidRequestUserId).forEach(([name, values]) => {
values = Array.isArray(values) ? values : [values];
const eids = name === 'pubProvidedId' ? values : values.map(value => createEidObject(value, name));
eids.filter(eid => eid != null).forEach(collect);
})
return Object.values(allEids);
}

/**
Expand Down
69 changes: 69 additions & 0 deletions test/spec/modules/userId_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,75 @@ describe('User ID', function () {
});
});

describe('createEidsArray', () => {
beforeEach(() => {
init(config);
setSubmoduleRegistry([
createMockIdSubmodule('mockId1', null, null,
{'mockId1': {source: 'mock1source', atype: 1}}),
createMockIdSubmodule('mockId2v1', null, null,
{'mockId2v1': {source: 'mock2source', atype: 2, getEidExt: () => ({v: 1})}}),
createMockIdSubmodule('mockId2v2', null, null,
{'mockId2v2': {source: 'mock2source', atype: 2, getEidExt: () => ({v: 2})}}),
]);
});

it('should group UIDs by source and ext', () => {
const eids = createEidsArray({
mockId1: ['mock-1-1', 'mock-1-2'],
mockId2v1: ['mock-2-1', 'mock-2-2'],
mockId2v2: ['mock-2-1', 'mock-2-2']
});
expect(eids).to.eql([
{
source: 'mock1source',
uids: [
{
id: 'mock-1-1',
atype: 1,
},
{
id: 'mock-1-2',
atype: 1,
}
]
},
{
source: 'mock2source',
ext: {
v: 1
},
uids: [
{
id: 'mock-2-1',
atype: 2,
},
{
id: 'mock-2-2',
atype: 2,
}
]
},
{
source: 'mock2source',
ext: {
v: 2
},
uids: [
{
id: 'mock-2-1',
atype: 2,
},
{
id: 'mock-2-2',
atype: 2,
}
]
}
])
})
})

it('pbjs.getUserIds', function (done) {
init(config);
setSubmoduleRegistry([sharedIdSystemSubmodule]);
Expand Down

0 comments on commit 425cf20

Please sign in to comment.