@blotoutio/edgetag-sdk-js 0.5.2 → 0.6.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.cjs CHANGED
@@ -2,6 +2,8 @@
2
2
 
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
+ var uuid = require('uuid');
6
+
5
7
  /******************************************************************************
6
8
  Copyright (c) Microsoft Corporation.
7
9
 
@@ -33,521 +35,545 @@ const providersKey = 'providers';
33
35
  const keyPrefix = `_worker`;
34
36
 
35
37
  const initKey = `${keyPrefix}Store`;
38
+ const getCookieValue = (key) => {
39
+ if (!document || !document.cookie) {
40
+ return '';
41
+ }
42
+ const name = `${key}=`;
43
+ const decodedCookie = decodeURIComponent(document.cookie);
44
+ const ca = decodedCookie.split(';');
45
+ for (let i = 0; i < ca.length; i++) {
46
+ let c = ca[i];
47
+ while (c.charAt(0) === ' ') {
48
+ c = c.substring(1);
49
+ }
50
+ if (c.indexOf(name) === 0) {
51
+ return c.substring(name.length, c.length);
52
+ }
53
+ }
54
+ return '';
55
+ };
36
56
  const saveDataPerKey = (persistType, provider, value, key) => {
37
- const storage = getData$1(persistType);
38
-
39
- if (!storage['data']) {
40
- storage['data'] = {};
41
- }
42
-
43
- if (!storage['data'][provider]) {
44
- storage['data'][provider] = {};
45
- }
46
-
47
- storage['data'][provider][key] = value;
48
- saveData(persistType, storage);
57
+ const storage = getData$1(persistType);
58
+ if (!storage['data']) {
59
+ storage['data'] = {};
60
+ }
61
+ if (!storage['data'][provider]) {
62
+ storage['data'][provider] = {};
63
+ }
64
+ storage['data'][provider][key] = value;
65
+ saveData(persistType, storage);
49
66
  };
50
67
  const savePerKey = (persistType, provider, value, key) => {
51
- const storage = getData$1(persistType);
52
-
53
- if (!storage[provider]) {
54
- storage[provider] = {};
55
- }
56
-
57
- storage[provider][key] = value;
58
- saveData(persistType, storage);
68
+ const storage = getData$1(persistType);
69
+ if (!storage[provider]) {
70
+ storage[provider] = {};
71
+ }
72
+ storage[provider][key] = value;
73
+ saveData(persistType, storage);
59
74
  };
60
75
  const getDataPerKey = (persistType, provider, key) => {
61
- const storage = getData$1(persistType);
62
-
63
- if (!storage[provider]) {
64
- return undefined;
65
- }
66
-
67
- return storage[provider][key];
76
+ const storage = getData$1(persistType);
77
+ if (!storage[provider]) {
78
+ return undefined;
79
+ }
80
+ return storage[provider][key];
68
81
  };
69
82
  const saveData = (persistType, value, key = initKey) => {
70
- if (persistType === 'session') {
71
- saveSession(value, key);
72
- return;
73
- }
74
-
75
- saveLocal(value, key);
83
+ if (persistType === 'session') {
84
+ saveSession(value, key);
85
+ return;
86
+ }
87
+ saveLocal(value, key);
76
88
  };
77
89
  const getData$1 = (persistType, key = initKey) => {
78
- if (persistType === 'session') {
79
- return getSession(key);
80
- }
81
-
82
- return getLocal(key);
90
+ if (persistType === 'session') {
91
+ return getSession(key);
92
+ }
93
+ return getLocal(key);
83
94
  };
84
- const saveKV = data => {
85
- let currentSession = getData$1('session');
86
-
87
- if (!currentSession) {
88
- currentSession = {};
89
- }
90
-
91
- if (!currentSession['kv']) {
92
- currentSession['kv'] = {};
93
- }
94
-
95
- currentSession['kv'] = Object.assign(Object.assign({}, currentSession['kv']), data);
96
- saveData('session', currentSession);
95
+ const saveKV = (data) => {
96
+ let currentSession = getData$1('session');
97
+ if (!currentSession) {
98
+ currentSession = {};
99
+ }
100
+ if (!currentSession['kv']) {
101
+ currentSession['kv'] = {};
102
+ }
103
+ currentSession['kv'] = Object.assign(Object.assign({}, currentSession['kv']), data);
104
+ saveData('session', currentSession);
97
105
  };
98
-
99
106
  const saveLocal = (value, key) => {
100
- localStorage.setItem(key, JSON.stringify(value));
107
+ localStorage.setItem(key, JSON.stringify(value));
101
108
  };
102
-
103
- const getLocal = key => {
104
- const data = localStorage.getItem(key);
105
-
106
- if (!data) {
107
- return {};
108
- }
109
-
110
- return JSON.parse(data) || {};
109
+ const getLocal = (key) => {
110
+ const data = localStorage.getItem(key);
111
+ if (!data) {
112
+ return {};
113
+ }
114
+ return JSON.parse(data) || {};
111
115
  };
112
-
113
116
  const saveSession = (value, key) => {
114
- sessionStorage.setItem(key, JSON.stringify(value));
117
+ sessionStorage.setItem(key, JSON.stringify(value));
115
118
  };
116
-
117
- const getSession = key => {
118
- const data = sessionStorage.getItem(key);
119
-
120
- if (!data) {
121
- return {};
122
- }
123
-
124
- return JSON.parse(data) || {};
119
+ const getSession = (key) => {
120
+ const data = sessionStorage.getItem(key);
121
+ if (!data) {
122
+ return {};
123
+ }
124
+ return JSON.parse(data) || {};
125
125
  };
126
126
 
127
127
  let endpointUrl = '';
128
-
129
- const generateUrl = path => {
130
- const endpoint = getUrl();
131
-
132
- if (!endpoint) {
133
- console.log('URL is not valid');
134
- return '';
135
- }
136
-
137
- return `${endpoint}${path}`;
128
+ const generateUrl = (path) => {
129
+ const endpoint = getUrl();
130
+ if (!endpoint) {
131
+ console.log('URL is not valid');
132
+ return '';
133
+ }
134
+ return `${endpoint}${path}`;
138
135
  };
139
-
140
136
  const getUrl = () => {
141
- return endpointUrl;
137
+ return endpointUrl;
142
138
  };
143
- const setUrl = url => {
144
- if (url == null) {
145
- return;
146
- }
147
-
148
- endpointUrl = url;
139
+ const setUrl = (url) => {
140
+ if (url == null) {
141
+ return;
142
+ }
143
+ endpointUrl = url;
149
144
  };
150
145
  const getTagURL = () => {
151
- return generateUrl('/tag');
146
+ return generateUrl('/tag');
152
147
  };
153
148
  const getInitURL = () => {
154
- return generateUrl('/init');
149
+ return generateUrl('/init');
155
150
  };
156
151
  const getConsentURL = () => {
157
- return generateUrl('/consent');
152
+ return generateUrl('/consent');
158
153
  };
159
154
  const getUserURL = () => {
160
- return generateUrl('/user');
155
+ return generateUrl('/user');
161
156
  };
162
157
  const getDataURL = () => {
163
- return generateUrl(`/data`);
158
+ return generateUrl(`/data`);
164
159
  };
165
- const getGetDataURL = keys => {
166
- return generateUrl(`/data?keys=${encodeURIComponent(keys.join(','))}`);
160
+ const getGetDataURL = (keys) => {
161
+ return generateUrl(`/data?keys=${encodeURIComponent(keys.join(','))}`);
167
162
  };
168
163
  const getKeysURL = () => {
169
- return generateUrl(`/keys`);
164
+ return generateUrl(`/keys`);
170
165
  };
171
166
 
172
167
  let consentDisabled = false;
173
- const setPreferences = preferences => {
174
- if (!preferences) {
175
- return false;
176
- }
177
-
178
- if (!preferences.edgeURL) {
179
- console.error('Please provide URL for EdgeTag');
180
- return false;
181
- }
182
-
183
- consentDisabled = !!preferences.disableConsentCheck;
184
- setUrl(preferences.edgeURL);
185
- return true;
168
+ let providers = [];
169
+ const setPreferences = (preferences) => {
170
+ if (!preferences) {
171
+ return false;
172
+ }
173
+ if (!preferences.edgeURL) {
174
+ console.error('Please provide URL for EdgeTag');
175
+ return false;
176
+ }
177
+ consentDisabled = !!preferences.disableConsentCheck;
178
+ providers = preferences.providers || [];
179
+ if (window && window.edgetagPackages) {
180
+ providers = [...window.edgetagPackages, ...providers];
181
+ }
182
+ setUrl(preferences.edgeURL);
183
+ return true;
186
184
  };
187
185
  const isConsentDisabled = () => consentDisabled;
186
+ const getProvidersPackage = () => providers;
188
187
 
189
188
  const getUserAgent = () => {
190
- const nav = navigator;
191
- let ua = nav.userAgent;
192
- ua += nav.brave ? `${ua} Brave` : '';
193
- return ua;
194
- };
195
- const allowTag = providers => {
196
- const consent = getDataPerKey('local', tagStorage, consentKey);
197
-
198
- if (isConsentDisabled()) {
199
- return true;
200
- }
201
-
202
- if (!consent) {
189
+ const nav = navigator;
190
+ let ua = nav.userAgent;
191
+ ua += nav.brave ? `${ua} Brave` : '';
192
+ return ua;
193
+ };
194
+ const allowTag = (providers) => {
195
+ const consent = getDataPerKey('local', tagStorage, consentKey);
196
+ if (isConsentDisabled()) {
197
+ return true;
198
+ }
199
+ if (!consent) {
200
+ return false;
201
+ }
202
+ if (consent['all']) {
203
+ return true;
204
+ }
205
+ if (!providers) {
206
+ return (Object.values(consent).find((isAllowed) => isAllowed) || false);
207
+ }
208
+ const allProviders = (getDataPerKey('local', tagStorage, providersKey) ||
209
+ []);
210
+ for (const provider of allProviders) {
211
+ const tagProviderSetting = providers[provider];
212
+ if (tagProviderSetting ||
213
+ (providers['all'] === true && tagProviderSetting === undefined) ||
214
+ (providers['all'] === false && tagProviderSetting === true)) {
215
+ if (consent[provider]) {
216
+ return true;
217
+ }
218
+ }
219
+ }
203
220
  return false;
204
- }
205
-
206
- if (consent['all']) {
207
- return true;
208
- }
209
-
210
- if (!providers) {
211
- return Object.values(consent).find(isAllowed => isAllowed) || false;
212
- }
213
-
214
- const allProviders = getDataPerKey('local', tagStorage, providersKey) || [];
215
-
216
- for (const provider of allProviders) {
217
- const tagProviderSetting = providers[provider];
218
-
219
- if (tagProviderSetting || providers['all'] === true && tagProviderSetting === undefined || providers['all'] === false && tagProviderSetting === true) {
220
- if (consent[provider]) {
221
+ };
222
+ const allowProvider = (providers, providerId) => {
223
+ const consent = getDataPerKey('local', tagStorage, consentKey);
224
+ if (isConsentDisabled()) {
221
225
  return true;
222
- }
223
226
  }
224
- }
225
-
226
- return false;
227
+ if (!consent) {
228
+ return false;
229
+ }
230
+ if (consent['all']) {
231
+ return true;
232
+ }
233
+ if (!providers) {
234
+ return consent[providerId];
235
+ }
236
+ const tagProvider = providers[providerId];
237
+ if (tagProvider ||
238
+ (providers['all'] === true && tagProvider === undefined) ||
239
+ (providers['all'] === false && tagProvider === true)) {
240
+ if (consent[providerId]) {
241
+ return true;
242
+ }
243
+ }
244
+ return false;
227
245
  };
228
246
 
229
247
  const beacon = (url, payload) => {
230
- let blob;
231
-
232
- if (payload) {
233
- blob = new Blob([JSON.stringify(payload)], {
234
- type: 'application/json'
235
- });
236
- }
237
-
238
- return navigator.sendBeacon(url, blob);
248
+ let blob;
249
+ if (payload) {
250
+ blob = new Blob([JSON.stringify(payload)], { type: 'application/json' });
251
+ }
252
+ return navigator.sendBeacon(url, blob);
239
253
  };
240
-
241
254
  const ajax = (method, url, payload) => __awaiter(void 0, void 0, void 0, function* () {
242
- return yield fetch(url, {
243
- method,
244
- headers: {
245
- 'Content-type': 'application/json; charset=utf-8',
246
- Accept: 'application/json; charset=utf-8'
247
- },
248
- body: JSON.stringify(payload),
249
- credentials: 'include'
250
- }).then(response => response.json().then(data => ({
251
- status: response.status,
252
- body: data
253
- }))).then(data => {
254
- if (data.status < 200 || data.status >= 300) {
255
- // Q: do we need to retry?
256
- return Promise.reject(new Error(JSON.stringify(data.body)));
257
- }
258
-
259
- return Promise.resolve(data.body);
260
- }).catch(error => {
261
- // Q: do we need to retry?
262
- return Promise.reject(new Error(error));
263
- });
255
+ return yield fetch(url, {
256
+ method,
257
+ headers: {
258
+ 'Content-type': 'application/json; charset=utf-8',
259
+ Accept: 'application/json; charset=utf-8',
260
+ },
261
+ body: JSON.stringify(payload),
262
+ credentials: 'include',
263
+ })
264
+ .then((response) => response.json().then((data) => ({ status: response.status, body: data })))
265
+ .then((data) => {
266
+ if (data.status < 200 || data.status >= 300) {
267
+ // Q: do we need to retry?
268
+ return Promise.reject(new Error(JSON.stringify(data.body)));
269
+ }
270
+ return Promise.resolve(data.body);
271
+ })
272
+ .catch((error) => {
273
+ // Q: do we need to retry?
274
+ return Promise.reject(new Error(error));
275
+ });
264
276
  });
265
-
266
- const getStandardPayload = payload => {
267
- const data = Object.assign({
268
- pageUrl: window.location.href,
269
- userAgent: getUserAgent()
270
- }, payload || {});
271
- let storage = {};
272
- const session = getData$1('session');
273
-
274
- if (session) {
275
- storage = Object.assign(Object.assign({}, storage), session);
276
- }
277
-
278
- const local = getData$1('local');
279
-
280
- if (local) {
281
- storage = Object.assign(Object.assign({}, storage), local);
282
- }
283
-
284
- data.storage = storage;
285
- return data;
286
- };
287
-
288
- function postRequest(url, data, options) {
289
- return __awaiter(this, void 0, void 0, function* () {
290
- if (!url) {
291
- return Promise.reject(new Error('URL is empty'));
277
+ const getStandardPayload = (payload) => {
278
+ const data = Object.assign({ pageUrl: window.location.href, userAgent: getUserAgent() }, (payload || {}));
279
+ let storage = {};
280
+ const session = getData$1('session');
281
+ if (session) {
282
+ storage = Object.assign(Object.assign({}, storage), session);
292
283
  }
293
-
294
- const payload = getStandardPayload(data);
295
-
296
- if (options && options.method === 'beacon') {
297
- return Promise.resolve(beacon(url, payload));
284
+ const local = getData$1('local');
285
+ if (local) {
286
+ storage = Object.assign(Object.assign({}, storage), local);
298
287
  }
299
-
300
- return yield ajax('POST', url, payload);
301
- });
288
+ data.storage = storage;
289
+ return data;
290
+ };
291
+ function postRequest(url, data, options) {
292
+ return __awaiter(this, void 0, void 0, function* () {
293
+ if (!url) {
294
+ return Promise.reject(new Error('URL is empty'));
295
+ }
296
+ const payload = getStandardPayload(data);
297
+ if (options && options.method === 'beacon') {
298
+ return Promise.resolve(beacon(url, payload));
299
+ }
300
+ return yield ajax('POST', url, payload);
301
+ });
302
302
  }
303
303
  function getRequest(url, options) {
304
- return __awaiter(this, void 0, void 0, function* () {
305
- if (!url) {
306
- return Promise.reject(new Error('URL is empty'));
307
- }
308
-
309
- if (options && options.method === 'beacon') {
310
- return {
311
- result: Promise.resolve(beacon(url))
312
- };
313
- }
314
-
315
- return yield ajax('GET', url);
316
- });
304
+ return __awaiter(this, void 0, void 0, function* () {
305
+ if (!url) {
306
+ return Promise.reject(new Error('URL is empty'));
307
+ }
308
+ if (options && options.method === 'beacon') {
309
+ return {
310
+ result: Promise.resolve(beacon(url)),
311
+ };
312
+ }
313
+ return yield ajax('GET', url);
314
+ });
317
315
  }
318
316
 
319
- const info = data => {
317
+ const info = (data) => {
320
318
  };
321
319
 
322
- const saveConsent = consent => {
323
- savePerKey('local', tagStorage, consent, consentKey);
320
+ const saveConsent = (consent) => {
321
+ savePerKey('local', tagStorage, consent, consentKey);
324
322
  };
325
- const handleConsent = consent => {
326
- const payload = {
327
- consentString: consent
328
- };
329
- saveConsent(consent);
330
- postRequest(getConsentURL(), payload).catch(info);
323
+ const handleConsent = (consent) => {
324
+ const payload = {
325
+ consentString: consent,
326
+ };
327
+ saveConsent(consent);
328
+ postRequest(getConsentURL(), payload).catch(info);
331
329
  };
332
330
 
333
- const handleTag = (name, data, providers, options) => {
334
- const payload = {
335
- eventName: name
336
- };
337
-
338
- if (data) {
339
- payload.data = data;
340
- }
341
-
342
- if (providers) {
343
- payload.providers = providers;
344
- }
345
-
346
- if (!allowTag(providers)) {
347
- console.log('No consent');
348
- return;
349
- }
350
-
351
- postRequest(getTagURL(), payload, options).catch(info);
331
+ const generateEventId = (name) => {
332
+ let time = Date.now().toString();
333
+ if (typeof performance !== 'undefined' &&
334
+ typeof performance.now === 'function') {
335
+ const perf = performance.now();
336
+ if (perf) {
337
+ time = perf.toFixed(4);
338
+ }
339
+ }
340
+ return `${btoa(name)}-${uuid.v4()}-${time}`;
352
341
  };
353
342
 
354
- const getCookieValue = key => {
355
- const name = `${key}=`;
356
- const decodedCookie = decodeURIComponent(document.cookie);
357
- const ca = decodedCookie.split(';');
343
+ const handleGetUserId = () => {
344
+ return getCookieValue('tag_user_id');
345
+ };
358
346
 
359
- for (let i = 0; i < ca.length; i++) {
360
- let c = ca[i];
347
+ const manifestVariables = {};
348
+ const addProviderVariable = (name, variables) => {
349
+ manifestVariables[name] = variables;
350
+ };
351
+ const getProviderVariables = (name) => manifestVariables[name] || {};
361
352
 
362
- while (c.charAt(0) === ' ') {
363
- c = c.substring(1);
353
+ const sendTag = ({ eventName, eventId, data, providerData, providers, options, }) => {
354
+ const payload = {
355
+ eventName,
356
+ eventId,
357
+ timestamp: Date.now(),
358
+ data,
359
+ providerData,
360
+ };
361
+ if (providers) {
362
+ payload.providers = providers;
364
363
  }
365
-
366
- if (c.indexOf(name) === 0) {
367
- return c.substring(name.length, c.length);
364
+ postRequest(getTagURL(), payload, options).catch(info);
365
+ };
366
+ const handleTag = (eventName, data = {}, providers, options) => {
367
+ if (!allowTag(providers)) {
368
+ console.log('No consent');
369
+ return;
368
370
  }
369
- }
370
-
371
- return '';
371
+ let eventId = data['eventId'];
372
+ if (!eventId) {
373
+ eventId = generateEventId(eventName);
374
+ }
375
+ const providerPackages = getProvidersPackage();
376
+ const userId = handleGetUserId();
377
+ const providerData = {};
378
+ if (providerPackages) {
379
+ providerPackages.forEach((pkg) => {
380
+ if (!allowProvider(providers, pkg.name)) {
381
+ return;
382
+ }
383
+ if (pkg && pkg.tag) {
384
+ const result = pkg.tag({
385
+ userId,
386
+ eventName,
387
+ eventId,
388
+ data,
389
+ sendTag,
390
+ manifestVariables: getProviderVariables(pkg.name),
391
+ });
392
+ if (result) {
393
+ providerData[pkg.name] = result;
394
+ }
395
+ }
396
+ });
397
+ }
398
+ sendTag({
399
+ eventName,
400
+ eventId,
401
+ data,
402
+ providerData,
403
+ providers,
404
+ options,
405
+ });
372
406
  };
373
407
 
374
408
  const handleCaptureQuery = (provider, key, persistType) => {
375
- const params = new URLSearchParams(window.location.search);
376
-
377
- if (!params || !params.get(key)) {
378
- return;
379
- }
380
-
381
- saveDataPerKey(persistType, provider, params.get(key), key);
409
+ if (!window) {
410
+ return;
411
+ }
412
+ const params = new URLSearchParams(window.location.search);
413
+ if (!params || !params.get(key)) {
414
+ return;
415
+ }
416
+ saveDataPerKey(persistType, provider, params.get(key), key);
382
417
  };
383
-
384
418
  const handleCaptureStorage = (provider, key, persistType, location) => {
385
- let data;
386
-
387
- switch (location) {
388
- case 'cookie':
389
- {
390
- data = getCookieValue(key);
391
- break;
392
- }
393
-
394
- case 'local':
395
- {
396
- data = sessionStorage.getItem(key);
397
- break;
398
- }
399
-
400
- case 'session':
401
- {
402
- data = localStorage.getItem(key);
403
- break;
404
- }
405
- }
406
-
407
- if (!data) {
408
- return;
409
- }
410
-
411
- saveDataPerKey(persistType, provider, data, key);
412
- };
413
-
414
- const handleCapture = (provider, params) => {
415
- params.forEach(param => {
416
- switch (param.type) {
417
- case 'query':
418
- {
419
- handleCaptureQuery(provider, param.key, param.persist);
420
- break;
419
+ let data;
420
+ switch (location) {
421
+ case 'cookie': {
422
+ data = getCookieValue(key);
423
+ break;
421
424
  }
422
-
423
- case 'storage':
424
- {
425
- handleCaptureStorage(provider, param.key, param.persist, param.location);
426
- break;
425
+ case 'local': {
426
+ data = sessionStorage && sessionStorage.getItem(key);
427
+ break;
427
428
  }
429
+ case 'session': {
430
+ data = localStorage && localStorage.getItem(key);
431
+ break;
432
+ }
433
+ }
434
+ if (!data) {
435
+ return;
428
436
  }
429
- });
437
+ saveDataPerKey(persistType, provider, data, key);
430
438
  };
431
-
432
- const handleManifest = manifest => {
433
- const providers = [];
434
- manifest.forEach(provider => {
435
- providers.push(provider.package);
436
-
437
- if (provider.rules) {
438
- Object.entries(provider.rules).forEach(([name, recipe]) => {
439
- switch (name) {
440
- case 'capture':
441
- {
442
- handleCapture(provider.package, recipe);
443
- return;
439
+ const handleCapture = (provider, params) => {
440
+ params.forEach((param) => {
441
+ switch (param.type) {
442
+ case 'query': {
443
+ handleCaptureQuery(provider, param.key, param.persist);
444
+ break;
445
+ }
446
+ case 'storage': {
447
+ handleCaptureStorage(provider, param.key, param.persist, param.location);
448
+ break;
444
449
  }
445
450
  }
446
- });
447
- }
448
- });
449
- savePerKey('local', tagStorage, providers, providersKey);
451
+ });
450
452
  };
451
453
 
452
- const handleInit = preferences => {
453
- const success = setPreferences(preferences);
454
-
455
- if (!success) {
456
- return;
457
- }
458
-
459
- const url = new URL(getInitURL());
460
-
461
- if (preferences.disableConsentCheck) {
462
- url.searchParams.set('consentDisabled', 'true');
463
- saveConsent({
464
- all: true
454
+ const handleManifest = (manifest) => {
455
+ const providers = [];
456
+ const providerPackages = getProvidersPackage();
457
+ const userId = handleGetUserId();
458
+ manifest.forEach((provider) => {
459
+ providers.push(provider.package);
460
+ addProviderVariable(provider.package, provider.variables);
461
+ if (provider.rules) {
462
+ Object.entries(provider.rules).forEach(([name, recipe]) => {
463
+ switch (name) {
464
+ case 'capture': {
465
+ handleCapture(provider.package, recipe);
466
+ return;
467
+ }
468
+ }
469
+ });
470
+ }
471
+ if (providerPackages) {
472
+ const pkg = providerPackages.find((pkg) => pkg.name === provider.package);
473
+ if (pkg && pkg.init) {
474
+ pkg.init({ userId, manifest: provider, sendTag });
475
+ }
476
+ }
465
477
  });
466
- }
467
-
468
- if (preferences.userId) {
469
- url.searchParams.set('userId', preferences.userId);
470
- }
478
+ savePerKey('local', tagStorage, providers, providersKey);
479
+ };
471
480
 
472
- getRequest(url.href).then(result => {
473
- if (!result) {
474
- console.log('init failed');
475
- return;
481
+ const handleInit = (preferences) => {
482
+ const success = setPreferences(preferences);
483
+ if (!success) {
484
+ return;
476
485
  }
477
-
478
- handleManifest(result.result);
479
- }).catch(info);
486
+ const url = new URL(getInitURL());
487
+ if (preferences.disableConsentCheck) {
488
+ url.searchParams.set('consentDisabled', 'true');
489
+ saveConsent({ all: true });
490
+ }
491
+ if (preferences.userId) {
492
+ url.searchParams.set('userId', preferences.userId);
493
+ }
494
+ getRequest(url.href)
495
+ .then((result) => {
496
+ if (!result) {
497
+ console.log('init failed');
498
+ return;
499
+ }
500
+ handleManifest(result.result);
501
+ })
502
+ .catch(info);
480
503
  };
481
504
 
482
505
  const handleUser = (key, value, options) => {
483
- if (!key || !value) {
484
- console.error('Key or Value is missing in user API.');
485
- return;
486
- }
487
-
488
- saveKV({
489
- [key]: value
490
- });
491
- postRequest(getUserURL(), {
492
- key,
493
- value
494
- }, options).catch(info);
506
+ if (!key || !value) {
507
+ console.error('Key or Value is missing in user API.');
508
+ return;
509
+ }
510
+ saveKV({
511
+ [key]: value,
512
+ });
513
+ postRequest(getUserURL(), {
514
+ key,
515
+ value,
516
+ }, options).catch(info);
495
517
  };
496
518
 
497
519
  const handleData = (data, options) => {
498
- if (!data || Object.keys(data).length === 0) {
499
- console.error('Provide data for data API.');
500
- return;
501
- }
502
-
503
- saveKV(data);
504
- postRequest(getDataURL(), {
505
- data
506
- }, options).catch(info);
520
+ if (!data || Object.keys(data).length === 0) {
521
+ console.error('Provide data for data API.');
522
+ return;
523
+ }
524
+ saveKV(data);
525
+ postRequest(getDataURL(), { data }, options).catch(info);
507
526
  };
508
527
 
509
528
  const handleGetData = (keys, callback) => {
510
- if (!keys || keys.length === 0) {
511
- console.error('Provide keys for get data API.');
512
- return;
513
- }
514
-
515
- getRequest(getGetDataURL(keys)).then(result => {
516
- callback((result === null || result === void 0 ? void 0 : result.result) || {});
517
- }).catch(info);
529
+ if (!keys || keys.length === 0) {
530
+ console.error('Provide keys for get data API.');
531
+ return;
532
+ }
533
+ getRequest(getGetDataURL(keys))
534
+ .then((result) => {
535
+ callback((result === null || result === void 0 ? void 0 : result.result) || {});
536
+ })
537
+ .catch(info);
518
538
  };
519
539
 
520
- const handleKeys = callback => {
521
- getRequest(getKeysURL()).then(result => {
522
- callback((result === null || result === void 0 ? void 0 : result.result) || []);
523
- }).catch(info);
540
+ const handleKeys = (callback) => {
541
+ getRequest(getKeysURL())
542
+ .then((result) => {
543
+ callback((result === null || result === void 0 ? void 0 : result.result) || []);
544
+ })
545
+ .catch(info);
524
546
  };
525
547
 
526
- const init = preferences => {
527
- handleInit(preferences);
548
+ const init = (preferences) => {
549
+ handleInit(preferences);
528
550
  };
529
551
  const tag = (name, data, providers, options) => {
530
- handleTag(name, data, providers, options);
552
+ handleTag(name, data, providers, options);
531
553
  };
532
- const consent = consent => {
533
- handleConsent(consent);
554
+ const consent = (consent) => {
555
+ handleConsent(consent);
534
556
  };
535
557
  const user = (key, value, options) => {
536
- handleUser(key, value, options);
558
+ handleUser(key, value, options);
537
559
  };
538
560
  const data = (data, options) => {
539
- handleData(data, options);
561
+ handleData(data, options);
540
562
  };
541
563
  const getData = (keys, callback) => {
542
- handleGetData(keys, callback);
564
+ handleGetData(keys, callback);
565
+ };
566
+ const keys = (callback) => {
567
+ handleKeys(callback);
543
568
  };
544
- const keys = callback => {
545
- handleKeys(callback);
569
+ const getUserId = () => {
570
+ return handleGetUserId();
546
571
  };
547
572
 
548
573
  exports.consent = consent;
549
574
  exports.data = data;
550
575
  exports.getData = getData;
576
+ exports.getUserId = getUserId;
551
577
  exports.init = init;
552
578
  exports.keys = keys;
553
579
  exports.tag = tag;