Skip to content
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 10 commits into from
Jul 25, 2022
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
149 changes: 149 additions & 0 deletions modules/dataControllerModule/index.js
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);
Copy link
Collaborator

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.

Copy link
Contributor Author

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.


function createLogInfo(prefix) {
return function (...strings) {
logInfo(prefix + ' ', ...strings);
}
}

function hasValidConfiguration() {
dataControllerConfig = config.getConfig(MODULE_NAME);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

may I suggest something like setupConfig instead of hasValidConfiguration? to make it clear that the function has side effects.


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() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the better pattern is config.getConfig(MODULE_NAME, (cfg) => {if (isValid(cfg) enable() else disable())}) - it's the approach taken by most (if not all) other modules:

  • it keeps better separation between setup logic and business logic
  • it doesn't use cpu cycles during the auction in the case when the module has not been enabled
  • it needs to worry about fewer edge cases, like what happens if configuration is set during (or after) the auction

getHook('processBidderRequests').before(filterBidData);
}

initHook();
module(MODULE_NAME, initHook);
30 changes: 30 additions & 0 deletions modules/dataControllerModule/index.md
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.
Copy link
Contributor

Choose a reason for hiding this comment

The 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']
}
});

```
Loading