forked from prebid/Prebid.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmygaruIdSystem.js
104 lines (96 loc) · 2.46 KB
/
mygaruIdSystem.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
/**
* This module adds MyGaru Real Time User Sync to the User ID module
* The {@link module:modules/userId} module is required
* @module modules/mygaruIdSystem
* @requires module:modules/userId
*/
import { ajax } from '../src/ajax.js';
import { submodule } from '../src/hook.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
*/
const bidderCode = 'mygaruId';
const syncUrl = 'https://ident.mygaru.com/v2/id';
export function buildUrl(opts) {
const queryPairs = [];
for (let key in opts) {
if (opts[key] !== undefined) {
queryPairs.push(`${key}=${encodeURIComponent(opts[key])}`);
}
}
return `${syncUrl}?${queryPairs.join('&')}`;
}
function requestRemoteIdAsync(url) {
return new Promise((resolve) => {
ajax(
url,
{
success: response => {
try {
const jsonResponse = JSON.parse(response);
const { iuid } = jsonResponse;
resolve(iuid);
} catch (e) {
resolve();
}
},
error: () => {
resolve();
},
},
undefined,
{
method: 'GET',
contentType: 'application/json'
}
);
});
}
/** @type {Submodule} */
export const mygaruIdSubmodule = {
/**
* used to link submodule with config
* @type {string}
*/
name: bidderCode,
/**
* decode the stored id value for passing to bid requests
* @function
* @returns {{id: string} | null}
*/
decode(id) {
return id;
},
/**
* get the MyGaru Id from local storages and initiate a new user sync
* @function
* @param {SubmoduleConfig} [config]
* @param {ConsentData} [consentData]
* @returns {{id: string | undefined}}
*/
getId(config, consentData) {
const gdprApplies = consentData && typeof consentData.gdprApplies === 'boolean' && consentData.gdprApplies ? 1 : 0;
const gdprConsentString = gdprApplies ? consentData.consentString : undefined;
const url = buildUrl({
gdprApplies,
gdprConsentString
});
return {
url,
callback: function (done) {
return requestRemoteIdAsync(url).then((id) => {
done({ mygaruId: id });
})
}
}
},
eids: {
'mygaruId': {
source: 'mygaru.com',
atype: 1
},
}
};
submodule('userId', mygaruIdSubmodule);