forked from prebid/Prebid.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmobianRtdProvider.js
203 lines (171 loc) · 5.39 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
/**
* 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 CONTEXT_KEYS = [
'apValues',
'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();
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 {MobianConfigParams} parsedConfig
* @param {MobianContextData} contextData
* @returns {function}
*/
export function setTargeting(parsedConfig, contextData) {
const { publisherTargeting, prefix } = parsedConfig;
logMessage('context', contextData);
CONTEXT_KEYS.forEach((key) => {
if (!publisherTargeting.includes(key)) return;
if (key === 'apValues') {
AP_KEYS.forEach((apKey) => {
if (!contextData[key]?.[apKey]?.length) return;
logMessage(`${prefix}_ap_${apKey}`, contextData[key][apKey]);
setKeyValue(`${prefix}_ap_${apKey}`, contextData[key][apKey]);
});
return;
}
if (contextData[key]?.length) {
logMessage(`${prefix}_${key}`, contextData[key]);
setKeyValue(`${prefix}_${key}`, contextData[key]);
}
});
}
export function makeDataFromResponse(contextData) {
const data = typeof contextData === 'string' ? safeJSONParse(contextData) : contextData;
const results = data.results;
if (!results) {
return {};
}
return {
apValues: 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,
};
}
export function extendBidRequestConfig(bidReqConfig, contextData) {
logMessage('extendBidRequestConfig', bidReqConfig, contextData);
const { site: ortb2Site } = bidReqConfig.ortb2Fragments.global;
ortb2Site.ext = ortb2Site.ext || {};
ortb2Site.ext.data = {
...(ortb2Site.ext.data || {}),
...contextData
};
return bidReqConfig;
}
/**
* @param {MobianConfig} config
* @returns {boolean}
*/
function init(config) {
logMessage('init', config);
const parsedConfig = getConfig(config);
if (parsedConfig.publisherTargeting.length) {
getContextData().then((contextData) => setTargeting(parsedConfig, contextData));
}
return true;
}
function getBidRequestData(bidReqConfig, callback, config) {
logMessage('getBidRequestData', bidReqConfig);
const { advertiserTargeting } = getConfig(config);
if (!advertiserTargeting.length) {
callback();
return;
}
getContextData()
.then((contextData) => {
extendBidRequestConfig(bidReqConfig, contextData);
})
.catch(() => {})
.finally(() => callback());
}
/** @type {RtdSubmodule} */
export const mobianBrandSafetySubmodule = {
name: 'mobianBrandSafety',
init: init,
getBidRequestData: getBidRequestData
};
submodule('realTimeData', mobianBrandSafetySubmodule);