forked from prebid/Prebid.js
-
Notifications
You must be signed in to change notification settings - Fork 1
/
dgkeywordRtdProvider.js
189 lines (178 loc) · 5.7 KB
/
dgkeywordRtdProvider.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
/**
* This module adds dgkeyword provider to the real time data module
* The {@link module:modules/realTimeData} module is required
* The module will get keywords from 1plux profile api.
* This module can work only with AppNexusBidAdapter.
* @module modules/dgkeywordProvider
* @requires module:modules/realTimeData
*/
import { logMessage, deepSetValue, logError, logInfo, isStr, isArray } from '../src/utils.js';
import { ajax } from '../src/ajax.js';
import { submodule } from '../src/hook.js';
import { getGlobal } from '../src/prebidGlobal.js';
/**
* @typedef {import('../modules/rtdModule/index.js').RtdSubmodule} RtdSubmodule
*/
/**
* get keywords from api server. and set keywords.
* @param {Object} reqBidsConfigObj
* @param {function} callback
* @param {Object} moduleConfig
* @param {Object} userConsent
*/
export function getDgKeywordsAndSet(reqBidsConfigObj, callback, moduleConfig, userConsent) {
const PROFILE_TIMEOUT_MS = 1000;
const timeout = (moduleConfig && moduleConfig.params && moduleConfig.params.timeout && Number(moduleConfig.params.timeout) > 0) ? Number(moduleConfig.params.timeout) : PROFILE_TIMEOUT_MS;
const adUnits = reqBidsConfigObj.adUnits || getGlobal().adUnits;
callback = (function(cb) {
let done = false;
return function () {
if (!done) {
done = true;
return cb.apply(this, arguments);
}
}
})(callback);
let isFinish = false;
logMessage('[dgkeyword sub module]', adUnits, timeout);
let setKeywordTargetBidders = getTargetBidderOfDgKeywords(adUnits);
if (setKeywordTargetBidders.length <= 0) {
logMessage('[dgkeyword sub module] no dgkeyword targets.');
callback();
} else {
logMessage('[dgkeyword sub module] dgkeyword targets:', setKeywordTargetBidders);
logMessage('[dgkeyword sub module] get targets from profile api start.');
ajax(getProfileApiUrl(moduleConfig?.params?.url, moduleConfig?.params?.enableReadFpid), {
success: function(response) {
const res = JSON.parse(response);
if (!isFinish) {
logMessage('[dgkeyword sub module] get targets from profile api end.');
if (res) {
let keywords = {};
if (res['s'] != null && res['s'].length > 0) {
keywords['opeaud'] = res['s'];
}
if (res['t'] != null && res['t'].length > 0) {
keywords['opectx'] = res['t'];
}
if (Object.keys(keywords).length > 0) {
const targetBidKeys = {};
for (let bid of setKeywordTargetBidders) {
// set keywords to ortb2Imp
deepSetValue(bid, 'ortb2Imp.ext.data.keywords', convertKeywordsToString(keywords));
if (!targetBidKeys[bid.bidder]) {
targetBidKeys[bid.bidder] = true;
}
}
}
}
isFinish = true;
}
callback();
},
error: function(errorStatus) {
// error occur
logError('[dgkeyword sub module] profile api access error.', errorStatus);
callback();
}
}, null, {
withCredentials: true,
});
setTimeout(function () {
if (!isFinish) {
// profile api timeout
logInfo('[dgkeyword sub module] profile api timeout. [timeout: ' + timeout + 'ms]');
isFinish = true;
}
callback();
}, timeout);
}
}
export function getProfileApiUrl(customeUrl, enableReadFpid) {
const URL = 'https://mediaconsortium.profiles.tagger.opecloud.com/api/v1';
const fpid = (enableReadFpid) ? readFpidFromLocalStrage() : '';
let url = customeUrl || URL;
url = url + '?url=' + encodeURIComponent(window.location.href) + ((fpid) ? `&fpid=${fpid}` : '');
return url;
}
export function readFpidFromLocalStrage() {
try {
// TODO: use storageManager
// eslint-disable-next-line prebid/no-global
const fpid = window.localStorage.getItem('ope_fpid');
if (fpid) {
return fpid;
}
return null;
} catch (error) {
return null;
}
}
/**
* get all bidder which hava {dgkeyword: true} in params
* @param {Object} adUnits
*/
export function getTargetBidderOfDgKeywords(adUnits) {
let setKeywordTargetBidders = [];
for (let adUnit of adUnits) {
for (let bid of adUnit.bids) {
if (bid.params && bid.params['dgkeyword'] === true) {
delete bid.params['dgkeyword'];
setKeywordTargetBidders.push(bid);
}
}
}
return setKeywordTargetBidders;
}
/** @type {RtdSubmodule} */
export const dgkeywordSubmodule = {
/**
* used to link submodule with realTimeData
* @type {string}
*/
name: 'dgkeyword',
/**
* get data and send back to realTimeData module
* @function
* @param {string[]} adUnitsCodes
*/
getBidRequestData: getDgKeywordsAndSet,
init: init,
};
function init(moduleConfig) {
return true;
}
function registerSubModule() {
submodule('realTimeData', dgkeywordSubmodule);
}
// keywords: { 'genre': ['rock', 'pop'], 'pets': ['dog'] } goes to 'genre=rock,genre=pop,pets=dog'
export function convertKeywordsToString(keywords) {
let result = '';
Object.keys(keywords).forEach(key => {
// if 'text' or ''
if (isStr(keywords[key])) {
if (keywords[key] !== '') {
result += `${key}=${keywords[key]},`
} else {
result += `${key},`;
}
} else if (isArray(keywords[key])) {
let isValSet = false
keywords[key].forEach(val => {
if (isStr(val) && val) {
result += `${key}=${val},`
isValSet = true
}
});
if (!isValSet) {
result += `${key},`
}
} else {
result += `${key},`
}
});
// remove last trailing comma
result = result.substring(0, result.length - 1);
return result;
}
registerSubModule();