@blotoutio/edgetag-sdk-js 0.3.0 → 0.4.0

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/README.md CHANGED
@@ -1,3 +1,3 @@
1
1
  # EdgeTag SDK Core
2
2
 
3
- More info on https://edgetag.io/integration/js
3
+ More info on https://app.edgetag.io/integration/js
package/api/data.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ import { DataValue } from '../typings';
2
+ export declare const handleData: (data: Record<string, DataValue>) => void;
@@ -0,0 +1,2 @@
1
+ import { DataValue } from '../typings';
2
+ export declare const handleGetData: (keys: string[], callback: (data: Record<string, DataValue>) => void) => void;
package/api/keys.d.ts ADDED
@@ -0,0 +1 @@
1
+ export declare const handleKeys: (callback: (keys: string[]) => void) => void;
package/api/tag.d.ts CHANGED
@@ -1,2 +1,2 @@
1
1
  import { Data } from '../typings';
2
- export declare const handleTag: (name: string, data?: Data | undefined, providers?: Data | undefined) => void;
2
+ export declare const handleTag: (name: string, data?: Data, providers?: Data) => void;
package/api/user.d.ts CHANGED
@@ -1,2 +1,2 @@
1
- import { UserKeys } from '../typings';
2
- export declare const handleUser: (key: UserKeys, value: string) => void;
1
+ import { UserKey } from '../typings';
2
+ export declare const handleUser: (key: UserKey, value: string) => void;
@@ -1,6 +1,8 @@
1
1
  import { PersistType } from '../typings/internal';
2
+ import { DataValue } from '../typings';
2
3
  export declare const saveDataPerKey: (persistType: PersistType, provider: string, value: unknown, key: string) => void;
3
4
  export declare const savePerKey: (persistType: PersistType, provider: string, value: unknown, key: string) => void;
4
5
  export declare const getDataPerKey: (persistType: PersistType, provider: string, key: string) => unknown;
5
6
  export declare const saveData: (persistType: PersistType, value: unknown, key?: string) => void;
6
7
  export declare const getData: (persistType: PersistType, key?: string) => Record<string, Record<string, unknown>>;
8
+ export declare const saveDataAPI: (data: Record<string, DataValue>) => void;
package/common/utils.d.ts CHANGED
@@ -1,3 +1,3 @@
1
1
  import { Data } from '../typings';
2
2
  export declare const getUserAgent: () => string;
3
- export declare const allowTag: (providers?: Data | undefined) => boolean;
3
+ export declare const allowTag: (providers?: Data) => boolean;
@@ -34,7 +34,7 @@ const keyPrefix = `_worker`;
34
34
 
35
35
  const initKey = `${keyPrefix}Store`;
