-
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 5 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,149 @@ | ||
/** | ||
* 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, logInfo} from '../../src/utils.js'; | ||
import {processBidderRequests} from '../../src/adapters/bidderFactory.js'; | ||
|
||
const LOG_PRE_FIX = 'Data_Controller : '; | ||
const ALL = '*'; | ||
const MODULE_NAME = 'dataController'; | ||
let dataControllerConfig; | ||
|
||
const _logInfo = createLogInfo(LOG_PRE_FIX); | ||
|
||
function createLogInfo(prefix) { | ||
return function (...strings) { | ||
logInfo(prefix + ' ', ...strings); | ||
} | ||
} | ||
|
||
function hasValidConfiguration() { | ||
dataControllerConfig = config.getConfig(MODULE_NAME); | ||
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. may I suggest something like |
||
|
||
if (!dataControllerConfig) { | ||
_logInfo(`Data Controller is not configred`); | ||
processBidderRequests.getHooks({hook: filterBidData}).remove(); | ||
return false; | ||
} | ||
|
||
if (dataControllerConfig.filterEIDwhenSDA && dataControllerConfig.filterSDAwhenEID) { | ||
_logInfo(`Data Controller can be configured with either filterEIDwhenSDA or filterSDAwhenEID`); | ||
processBidderRequests.getHooks({hook: filterBidData}).remove(); | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
/** | ||
* BidderRequests hook to intiate module and reset data object | ||
*/ | ||
export function filterBidData(fn, specDetails, bids, bidderRequest, ...args) { | ||
if (hasValidConfiguration()) { | ||
if (dataControllerConfig.filterEIDwhenSDA) { | ||
filterEIDs(bids, bidderRequest.bidderCode); | ||
} | ||
|
||
if (dataControllerConfig.filterSDAwhenEID) { | ||
filterSDA(bids, bidderRequest.bidderCode); | ||
} | ||
} | ||
fn.call(this, specDetails, bids, bidderRequest, ...args); | ||
} | ||
|
||
function filterEIDs(bids, bidderCode) { | ||
let allBidderConfigs = config.getBidderConfig(); | ||
let bidderConfig = allBidderConfigs[bidderCode]; | ||
let resetEID = containsConfiguredSDA(bidderConfig); | ||
if (resetEID) { | ||
bids.forEach(bid => { | ||
bid.userIdAsEids = []; | ||
}) | ||
} | ||
} | ||
|
||
function containsConfiguredEIDS(eidSources) { | ||
if (dataControllerConfig.filterSDAwhenEID.includes(ALL)) { | ||
return true; | ||
} | ||
let containsSource = false; | ||
dataControllerConfig.filterSDAwhenEID.forEach(source => { | ||
if (eidSources.has(source)) { | ||
containsSource = true; | ||
} | ||
}); | ||
return containsSource; | ||
} | ||
|
||
function containsConfiguredSDA(bidderConfig) { | ||
if (dataControllerConfig.filterEIDwhenSDA.includes(ALL)) { | ||
return true; | ||
} | ||
let segementSet = getSegmentConfig(bidderConfig); | ||
|
||
let containsSegment = false; | ||
dataControllerConfig.filterEIDwhenSDA.forEach(segment => { | ||
if (segementSet.has(segment)) { | ||
containsSegment = true; | ||
} | ||
}); | ||
return containsSegment; | ||
} | ||
|
||
function getSegmentConfig(bidderConfig) { | ||
let segementSet = new Set(); | ||
let userData = deepAccess(bidderConfig, 'ortb2.user.data') || []; | ||
if (userData) { | ||
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++) { | ||
segementSet.add(segmentPrefix + segments[j].id); | ||
} | ||
} | ||
} | ||
return segementSet; | ||
} | ||
|
||
function getEIDsSource(requestObject) { | ||
let source = new Set(); | ||
|
||
requestObject.forEach(eids => { | ||
if ('userIdAsEids' in eids) { | ||
eids.userIdAsEids.forEach((value) => { | ||
if ('source' in value) { | ||
source.add(value['source']); | ||
} | ||
}); | ||
} | ||
}); | ||
return source; | ||
} | ||
|
||
function filterSDA(bids, bidderCode) { | ||
let eidSources = getEIDsSource(bids); | ||
|
||
let resetSDA = containsConfiguredEIDS(eidSources); | ||
const allBidderConfigs = config.getBidderConfig(); | ||
if (resetSDA) { | ||
let bidderConfig = allBidderConfigs[bidderCode]; | ||
bidderConfig.ortb2.user.data = []; | ||
config.setBidderConfig(allBidderConfigs, false); | ||
} | ||
} | ||
|
||
export function initHook() { | ||
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 the better pattern is
|
||
getHook('processBidderRequests').before(filterBidData); | ||
} | ||
|
||
initHook(); | ||
module(MODULE_NAME, initHook); |
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'] | ||
} | ||
}); | ||
|
||
``` |
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 is a
prefixLog
utility function that does something similar already.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.
@dgirardi Have updated the module to be called before startAuction and fixed other issues pointed out.