Skip to content

Commit

Permalink
1plus x RTD Adapter: remove bidder specific handling enforcement (DC …
Browse files Browse the repository at this point in the history
…3634) (#10001)

* 1plus x RTD Adapter: remove bidder specific handling enforcement (DC 3634)

* linting

* Requested changes addressed

* Fix test setTargetingDataToConfig

* Requested changes addressed

* fix lint error

* default bidder config to {} before updating it
  • Loading branch information
bwhisp authored Jun 6, 2023
1 parent 4d02dd1 commit 1a0db49
Show file tree
Hide file tree
Showing 2 changed files with 129 additions and 209 deletions.
91 changes: 31 additions & 60 deletions modules/1plusXRtdProvider.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { submodule } from '../src/hook.js';
import { config } from '../src/config.js';
import { ajax } from '../src/ajax.js';
import {
logMessage, logError,
deepAccess, mergeDeep,
isNumber, isArray, deepSetValue
deepAccess, deepSetValue, mergeDeep,
isNumber, isArray,
} from '../src/utils.js';

// Constants
Expand All @@ -14,7 +13,6 @@ const ORTB2_NAME = '1plusX.com'
const PAPI_VERSION = 'v1.0';
const LOG_PREFIX = '[1plusX RTD Module]: ';
const OPE_FPID = 'ope_fpid'
const LEGACY_SITE_KEYWORDS_BIDDERS = ['appnexus'];
export const segtaxes = {
// cf. https://github.com/InteractiveAdvertisingBureau/openrtb/pull/108
AUDIENCE: 526,
Expand Down Expand Up @@ -151,18 +149,7 @@ const getTargetingDataFromPapi = (papiUrl) => {
* @param {string[]} topics Represents the topics of the page
* @returns {Object} Object describing the updates to make on bidder configs
*/
export const buildOrtb2Updates = ({ segments = [], topics = [] }, bidder) => {
// Currently appnexus bidAdapter doesn't support topics in `site.content.data.segment`
// Therefore, writing them in `site.keywords` until it's supported
// Other bidAdapters do fine with `site.content.data.segment`
const writeToLegacySiteKeywords = LEGACY_SITE_KEYWORDS_BIDDERS.includes(bidder);
if (writeToLegacySiteKeywords) {
const site = {
keywords: topics.join(',')
};
return { site };
}

export const buildOrtb2Updates = ({ segments = [], topics = [] }) => {
const userData = {
name: ORTB2_NAME,
segment: segments.map((segmentId) => ({ id: segmentId }))
Expand All @@ -172,81 +159,64 @@ export const buildOrtb2Updates = ({ segments = [], topics = [] }, bidder) => {
segment: topics.map((topicId) => ({ id: topicId })),
ext: { segtax: segtaxes.CONTENT }
}
return { userData, siteContentData };
// Currently appnexus bidAdapter doesn't support topics in `site.content.data.segment`
// Therefore, writing them in `site.keywords` until it's supported
// Other bidAdapters do fine with `site.content.data.segment`
const siteKeywords = topics.map(topic => `1plusX=${topic}`).join(',');

return { userData, siteContentData, siteKeywords };
}

/**
* Merges the targeting data with the existing config for bidder and updates
* @param {string} bidder Bidder for which to set config
* @param {Object} ortb2Updates Updates to be applied to bidder config
* @param {Object} bidderConfigs All current bidder configs
* @returns {Object} Updated bidder config
* @param {Object} biddersOrtb2 All current bidder configs
*/
export const updateBidderConfig = (bidder, ortb2Updates, bidderConfigs) => {
const { site, siteContentData, userData } = ortb2Updates;
const bidderConfigCopy = mergeDeep({}, bidderConfigs[bidder]);
export const updateBidderConfig = (bidder, ortb2Updates, biddersOrtb2) => {
const { siteKeywords, siteContentData, userData } = ortb2Updates;
mergeDeep(biddersOrtb2, { [bidder]: {} });
const bidderConfig = deepAccess(biddersOrtb2, bidder);

if (site) {
// Legacy : cf. comment on buildOrtb2Updates first lines
const currentSite = deepAccess(bidderConfigCopy, 'ortb2.site');
const updatedSite = mergeDeep(currentSite, site);
deepSetValue(bidderConfigCopy, 'ortb2.site', updatedSite);
{
// Legacy : cf. comment on buildOrtb2Updates
const siteKeywordsPath = 'site.keywords';
deepSetValue(bidderConfig, siteKeywordsPath, siteKeywords);
}

if (siteContentData) {
const siteDataPath = 'ortb2.site.content.data';
const currentSiteContentData = deepAccess(bidderConfigCopy, siteDataPath) || [];
{
const siteDataPath = 'site.content.data';
const currentSiteContentData = deepAccess(bidderConfig, siteDataPath) || [];
const updatedSiteContentData = [
...currentSiteContentData.filter(({ name }) => name != siteContentData.name),
siteContentData
];
deepSetValue(bidderConfigCopy, siteDataPath, updatedSiteContentData);
deepSetValue(bidderConfig, siteDataPath, updatedSiteContentData);
}

if (userData) {
const userDataPath = 'ortb2.user.data';
const currentUserData = deepAccess(bidderConfigCopy, userDataPath) || [];
{
const userDataPath = 'user.data';
const currentUserData = deepAccess(bidderConfig, userDataPath) || [];
const updatedUserData = [
...currentUserData.filter(({ name }) => name != userData.name),
userData
];
deepSetValue(bidderConfigCopy, userDataPath, updatedUserData);
deepSetValue(bidderConfig, userDataPath, updatedUserData);
}

return bidderConfigCopy;
};

const setAppnexusAudiences = (audiences) => {
config.setConfig({
appnexusAuctionKeywords: {
'1plusX': audiences,
},
});
}

/**
* Updates bidder configs with the targeting data retreived from Profile API
* @param {Object} papiResponse Response from Profile API
* @param {Object} config Module configuration
* @param {string[]} config.bidders Bidders specified in module's configuration
*/
export const setTargetingDataToConfig = (papiResponse, { bidders }) => {
const bidderConfigs = config.getBidderConfig();
export const setTargetingDataToConfig = (papiResponse, { bidders, biddersOrtb2 }) => {
const { s: segments, t: topics } = papiResponse;

const ortb2Updates = buildOrtb2Updates({ segments, topics });
for (const bidder of bidders) {
const ortb2Updates = buildOrtb2Updates({ segments, topics }, bidder);
const updatedBidderConfig = updateBidderConfig(bidder, ortb2Updates, bidderConfigs);
if (updatedBidderConfig) {
config.setBidderConfig({
bidders: [bidder],
config: updatedBidderConfig
});
}
if (bidder === 'appnexus') {
// Do the legacy stuff for appnexus with segments
setAppnexusAudiences(segments);
}
updateBidderConfig(bidder, ortb2Updates, biddersOrtb2);
}
}

Expand All @@ -272,13 +242,14 @@ const getBidRequestData = (reqBidsConfigObj, callback, moduleConfig, userConsent
try {
// Get the required config
const { customerId, bidders } = extractConfig(moduleConfig, reqBidsConfigObj);
const { ortb2Fragments: { bidder: biddersOrtb2 } } = reqBidsConfigObj;
// Get PAPI URL
const papiUrl = getPapiUrl(customerId, extractConsent(userConsent) || {}, extractFpid())
// Call PAPI
getTargetingDataFromPapi(papiUrl)
.then((papiResponse) => {
logMessage(LOG_PREFIX, 'Get targeting data request successful');
setTargetingDataToConfig(papiResponse, { bidders });
setTargetingDataToConfig(papiResponse, { bidders, biddersOrtb2 });
callback();
})
} catch (error) {
Expand Down
Loading

0 comments on commit 1a0db49

Please sign in to comment.