forked from prebid/Prebid.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiasRtdProvider.js
232 lines (212 loc) · 6.06 KB
/
iasRtdProvider.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
import {submodule} from '../src/hook.js';
import * as utils from '../src/utils.js';
import {ajax} from '../src/ajax.js';
import {getGlobal} from '../src/prebidGlobal.js';
import {getAdUnitSizes} from '../libraries/sizeUtils/sizeUtils.js';
import {getGptSlotInfoForAdUnitCode} from '../libraries/gptUtils/gptUtils.js';
/** @type {string} */
const MODULE_NAME = 'realTimeData';
const SUBMODULE_NAME = 'ias';
const IAS_HOST = 'https://pixel.adsafeprotected.com/services/pub';
export let iasTargeting = {};
const BRAND_SAFETY_OBJECT_FIELD_NAME = 'brandSafety';
const FRAUD_FIELD_NAME = 'fr';
const SLOTS_OBJECT_FIELD_NAME = 'slots';
const CUSTOM_FIELD_NAME = 'custom';
const IAS_KW = 'ias-kw';
const IAS_KEY_MAPPINGS = {
adt: 'adt',
alc: 'alc',
dlm: 'dlm',
hat: 'hat',
off: 'off',
vio: 'vio',
drg: 'drg',
'ias-kw': 'ias-kw',
fr: 'fr',
vw: 'vw',
grm: 'grm',
pub: 'pub',
vw05: 'vw05',
vw10: 'vw10',
vw15: 'vw15',
vw30: 'vw30',
vw_vv: 'vw_vv',
grm_vv: 'grm_vv',
pub_vv: 'pub_vv',
id: 'id'
};
/**
* Module init
* @param {Object} config
* @param {Object} userConsent
* @return {boolean}
*/
export function init(config, userConsent) {
const params = config.params;
if (!params || !params.pubId) {
utils.logError('missing pubId param for IAS provider');
return false;
}
if (params.hasOwnProperty('keyMappings')) {
const keyMappings = params.keyMappings;
for (let prop in keyMappings) {
if (IAS_KEY_MAPPINGS.hasOwnProperty(prop)) {
IAS_KEY_MAPPINGS[prop] = keyMappings[prop]
}
}
}
return true;
}
function stringifySlotSizes(sizes) {
let result = '';
if (utils.isArray(sizes)) {
result = sizes.reduce((acc, size) => {
acc.push(size.join('.'));
return acc;
}, []);
result = '[' + result.join(',') + ']';
}
return result;
}
function getAdUnitPath(adSlot, bidRequest, adUnitPath) {
let p = bidRequest.code;
if (!utils.isEmpty(adSlot)) {
p = adSlot.gptSlot;
} else {
if (!utils.isEmpty(adUnitPath) && adUnitPath.hasOwnProperty(bidRequest.code)) {
if (utils.isStr(adUnitPath[bidRequest.code]) && !utils.isEmpty(adUnitPath[bidRequest.code])) {
p = adUnitPath[bidRequest.code];
}
}
}
return p;
}
function stringifySlot(bidRequest, adUnitPath) {
const sizes = getAdUnitSizes(bidRequest);
const id = bidRequest.code;
const ss = stringifySlotSizes(sizes);
const adSlot = getGptSlotInfoForAdUnitCode(bidRequest.code);
const p = getAdUnitPath(adSlot, bidRequest, adUnitPath);
const slot = { id, ss, p };
const keyValues = Object.keys(slot).map(function (key) {
return [key, slot[key]].join(':');
});
return '{' + keyValues.join(',') + '}';
}
function stringifyWindowSize() {
return [window.innerWidth || -1, window.innerHeight || -1].join('.');
}
function stringifyScreenSize() {
return [(window.screen && window.screen.width) || -1, (window.screen && window.screen.height) || -1].join('.');
}
function renameKeyValues(source) {
let result = {};
for (let prop in IAS_KEY_MAPPINGS) {
if (source.hasOwnProperty(prop)) {
result[IAS_KEY_MAPPINGS[prop]] = source[prop];
}
}
return result;
}
function formatTargetingData(adUnit) {
let result = {};
if (iasTargeting[BRAND_SAFETY_OBJECT_FIELD_NAME]) {
utils.mergeDeep(result, iasTargeting[BRAND_SAFETY_OBJECT_FIELD_NAME]);
}
if (iasTargeting[FRAUD_FIELD_NAME]) {
result[FRAUD_FIELD_NAME] = iasTargeting[FRAUD_FIELD_NAME];
}
if (iasTargeting[CUSTOM_FIELD_NAME] && IAS_KW in iasTargeting[CUSTOM_FIELD_NAME]) {
result[IAS_KW] = iasTargeting[CUSTOM_FIELD_NAME][IAS_KW];
}
if (iasTargeting[SLOTS_OBJECT_FIELD_NAME] && adUnit in iasTargeting[SLOTS_OBJECT_FIELD_NAME]) {
utils.mergeDeep(result, iasTargeting[SLOTS_OBJECT_FIELD_NAME][adUnit]);
}
return renameKeyValues(result);
}
function constructQueryString(anId, adUnits, pageUrl, adUnitPath) {
let queries = [];
queries.push(['anId', anId]);
queries = queries.concat(adUnits.reduce(function (acc, request) {
acc.push(['slot', stringifySlot(request, adUnitPath)]);
return acc;
}, []));
queries.push(['wr', stringifyWindowSize()]);
queries.push(['sr', stringifyScreenSize()]);
queries.push(['url', encodeURIComponent(pageUrl)]);
return encodeURI(queries.map(qs => qs.join('=')).join('&'));
}
function parseResponse(result) {
let iasResponse = {};
try {
iasResponse = JSON.parse(result);
} catch (err) {
utils.logError('error', err);
}
iasTargeting = iasResponse;
}
function getTargetingData(adUnits, config, userConsent) {
const targeting = {};
try {
if (!utils.isEmpty(iasTargeting)) {
adUnits.forEach(function (adUnit) {
targeting[adUnit] = formatTargetingData(adUnit);
});
}
} catch (err) {
utils.logError('error', err);
}
utils.logInfo('IAS targeting', targeting);
return targeting;
}
function isValidHttpUrl(string) {
let url;
try {
url = new URL(string);
} catch (_) {
return false;
}
return url.protocol === 'http:' || url.protocol === 'https:';
}
export function getApiCallback() {
return {
success: function (response, req) {
if (req.status === 200) {
try {
parseResponse(response);
} catch (e) {
utils.logError('Unable to parse IAS response.', e);
}
}
},
error: function () {
utils.logError('failed to retrieve IAS data');
}
}
}
function getBidRequestData(reqBidsConfigObj, callback, config, userConsent) {
const adUnits = reqBidsConfigObj.adUnits || getGlobal().adUnits;
const { pubId } = config.params;
let { pageUrl } = config.params;
const { adUnitPath } = config.params;
if (!isValidHttpUrl(pageUrl)) {
pageUrl = document.location.href;
}
const queryString = constructQueryString(pubId, adUnits, pageUrl, adUnitPath);
ajax(
`${IAS_HOST}?${queryString}`,
getApiCallback(),
undefined,
{ method: 'GET' }
);
callback()
}
/** @type {RtdSubmodule} */
export const iasSubModule = {
name: SUBMODULE_NAME,
init: init,
getTargetingData: getTargetingData,
getBidRequestData: getBidRequestData
};
submodule(MODULE_NAME, iasSubModule);