forked from prebid/Prebid.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathamxIdSystem.js
153 lines (133 loc) · 3.49 KB
/
amxIdSystem.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
/**
* This module adds AMX to the User ID Module
* The {@link module:modules/userId} is required
*
* @module modules/amxIdSystem
* @requires module:modules/userId
*/
import { uspDataHandler } from '../src/adapterManager.js';
import { ajaxBuilder } from '../src/ajax.js';
import { submodule } from '../src/hook.js';
import { getRefererInfo } from '../src/refererDetection.js';
import { deepAccess, getWindowTop, logError } from '../src/utils.js';
const NAME = 'amxId';
const GVL_ID = 737;
const ID_KEY = NAME;
const version = '1.0';
const SYNC_URL = 'https://id.a-mx.com/sync/';
const AJAX_TIMEOUT = 300;
function validateConfig(config) {
if (config == null || config.storage == null) {
logError(`${NAME}: config.storage is required.`);
return false;
}
if (config.storage.type !== 'html5') {
logError(
`${NAME} only supports storage.type "html5". ${config.storage.type} was provided`
);
return false;
}
if (
typeof config.storage.expires === 'number' &&
config.storage.expires > 30
) {
logError(
`${NAME}: storage.expires must be <= 30. ${config.storage.expires} was provided`
);
return false;
}
return true;
}
function handleSyncResponse(client, response, callback) {
if (response.id != null && response.id.length > 0) {
callback(response.id);
return;
}
if (response.u == null || response.u.length === 0) {
callback(null);
return;
}
client(response.u, {
error(e) {
logError(`${NAME} failed on ${response.u}`, e);
callback(null);
},
success(complete) {
if (complete != null && complete.length > 0) {
const value = JSON.parse(complete);
if (value.id != null) {
callback(value.id);
return;
}
}
logError(`${NAME} invalid value`, complete);
callback(null);
},
});
}
export const amxIdSubmodule = {
/**
* @type {string}
*/
name: NAME,
/**
* @type {string}
*/
version,
/**
* IAB TCF Vendor ID
* @type {string}
*/
gvlid: GVL_ID,
decode: (value) =>
value != null && value.length > 0
? { [ID_KEY]: value }
: undefined,
getId(config, consentData, _extant) {
if (!validateConfig(config)) {
return undefined;
}
const consent = consentData || { gdprApplies: false, consentString: '' };
const client = ajaxBuilder(AJAX_TIMEOUT);
const usp = uspDataHandler.getConsentData();
const ref = getRefererInfo();
const params = {
tagId: deepAccess(config, 'params.tagId', ''),
ref: ref.referer,
u: ref.stack[0] || getWindowTop().location.href,
v: '$prebid.version$',
vg: '$$PREBID_GLOBAL$$',
us_privacy: usp,
gdpr: consent.gdprApplies ? 1 : 0,
gdpr_consent: consent.consentString,
};
const callback = (done) =>
client(
SYNC_URL,
{
error(e) {
logError(`${NAME} failed to load`, e);
done(null);
},
success(responseText) {
if (responseText != null && responseText.length > 0) {
try {
const parsed = JSON.parse(responseText);
handleSyncResponse(client, parsed, done);
return;
} catch (e) {
logError(`${NAME} invalid response`, responseText);
}
}
done(null);
},
},
params,
{
method: 'GET'
}
);
return { callback };
},
};
submodule('userId', amxIdSubmodule);