Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
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);
Copy link
Collaborator

Choose a reason for hiding this comment

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

bidderConfig can be undefined here, if you are the first to set it up for a particular bidder. You want to change this to

const bidderConfig = biddersOrtb2[bidder] = biddersOrtb2[bidder] || {}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Then, right before this line, I'll do a:

  mergeDeep(biddersOrtb2, { [bidder]: {} });

That way I'll be sure to set something in the bidderConfig.

If I did

const bidderConfig = biddersOrtb2[bidder] = biddersOrtb2[bidder] || {}

I would set values to an object I'll lose after exiting the function


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