forked from prebid/Prebid.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimRtdProvider.js
248 lines (224 loc) · 7.13 KB
/
imRtdProvider.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
/**
* The {@link module:modules/realTimeData} module is required
* The module will fetch real-time data from Intimate Merger
* @module modules/imRtdProvider
* @requires module:modules/realTimeData
*/
import {ajax} from '../src/ajax.js';
import {config} from '../src/config.js';
import {getGlobal} from '../src/prebidGlobal.js'
import {getStorageManager} from '../src/storageManager.js';
import {
deepSetValue,
deepAccess,
timestamp,
mergeDeep,
logError,
logInfo,
isFn
} from '../src/utils.js'
import {submodule} from '../src/hook.js';
import {MODULE_TYPE_RTD} from '../src/activities/modules.js';
/**
* @typedef {import('../modules/rtdModule/index.js').RtdSubmodule} RtdSubmodule
*/
export const imUidLocalName = '__im_uid';
export const imVidCookieName = '_im_vid';
export const imRtdLocalName = '__im_sids';
const submoduleName = 'im';
const segmentsMaxAge = 3600000; // 1 hour (30 * 60 * 1000)
const uidMaxAge = 1800000; // 30 minites (30 * 60 * 1000)
const vidMaxAge = 97200000000; // 37 months ((365 * 3 + 30) * 24 * 60 * 60 * 1000)
export const storage = getStorageManager({moduleType: MODULE_TYPE_RTD, moduleName: submoduleName});
function setImDataInCookie(value) {
storage.setCookie(
imVidCookieName,
value,
new Date(timestamp() + vidMaxAge).toUTCString(),
'none'
);
}
/**
* @param {Object} segments
* @param {Object} moduleConfig
*/
function getSegments(segments, moduleConfig) {
if (!segments) return;
const maxSegments = !Number.isNaN(moduleConfig.params.maxSegments) ? moduleConfig.params.maxSegments : 200;
return segments.slice(0, maxSegments);
}
/**
* @param {string} bidderName
*/
export function getBidderFunction(bidderName) {
const biddersFunction = {
pubmatic: function (bid, data, moduleConfig) {
if (data.im_segments && data.im_segments.length) {
const segments = getSegments(data.im_segments, moduleConfig);
const dctr = deepAccess(bid, 'params.dctr');
deepSetValue(
bid,
'params.dctr',
`${dctr ? dctr + '|' : ''}im_segments=${segments.join(',')}`
);
}
return bid
},
fluct: function (bid, data, moduleConfig) {
if (data.im_segments && data.im_segments.length) {
const segments = getSegments(data.im_segments, moduleConfig);
deepSetValue(
bid,
'params.kv.imsids',
segments
);
}
return bid
}
}
return biddersFunction[bidderName] || null;
}
export function getCustomBidderFunction(config, bidder) {
const overwriteFn = deepAccess(config, `params.overwrites.${bidder}`)
if (overwriteFn && isFn(overwriteFn)) {
return overwriteFn
} else {
return null
}
}
/**
* Add real-time data.
* @param {Object} bidConfig
* @param {Object} moduleConfig
* @param {Object} data
*/
export function setRealTimeData(bidConfig, moduleConfig, data) {
const adUnits = bidConfig.adUnits || getGlobal().adUnits;
const utils = {deepSetValue, deepAccess, logInfo, logError, mergeDeep};
if (data.im_segments) {
const segments = getSegments(data.im_segments, moduleConfig);
const ortb2 = bidConfig.ortb2Fragments?.global || {};
deepSetValue(ortb2, 'user.ext.data.im_segments', segments);
if (moduleConfig.params.setGptKeyValues || !moduleConfig.params.hasOwnProperty('setGptKeyValues')) {
window.googletag = window.googletag || {cmd: []};
window.googletag.cmd = window.googletag.cmd || [];
window.googletag.cmd.push(() => {
window.googletag.pubads().setTargeting('im_segments', segments);
});
}
}
adUnits.forEach(adUnit => {
adUnit.bids.forEach(bid => {
const bidderFunction = getBidderFunction(bid.bidder);
const overwriteFunction = getCustomBidderFunction(moduleConfig, bid.bidder);
if (overwriteFunction) {
overwriteFunction(bid, data, utils, config);
} else if (bidderFunction) {
bidderFunction(bid, data, moduleConfig);
}
})
});
}
/**
* Real-time data retrieval from Intimate Merger
* @param {Object} reqBidsConfigObj
* @param {function} onDone
* @param {Object} moduleConfig
*/
export function getRealTimeData(reqBidsConfigObj, onDone, moduleConfig) {
const cid = deepAccess(moduleConfig, 'params.cid');
if (!cid) {
logError('imRtdProvider requires a valid cid to be defined');
onDone();
return;
}
const sids = storage.getDataFromLocalStorage(imRtdLocalName);
const parsedSids = sids ? sids.split(',') : [];
const mt = storage.getDataFromLocalStorage(`${imRtdLocalName}_mt`);
const localVid = storage.getCookie(imVidCookieName);
let apiUrl = `https://sync6.im-apps.net/${cid}/rtd`;
let expired = true;
let alreadyDone = false;
if (localVid) {
apiUrl += `?vid=${localVid}`;
setImDataInCookie(localVid);
}
if (Date.parse(mt) && Date.now() - (new Date(mt)).getTime() < segmentsMaxAge) {
expired = false;
}
if (sids !== null) {
setRealTimeData(reqBidsConfigObj, moduleConfig, {im_segments: parsedSids});
onDone();
alreadyDone = true;
}
if (expired) {
ajax(
apiUrl,
getApiCallback(reqBidsConfigObj, alreadyDone ? undefined : onDone, moduleConfig),
undefined,
{method: 'GET', withCredentials: true}
);
}
}
/**
* Api callback from Intimate Merger
* @param {Object} reqBidsConfigObj
* @param {function} onDone
* @param {Object} moduleConfig
*/
export function getApiCallback(reqBidsConfigObj, onDone, moduleConfig) {
return {
success: function (response, req) {
let parsedResponse = {};
if (req.status === 200) {
try {
parsedResponse = JSON.parse(response);
} catch (e) {
logError('unable to get Intimate Merger segment data');
}
if (parsedResponse.uid) {
const imuid = storage.getDataFromLocalStorage(imUidLocalName);
const imuidMt = storage.getDataFromLocalStorage(`${imUidLocalName}_mt`);
const imuidExpired = Date.parse(imuidMt) && Date.now() - (new Date(imuidMt)).getTime() < uidMaxAge;
if (!imuid || imuidExpired) {
storage.setDataInLocalStorage(imUidLocalName, parsedResponse.uid);
storage.setDataInLocalStorage(`${imUidLocalName}_mt`, new Date(timestamp()).toUTCString());
}
}
if (parsedResponse.vid) {
setImDataInCookie(parsedResponse.vid);
}
if (parsedResponse.segments) {
setRealTimeData(reqBidsConfigObj, moduleConfig, {im_segments: parsedResponse.segments});
storage.setDataInLocalStorage(imRtdLocalName, parsedResponse.segments);
storage.setDataInLocalStorage(`${imRtdLocalName}_mt`, new Date(timestamp()).toUTCString());
}
}
if (onDone) {
onDone();
}
},
error: function () {
if (onDone) {
onDone();
}
logError('unable to get Intimate Merger segment data');
}
}
}
/**
* Module init
* @param {Object} provider
* @param {Object} userConsent
* @return {boolean}
*/
function init(provider, userConsent) {
return true;
}
/** @type {RtdSubmodule} */
export const imRtdSubmodule = {
name: submoduleName,
getBidRequestData: getRealTimeData,
init: init
};
submodule('realTimeData', imRtdSubmodule);