-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
utiqIdSystem.js
138 lines (131 loc) · 3.51 KB
/
utiqIdSystem.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
/**
* This module adds Utiq provided by Utiq SA/NV to the User ID module
* The {@link module:modules/userId} module is required
* @module modules/utiqIdSystem
* @requires module:modules/userId
*/
import { logInfo } from '../src/utils.js';
import { submodule } from '../src/hook.js';
import { getStorageManager } from '../src/storageManager.js';
import { MODULE_TYPE_UID } from '../src/activities/modules.js';
const MODULE_NAME = 'utiqId';
const LOG_PREFIX = 'Utiq module';
export const storage = getStorageManager({
moduleType: MODULE_TYPE_UID,
moduleName: MODULE_NAME,
});
/**
* Get the "atid" from html5 local storage to make it available to the UserId module.
* @param config
* @returns {{utiq: (*|string)}}
*/
function getUtiqFromStorage() {
let utiqPass;
let utiqPassStorage = JSON.parse(
storage.getDataFromLocalStorage('utiqPass')
);
logInfo(
`${LOG_PREFIX}: Local storage utiqPass: ${JSON.stringify(
utiqPassStorage
)}`
);
if (
utiqPassStorage &&
utiqPassStorage.connectId &&
Array.isArray(utiqPassStorage.connectId.idGraph) &&
utiqPassStorage.connectId.idGraph.length > 0
) {
utiqPass = utiqPassStorage.connectId.idGraph[0];
}
logInfo(
`${LOG_PREFIX}: Graph of utiqPass: ${JSON.stringify(
utiqPass
)}`
);
return {
utiq:
utiqPass && utiqPass.atid
? utiqPass.atid
: null,
};
}
/** @type {Submodule} */
export const utiqIdSubmodule = {
/**
* Used to link submodule with config
* @type {string}
*/
name: MODULE_NAME,
/**
* Decodes the stored id value for passing to bid requests.
* @function
* @returns {{utiq: string} | null}
*/
decode(bidId) {
logInfo(`${LOG_PREFIX}: Decoded ID value ${JSON.stringify(bidId)}`);
return bidId.utiq ? bidId : null;
},
/**
* Get the id from helper function and initiate a new user sync.
* @param config
* @returns {{callback: result}|{id: {utiq: string}}}
*/
getId: function (config) {
const data = getUtiqFromStorage();
if (data.utiq) {
logInfo(`${LOG_PREFIX}: Local storage ID value ${JSON.stringify(data)}`);
return { id: { utiq: data.utiq } };
} else {
if (!config) {
config = {};
}
if (!config.params) {
config.params = {};
}
if (
typeof config.params.maxDelayTime === 'undefined' ||
config.params.maxDelayTime === null
) {
config.params.maxDelayTime = 1000;
}
// Current delay and delay step in milliseconds
let currentDelay = 0;
const delayStep = 50;
const result = (callback) => {
const data = getUtiqFromStorage();
if (!data.utiq) {
if (currentDelay > config.params.maxDelayTime) {
logInfo(
`${LOG_PREFIX}: No utiq value set after ${config.params.maxDelayTime} max allowed delay time`
);
callback(null);
} else {
currentDelay += delayStep;
setTimeout(() => {
result(callback);
}, delayStep);
}
} else {
const dataToReturn = { utiq: data.utiq };
logInfo(
`${LOG_PREFIX}: Returning ID value data of ${JSON.stringify(
dataToReturn
)}`
);
callback(dataToReturn);
}
};
return { callback: result };
}
},
eids: {
'utiq': {
source: 'utiq.com',
atype: 1,
getValue: function (data) {
return data;
},
},
}
};
submodule('userId', utiqIdSubmodule);