forked from prebid/Prebid.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnovatiqIdSystem.js
281 lines (230 loc) · 8.3 KB
/
novatiqIdSystem.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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/**
* This module adds novatiqId to the User ID module
* The {@link module:modules/userId} module is required
* @module modules/novatiqIdSystem
* @requires module:modules/userId
*/
import { logInfo, getWindowLocation } from '../src/utils.js';
import { ajax } from '../src/ajax.js';
import { submodule } from '../src/hook.js';
import {getStorageManager} from '../src/storageManager.js';
import {MODULE_TYPE_UID} from '../src/activities/modules.js';
/**
* @typedef {import('../modules/userId/index.js').Submodule} Submodule
* @typedef {import('../modules/userId/index.js').SubmoduleConfig} SubmoduleConfig
*/
const MODULE_NAME = 'novatiq';
/** @type {Submodule} */
export const novatiqIdSubmodule = {
/**
* used to link submodule with config
* @type {string}
*/
name: MODULE_NAME,
/**
* used to specify vendor id
* @type {number}
*/
gvlid: 1119,
/**
* decode the stored id value for passing to bid requests
* @function
* @returns {novatiq: {snowflake: string}}
*/
decode(novatiqId, config) {
let responseObj = {
novatiq: {
snowflake: novatiqId
}
};
if (novatiqId.syncResponse !== undefined) {
responseObj.novatiq.ext = {};
responseObj.novatiq.ext.syncResponse = novatiqId.syncResponse;
}
if (typeof config != 'undefined' && typeof config.params !== 'undefined' && typeof config.params.removeAdditionalInfo !== 'undefined' && config.params.removeAdditionalInfo === true) {
delete responseObj.novatiq.snowflake.syncResponse;
}
return responseObj;
},
/**
* performs action to obtain id and return a value in the callback's response argument
* @function
* @param {SubmoduleConfig} config
* @returns {id: string}
*/
getId(config) {
const configParams = config.params || {};
const urlParams = this.getUrlParams(configParams);
const srcId = this.getSrcId(configParams, urlParams);
const sharedId = this.getSharedId(configParams);
const useCallbacks = this.useCallbacks(configParams);
logInfo('NOVATIQ config params: ' + JSON.stringify(configParams));
logInfo('NOVATIQ Sync request used sourceid param: ' + srcId);
logInfo('NOVATIQ Sync request Shared ID: ' + sharedId);
return this.sendSyncRequest(useCallbacks, sharedId, srcId, urlParams);
},
sendSyncRequest(useCallbacks, sharedId, sspid, urlParams) {
const syncUrl = this.getSyncUrl(sharedId, sspid, urlParams);
const url = syncUrl.url;
const novatiqId = syncUrl.novatiqId;
// for testing
const sharedStatus = (sharedId != undefined && sharedId != false) ? 'Found' : 'Not Found';
if (useCallbacks) {
let res = this.sendAsyncSyncRequest(novatiqId, url); ;
res.sharedStatus = sharedStatus;
return res;
} else {
this.sendSimpleSyncRequest(novatiqId, url);
return { 'id': novatiqId,
'sharedStatus': sharedStatus }
}
},
sendAsyncSyncRequest(novatiqId, url) {
logInfo('NOVATIQ Setting up ASYNC sync request');
const resp = function (callback) {
logInfo('NOVATIQ *** Calling ASYNC sync request');
function onSuccess(response, responseObj) {
let syncrc;
var novatiqIdJson = { syncResponse: 0 };
syncrc = responseObj.status;
logInfo('NOVATIQ Sync Response Code:' + syncrc);
logInfo('NOVATIQ *** ASYNC request returned ' + syncrc);
if (syncrc === 200) {
novatiqIdJson = { 'id': novatiqId, syncResponse: 1 };
} else {
if (syncrc === 204) {
novatiqIdJson = { 'id': novatiqId, syncResponse: 2 };
}
}
callback(novatiqIdJson);
}
ajax(url,
{ success: onSuccess },
undefined, { method: 'GET', withCredentials: false });
}
return {callback: resp};
},
sendSimpleSyncRequest(novatiqId, url) {
logInfo('NOVATIQ Sending SIMPLE sync request');
ajax(url, undefined, undefined, { method: 'GET', withCredentials: false });
logInfo('NOVATIQ snowflake: ' + novatiqId);
},
getNovatiqId(urlParams) {
// standard uuid format
let uuidFormat = [1e7] + -1e3 + -4e3 + -8e3 + -1e11;
if (urlParams.useStandardUuid === false) {
// novatiq standard uuid(like) format
uuidFormat = uuidFormat + 1e3;
}
return (uuidFormat).replace(/[018]/g, c =>
(c ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> c / 4).toString(16)
);
},
getSyncUrl(sharedId, sspid, urlParams) {
let novatiqId = this.getNovatiqId(urlParams);
let url = 'https://spadsync.com/sync?' + urlParams.novatiqId + '=' + novatiqId;
if (urlParams.useSspId) {
url = url + '&sspid=' + sspid;
}
if (urlParams.useSspHost) {
let ssphost = getWindowLocation().hostname;
logInfo('NOVATIQ partner hostname: ' + ssphost);
url = url + '&ssphost=' + ssphost;
}
// append on the shared ID if we have one
if (sharedId != null) {
url = url + '&sharedId=' + sharedId;
}
return {
url: url,
novatiqId: novatiqId
}
},
getUrlParams(configParams) {
let urlParams = {
novatiqId: 'snowflake',
useStandardUuid: false,
useSspId: true,
useSspHost: true
}
if (typeof configParams.urlParams != 'undefined') {
if (configParams.urlParams.novatiqId != undefined) {
urlParams.novatiqId = configParams.urlParams.novatiqId;
}
if (configParams.urlParams.useStandardUuid != undefined) {
urlParams.useStandardUuid = configParams.urlParams.useStandardUuid;
}
if (configParams.urlParams.useSspId != undefined) {
urlParams.useSspId = configParams.urlParams.useSspId;
}
if (configParams.urlParams.useSspHost != undefined) {
urlParams.useSspHost = configParams.urlParams.useSspHost;
}
}
return urlParams;
},
useCallbacks(configParams) {
return typeof configParams.useCallbacks != 'undefined' && configParams.useCallbacks === true;
},
useSharedId(configParams) {
return typeof configParams.useSharedId != 'undefined' && configParams.useSharedId === true;
},
getCookieOrStorageID(configParams) {
let cookieOrStorageID = '_pubcid';
if (typeof configParams.sharedIdName != 'undefined' && configParams.sharedIdName != null && configParams.sharedIdName != '') {
cookieOrStorageID = configParams.sharedIdName;
logInfo('NOVATIQ sharedID name redefined: ' + cookieOrStorageID);
}
return cookieOrStorageID;
},
// return null if we aren't supposed to use one or we are but there isn't one present
getSharedId(configParams) {
let sharedId = null;
if (this.useSharedId(configParams)) {
let cookieOrStorageID = this.getCookieOrStorageID(configParams);
const storage = getStorageManager({moduleType: MODULE_TYPE_UID, moduleName: MODULE_NAME});
// first check local storage
if (storage.hasLocalStorage()) {
sharedId = storage.getDataFromLocalStorage(cookieOrStorageID);
logInfo('NOVATIQ sharedID retrieved from local storage:' + sharedId);
}
// if nothing check the local cookies
if (sharedId == null) {
sharedId = storage.getCookie(cookieOrStorageID);
logInfo('NOVATIQ sharedID retrieved from cookies:' + sharedId);
}
}
logInfo('NOVATIQ sharedID returning: ' + sharedId);
return sharedId;
},
getSrcId(configParams, urlParams) {
if (urlParams.useSspId == false) {
logInfo('NOVATIQ Configured to NOT use sspid');
return '';
}
logInfo('NOVATIQ Configured sourceid param: ' + configParams.sourceid);
let srcId;
if (typeof configParams.sourceid === 'undefined' || configParams.sourceid === null || configParams.sourceid === '') {
srcId = '000';
logInfo('NOVATIQ sourceid param set to value 000 due to undefined parameter or missing value in config section');
} else if (configParams.sourceid.length < 3 || configParams.sourceid.length > 3) {
srcId = '001';
logInfo('NOVATIQ sourceid param set to value 001 due to wrong size in config section 3 chars max e.g. 1ab');
} else {
srcId = configParams.sourceid;
}
return srcId;
},
eids: {
'novatiq': {
getValue: function(data) {
if (data.snowflake.id === undefined) {
return data.snowflake;
}
return data.snowflake.id;
},
source: 'novatiq.com',
},
}
};
submodule('userId', novatiqIdSubmodule);