forked from prebid/Prebid.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhadronRtdProvider.js
196 lines (176 loc) · 5.34 KB
/
hadronRtdProvider.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
/**
* This module adds the Audigent Hadron provider to the real time data module
* The {@link module:modules/realTimeData} module is required
* The module will fetch real-time data from Audigent
* @module modules/hadronRtdProvider
* @requires module:modules/realTimeData
*/
import {config} from '../src/config.js';
import {getGlobal} from '../src/prebidGlobal.js';
import {getStorageManager} from '../src/storageManager.js';
import {submodule} from '../src/hook.js';
import {isFn, isStr, isArray, deepEqual, isPlainObject, logInfo} from '../src/utils.js';
import {loadExternalScript} from '../src/adloader.js';
import {MODULE_TYPE_RTD} from '../src/activities/modules.js';
/**
* @typedef {import('../modules/rtdModule/index.js').RtdSubmodule} RtdSubmodule
*/
const LOG_PREFIX = '[HadronRtdProvider] ';
const MODULE_NAME = 'realTimeData';
const SUBMODULE_NAME = 'hadron';
const AU_GVLID = 561;
const HADRON_JS_URL = 'https://cdn.hadronid.net/hadron.js';
const LS_TAM_KEY = 'auHadronId';
const RTD_LOCAL_NAME = 'auHadronRtd';
export const storage = getStorageManager({moduleType: MODULE_TYPE_RTD, moduleName: SUBMODULE_NAME});
/**
* @param {string} url
* @param {string} params
* @returns {string}
*/
const urlAddParams = (url, params) => {
return url + (url.indexOf('?') > -1 ? '&' : '?') + params
};
/**
* Deep object merging with array deduplication.
* @param {Object} target
* @param {Object} sources
*/
function mergeDeep(target, ...sources) {
if (!sources.length) return target;
const source = sources.shift();
if (isPlainObject(target) && isPlainObject(source)) {
for (const key in source) {
if (isPlainObject(source[key])) {
if (!target[key]) Object.assign(target, {[key]: {}});
mergeDeep(target[key], source[key]);
} else if (isArray(source[key])) {
if (!target[key]) {
Object.assign(target, {[key]: source[key]});
} else if (isArray(target[key])) {
source[key].forEach(obj => {
let e = 1;
for (let i = 0; i < target[key].length; i++) {
if (deepEqual(target[key][i], obj)) {
e = 0;
break;
}
}
if (e) {
target[key].push(obj);
}
});
}
} else {
Object.assign(target, {[key]: source[key]});
}
}
}
return mergeDeep(target, ...sources);
}
/**
* Lazy merge objects.
* @param {Object} target
* @param {Object} source
*/
function mergeLazy(target, source) {
if (!isPlainObject(target)) {
target = {};
}
if (!isPlainObject(source)) {
source = {};
}
return mergeDeep(target, source);
}
/**
* Param or default.
* @param {String|Function} param
* @param {String} defaultVal
* @param {Object} arg
*/
function paramOrDefault(param, defaultVal, arg) {
if (isFn(param)) {
return param(arg);
} else if (isStr(param)) {
return param;
}
return defaultVal;
}
/**
* Add real-time data & merge segments.
* @param {Object} bidConfig
* @param {Object} rtd
* @param {Object} rtdConfig
*/
export function addRealTimeData(bidConfig, rtd, rtdConfig) {
if (rtdConfig.params && rtdConfig.params.handleRtd) {
rtdConfig.params.handleRtd(bidConfig, rtd, rtdConfig, config);
} else {
if (isPlainObject(rtd.ortb2)) {
mergeLazy(bidConfig.ortb2Fragments?.global, rtd.ortb2);
}
if (isPlainObject(rtd.ortb2b)) {
mergeLazy(bidConfig.ortb2Fragments?.bidder, Object.fromEntries(Object.entries(rtd.ortb2b).map(([_, cfg]) => [_, cfg.ortb2])));
}
}
}
/**
* Real-time data retrieval from Audigent
* @param {Object} bidConfig
* @param {function} onDone
* @param {Object} rtdConfig
* @param {Object} userConsent
*/
export function getRealTimeData(bidConfig, onDone, rtdConfig, userConsent) {
if (!storage.getDataFromLocalStorage(LS_TAM_KEY)) {
const partnerId = rtdConfig.params.partnerId | 0;
const hadronIdUrl = rtdConfig.params && rtdConfig.params.hadronIdUrl;
const scriptUrl = urlAddParams(
paramOrDefault(hadronIdUrl, HADRON_JS_URL, {}),
`partner_id=${partnerId}&_it=prebid`
);
loadExternalScript(scriptUrl, SUBMODULE_NAME, () => {
logInfo(LOG_PREFIX, 'hadronId JS snippet loaded', scriptUrl);
})
}
if (rtdConfig && isPlainObject(rtdConfig.params) && rtdConfig.params.segmentCache) {
let jsonData = storage.getDataFromLocalStorage(RTD_LOCAL_NAME);
if (jsonData) {
let data = JSON.parse(jsonData);
if (data.rtd) {
addRealTimeData(bidConfig, data.rtd, rtdConfig);
onDone();
return;
}
}
}
const userIds = {};
const allUserIds = getGlobal().getUserIds();
if (allUserIds.hasOwnProperty('hadronId')) {
userIds['hadronId'] = allUserIds.hadronId;
logInfo(LOG_PREFIX, 'hadronId user module found', allUserIds.hadronId);
} else {
let hadronId = storage.getDataFromLocalStorage(LS_TAM_KEY);
if (isStr(hadronId) && hadronId.length > 0) {
userIds['hadronId'] = hadronId;
logInfo(LOG_PREFIX, 'hadronId TAM found', hadronId);
}
}
}
/**
* Module init
* @param {Object} provider
* @param {Object} userConsent
* @return {boolean}
*/
function init(provider, userConsent) {
return true;
}
/** @type {RtdSubmodule} */
export const hadronSubmodule = {
name: SUBMODULE_NAME,
getBidRequestData: getRealTimeData,
init: init,
gvlid: AU_GVLID,
};
submodule(MODULE_NAME, hadronSubmodule);