forked from prebid/Prebid.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadqueryIdSystem.js
136 lines (119 loc) · 3.34 KB
/
adqueryIdSystem.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
/**
* This module adds Adquery QID to the User ID module
* The {@link module:modules/userId} module is required
* @module modules/adqueryIdSystem
* @requires module:modules/userId
*/
import {ajax} from '../src/ajax.js';
import {getStorageManager} from '../src/storageManager.js';
import {submodule} from '../src/hook.js';
import {isFn, isPlainObject, isStr, logError, logInfo, logMessage} from '../src/utils.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
* @typedef {import('../modules/userId/index.js').IdResponse} IdResponse
*/
const MODULE_NAME = 'qid';
const AU_GVLID = 902;
export const storage = getStorageManager({moduleType: MODULE_TYPE_UID, moduleName: 'qid'});
/**
* Param or default.
* @param {String} param
* @param {String} defaultVal
*/
function paramOrDefault(param, defaultVal, arg) {
if (isFn(param)) {
return param(arg);
} else if (isStr(param)) {
return param;
}
return defaultVal;
}
/** @type {Submodule} */
export const adqueryIdSubmodule = {
/**
* used to link submodule with config
* @type {string}
*/
name: MODULE_NAME,
/**
* IAB TCF Vendor ID
* @type {string}
*/
gvlid: AU_GVLID,
/**
* decode the stored id value for passing to bid requests
* @function
* @param {{value:string}} value
* @returns {{qid:Object}}
*/
decode(value) {
return {qid: value}
},
/**
* performs action to obtain id and return a value in the callback's response argument
* @function
* @param {SubmoduleConfig} [config]
* @returns {IdResponse|undefined}
*/
getId(config) {
logMessage('adqueryIdSubmodule getId');
let qid = storage.getDataFromLocalStorage('qid');
if (qid) {
return {
callback: function (callback) {
callback(qid);
}
}
}
if (!isPlainObject(config.params)) {
config.params = {};
}
const url = paramOrDefault(
config.params.url,
`https://bidder.adquery.io/prebid/qid`,
config.params.urlArg
);
const resp = function (callback) {
let qid = window.qid;
if (!qid) {
const ramdomValues = Array.from(window.crypto.getRandomValues(new Uint32Array(4)));
qid = ramdomValues.map(val => val.toString(36)).join('').substring(0, 20);
logInfo('adqueryIdSubmodule ID QID GENERTAED:', qid);
}
logInfo('adqueryIdSubmodule ID QID:', qid);
const callbacks = {
success: response => {
let responseObj;
if (response) {
try {
responseObj = JSON.parse(response);
} catch (error) {
logError(error);
}
}
if (responseObj.qid) {
let myQid = responseObj.qid;
storage.setDataInLocalStorage('qid', myQid);
return callback(myQid);
}
callback();
},
error: error => {
logError(`${MODULE_NAME}: ID fetch encountered an error`, error);
callback();
}
};
ajax(url + '?qid=' + qid, callbacks, undefined, {method: 'GET'});
};
return {callback: resp};
},
eids: {
'qid': {
source: 'adquery.io',
atype: 1
},
}
};
submodule('userId', adqueryIdSubmodule);