forked from prebid/Prebid.js
-
Notifications
You must be signed in to change notification settings - Fork 1
/
1plusXRtdProvider.js
280 lines (257 loc) · 9.15 KB
/
1plusXRtdProvider.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
import { submodule } from '../src/hook.js';
import { MODULE_TYPE_RTD } from '../src/activities/modules.js';
import { ajax } from '../src/ajax.js';
import { getStorageManager, STORAGE_TYPE_COOKIES, STORAGE_TYPE_LOCALSTORAGE } from '../src/storageManager.js';
import {
logMessage, logError,
deepAccess, deepSetValue, mergeDeep,
isNumber, isArray,
} from '../src/utils.js';
// Constants
const REAL_TIME_MODULE = 'realTimeData';
const MODULE_NAME = '1plusX';
const ORTB2_NAME = '1plusX.com'
const PAPI_VERSION = 'v1.0';
const LOG_PREFIX = '[1plusX RTD Module]: ';
const OPE_FPID = 'ope_fpid'
export const fpidStorage = getStorageManager({ moduleType: MODULE_TYPE_RTD, moduleName: MODULE_NAME });
export const segtaxes = {
// cf. https://github.com/InteractiveAdvertisingBureau/openrtb/pull/108
AUDIENCE: 526,
CONTENT: 527,
};
// Functions
/**
* Extracts the parameters for 1plusX RTD module from the config object passed at instanciation
* @param {Object} moduleConfig Config object passed to the module
* @param {Object} reqBidsConfigObj Config object for the bidders; each adapter has its own entry
* @returns {Object} Extracted configuration parameters for the module
*/
export const extractConfig = (moduleConfig, reqBidsConfigObj) => {
// CustomerId
const customerId = deepAccess(moduleConfig, 'params.customerId');
if (!customerId) {
throw new Error('Missing parameter customerId in moduleConfig');
}
// Timeout
const tempTimeout = deepAccess(moduleConfig, 'params.timeout');
const timeout = isNumber(tempTimeout) && tempTimeout > 300 ? tempTimeout : 1000;
// Bidders
const biddersTemp = deepAccess(moduleConfig, 'params.bidders');
if (!isArray(biddersTemp) || !biddersTemp.length) {
throw new Error('Missing parameter bidders in moduleConfig');
}
const adUnitBidders = reqBidsConfigObj.adUnits
.flatMap(({ bids }) => bids.map(({ bidder }) => bidder))
.filter((e, i, a) => a.indexOf(e) === i);
if (!isArray(adUnitBidders) || !adUnitBidders.length) {
throw new Error('Missing parameter bidders in bidRequestConfig');
}
const bidders = biddersTemp.filter(bidder => adUnitBidders.includes(bidder));
if (!bidders.length) {
throw new Error('No bidRequestConfig bidder found in moduleConfig bidders');
}
const fpidStorageType = deepAccess(moduleConfig, 'params.fpidStorageType',
STORAGE_TYPE_LOCALSTORAGE)
if (
fpidStorageType !== STORAGE_TYPE_COOKIES &&
fpidStorageType !== STORAGE_TYPE_LOCALSTORAGE
) {
throw new Error(
`fpidStorageType must be ${STORAGE_TYPE_LOCALSTORAGE} or ${STORAGE_TYPE_COOKIES}`
)
}
return { customerId, timeout, bidders, fpidStorageType };
}
/**
* Extracts consent from the prebid consent object and translates it
* into a 1plusX profile api query parameter parameter dict
* @param {object} prebid gdpr object
* @returns dictionary of papi gdpr query parameters
*/
export const extractConsent = ({ gdpr }) => {
if (!gdpr) {
return null
}
const { gdprApplies, consentString } = gdpr
if (!(gdprApplies == '0' || gdprApplies == '1')) {
throw 'TCF Consent: gdprApplies has wrong format'
}
if (consentString && typeof consentString != 'string') {
throw 'TCF Consent: consentString must be string if defined'
}
const result = {
'gdpr_applies': gdprApplies,
'consent_string': consentString
}
return result
}
/**
* Extracts the OPE first party id field
* @param {string} fpidStorageType indicates where fpid should be read from
* @returns fpid string if found, else null
*/
export const extractFpid = (fpidStorageType) => {
try {
switch (fpidStorageType) {
case STORAGE_TYPE_COOKIES: return fpidStorage.getCookie(OPE_FPID)
case STORAGE_TYPE_LOCALSTORAGE: return fpidStorage.getDataFromLocalStorage(OPE_FPID)
default: {
logError(`Got unknown fpidStorageType ${fpidStorageType}. Aborting...`)
return null
}
}
} catch (error) {
return null;
}
}
/**
* Gets the URL of Profile Api from which targeting data will be fetched
* @param {string} config.customerId
* @param {object} consent query params as dict
* @param {string} oneplusx first party id (nullable)
* @returns {string} URL to access 1plusX Profile API
*/
export const getPapiUrl = (customerId, consent, fpid) => {
// https://[yourClientId].profiles.tagger.opecloud.com/[VERSION]/targeting?url=
const currentUrl = encodeURIComponent(window.location.href);
var papiUrl = `https://${customerId}.profiles.tagger.opecloud.com/${PAPI_VERSION}/targeting?url=${currentUrl}`;
if (consent) {
Object.entries(consent).forEach(([key, value]) => {
papiUrl += `&${key}=${value}`
})
}
if (fpid) {
papiUrl += `&fpid=${fpid}`
}
return papiUrl;
}
/**
* Fetches targeting data. It contains the audience segments & the contextual topics
* @param {string} papiUrl URL of profile API
* @returns {Promise} Promise object resolving with data fetched from Profile API
*/
const getTargetingDataFromPapi = (papiUrl) => {
return new Promise((resolve, reject) => {
const requestOptions = {
customHeaders: {
'Accept': 'application/json'
}
}
const callbacks = {
success(responseText, response) {
resolve(JSON.parse(response.response));
},
error(error) {
reject(error);
}
};
ajax(papiUrl, callbacks, null, requestOptions)
})
}
/**
* Prepares the update for the ORTB2 object
* @param {Object} targetingData Targeting data fetched from Profile API
* @param {string[]} segments Represents the audience segments of the user
* @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 = [] }) => {
const userData = {
name: ORTB2_NAME,
segment: segments.map((segmentId) => ({ id: segmentId })),
ext: { segtax: segtaxes.AUDIENCE }
};
const siteContentData = {
name: ORTB2_NAME,
segment: topics.map((topicId) => ({ id: topicId })),
ext: { segtax: segtaxes.CONTENT }
}
return { userData, siteContentData };
}
/**
* 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} biddersOrtb2 All current bidder configs
*/
export const updateBidderConfig = (bidder, ortb2Updates, biddersOrtb2) => {
const { siteContentData, userData } = ortb2Updates;
mergeDeep(biddersOrtb2, { [bidder]: {} });
const bidderConfig = deepAccess(biddersOrtb2, bidder);
{
const siteDataPath = 'site.content.data';
const currentSiteContentData = deepAccess(bidderConfig, siteDataPath) || [];
const updatedSiteContentData = [
...currentSiteContentData.filter(({ name }) => name != siteContentData.name),
siteContentData
];
deepSetValue(bidderConfig, siteDataPath, updatedSiteContentData);
}
{
const userDataPath = 'user.data';
const currentUserData = deepAccess(bidderConfig, userDataPath) || [];
const updatedUserData = [
...currentUserData.filter(({ name }) => name != userData.name),
userData
];
deepSetValue(bidderConfig, userDataPath, updatedUserData);
}
};
/**
* 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, biddersOrtb2 }) => {
const { s: segments, t: topics } = papiResponse;
const ortb2Updates = buildOrtb2Updates({ segments, topics });
for (const bidder of bidders) {
updateBidderConfig(bidder, ortb2Updates, biddersOrtb2);
}
}
// Functions exported in submodule object
/**
* Init
* @param {Object} config Module configuration
* @param {boolean} userConsent
* @returns true
*/
const init = (config, userConsent) => {
return true;
}
/**
*
* @param {Object} reqBidsConfigObj Bid request configuration object
* @param {Function} callback Called on completion
* @param {Object} moduleConfig Configuration for 1plusX RTD module
* @param {Object} userConsent
*/
const getBidRequestData = (reqBidsConfigObj, callback, moduleConfig, userConsent) => {
try {
// Get the required config
const { customerId, bidders, fpidStorageType } = extractConfig(moduleConfig, reqBidsConfigObj);
const { ortb2Fragments: { bidder: biddersOrtb2 } } = reqBidsConfigObj;
// Get PAPI URL
const papiUrl = getPapiUrl(customerId, extractConsent(userConsent) || {}, extractFpid(fpidStorageType))
// Call PAPI
getTargetingDataFromPapi(papiUrl)
.then((papiResponse) => {
logMessage(LOG_PREFIX, 'Get targeting data request successful');
setTargetingDataToConfig(papiResponse, { bidders, biddersOrtb2 });
callback();
})
} catch (error) {
logError(LOG_PREFIX, error);
callback();
}
}
// The RTD submodule object to be exported
export const onePlusXSubmodule = {
name: MODULE_NAME,
init,
getBidRequestData
}
// Register the onePlusXSubmodule as submodule of realTimeData
submodule(REAL_TIME_MODULE, onePlusXSubmodule);