forked from prebid/Prebid.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoneKeyIdSystem.js
111 lines (103 loc) · 3.07 KB
/
oneKeyIdSystem.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
/**
* This module adds Onekey data to the User ID module
* The {@link module:modules/userId} module is required
* @module modules/oneKeyIdSystem
* @requires module:modules/userId
*/
import {submodule} from '../src/hook.js';
import { logError, logMessage } from '../src/utils.js';
/**
* @typedef {import('../modules/userId/index.js').Submodule} Submodule
* @typedef {import('../modules/userId/index.js').SubmoduleConfig} SubmoduleConfig
* @typedef {import('../modules/userId/index.js').ConsentData} ConsentData
* @typedef {import('../modules/userId/index.js').IdResponse} IdResponse
*/
// Pre-init OneKey if it has not load yet.
window.OneKey = window.OneKey || {};
window.OneKey.queue = window.OneKey.queue || [];
const logPrefix = 'OneKey.Id-Module';
/**
* Generate callback that deserializes the result of getIdsAndPreferences.
*/
const onIdsAndPreferencesResult = (callback) => {
return (result) => {
logMessage(logPrefix, `Has got Ids and Prefs with status: `, result);
callback(result.data);
};
};
/**
* Call OneKey once it is loaded for retrieving
* the ids and the preferences.
*/
const getIdsAndPreferences = (callback) => {
logMessage(logPrefix, 'Queue getIdsAndPreferences call');
// Call OneKey in a queue so that we are sure
// it will be called when fully load and configured
// within the page.
window.OneKey.queue.push(() => {
logMessage(logPrefix, 'Get Ids and Prefs');
window.OneKey.getIdsAndPreferences()
.then(onIdsAndPreferencesResult(callback))
.catch(() => {
logError(logPrefix, 'Cannot retrieve the ids and preferences from OneKey.');
callback(undefined);
});
});
};
/** @type {Submodule} */
export const oneKeyIdSubmodule = {
/**
* used to link submodule with config
* @type {string}
*/
name: 'oneKeyData',
/**
* decode the stored data value for passing to bid requests
* @function decode
* @param {(Object|string)} value
* @returns {(Object|undefined)}
*/
decode(data) {
return { oneKeyData: data };
},
/**
* performs action to obtain id and return a value in the callback's response argument
* @function
* @param {SubmoduleConfig} [config]
* @param {ConsentData} [consentData]
* @param {(Object|undefined)} cacheIdObj
* @returns {IdResponse|undefined}
*/
getId(config) {
return {
callback: getIdsAndPreferences
};
},
eids: {
'oneKeyData': {
getValue: function(data) {
if (data && Array.isArray(data.identifiers) && data.identifiers[0]) {
return data.identifiers[0].value;
}
},
source: 'paf',
atype: 1,
getEidExt: function(data) {
if (data && data.preferences) {
return {preferences: data.preferences};
}
},
getUidExt: function(data) {
if (data && Array.isArray(data.identifiers) && data.identifiers[0]) {
const id = data.identifiers[0];
return {
version: id.version,
type: id.type,
source: id.source
};
}
}
}
}
};
submodule('userId', oneKeyIdSubmodule);