-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
Data Controller Module: initial release #8484
Changes from 7 commits
fcf32fb
4a08291
5aecd26
5332b4c
9f7622b
217c366
1d49b72
b4f7174
798b918
f5b14be
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,162 @@ | ||
/** | ||
* This module validates the configuration and filters data accordingly | ||
* @module modules/dataController | ||
*/ | ||
import {config} from '../../src/config.js'; | ||
import {getHook, module} from '../../src/hook.js'; | ||
import {deepAccess, prefixLog} from '../../src/utils.js'; | ||
import {startAuction} from '../../src/prebid.js'; | ||
|
||
const LOG_PRE_FIX = 'Data_Controller : '; | ||
const ALL = '*'; | ||
const MODULE_NAME = 'dataController'; | ||
let _dataControllerConfig; | ||
|
||
const _logger = prefixLog(LOG_PRE_FIX); | ||
|
||
/** | ||
* BidderRequests hook to intiate module and reset data object | ||
*/ | ||
export function filterBidData(fn, req) { | ||
if (_dataControllerConfig.filterEIDwhenSDA) { | ||
filterEIDs(req.adUnits, req.ortb2Fragments); | ||
} | ||
|
||
if (_dataControllerConfig.filterSDAwhenEID) { | ||
filterSDA(req.adUnits, req.ortb2Fragments); | ||
} | ||
fn.call(this, req); | ||
return req; | ||
} | ||
|
||
function containsConfiguredEIDS(eidSourcesMap, bidderCode) { | ||
if (_dataControllerConfig.filterSDAwhenEID.includes(ALL)) { | ||
return true; | ||
} | ||
let bidderEIDs = eidSourcesMap.get(bidderCode); | ||
if (bidderEIDs == undefined) { | ||
return false; | ||
} | ||
let containsEIDs = false; | ||
_dataControllerConfig.filterSDAwhenEID.forEach(source => { | ||
if (bidderEIDs.has(source)) { | ||
containsEIDs = true; | ||
} | ||
}); | ||
return containsEIDs; | ||
} | ||
|
||
export function containsConfiguredSDA(segementMap, bidderCode) { | ||
if (_dataControllerConfig.filterEIDwhenSDA.includes(ALL)) { | ||
return true; | ||
} | ||
|
||
let bidderSegement = segementMap.get(bidderCode); | ||
if (bidderSegement == undefined) { | ||
return false; | ||
} | ||
|
||
let containsSDA = false; | ||
_dataControllerConfig.filterEIDwhenSDA.forEach(segment => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same, |
||
if (bidderSegement.has(segment)) { | ||
containsSDA = true; | ||
} | ||
}); | ||
return containsSDA; | ||
} | ||
|
||
export function getSegmentConfig(ortb2Fragments) { | ||
let bidderSDAMap = new Map(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. there's some duplication in here, which you could avoid by reusing the same logic for both global and bidder data. Here's a possible refactor to illustrate what I mean:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed |
||
|
||
for (const [key, value] of Object.entries(ortb2Fragments.bidder)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here - should this look at global config? |
||
let userData = deepAccess(value, 'user.data') || []; | ||
|
||
if (userData) { | ||
let segmentSet = new Set(); | ||
for (let i = 0; i < userData.length; i++) { | ||
let segments = userData[i].segment; | ||
let segmentPrefix = ''; | ||
if (userData[i].name) { | ||
segmentPrefix = userData[i].name + ':'; | ||
} | ||
|
||
if (userData[i].ext && userData[i].ext.segtax) { | ||
segmentPrefix += userData[i].ext.segtax + ':'; | ||
} | ||
for (let j = 0; j < segments.length; j++) { | ||
segmentSet.add(segmentPrefix + segments[j].id); | ||
} | ||
} | ||
bidderSDAMap.set(key, segmentSet); | ||
} | ||
} | ||
return bidderSDAMap; | ||
} | ||
|
||
function getEIDsSource(adUnits) { | ||
let bidderEIDSMap = new Map(); | ||
adUnits.forEach(adUnit => { | ||
adUnit.bids.forEach(bid => { | ||
let userEIDs = deepAccess(bid, 'userIdAsEids') || []; | ||
|
||
if (userEIDs) { | ||
let sourceSet = new Set(); | ||
for (let i = 0; i < userEIDs.length; i++) { | ||
let source = userEIDs[i].source; | ||
sourceSet.add(source); | ||
} | ||
bidderEIDSMap.set(bid.bidder, sourceSet); | ||
} | ||
}); | ||
}); | ||
|
||
return bidderEIDSMap; | ||
} | ||
|
||
function filterSDA(adUnits, ortb2Fragments) { | ||
let bidderEIDSMap = getEIDsSource(adUnits); | ||
for (const [key, value] of Object.entries(ortb2Fragments.bidder)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
If as the pub I do
and assuming there's nothing else, here
so this logic would have no effect, and downstream in the adapters the final
|
||
let resetSDA = containsConfiguredEIDS(bidderEIDSMap, key); | ||
if (resetSDA) { | ||
value.user.data = [] | ||
} | ||
} | ||
} | ||
|
||
function filterEIDs(adUnits, ortb2Fragments) { | ||
let segementMap = getSegmentConfig(ortb2Fragments); | ||
|
||
adUnits.forEach(adUnit => { | ||
adUnit.bids.forEach(bid => { | ||
let resetEID = containsConfiguredSDA(segementMap, bid.bidder); | ||
if (resetEID) { | ||
bid.userIdAsEids = []; | ||
} | ||
}); | ||
}); | ||
|
||
return adUnits; | ||
} | ||
|
||
export function init() { | ||
const confListener = config.getConfig(MODULE_NAME, dataControllerConfig => { | ||
if (!dataControllerConfig || !dataControllerConfig.dataController) { | ||
_logger.logInfo(`Data Controller is not configured`); | ||
startAuction.getHooks({hook: filterBidData}).remove(); | ||
return; | ||
} | ||
|
||
if (dataControllerConfig.dataController.filterEIDwhenSDA && dataControllerConfig.dataController.filterSDAwhenEID) { | ||
_logger.logInfo(`Data Controller can be configured with either filterEIDwhenSDA or filterSDAwhenEID`); | ||
startAuction.getHooks({hook: filterBidData}).remove(); | ||
return; | ||
} | ||
confListener(); // unsubscribe config listener | ||
_dataControllerConfig = dataControllerConfig.dataController; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we can clean this up a little to store the dataController in a const then reference that instead or referencing the full path each time. i.e.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Updated |
||
|
||
getHook('startAuction').before(filterBidData); | ||
}); | ||
} | ||
|
||
init(); | ||
module(MODULE_NAME, init); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# Overview | ||
|
||
``` | ||
Module Name: Data Controller Module | ||
``` | ||
|
||
# Description | ||
|
||
This module will filter EIDs and SDA based on the configurations. | ||
The filtered EIDs are stored in 'dcUsersAsEids' configuration and filtered SDA are updated in bidder configuration. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this detail no longer relevant? I am not seeing any of this stored any longer, so if that is the case, can this be removed |
||
|
||
Sub module object with the following keys: | ||
|
||
| param name | type | Scope | Description | Params | | ||
| :------------ | :------------ | :------ | :------ | :------ | | ||
| filterEIDwhenSDA | function | optional | Filters user EIDs based on SDA | bidrequest | | ||
| filterSDAwhenEID | function | optional | Filters SDA based on configured EIDs | bidrequest | | ||
|
||
# Module Control Configuration | ||
|
||
``` | ||
|
||
pbjs.setConfig({ | ||
dataController: { | ||
filterEIDwhenSDA: ['*'] | ||
filterSDAwhenEID: ['id5-sync.com'] | ||
} | ||
}); | ||
|
||
``` |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
import {expect} from 'chai'; | ||
import {config} from 'src/config.js'; | ||
import {filterBidData, init} from 'modules/dataControllerModule/index.js'; | ||
import {startAuction} from 'src/prebid.js'; | ||
|
||
describe('data controller', function () { | ||
let spyFn; | ||
|
||
beforeEach(function () { | ||
spyFn = sinon.spy(); | ||
}); | ||
|
||
afterEach(function () { | ||
config.resetConfig(); | ||
}); | ||
|
||
describe('data controller', function () { | ||
let result; | ||
let callbackFn; | ||
let req; | ||
|
||
beforeEach(function () { | ||
init(); | ||
result = null; | ||
req = { | ||
'adUnits': [{ | ||
'bids': [ | ||
{ | ||
'bidder': 'ix', | ||
'userIdAsEids': [ | ||
{ | ||
'source': 'id5-sync.com', | ||
'uids': [ | ||
{ | ||
'id': 'ID5*UJzjz7J0FNIWPCp8fAmwGavBhGxnJ06V9umghosEVm4ZPjpn2iWahAoiPal59yKa', | ||
'atype': 1, | ||
'ext': { | ||
'linkType': 2 | ||
} | ||
} | ||
] | ||
} | ||
], | ||
|
||
} | ||
] | ||
}], | ||
'ortb2Fragments': { | ||
'bidder': { | ||
'ix': { | ||
'user': { | ||
'data': [ | ||
{ | ||
'name': 'permutive.com', | ||
'ext': { | ||
'segtax': 4 | ||
}, | ||
'segment': [ | ||
{ | ||
'id': '777777' | ||
}, | ||
{ | ||
'id': '888888' | ||
} | ||
] | ||
} | ||
] | ||
} | ||
} | ||
} | ||
} | ||
}; | ||
callbackFn = function (request) { | ||
result = request; | ||
}; | ||
}); | ||
|
||
afterEach(function () { | ||
config.resetConfig(); | ||
startAuction.getHooks({hook: filterBidData}).remove(); | ||
}); | ||
|
||
it('filterEIDwhenSDA for All SDA ', function () { | ||
let dataControllerConfiguration = { | ||
'dataController': { | ||
filterEIDwhenSDA: ['*'] | ||
} | ||
}; | ||
config.setConfig(dataControllerConfiguration); | ||
filterBidData(callbackFn, req); | ||
expect(req.adUnits[0].bids[0].userIdAsEids).that.is.empty; | ||
}); | ||
|
||
it('filterEIDwhenSDA for available SAD permutive.com:4:777777 ', function () { | ||
let dataControllerConfiguration = { | ||
'dataController': { | ||
filterEIDwhenSDA: ['permutive.com:4:777777'] | ||
} | ||
|
||
}; | ||
config.setConfig(dataControllerConfiguration); | ||
filterBidData(callbackFn, req); | ||
expect(req.adUnits[0].bids[0].userIdAsEids).that.is.empty; | ||
}); | ||
|
||
it('filterEIDwhenSDA for unavailable SAD test.com:4:9999 ', function () { | ||
let dataControllerConfiguration = { | ||
'dataController': { | ||
filterEIDwhenSDA: ['test.com:4:99999'] | ||
} | ||
}; | ||
config.setConfig(dataControllerConfiguration); | ||
filterBidData(callbackFn, req); | ||
expect(req.adUnits[0].bids[0].userIdAsEids).that.is.not.empty; | ||
}); | ||
|
||
it('filterSDAwhenEID for id5-sync.com EID ', function () { | ||
let dataControllerConfiguration = { | ||
'dataController': { | ||
filterSDAwhenEID: ['id5-sync.com'] | ||
} | ||
}; | ||
config.setConfig(dataControllerConfiguration); | ||
filterBidData(callbackFn, req); | ||
expect(req.ortb2Fragments.bidder.ix.user.data).that.is.empty; | ||
}); | ||
|
||
it('filterSDAwhenEID for All EID ', function () { | ||
let dataControllerConfiguration = { | ||
'dataController': { | ||
filterSDAwhenEID: ['*'] | ||
} | ||
}; | ||
config.setConfig(dataControllerConfiguration); | ||
|
||
filterBidData(callbackFn, req); | ||
expect(req.ortb2Fragments.bidder.ix.user.data).that.is.empty; | ||
}); | ||
}); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This could be rewritten as
return _dataControllerConfig.filterSDAwhenEID.some((source) => bidderEids.has(source))
..forEach
always loops over the whole iterable;.some
will stop as soon as the predicate returns true.