36
36
  const saveDataPerKey = (persistType, provider, value, key) => {
37
- const storage = getData(persistType);
37
+ const storage = getData$1(persistType);
38
38
 
39
39
  if (!storage['data']) {
40
40
  storage['data'] = {};
@@ -48,7 +48,7 @@ const saveDataPerKey = (persistType, provider, value, key) => {
48
48
  saveData(persistType, storage);
49
49
  };
50
50
  const savePerKey = (persistType, provider, value, key) => {
51
- const storage = getData(persistType);
51
+ const storage = getData$1(persistType);
52
52
 
53
53
  if (!storage[provider]) {
54
54
  storage[provider] = {};
@@ -58,7 +58,7 @@ const savePerKey = (persistType, provider, value, key) => {
58
58
  saveData(persistType, storage);
59
59
  };
60
60
  const getDataPerKey = (persistType, provider, key) => {
61
- const storage = getData(persistType);
61
+ const storage = getData$1(persistType);
62
62
 
63
63
  if (!storage[provider]) {
64
64
  return undefined;
@@ -74,13 +74,27 @@ const saveData = (persistType, value, key = initKey) => {
74
74
 
75
75
  saveLocal(value, key);
76
76
  };
77
- const getData = (persistType, key = initKey) => {
77
+ const getData$1 = (persistType, key = initKey) => {
78
78
  if (persistType === 'session') {
79
79
  return getSession(key);
80
80
  }
81
81
 
82
82
  return getLocal(key);
83
83
  };
84
+ const saveDataAPI = data => {
85
+ let currentSession = getData$1('session');
86
+
87
+ if (!currentSession) {
88
+ currentSession = {};
89
+ }
90
+
91
+ if (!currentSession['data']) {
92
+ currentSession['data'] = {};
93
+ }
94
+
95
+ currentSession['data'] = Object.assign(Object.assign({}, currentSession['data']), data);
96
+ saveData('session', currentSession);
97
+ };
84
98
 
85
99
  const saveLocal = (value, key) => {
86
100
  localStorage.setItem(key, JSON.stringify(value));
@@ -145,6 +159,15 @@ const getConsentURL = () => {
145
159
  const getUserURL = () => {
146
160
  return generateUrl('/user');
147
161
  };
162
+ const getDataURL = () => {
163
+ return generateUrl(`/data`);
164
+ };
165
+ const getGetDataURL = keys => {
166
+ return generateUrl(`/data?keys=${encodeURIComponent(keys.join(','))}`);
167
+ };
168
+ const getKeysURL = () => {
169
+ return generateUrl(`/keys`);
170
+ };
148
171
 
149
172
  let consentDisabled = false;
150
173
  const setPreferences = preferences => {
@@ -246,13 +269,13 @@ const getStandardPayload = payload => {
246
269
  userAgent: getUserAgent()
247
270
  }, payload || {});
248
271
  let storage = {};
249
- const session = getData('session');
272
+ const session = getData$1('session');
250
273
 
251
274
  if (session) {
252
275
  storage = Object.assign(Object.assign({}, storage), session);
253
276
  }
254
277
 
255
- const local = getData('local');
278
+ const local = getData$1('local');
256
279
 
257
280
  if (local) {
258
281
  storage = Object.assign(Object.assign({}, storage), local);
@@ -270,7 +293,7 @@ function postRequest(url, data, options) {
270
293
 
271
294
  const payload = getStandardPayload(data);
272
295
 
273
- if (options && options.method === 'beacon' && navigator.sendBeacon) {
296
+ if (options && options.method === 'beacon') {
274
297
  return Promise.resolve(beacon(url, payload));
275
298
  }
276
299
 
@@ -283,7 +306,7 @@ function getRequest(url, options) {
283
306
  return Promise.reject(new Error('URL is empty'));
284
307
  }
285
308
 
286
- if (options && options.method === 'beacon' && navigator.sendBeacon) {
309
+ if (options && options.method === 'beacon') {
287
310
  return {
288
311
  result: Promise.resolve(beacon(url))
289
312
  };
@@ -451,15 +474,46 @@ const handleInit = preferences => {
451
474
 
452
475
  const handleUser = (key, value) => {
453
476
  if (!key || !value) {
454
- console.error('Key or Value is missing in user api.');
477
+ console.error('Key or Value is missing in user API.');
455
478
  return;
456
479
  }
457
480
 
458
- const payload = {
481
+ saveDataAPI({
482
+ [key]: value
483
+ });
484
+ postRequest(getUserURL(), {
459
485
  key,
460
486
  value
461
- };
462
- postRequest(getUserURL(), payload).catch(info);
487
+ }).catch(info);
488
+ };
489
+
490
+ const handleData = data => {
491
+ if (!data || Object.keys(data).length === 0) {
492
+ console.error('Provide data for data API.');
493
+ return;
494
+ }
495
+
496
+ saveDataAPI(data);
497
+ postRequest(getDataURL(), {
498
+ data
499
+ }).catch(info);
500
+ };
501
+
502
+ const handleGetData = (keys, callback) => {
503
+ if (!keys || keys.length === 0) {
504
+ console.error('Provide keys for get data API.');
505
+ return;
506
+ }
507
+
508
+ getRequest(getGetDataURL(keys)).then(result => {
509
+ callback((result === null || result === void 0 ? void 0 : result.result) || {});
510
+ }).catch(info);
511
+ };
512
+
513
+ const handleKeys = callback => {
514
+ getRequest(getKeysURL()).then(result => {
515
+ callback((result === null || result === void 0 ? void 0 : result.result) || []);
516
+ }).catch(info);
463
517
  };
464
518
 
465
519
  const init = preferences => {
@@ -474,8 +528,20 @@ const consent = consent => {
474
528
  const user = (key, value) => {
475
529
  handleUser(key, value);
476
530
  };
531
+ const data = data => {
532
+ handleData(data);
533
+ };
534
+ const getData = (keys, callback) => {
535
+ handleGetData(keys, callback);
536
+ };
537
+ const keys = callback => {
538
+ handleKeys(callback);
539
+ };
477
540
 
478
541
  exports.consent = consent;
542
+ exports.data = data;
543
+ exports.getData = getData;
479
544
  exports.init = init;
545
+ exports.keys = keys;
480
546
  exports.tag = tag;
481
547
  exports.user = user;
package/index.d.ts CHANGED
@@ -1,5 +1,8 @@
1
- import { Data, InitPreferences, UserKeys } from './typings';
1
+ import { Data, DataValue, InitPreferences, UserKey } from './typings';
2
2
  export declare const init: (preferences: InitPreferences) => void;
3
- export declare const tag: (name: string, data?: Data | undefined, providers?: Data | undefined) => void;
3
+ export declare const tag: (name: string, data?: Data, providers?: Data) => void;
4
4
  export declare const consent: (consent: Data) => void;
5
- export declare const user: (key: UserKeys, value: string) => void;
5
+ export declare const user: (key: UserKey, value: string) => void;
6
+ export declare const data: (data: Record<string, DataValue>) => void;
7
+ export declare const getData: (keys: string[], callback: (data: Record<string, DataValue>) => void) => void;
8
+ export declare const keys: (callback: (keys: string[]) => void) => void;
@@ -30,7 +30,7 @@ const keyPrefix = `_worker`;
30
30
 
31
31
  const initKey = `${keyPrefix}Store`;
32
32
  const saveDataPerKey = (persistType, provider, value, key) => {
33
- const storage = getData(persistType);
33
+ const storage = getData$1(persistType);
34
34
 
35
35
  if (!storage['data']) {
36
36
  storage['data'] = {};
@@ -44,7 +44,7 @@ const saveDataPerKey = (persistType, provider, value, key) => {
44
44
  saveData(persistType, storage);
45
45
  };
46
46
  const savePerKey = (persistType, provider, value, key) => {
47
- const storage = getData(persistType);
47
+ const storage = getData$1(persistType);
48
48
 
49
49
  if (!storage[provider]) {
50
50
  storage[provider] = {};
@@ -54,7 +54,7 @@ const savePerKey = (persistType, provider, value, key) => {
54
54
  saveData(persistType, storage);
55
55
  };
56
56
  const getDataPerKey = (persistType, provider, key) => {
57
- const storage = getData(persistType);
57
+ const storage = getData$1(persistType);
58
58
 
59
59
  if (!storage[provider]) {
60
60
  return undefined;
@@ -70,13 +70,27 @@ const saveData = (persistType, value, key = initKey) => {
70
70
 
71
71
  saveLocal(value, key);
72
72
  };
73
- const getData = (persistType, key = initKey) => {
73
+ const getData$1 = (persistType, key = initKey) => {
74
74
  if (persistType === 'session') {
75
75
  return getSession(key);
76
76
  }
77
77
 
78
78
  return getLocal(key);
79
79
  };
80
+ const saveDataAPI = data => {
81
+ let currentSession = getData$1('session');
82
+
83
+ if (!currentSession) {
84
+ currentSession = {};
85
+ }
86
+
87
+ if (!currentSession['data']) {
88
+ currentSession['data'] = {};
89
+ }
90
+
91
+ currentSession['data'] = Object.assign(Object.assign({}, currentSession['data']), data);
92
+ saveData('session', currentSession);
93
+ };
80
94
 
81
95
  const saveLocal = (value, key) => {
82
96
  localStorage.setItem(key, JSON.stringify(value));
@@ -141,6 +155,15 @@ const getConsentURL = () => {
141
155
  const getUserURL = () => {
142
156
  return generateUrl('/user');
143
157
  };
158
+ const getDataURL = () => {
159
+ return generateUrl(`/data`);
160
+ };
161
+ const getGetDataURL = keys => {
162
+ return generateUrl(`/data?keys=${encodeURIComponent(keys.join(','))}`);
163
+ };
164
+ const getKeysURL = () => {
165
+ return generateUrl(`/keys`);
166
+ };
144
167
 
145
168
  let consentDisabled = false;
146
169
  const setPreferences = preferences => {
@@ -242,13 +265,13 @@ const getStandardPayload = payload => {
242
265
  userAgent: getUserAgent()
243
266
  }, payload || {});
244
267
  let storage = {};
245
- const session = getData('session');
268
+ const session = getData$1('session');
246
269
 
247
270
  if (session) {
248
271
  storage = Object.assign(Object.assign({}, storage), session);
249
272
  }
250
273
 
251
- const local = getData('local');
274
+ const local = getData$1('local');
252
275
 
253
276
  if (local) {
254
277
  storage = Object.assign(Object.assign({}, storage), local);
@@ -266,7 +289,7 @@ function postRequest(url, data, options) {
266
289
 
267
290
  const payload = getStandardPayload(data);
268
291
 
269
- if (options && options.method === 'beacon' && navigator.sendBeacon) {
292
+ if (options && options.method === 'beacon') {
270
293
  return Promise.resolve(beacon(url, payload));
271
294
  }
272
295
 
@@ -279,7 +302,7 @@ function getRequest(url, options) {
279
302
  return Promise.reject(new Error('URL is empty'));
280
303
  }
281
304
 
282
- if (options && options.method === 'beacon' && navigator.sendBeacon) {
305
+ if (options && options.method === 'beacon') {
283
306
  return {
284
307
  result: Promise.resolve(beacon(url))
285
308
  };
@@ -447,15 +470,46 @@ const handleInit = preferences => {
447
470
 
448
471
  const handleUser = (key, value) => {
449
472
  if (!key || !value) {
450
- console.error('Key or Value is missing in user api.');
473
+ console.error('Key or Value is missing in user API.');
451
474
  return;
452
475
  }
453
476
 
454
- const payload = {
477
+ saveDataAPI({
478
+ [key]: value
479
+ });
480
+ postRequest(getUserURL(), {
455
481
  key,
456
482
  value
457
- };
458
- postRequest(getUserURL(), payload).catch(info);
483
+ }).catch(info);
484
+ };
485
+
486
+ const handleData = data => {
487
+ if (!data || Object.keys(data).length === 0) {
488
+ console.error('Provide data for data API.');
489
+ return;
490
+ }
491
+
492
+ saveDataAPI(data);
493
+ postRequest(getDataURL(), {
494
+ data
495
+ }).catch(info);
496
+ };
497
+
498
+ const handleGetData = (keys, callback) => {
499
+ if (!keys || keys.length === 0) {
500
+ console.error('Provide keys for get data API.');
501
+ return;
502
+ }
503
+
504
+ getRequest(getGetDataURL(keys)).then(result => {
505
+ callback((result === null || result === void 0 ? void 0 : result.result) || {});
506
+ }).catch(info);
507
+ };
508
+
509
+ const handleKeys = callback => {
510
+ getRequest(getKeysURL()).then(result => {
511
+ callback((result === null || result === void 0 ? void 0 : result.result) || []);
512
+ }).catch(info);
459
513
  };
460
514
 
461
515
  const init = preferences => {
@@ -470,5 +524,14 @@ const consent = consent => {
470
524
  const user = (key, value) => {
471
525
  handleUser(key, value);
472
526
  };
527
+ const data = data => {
528
+ handleData(data);
529
+ };
530
+ const getData = (keys, callback) => {
531
+ handleGetData(keys, callback);
532
+ };
533
+ const keys = callback => {
534
+ handleKeys(callback);
535
+ };
473
536
 
474
- export { consent, init, tag, user };
537
+ export { consent, data, getData, init, keys, tag, user };
@@ -1,6 +1,9 @@
1
1
  export declare const getUrl: () => string;
2
- export declare const setUrl: (url?: string | undefined) => void;
2
+ export declare const setUrl: (url?: string) => void;
3
3
  export declare const getTagURL: () => string;
4
4
  export declare const getInitURL: () => string;
5
5
  export declare const getConsentURL: () => string;
6
6
  export declare const getUserURL: () => string;
7
+ export declare const getDataURL: () => string;
8
+ export declare const getGetDataURL: (keys: string[]) => string;
9
+ export declare const getKeysURL: () => string;
@@ -1,4 +1,3 @@
1
1
  import { EventOptions } from '../typings';
2
- import { PostData } from '../typings/internal';
3
- export declare function postRequest(url: string, data?: PostData, options?: EventOptions): Promise<unknown>;
4
- export declare function getRequest<Payload, Response>(url: string, options?: EventOptions): Promise<Response>;
2
+ export declare function postRequest<Payload, Response>(url: string, data?: Payload, options?: EventOptions): Promise<unknown>;
3
+ export declare function getRequest<Response>(url: string, options?: EventOptions): Promise<Response>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@blotoutio/edgetag-sdk-js",
3
- "version": "0.3.0",
3
+ "version": "0.4.0",
4
4
  "description": "JS SDK for EdgeTag",
5
5
  "author": "Blotout",
6
6
  "license": "MIT",
@@ -12,7 +12,15 @@
12
12
  "type": "git",
13
13
  "url": "git+https://github.com/blotoutio/edgetag-sdk.git"
14
14
  },
15
- "main": "./index.umd.js",
16
- "module": "./index.esm.js",
17
- "typings": "./index.d.ts"
15
+ "module": "./index.js",
16
+ "main": "./index.cjs",
17
+ "type": "module",
18
+ "types": "./index.d.ts",
19
+ "exports": {
20
+ ".": {
21
+ "types": "./index.d.ts",
22
+ "import": "./index.js",
23
+ "require": "./index.cjs"
24
+ }
25
+ }
18
26
  }
@@ -1,4 +1,4 @@
1
- type UserKeys =
1
+ type UserKey =
2
2
  | 'email'
3
3
  | 'phone'
4
4
  | 'firstName'
@@ -9,9 +9,12 @@ type UserKeys =
9
9
  | 'state'
10
10
  | 'city'
11
11
  | 'zip'
12
+ | 'address'
12
13
 
13
14
  type Data = Record<string, unknown>
14
15
 
16
+ type DataValue = string | number | boolean
17
+
15
18
  interface InitPreferences {
16
19
  edgeURL: string
17
20
  disableConsentCheck?: boolean
@@ -26,3 +29,14 @@ export declare const init: (preferences: InitPreferences) => void
26
29
  export declare const tag: (name: string, data?: Data) => void
27
30
 
28
31
  export declare const consent: (consent: Data) => void
32
+
33
+ export declare const user: (key: UserKey, value: string) => void
34
+
35
+ export declare const data: (data: Record<string, DataValue>) => void
36
+
37
+ export declare const getData: (
38
+ keys: string[],
39
+ callback: (data: Record<string, DataValue>) => void
40
+ ) => void
41
+
42
+ export declare const keys: (callback: (keys: string[]) => void) => void
@@ -1,30 +1,32 @@
1
- import { Data, UserKeys } from "./index";
1
+ import { Data, DataValue, UserKey } from './index'
2
2
 
3
3
  type PersistType = 'local' | 'session'
4
4
 
5
- interface Navigator {
5
+ type Navigator = {
6
6
  userAgent: string
7
7
  brave: unknown
8
8
  }
9
9
 
10
- interface TagRequest {
10
+ type TagPayload = {
11
11
  eventName: string
12
12
  data?: Data
13
13
  providers?: Data
14
14
  }
15
15
 
16
- interface ConsentRequest {
16
+ type ConsentPayload = {
17
17
  consentString: Data
18
18
  }
19
19
 
20
- interface UserRequest {
21
- key: UserKeys
20
+ type UserPayload = {
21
+ key: UserKey
22
22
  value: string
23
23
  }
24
24
 
25
- type PostData = TagRequest | ConsentRequest | UserRequest
25
+ type DataPayload = {
26
+ data: Record<string, DataValue>
27
+ }
26
28
 
27
- interface PostPayload {
29
+ type PostPayload = {
28
30
  pageUrl: string
29
31
  userAgent: string
30
32
  data?: Data
package/index.umd.js DELETED
@@ -1,487 +0,0 @@
1
- (function (global, factory) {
2
- typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
3
- typeof define === 'function' && define.amd ? define(['exports'], factory) :
4
- (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.BlotoutEdgeTagSDKJS = {}));
5
- })(this, (function (exports) { 'use strict';
6
-
7
- /******************************************************************************
8
- Copyright (c) Microsoft Corporation.
9
-
10
- Permission to use, copy, modify, and/or distribute this software for any
11
- purpose with or without fee is hereby granted.
12
-
13
- THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
14
- REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
15
- AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
16
- INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
17
- LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
18
- OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
19
- PERFORMANCE OF THIS SOFTWARE.
20
- ***************************************************************************** */
21
-
22
- function __awaiter(thisArg, _arguments, P, generator) {
23
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
24
- return new (P || (P = Promise))(function (resolve, reject) {
25
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
26
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
27
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
28
- step((generator = generator.apply(thisArg, _arguments || [])).next());
29
- });
30
- }
31
-
32
- const tagStorage = 'edgeTag';
33
- const consentKey = 'consent';
34
- const providersKey = 'providers';
35
- const keyPrefix = `_worker`;
36
-
37
- const initKey = `${keyPrefix}Store`;
38
- const saveDataPerKey = (persistType, provider, value, key) => {
39
- const storage = getData(persistType);
40
-
41
- if (!storage['data']) {
42
- storage['data'] = {};
43
- }
44
-
45
- if (!storage['data'][provider]) {
46
- storage['data'][provider] = {};
47
- }
48
-
49
- storage['data'][provider][key] = value;
50
- saveData(persistType, storage);
51
- };
52
- const savePerKey = (persistType, provider, value, key) => {
53
- const storage = getData(persistType);
54
-
55
- if (!storage[provider]) {
56
- storage[provider] = {};
57
- }
58
-
59
- storage[provider][key] = value;
60
- saveData(persistType, storage);
61
- };
62
- const getDataPerKey = (persistType, provider, key) => {
63
- const storage = getData(persistType);
64
-
65
- if (!storage[provider]) {
66
- return undefined;
67
- }
68
-
69
- return storage[provider][key];
70
- };
71
- const saveData = (persistType, value, key = initKey) => {
72
- if (persistType === 'session') {
73
- saveSession(value, key);
74
- return;
75
- }
76
-
77
- saveLocal(value, key);
78
- };
79
- const getData = (persistType, key = initKey) => {
80
- if (persistType === 'session') {
81
- return getSession(key);
82
- }
83
-
84
- return getLocal(key);
85
- };
86
-
87
- const saveLocal = (value, key) => {
88
- localStorage.setItem(key, JSON.stringify(value));
89
- };
90
-
91
- const getLocal = key => {
92
- const data = localStorage.getItem(key);
93
-
94
- if (!data) {
95
- return {};
96
- }
97
-
98
- return JSON.parse(data) || {};
99
- };
100
-
101
- const saveSession = (value, key) => {
102
- sessionStorage.setItem(key, JSON.stringify(value));
103
- };
104
-
105
- const getSession = key => {
106
- const data = sessionStorage.getItem(key);
107
-
108
- if (!data) {
109
- return {};
110
- }
111
-
112
- return JSON.parse(data) || {};
113
- };
114
-
115
- let endpointUrl = '';
116
-
117
- const generateUrl = path => {
118
- const endpoint = getUrl();
119
-
120
- if (!endpoint) {
121
- console.log('URL is not valid');
122
- return '';
123
- }
124
-
125
- return `${endpoint}${path}`;
126
- };
127
-
128
- const getUrl = () => {
129
- return endpointUrl;
130
- };
131
- const setUrl = url => {
132
- if (url == null) {
133
- return;
134
- }
135
-
136
- endpointUrl = url;
137
- };
138
- const getTagURL = () => {
139
- return generateUrl('/tag');
140
- };
141
- const getInitURL = () => {
142
- return generateUrl('/init');
143
- };
144
- const getConsentURL = () => {
145
- return generateUrl('/consent');
146
- };
147
- const getUserURL = () => {
148
- return generateUrl('/user');
149
- };
150
-
151
- let consentDisabled = false;
152
- const setPreferences = preferences => {
153
- if (!preferences) {
154
- return false;
155
- }
156
-
157
- if (!preferences.edgeURL) {
158
- console.error('Please provide URL for EdgeTag');
159
- return false;
160
- }
161
-
162
- consentDisabled = !!preferences.disableConsentCheck;
163
- setUrl(preferences.edgeURL);
164
- return true;
165
- };
166
- const isConsentDisabled = () => consentDisabled;
167
-
168
- const getUserAgent = () => {
169
- const nav = navigator;
170
- let ua = nav.userAgent;
171
- ua += nav.brave ? `${ua} Brave` : '';
172
- return ua;
173
- };
174
- const allowTag = providers => {
175
- const consent = getDataPerKey('local', tagStorage, consentKey);
176
-
177
- if (isConsentDisabled()) {
178
- return true;
179
- }
180
-
181
- if (!consent) {
182
- return false;
183
- }
184
-
185
- if (consent['all']) {
186
- return true;
187
- }
188
-
189
- if (!providers) {
190
- return Object.values(consent).find(isAllowed => isAllowed) || false;
191
- }
192
-
193
- const allProviders = getDataPerKey('local', tagStorage, providersKey) || [];
194
-
195
- for (const provider of allProviders) {
196
- const tagProviderSetting = providers[provider];
197
-
198
- if (tagProviderSetting || providers['all'] === true && tagProviderSetting === undefined || providers['all'] === false && tagProviderSetting === true) {
199
- if (consent[provider]) {
200
- return true;
201
- }
202
- }
203
- }
204
-
205
- return false;
206
- };
207
-
208
- const beacon = (url, payload) => {
209
- let blob;
210
-
211
- if (payload) {
212
- blob = new Blob([JSON.stringify(payload)], {
213
- type: 'application/json'
214
- });
215
- }
216
-
217
- return navigator.sendBeacon(url, blob);
218
- };
219
-
220
- const ajax = (method, url, payload) => __awaiter(void 0, void 0, void 0, function* () {
221
- return yield fetch(url, {
222
- method,
223
- headers: {
224
- 'Content-type': 'application/json; charset=utf-8',
225
- Accept: 'application/json; charset=utf-8'
226
- },
227
- body: JSON.stringify(payload),
228
- credentials: 'include'
229
- }).then(response => response.json().then(data => ({
230
- status: response.status,
231
- body: data
232
- }))).then(data => {
233
- if (data.status < 200 || data.status >= 300) {
234
- // Q: do we need to retry?
235
- return Promise.reject(new Error(JSON.stringify(data.body)));
236
- }
237
-
238
- return Promise.resolve(data.body);
239
- }).catch(error => {
240
- // Q: do we need to retry?
241
- return Promise.reject(new Error(error));
242
- });
243
- });
244
-
245
- const getStandardPayload = payload => {
246
- const data = Object.assign({
247
- pageUrl: window.location.href,
248
- userAgent: getUserAgent()
249
- }, payload || {});
250
- let storage = {};
251
- const session = getData('session');
252
-
253
- if (session) {
254
- storage = Object.assign(Object.assign({}, storage), session);
255
- }
256
-
257
- const local = getData('local');
258
-
259
- if (local) {
260
- storage = Object.assign(Object.assign({}, storage), local);
261
- }
262
-
263
- data.storage = storage;
264
- return data;
265
- };
266
-
267
- function postRequest(url, data, options) {
268
- return __awaiter(this, void 0, void 0, function* () {
269
- if (!url) {
270
- return Promise.reject(new Error('URL is empty'));
271
- }
272
-
273
- const payload = getStandardPayload(data);
274
-
275
- if (options && options.method === 'beacon' && navigator.sendBeacon) {
276
- return Promise.resolve(beacon(url, payload));
277
- }
278
-
279
- return yield ajax('POST', url, payload);
280
- });
281
- }
282
- function getRequest(url, options) {
283
- return __awaiter(this, void 0, void 0, function* () {
284
- if (!url) {
285
- return Promise.reject(new Error('URL is empty'));
286
- }
287
-
288
- if (options && options.method === 'beacon' && navigator.sendBeacon) {
289
- return {
290
- result: Promise.resolve(beacon(url))
291
- };
292
- }
293
-
294
- return yield ajax('GET', url);
295
- });
296
- }
297
-
298
- const info = data => {
299
- };
300
-
301
- const saveConsent = consent => {
302
- savePerKey('local', tagStorage, consent, consentKey);
303
- };
304
- const handleConsent = consent => {
305
- const payload = {
306
- consentString: consent
307
- };
308
- saveConsent(consent);
309
- postRequest(getConsentURL(), payload).catch(info);
310
- };
311
-
312
- const handleTag = (name, data, providers) => {
313
- const payload = {
314
- eventName: name
315
- };
316
-
317
- if (data) {
318
- payload.data = data;
319
- }
320
-
321
- if (providers) {
322
- payload.providers = providers;
323
- }
324
-
325
- if (!allowTag(providers)) {
326
- console.log('No consent');
327
- return;
328
- }
329
-
330
- postRequest(getTagURL(), payload).catch(info);
331
- };
332
-
333
- const getCookieValue = key => {
334
- const name = `${key}=`;
335
- const decodedCookie = decodeURIComponent(document.cookie);
336
- const ca = decodedCookie.split(';');
337
-
338
- for (let i = 0; i < ca.length; i++) {
339
- let c = ca[i];
340
-
341
- while (c.charAt(0) === ' ') {
342
- c = c.substring(1);
343
- }
344
-
345
- if (c.indexOf(name) === 0) {
346
- return c.substring(name.length, c.length);
347
- }
348
- }
349
-
350
- return '';
351
- };
352
-
353
- const handleCaptureQuery = (provider, key, persistType) => {
354
- const params = new URLSearchParams(window.location.search);
355
-
356
- if (!params || !params.get(key)) {
357
- return;
358
- }
359
-
360
- saveDataPerKey(persistType, provider, params.get(key), key);
361
- };
362
-
363
- const handleCaptureStorage = (provider, key, persistType, location) => {
364
- let data;
365
-
366
- switch (location) {
367
- case 'cookie':
368
- {
369
- data = getCookieValue(key);
370
- break;
371
- }
372
-
373
- case 'local':
374
- {
375
- data = sessionStorage.getItem(key);
376
- break;
377
- }
378
-
379
- case 'session':
380
- {
381
- data = localStorage.getItem(key);
382
- break;
383
- }
384
- }
385
-
386
- if (!data) {
387
- return;
388
- }
389
-
390
- saveDataPerKey(persistType, provider, data, key);
391
- };
392
-
393
- const handleCapture = (provider, params) => {
394
- params.forEach(param => {
395
- switch (param.type) {
396
- case 'query':
397
- {
398
- handleCaptureQuery(provider, param.key, param.persist);
399
- break;
400
- }
401
-
402
- case 'storage':
403
- {
404
- handleCaptureStorage(provider, param.key, param.persist, param.location);
405
- break;
406
- }
407
- }
408
- });
409
- };
410
-
411
- const handleManifest = manifest => {
412
- const providers = [];
413
- manifest.forEach(provider => {
414
- providers.push(provider.package);
415
- Object.entries(provider.rules).forEach(([name, recipe]) => {
416
- switch (name) {
417
- case 'capture':
418
- {
419
- handleCapture(provider.package, recipe);
420
- return;
421
- }
422
- }
423
- });
424
- });
425
- savePerKey('local', tagStorage, providers, providersKey);
426
- };
427
-
428
- const handleInit = preferences => {
429
- const success = setPreferences(preferences);
430
-
431
- if (!success) {
432
- return;
433
- }
434
-
435
- let url = `${getInitURL()}`;
436
-
437
- if (preferences.disableConsentCheck) {
438
- url = `${url}?consentDisabled=true`;
439
- saveConsent({
440
- all: true
441
- });
442
- }
443
-
444
- getRequest(url).then(result => {
445
- if (!result) {
446
- console.log('init failed');
447
- return;
448
- }
449
-
450
- handleManifest(result.result);
451
- }).catch(info);
452
- };
453
-
454
- const handleUser = (key, value) => {
455
- if (!key || !value) {
456
- console.error('Key or Value is missing in user api.');
457
- return;
458
- }
459
-
460
- const payload = {
461
- key,
462
- value
463
- };
464
- postRequest(getUserURL(), payload).catch(info);
465
- };
466
-
467
- const init = preferences => {
468
- handleInit(preferences);
469
- };
470
- const tag = (name, data, providers) => {
471
- handleTag(name, data, providers);
472
- };
473
- const consent = consent => {
474
- handleConsent(consent);
475
- };
476
- const user = (key, value) => {
477
- handleUser(key, value);
478
- };
479
-
480
- exports.consent = consent;
481
- exports.init = init;
482
- exports.tag = tag;
483
- exports.user = user;
484
-
485
- Object.defineProperty(exports, '__esModule', { value: true });
486
-
487
- }));