forked from prebid/Prebid.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblueconicRtdProvider.js
98 lines (87 loc) · 2.45 KB
/
blueconicRtdProvider.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
/**
* This module adds the blueconic provider to the real time data module
* The {@link module:modules/realTimeData} module is required
* The module will fetch real-time data from Blueconic
* @module modules/blueconicRtdProvider
* @requires module:modules/realTimeData
*/
import {getStorageManager} from '../src/storageManager.js';
import {submodule} from '../src/hook.js';
import {mergeDeep, isPlainObject, logMessage, logError} from '../src/utils.js';
import {MODULE_TYPE_RTD} from '../src/activities/modules.js';
/**
* @typedef {import('../modules/rtdModule/index.js').RtdSubmodule} RtdSubmodule
*/
const MODULE_NAME = 'realTimeData';
const SUBMODULE_NAME = 'blueconic';
export const RTD_LOCAL_NAME = 'bcPrebidData';
export const storage = getStorageManager({moduleType: MODULE_TYPE_RTD, moduleName: SUBMODULE_NAME});
/**
* Try parsing stringified array of data.
* @param {String} data
*/
function parseJson(data) {
try {
return JSON.parse(data);
} catch (err) {
logError(`blueconicRtdProvider: failed to parse json:`, data);
return null;
}
}
/**
* Add real-time data & merge segments.
* @param {Object} ortb2
* @param {Object} rtd
*/
export function addRealTimeData(ortb2, rtd) {
if (isPlainObject(rtd.ortb2)) {
mergeDeep(ortb2, rtd.ortb2);
}
}
/**
* Real-time data retrieval from BlueConic
* @param {Object} reqBidsConfigObj
* @param {function} onDone
* @param {Object} rtdConfig
* @param {Object} userConsent
*/
export function getRealTimeData(reqBidsConfigObj, onDone, rtdConfig, userConsent) {
if (rtdConfig && isPlainObject(rtdConfig.params)) {
const jsonData = storage.getDataFromLocalStorage(RTD_LOCAL_NAME);
if (jsonData) {
const parsedData = parseJson(jsonData);
if (!parsedData) {
return;
}
const userData = {name: 'blueconic', ...parsedData}
logMessage('blueconicRtdProvider: userData: ', userData);
const data = {
ortb2: {
user: {
data: [
userData
]
}
}
}
addRealTimeData(reqBidsConfigObj.ortb2Fragments?.global, data);
onDone();
}
}
}
/**
* Module init
* @param {Object} provider
* @param {Object} userConsent
* @return {boolean}
*/
function init(provider, userConsent) {
return true;
}
/** @type {RtdSubmodule} */
export const blueconicSubmodule = {
name: SUBMODULE_NAME,
getBidRequestData: getRealTimeData,
init: init
};
submodule(MODULE_NAME, blueconicSubmodule);