forked from prebid/Prebid.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmobianRtdProvider.js
226 lines (196 loc) · 6.25 KB
/
mobianRtdProvider.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
/**
* This module adds the Mobian RTD provider to the real time data module
* The {@link module:modules/realTimeData} module is required
*/
import { submodule } from '../src/hook.js';
import { ajaxBuilder } from '../src/ajax.js';
import { safeJSONParse, logMessage as _logMessage } from '../src/utils.js';
import { setKeyValue } from '../libraries/gptUtils/gptUtils.js';
/**
* @typedef {import('../modules/rtdModule/index.js').RtdSubmodule} RtdSubmodule
*/
/**
* @typedef {Object} MobianConfig
* @property {MobianConfigParams} params
*/
/**
* @typedef {Object} MobianConfigParams
* @property {string} [prefix] - Optional prefix for targeting keys (default: 'mobian')
* @property {boolean} [publisherTargeting] - Optional boolean to enable targeting for publishers (default: false)
* @property {boolean} [advertiserTargeting] - Optional boolean to enable targeting for advertisers (default: false)
*/
/**
* @typedef {Object} MobianContextData
* @property {Object} apValues
* @property {string[]} categories
* @property {string[]} emotions
* @property {string[]} genres
* @property {string} risk
* @property {string} sentiment
* @property {string[]} themes
* @property {string[]} tones
*/
export const MOBIAN_URL = 'https://prebid.outcomes.net/api/prebid/v1/assessment/async';
export const AP_VALUES = 'apValues';
export const CATEGORIES = 'categories';
export const EMOTIONS = 'emotions';
export const GENRES = 'genres';
export const RISK = 'risk';
export const SENTIMENT = 'sentiment';
export const THEMES = 'themes';
export const TONES = 'tones';
export const CONTEXT_KEYS = [
AP_VALUES,
CATEGORIES,
EMOTIONS,
GENRES,
RISK,
SENTIMENT,
THEMES,
TONES
];
const AP_KEYS = ['a0', 'a1', 'p0', 'p1'];
const logMessage = (...args) => {
_logMessage('Mobian', ...args);
};
function makeMemoizedFetch() {
let cachedResponse = null;
return async function () {
if (cachedResponse) {
return Promise.resolve(cachedResponse);
}
try {
const response = await fetchContextData();
cachedResponse = makeDataFromResponse(response);
return cachedResponse;
} catch (error) {
logMessage('error', error);
return Promise.resolve({});
}
}
}
export const getContextData = makeMemoizedFetch();
const entriesToObjectReducer = (acc, [key, value]) => ({ ...acc, [key]: value });
export function makeContextDataToKeyValuesReducer(config) {
const { prefix } = config;
return function contextDataToKeyValuesReducer(keyValues, [key, value]) {
if (key === AP_VALUES) {
AP_KEYS.forEach((apKey) => {
if (!value?.[apKey]?.length) return;
keyValues.push([`${prefix}_ap_${apKey}`, value[apKey].map((v) => String(v))]);
});
}
if (value?.length) {
keyValues.push([`${prefix}_${key}`, value]);
}
return keyValues;
}
}
export async function fetchContextData() {
const pageUrl = encodeURIComponent(window.location.href);
const requestUrl = `${MOBIAN_URL}?url=${pageUrl}`;
const request = ajaxBuilder();
return new Promise((resolve, reject) => {
request(requestUrl, { success: resolve, error: reject });
});
}
export function getConfig(config) {
const [advertiserTargeting, publisherTargeting] = ['advertiserTargeting', 'publisherTargeting'].map((key) => {
const value = config?.params?.[key];
if (!value) {
return [];
} else if (value === true) {
return CONTEXT_KEYS;
} else if (Array.isArray(value) && value.length) {
return value.filter((key) => CONTEXT_KEYS.includes(key));
}
return [];
});
const prefix = config?.params?.prefix || 'mobian';
return { advertiserTargeting, prefix, publisherTargeting };
}
/**
* @param {MobianConfig} config
* @param {MobianContextData} contextData
*/
export function setTargeting(config, contextData) {
logMessage('context', contextData);
const keyValues = Object.entries(contextData)
.filter(([key]) => config.publisherTargeting.includes(key))
.reduce(makeContextDataToKeyValuesReducer(config), [])
keyValues.forEach(([key, value]) => setKeyValue(key, value));
}
/**
* @param {Object|string} contextData
* @returns {MobianContextData}
*/
export function makeDataFromResponse(contextData) {
const data = typeof contextData === 'string' ? safeJSONParse(contextData) : contextData;
const results = data.results;
if (!results) {
return {};
}
return {
[AP_VALUES]: results.ap || {},
[CATEGORIES]: results.mobianContentCategories,
[EMOTIONS]: results.mobianEmotions,
[GENRES]: results.mobianGenres,
[RISK]: results.mobianRisk || 'unknown',
[SENTIMENT]: results.mobianSentiment || 'unknown',
[THEMES]: results.mobianThemes,
[TONES]: results.mobianTones,
};
}
/**
* @param {Object} bidReqConfig
* @param {MobianContextData} contextData
* @param {MobianConfig} config
*/
export function extendBidRequestConfig(bidReqConfig, contextData, config) {
logMessage('extendBidRequestConfig', bidReqConfig, contextData);
const { site: ortb2Site } = bidReqConfig.ortb2Fragments.global;
const keyValues = Object.entries(contextData)
.filter(([key]) => config.advertiserTargeting.includes(key))
.reduce(makeContextDataToKeyValuesReducer(config), [])
.reduce(entriesToObjectReducer, {});
ortb2Site.ext = ortb2Site.ext || {};
ortb2Site.ext.data = {
...(ortb2Site.ext.data || {}),
...keyValues
};
return bidReqConfig;
}
/**
* @param {MobianConfig} rawConfig
* @returns {boolean}
*/
function init(rawConfig) {
logMessage('init', rawConfig);
const config = getConfig(rawConfig);
if (config.publisherTargeting.length) {
getContextData().then((contextData) => setTargeting(config, contextData));
}
return true;
}
function getBidRequestData(bidReqConfig, callback, rawConfig) {
logMessage('getBidRequestData', bidReqConfig);
const config = getConfig(rawConfig);
const { advertiserTargeting } = config;
if (!advertiserTargeting.length) {
callback();
return;
}
getContextData()
.then((contextData) => {
extendBidRequestConfig(bidReqConfig, contextData, config);
})
.catch(() => {})
.finally(() => callback());
}
/** @type {RtdSubmodule} */
export const mobianBrandSafetySubmodule = {
name: 'mobianBrandSafety',
init: init,
getBidRequestData: getBidRequestData
};
submodule('realTimeData', mobianBrandSafetySubmodule);