|
| 1 | +import { submodule } from '../src/hook.js'; |
| 2 | +import { config } from '../src/config.js'; |
| 3 | +import { ajax } from '../src/ajax.js'; |
| 4 | +import { |
| 5 | + logMessage, logError, |
| 6 | + deepAccess, mergeDeep, |
| 7 | + isNumber, isArray, deepSetValue |
| 8 | +} from '../src/utils.js'; |
| 9 | + |
| 10 | +// Constants |
| 11 | +const REAL_TIME_MODULE = 'realTimeData'; |
| 12 | +const MODULE_NAME = '1plusX'; |
| 13 | +const ORTB2_NAME = '1plusX.com' |
| 14 | +const PAPI_VERSION = 'v1.0'; |
| 15 | +const LOG_PREFIX = '[1plusX RTD Module]: '; |
| 16 | +const LEGACY_SITE_KEYWORDS_BIDDERS = ['appnexus']; |
| 17 | +export const segtaxes = { |
| 18 | + // cf. https://github.com/InteractiveAdvertisingBureau/openrtb/pull/108 |
| 19 | + AUDIENCE: 526, |
| 20 | + CONTENT: 527, |
| 21 | +}; |
| 22 | +// Functions |
| 23 | +/** |
| 24 | + * Extracts the parameters for 1plusX RTD module from the config object passed at instanciation |
| 25 | + * @param {Object} moduleConfig Config object passed to the module |
| 26 | + * @param {Object} reqBidsConfigObj Config object for the bidders; each adapter has its own entry |
| 27 | + * @returns {Object} Extracted configuration parameters for the module |
| 28 | + */ |
| 29 | +export const extractConfig = (moduleConfig, reqBidsConfigObj) => { |
| 30 | + // CustomerId |
| 31 | + const customerId = deepAccess(moduleConfig, 'params.customerId'); |
| 32 | + if (!customerId) { |
| 33 | + throw new Error('Missing parameter customerId in moduleConfig'); |
| 34 | + } |
| 35 | + // Timeout |
| 36 | + const tempTimeout = deepAccess(moduleConfig, 'params.timeout'); |
| 37 | + const timeout = isNumber(tempTimeout) && tempTimeout > 300 ? tempTimeout : 1000; |
| 38 | + |
| 39 | + // Bidders |
| 40 | + const biddersTemp = deepAccess(moduleConfig, 'params.bidders'); |
| 41 | + if (!isArray(biddersTemp) || !biddersTemp.length) { |
| 42 | + throw new Error('Missing parameter bidders in moduleConfig'); |
| 43 | + } |
| 44 | + |
| 45 | + const adUnitBidders = reqBidsConfigObj.adUnits |
| 46 | + .flatMap(({ bids }) => bids.map(({ bidder }) => bidder)) |
| 47 | + .filter((e, i, a) => a.indexOf(e) === i); |
| 48 | + if (!isArray(adUnitBidders) || !adUnitBidders.length) { |
| 49 | + throw new Error('Missing parameter bidders in bidRequestConfig'); |
| 50 | + } |
| 51 | + |
| 52 | + const bidders = biddersTemp.filter(bidder => adUnitBidders.includes(bidder)); |
| 53 | + if (!bidders.length) { |
| 54 | + throw new Error('No bidRequestConfig bidder found in moduleConfig bidders'); |
| 55 | + } |
| 56 | + |
| 57 | + return { customerId, timeout, bidders }; |
| 58 | +} |
| 59 | + |
| 60 | +/** |
| 61 | + * Gets the URL of Profile Api from which targeting data will be fetched |
| 62 | + * @param {Object} config |
| 63 | + * @param {string} config.customerId |
| 64 | + * @returns {string} URL to access 1plusX Profile API |
| 65 | + */ |
| 66 | +const getPapiUrl = ({ customerId }) => { |
| 67 | + // https://[yourClientId].profiles.tagger.opecloud.com/[VERSION]/targeting?url= |
| 68 | + const currentUrl = encodeURIComponent(window.location.href); |
| 69 | + const papiUrl = `https://${customerId}.profiles.tagger.opecloud.com/${PAPI_VERSION}/targeting?url=${currentUrl}`; |
| 70 | + return papiUrl; |
| 71 | +} |
| 72 | + |
| 73 | +/** |
| 74 | + * Fetches targeting data. It contains the audience segments & the contextual topics |
| 75 | + * @param {string} papiUrl URL of profile API |
| 76 | + * @returns {Promise} Promise object resolving with data fetched from Profile API |
| 77 | + */ |
| 78 | +const getTargetingDataFromPapi = (papiUrl) => { |
| 79 | + return new Promise((resolve, reject) => { |
| 80 | + const requestOptions = { |
| 81 | + customHeaders: { |
| 82 | + 'Accept': 'application/json' |
| 83 | + } |
| 84 | + } |
| 85 | + const callbacks = { |
| 86 | + success(responseText, response) { |
| 87 | + resolve(JSON.parse(response.response)); |
| 88 | + }, |
| 89 | + error(error) { |
| 90 | + reject(error); |
| 91 | + } |
| 92 | + }; |
| 93 | + ajax(papiUrl, callbacks, null, requestOptions) |
| 94 | + }) |
| 95 | +} |
| 96 | + |
| 97 | +/** |
| 98 | + * Prepares the update for the ORTB2 object |
| 99 | + * @param {Object} targetingData Targeting data fetched from Profile API |
| 100 | + * @param {string[]} segments Represents the audience segments of the user |
| 101 | + * @param {string[]} topics Represents the topics of the page |
| 102 | + * @returns {Object} Object describing the updates to make on bidder configs |
| 103 | + */ |
| 104 | +export const buildOrtb2Updates = ({ segments = [], topics = [] }, bidder) => { |
| 105 | + // Currently appnexus bidAdapter doesn't support topics in `site.content.data.segment` |
| 106 | + // Therefore, writing them in `site.keywords` until it's supported |
| 107 | + // Other bidAdapters do fine with `site.content.data.segment` |
| 108 | + const writeToLegacySiteKeywords = LEGACY_SITE_KEYWORDS_BIDDERS.includes(bidder); |
| 109 | + if (writeToLegacySiteKeywords) { |
| 110 | + const site = { |
| 111 | + keywords: topics.join(',') |
| 112 | + }; |
| 113 | + return { site }; |
| 114 | + } |
| 115 | + |
| 116 | + const userData = { |
| 117 | + name: ORTB2_NAME, |
| 118 | + segment: segments.map((segmentId) => ({ id: segmentId })) |
| 119 | + }; |
| 120 | + const siteContentData = { |
| 121 | + name: ORTB2_NAME, |
| 122 | + segment: topics.map((topicId) => ({ id: topicId })), |
| 123 | + ext: { segtax: segtaxes.CONTENT } |
| 124 | + } |
| 125 | + return { userData, siteContentData }; |
| 126 | +} |
| 127 | + |
| 128 | +/** |
| 129 | + * Merges the targeting data with the existing config for bidder and updates |
| 130 | + * @param {string} bidder Bidder for which to set config |
| 131 | + * @param {Object} ortb2Updates Updates to be applied to bidder config |
| 132 | + * @param {Object} bidderConfigs All current bidder configs |
| 133 | + * @returns {Object} Updated bidder config |
| 134 | + */ |
| 135 | +export const updateBidderConfig = (bidder, ortb2Updates, bidderConfigs) => { |
| 136 | + const { site, siteContentData, userData } = ortb2Updates; |
| 137 | + const bidderConfigCopy = mergeDeep({}, bidderConfigs[bidder]); |
| 138 | + |
| 139 | + if (site) { |
| 140 | + // Legacy : cf. comment on buildOrtb2Updates first lines |
| 141 | + const currentSite = deepAccess(bidderConfigCopy, 'ortb2.site') |
| 142 | + const updatedSite = mergeDeep(currentSite, site); |
| 143 | + deepSetValue(bidderConfigCopy, 'ortb2.site', updatedSite); |
| 144 | + } |
| 145 | + |
| 146 | + if (siteContentData) { |
| 147 | + const siteDataPath = 'ortb2.site.content.data'; |
| 148 | + const currentSiteContentData = deepAccess(bidderConfigCopy, siteDataPath) || []; |
| 149 | + const updatedSiteContentData = [ |
| 150 | + ...currentSiteContentData.filter(({ name }) => name != siteContentData.name), |
| 151 | + siteContentData |
| 152 | + ]; |
| 153 | + deepSetValue(bidderConfigCopy, siteDataPath, updatedSiteContentData); |
| 154 | + } |
| 155 | + |
| 156 | + if (userData) { |
| 157 | + const userDataPath = 'ortb2.user.data'; |
| 158 | + const currentUserData = deepAccess(bidderConfigCopy, userDataPath) || []; |
| 159 | + const updatedUserData = [ |
| 160 | + ...currentUserData.filter(({ name }) => name != userData.name), |
| 161 | + userData |
| 162 | + ]; |
| 163 | + deepSetValue(bidderConfigCopy, userDataPath, updatedUserData); |
| 164 | + } |
| 165 | + |
| 166 | + return bidderConfigCopy; |
| 167 | +}; |
| 168 | + |
| 169 | +const setAppnexusAudiences = (audiences) => { |
| 170 | + config.setConfig({ |
| 171 | + appnexusAuctionKeywords: { |
| 172 | + '1plusX': audiences, |
| 173 | + }, |
| 174 | + }); |
| 175 | +} |
| 176 | + |
| 177 | +/** |
| 178 | + * Updates bidder configs with the targeting data retreived from Profile API |
| 179 | + * @param {Object} papiResponse Response from Profile API |
| 180 | + * @param {Object} config Module configuration |
| 181 | + * @param {string[]} config.bidders Bidders specified in module's configuration |
| 182 | + */ |
| 183 | +export const setTargetingDataToConfig = (papiResponse, { bidders }) => { |
| 184 | + const bidderConfigs = config.getBidderConfig(); |
| 185 | + const { s: segments, t: topics } = papiResponse; |
| 186 | + |
| 187 | + for (const bidder of bidders) { |
| 188 | + const ortb2Updates = buildOrtb2Updates({ segments, topics }, bidder); |
| 189 | + const updatedBidderConfig = updateBidderConfig(bidder, ortb2Updates, bidderConfigs); |
| 190 | + if (updatedBidderConfig) { |
| 191 | + config.setBidderConfig({ |
| 192 | + bidders: [bidder], |
| 193 | + config: updatedBidderConfig |
| 194 | + }); |
| 195 | + } |
| 196 | + if (bidder === 'appnexus') { |
| 197 | + // Do the legacy stuff for appnexus with segments |
| 198 | + setAppnexusAudiences(segments); |
| 199 | + } |
| 200 | + } |
| 201 | +} |
| 202 | + |
| 203 | +// Functions exported in submodule object |
| 204 | +/** |
| 205 | + * Init |
| 206 | + * @param {Object} config Module configuration |
| 207 | + * @param {boolean} userConsent |
| 208 | + * @returns true |
| 209 | + */ |
| 210 | +const init = (config, userConsent) => { |
| 211 | + return true; |
| 212 | +} |
| 213 | + |
| 214 | +/** |
| 215 | + * |
| 216 | + * @param {Object} reqBidsConfigObj Bid request configuration object |
| 217 | + * @param {Function} callback Called on completion |
| 218 | + * @param {Object} moduleConfig Configuration for 1plusX RTD module |
| 219 | + * @param {boolean} userConsent |
| 220 | + */ |
| 221 | +const getBidRequestData = (reqBidsConfigObj, callback, moduleConfig, userConsent) => { |
| 222 | + try { |
| 223 | + // Get the required config |
| 224 | + const { customerId, bidders } = extractConfig(moduleConfig, reqBidsConfigObj); |
| 225 | + // Get PAPI URL |
| 226 | + const papiUrl = getPapiUrl({ customerId }) |
| 227 | + // Call PAPI |
| 228 | + getTargetingDataFromPapi(papiUrl) |
| 229 | + .then((papiResponse) => { |
| 230 | + logMessage(LOG_PREFIX, 'Get targeting data request successful'); |
| 231 | + setTargetingDataToConfig(papiResponse, { bidders }); |
| 232 | + callback(); |
| 233 | + }) |
| 234 | + .catch((error) => { |
| 235 | + throw error; |
| 236 | + }) |
| 237 | + } catch (error) { |
| 238 | + logError(LOG_PREFIX, error); |
| 239 | + callback(); |
| 240 | + } |
| 241 | +} |
| 242 | + |
| 243 | +// The RTD submodule object to be exported |
| 244 | +export const onePlusXSubmodule = { |
| 245 | + name: MODULE_NAME, |
| 246 | + init, |
| 247 | + getBidRequestData |
| 248 | +} |
| 249 | + |
| 250 | +// Register the onePlusXSubmodule as submodule of realTimeData |
| 251 | +submodule(REAL_TIME_MODULE, onePlusXSubmodule); |
0 commit comments