-
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
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
fcf32fb
Data Controller Module
4a08291
Data Controller Module
5aecd26
Correcting typo
5332b4c
Setting hook based on data controller configuration
9f7622b
Setting hook based on data controller configuration
217c366
Merge branch 'master' into DC
1d49b72
Updating to filter data before startAuction event
b4f7174
Updating to filter data before startAuction event
798b918
Review comment fixes
f5b14be
Review comment fixes
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,195 @@ | ||
/** | ||
* 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, deepSetValue, prefixLog} from '../../src/utils.js'; | ||
import {startAuction} from '../../src/prebid.js'; | ||
|
||
const LOG_PRE_FIX = 'Data_Controller : '; | ||
const ALL = '*'; | ||
const MODULE_NAME = 'dataController'; | ||
const GLOBAL = {}; | ||
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.some(source => { | ||
if (bidderEIDs.has(source)) { | ||
containsEIDs = true; | ||
} | ||
}); | ||
return containsEIDs; | ||
} | ||
|
||
function containsConfiguredSDA(segementMap, bidderCode) { | ||
if (_dataControllerConfig.filterEIDwhenSDA.includes(ALL)) { | ||
return true; | ||
} | ||
return hasValue(segementMap.get(bidderCode)) || hasValue(segementMap.get(GLOBAL)) | ||
} | ||
|
||
function hasValue(bidderSegement) { | ||
let containsSDA = false; | ||
if (bidderSegement == undefined) { | ||
return false; | ||
} | ||
_dataControllerConfig.filterEIDwhenSDA.some(segment => { | ||
if (bidderSegement.has(segment)) { | ||
containsSDA = true; | ||
} | ||
}); | ||
return containsSDA; | ||
} | ||
|
||
function getSegmentConfig(ortb2Fragments) { | ||
let bidderSDAMap = new Map(); | ||
let globalObject = deepAccess(ortb2Fragments, 'global') || {}; | ||
|
||
collectSegments(bidderSDAMap, GLOBAL, globalObject); | ||
if (ortb2Fragments.bidder) { | ||
for (const [key, value] of Object.entries(ortb2Fragments.bidder)) { | ||
collectSegments(bidderSDAMap, key, value); | ||
} | ||
} | ||
return bidderSDAMap; | ||
} | ||
|
||
function collectSegments(bidderSDAMap, key, data) { | ||
let segmentSet = constructSegment(deepAccess(data, 'user.data') || []); | ||
if (segmentSet && segmentSet.size > 0) bidderSDAMap.set(key, segmentSet); | ||
} | ||
|
||
function constructSegment(userData) { | ||
let segmentSet; | ||
if (userData) { | ||
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); | ||
} | ||
} | ||
} | ||
|
||
return segmentSet; | ||
} | ||
|
||
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); | ||
let resetGlobal = false; | ||
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) { | ||
deepSetValue(value, 'user.data', []); | ||
resetGlobal = true; | ||
} | ||
} | ||
if (resetGlobal) { | ||
deepSetValue(ortb2Fragments, 'global.user.data', []) | ||
} | ||
} | ||
|
||
function filterEIDs(adUnits, ortb2Fragments) { | ||
let segementMap = getSegmentConfig(ortb2Fragments); | ||
let globalEidUpdate = false; | ||
adUnits.forEach(adUnit => { | ||
adUnit.bids.forEach(bid => { | ||
let resetEID = containsConfiguredSDA(segementMap, bid.bidder); | ||
if (resetEID) { | ||
globalEidUpdate = true; | ||
bid.userIdAsEids = []; | ||
bid.userId = {}; | ||
if (ortb2Fragments.bidder) { | ||
let bidderFragment = ortb2Fragments.bidder[bid.bidder]; | ||
let userExt = deepAccess(bidderFragment, 'user.ext.eids') || []; | ||
if (userExt) { | ||
deepSetValue(bidderFragment, 'user.ext.eids', []) | ||
} | ||
} | ||
} | ||
}); | ||
}); | ||
|
||
if (globalEidUpdate) { | ||
deepSetValue(ortb2Fragments, 'global.user.ext.eids', []) | ||
} | ||
return adUnits; | ||
} | ||
|
||
export function init() { | ||
const confListener = config.getConfig(MODULE_NAME, dataControllerConfig => { | ||
const dataController = dataControllerConfig && dataControllerConfig.dataController; | ||
if (!dataController) { | ||
_logger.logInfo(`Data Controller is not configured`); | ||
startAuction.getHooks({hook: filterBidData}).remove(); | ||
return; | ||
} | ||
|
||
if (dataController.filterEIDwhenSDA && 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 = dataController; | ||
|
||
getHook('startAuction').before(filterBidData); | ||
}); | ||
} | ||
|
||
init(); | ||
module(MODULE_NAME, init); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# Overview | ||
|
||
``` | ||
Module Name: Data Controller Module | ||
``` | ||
|
||
# Description | ||
|
||
This module will filter EIDs and SDA based on the configurations. | ||
|
||
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'] | ||
} | ||
}); | ||
|
||
``` |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
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 comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed