@blotoutio/edgetag-sdk-browser 0.6.8 → 0.7.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.
Files changed (2) hide show
  1. package/index.js +222 -167
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -38,132 +38,6 @@
38
38
  });
39
39
  }
40
40
 
41
- const tagStorage = 'edgeTag';
42
- const consentKey = 'consent';
43
- const keyPrefix = `_worker`;
44
-
45
- const initKey = `${keyPrefix}Store`;
46
- const getCookieValue = (key) => {
47
- if (!document || !document.cookie) {
48
- return '';
49
- }
50
- const name = `${key}=`;
51
- const decodedCookie = decodeURIComponent(document.cookie);
52
- const ca = decodedCookie.split(';');
53
- for (let i = 0; i < ca.length; i++) {
54
- let c = ca[i];
55
- while (c.charAt(0) === ' ') {
56
- c = c.substring(1);
57
- }
58
- if (c.indexOf(name) === 0) {
59
- return c.substring(name.length, c.length);
60
- }
61
- }
62
- return '';
63
- };
64
- const saveDataPerKey = (persistType, provider, value, key) => {
65
- const storage = getData$1(persistType);
66
- if (!storage['data']) {
67
- storage['data'] = {};
68
- }
69
- if (!storage['data'][provider]) {
70
- storage['data'][provider] = {};
71
- }
72
- storage['data'][provider][key] = value;
73
- saveData(persistType, storage);
74
- };
75
- const savePerKey = (persistType, provider, value, key) => {
76
- const storage = getData$1(persistType);
77
- if (!storage[provider]) {
78
- storage[provider] = {};
79
- }
80
- storage[provider][key] = value;
81
- saveData(persistType, storage);
82
- };
83
- const getDataPerKey = (persistType, provider, key) => {
84
- const storage = getData$1(persistType);
85
- if (!storage[provider]) {
86
- return undefined;
87
- }
88
- return storage[provider][key];
89
- };
90
- const saveData = (persistType, value, key = initKey) => {
91
- if (persistType === 'session') {
92
- saveSession(value, key);
93
- return;
94
- }
95
- saveLocal(value, key);
96
- };
97
- const getData$1 = (persistType, key = initKey) => {
98
- if (persistType === 'session') {
99
- return getSession(key);
100
- }
101
- return getLocal(key);
102
- };
103
- const saveKV = (data) => {
104
- let currentSession = getData$1('session');
105
- if (!currentSession) {
106
- currentSession = {};
107
- }
108
- if (!currentSession['kv']) {
109
- currentSession['kv'] = {};
110
- }
111
- currentSession['kv'] = Object.assign(Object.assign({}, currentSession['kv']), data);
112
- saveData('session', currentSession);
113
- };
114
- const saveLocal = (value, key) => {
115
- try {
116
- if (!localStorage) {
117
- return;
118
- }
119
- localStorage.setItem(key, JSON.stringify(value));
120
- }
121
- catch (_a) {
122
- console.log('Local storage not supported');
123
- }
124
- };
125
- const getLocal = (key) => {
126
- try {
127
- if (!localStorage) {
128
- return {};
129
- }
130
- const data = localStorage.getItem(key);
131
- if (!data) {
132
- return {};
133
- }
134
- return JSON.parse(data) || {};
135
- }
136
- catch (_a) {
137
- return {};
138
- }
139
- };
140
- const saveSession = (value, key) => {
141
- try {
142
- if (!sessionStorage) {
143
- return;
144
- }
145
- sessionStorage.setItem(key, JSON.stringify(value));
146
- }
147
- catch (_a) {
148
- console.log('Session storage not supported');
149
- }
150
- };
151
- const getSession = (key) => {
152
- try {
153
- if (!sessionStorage) {
154
- return {};
155
- }
156
- const data = sessionStorage.getItem(key);
157
- if (!data) {
158
- return {};
159
- }
160
- return JSON.parse(data) || {};
161
- }
162
- catch (_a) {
163
- return {};
164
- }
165
- };
166
-
167
41
  let endpointUrl = '';
168
42
  const generateUrl = (path) => {
169
43
  const endpoint = getUrl();
@@ -224,13 +98,18 @@
224
98
  }
225
99
  providersPackages[provider.name] = provider;
226
100
  });
227
- if (window && Array.isArray(window.edgetagProviders)) {
228
- window.edgetagProviders.forEach((provider) => {
229
- if (!provider.name) {
230
- return;
231
- }
232
- providersPackages[provider.name] = provider;
233
- });
101
+ try {
102
+ if (window && Array.isArray(window.edgetagProviders)) {
103
+ window.edgetagProviders.forEach((provider) => {
104
+ if (!provider.name) {
105
+ return;
106
+ }
107
+ providersPackages[provider.name] = provider;
108
+ });
109
+ }
110
+ }
111
+ catch (_b) {
112
+ // do nothing
234
113
  }
235
114
  setUrl(preferences.edgeURL);
236
115
  return true;
@@ -247,16 +126,21 @@
247
126
  };
248
127
 
249
128
  const getUserAgent = () => {
250
- const nav = navigator;
251
- let ua = nav.userAgent;
252
- ua += nav.brave ? `${ua} Brave` : '';
253
- return ua;
129
+ try {
130
+ const nav = navigator;
131
+ let ua = nav.userAgent;
132
+ ua += nav.brave ? `${ua} Brave` : '';
133
+ return ua;
134
+ }
135
+ catch (_a) {
136
+ return '';
137
+ }
138
+ };
139
+ const isProviderIncluded = (providers, providerValue) => {
140
+ return (providerValue || (providers['all'] === true && providerValue === undefined));
254
141
  };
255
142
  const allowTag = (providers) => {
256
- if (!allowProviders(providers)) {
257
- return false;
258
- }
259
- const consent = getDataPerKey('local', tagStorage, consentKey);
143
+ const consent = getConsent();
260
144
  if (isConsentDisabled()) {
261
145
  return true;
262
146
  }
@@ -271,19 +155,15 @@
271
155
  }
272
156
  const allProviders = getAllowedProviders();
273
157
  for (const provider of allProviders) {
274
- const tagProviderSetting = providers[provider];
275
- if (tagProviderSetting ||
276
- (providers['all'] === true && tagProviderSetting === undefined) ||
277
- (providers['all'] === false && tagProviderSetting === true)) {
278
- if (consent[provider]) {
279
- return true;
280
- }
158
+ if (isProviderIncluded(providers, !!providers[provider]) &&
159
+ consent[provider]) {
160
+ return true;
281
161
  }
282
162
  }
283
163
  return false;
284
164
  };
285
165
  const allowProviders = (providers) => {
286
- if (!providers || Object.keys(providers).length === 0) {
166
+ if (!providers || Object.keys(providers).length === 0 || providers['all']) {
287
167
  return true;
288
168
  }
289
169
  const allowedProvider = getAllowedProviders();
@@ -323,7 +203,7 @@
323
203
  return false;
324
204
  }
325
205
  }
326
- const consent = getDataPerKey('local', tagStorage, consentKey);
206
+ const consent = getConsent();
327
207
  if (isConsentDisabled()) {
328
208
  return true;
329
209
  }
@@ -336,6 +216,148 @@
336
216
  return consent[providerId];
337
217
  };
338
218
 
219
+ const tagStorage = 'edgeTag';
220
+ const consentKey = 'consent';
221
+ const keyPrefix = `_worker`;
222
+
223
+ const initKey = `${keyPrefix}Store`;
224
+ const getCookieValue = (key) => {
225
+ try {
226
+ if (!document || !document.cookie) {
227
+ return '';
228
+ }
229
+ const name = `${key}=`;
230
+ const decodedCookie = decodeURIComponent(document.cookie);
231
+ const ca = decodedCookie.split(';');
232
+ for (let i = 0; i < ca.length; i++) {
233
+ let c = ca[i];
234
+ while (c.charAt(0) === ' ') {
235
+ c = c.substring(1);
236
+ }
237
+ if (c.indexOf(name) === 0) {
238
+ return c.substring(name.length, c.length);
239
+ }
240
+ }
241
+ return '';
242
+ }
243
+ catch (_a) {
244
+ return '';
245
+ }
246
+ };
247
+ const saveDataPerKey = (persistType, provider, value, key) => {
248
+ const storage = getData$1(persistType);
249
+ if (!storage['data']) {
250
+ storage['data'] = {};
251
+ }
252
+ if (!storage['data'][provider]) {
253
+ storage['data'][provider] = {};
254
+ }
255
+ storage['data'][provider][key] = value;
256
+ saveData(persistType, storage);
257
+ };
258
+ const savePerKey = (persistType, provider, value, key) => {
259
+ const storage = getData$1(persistType);
260
+ if (!storage[provider]) {
261
+ storage[provider] = {};
262
+ }
263
+ storage[provider][key] = value;
264
+ saveData(persistType, storage);
265
+ };
266
+ const getDataPerKey = (persistType, provider, key) => {
267
+ const storage = getData$1(persistType);
268
+ if (!storage[provider]) {
269
+ return undefined;
270
+ }
271
+ return storage[provider][key];
272
+ };
273
+ const saveData = (persistType, value, key = initKey) => {
274
+ if (persistType === 'session') {
275
+ saveSession(value, key);
276
+ return;
277
+ }
278
+ saveLocal(value, key);
279
+ };
280
+ const getData$1 = (persistType, key = initKey) => {
281
+ if (persistType === 'session') {
282
+ return getSession(key);
283
+ }
284
+ return getLocal(key);
285
+ };
286
+ const saveKV = (data) => {
287
+ let currentSession = getData$1('session');
288
+ if (!currentSession) {
289
+ currentSession = {};
290
+ }
291
+ if (!currentSession['kv']) {
292
+ currentSession['kv'] = {};
293
+ }
294
+ currentSession['kv'] = Object.assign(Object.assign({}, currentSession['kv']), data);
295
+ saveData('session', currentSession);
296
+ };
297
+ const saveLocal = (value, key) => {
298
+ try {
299
+ if (!localStorage) {
300
+ return;
301
+ }
302
+ localStorage.setItem(key, JSON.stringify(value));
303
+ }
304
+ catch (_a) {
305
+ console.log('Local storage not supported');
306
+ }
307
+ };
308
+ const getLocal = (key) => {
309
+ try {
310
+ if (!localStorage) {
311
+ return {};
312
+ }
313
+ const data = localStorage.getItem(key);
314
+ if (!data) {
315
+ return {};
316
+ }
317
+ return JSON.parse(data) || {};
318
+ }
319
+ catch (_a) {
320
+ return {};
321
+ }
322
+ };
323
+ const saveSession = (value, key) => {
324
+ try {
325
+ if (!sessionStorage) {
326
+ return;
327
+ }
328
+ sessionStorage.setItem(key, JSON.stringify(value));
329
+ }
330
+ catch (_a) {
331
+ console.log('Session storage not supported');
332
+ }
333
+ };
334
+ const getSession = (key) => {
335
+ try {
336
+ if (!sessionStorage) {
337
+ return {};
338
+ }
339
+ const data = sessionStorage.getItem(key);
340
+ if (!data) {
341
+ return {};
342
+ }
343
+ return JSON.parse(data) || {};
344
+ }
345
+ catch (_a) {
346
+ return {};
347
+ }
348
+ };
349
+
350
+ let initUserId = '';
351
+ const handleGetUserId = () => {
352
+ if (initUserId) {
353
+ return initUserId;
354
+ }
355
+ return getCookieValue('tag_user_id');
356
+ };
357
+ const setUserId = (userId) => {
358
+ initUserId = userId;
359
+ };
360
+
339
361
  const beacon = (url, payload) => {
340
362
  let blob;
341
363
  if (payload) {
@@ -349,6 +371,7 @@
349
371
  headers: {
350
372
  'Content-type': 'application/json; charset=utf-8',
351
373
  Accept: 'application/json; charset=utf-8',
374
+ EdgeTagUserId: handleGetUserId(),
352
375
  },
353
376
  body: JSON.stringify(payload),
354
377
  credentials: 'include',
@@ -367,7 +390,14 @@
367
390
  });
368
391
  });
369
392
  const getStandardPayload = (payload) => {
370
- const data = Object.assign({ pageUrl: window.location.href, userAgent: getUserAgent() }, (payload || {}));
393
+ let pageUrl;
394
+ try {
395
+ pageUrl = window.location.href;
396
+ }
397
+ catch (_a) {
398
+ pageUrl = '';
399
+ }
400
+ const data = Object.assign({ pageUrl, userAgent: getUserAgent() }, (payload || {}));
371
401
  let storage = {};
372
402
  const session = getData$1('session');
373
403
  if (session) {
@@ -409,7 +439,9 @@
409
439
  const info = (data) => {
410
440
  };
411
441
 
442
+ let memoryConsent;
412
443
  const saveConsent = (consent) => {
444
+ setConsent(consent);
413
445
  savePerKey('local', tagStorage, consent, consentKey);
414
446
  };
415
447
  const handleConsent = (consent) => {
@@ -419,6 +451,16 @@
419
451
  saveConsent(consent);
420
452
  postRequest(getConsentURL(), payload).catch(info);
421
453
  };
454
+ const setConsent = (newConsent) => {
455
+ memoryConsent = newConsent;
456
+ };
457
+ const getConsent = () => {
458
+ const storageConsent = getDataPerKey('local', tagStorage, consentKey);
459
+ if (storageConsent) {
460
+ return storageConsent;
461
+ }
462
+ return memoryConsent;
463
+ };
422
464
 
423
465
  // Unique ID creation requires a high quality random # generator. In the browser we therefore
424
466
  // require the crypto API and do not support built-in fallback to lower quality random number
@@ -912,10 +954,6 @@
912
954
  return `${btoa(name)}-${v4()}-${time}`;
913
955
  };
914
956
 
915
- const handleGetUserId = () => {
916
- return getCookieValue('tag_user_id');
917
- };
918
-
919
957
  const manifestVariables = {};
920
958
  const addProviderVariable = (name, variables) => {
921
959
  manifestVariables[name] = variables;
@@ -934,6 +972,7 @@
934
972
  // eslint-disable-next-line @typescript-eslint/ban-ts-comment
935
973
  // @ts-ignore
936
974
  stubs.forEach((stub) => api$1[stub.name](...(stub.arguments || [])));
975
+ stubs = [];
937
976
  }
938
977
  catch (e) {
939
978
  console.error(e);
@@ -942,6 +981,7 @@
942
981
 
943
982
  const sendTag = ({ eventName, eventId, data, providerData, providers, options, }) => {
944
983
  if (!allowProviders(providers)) {
984
+ console.log('Provider is not allowed.');
945
985
  return;
946
986
  }
947
987
  const payload = {
@@ -964,6 +1004,10 @@
964
1004
  });
965
1005
  return;
966
1006
  }
1007
+ if (!allowProviders(providers)) {
1008
+ console.log('Provider is not allowed.');
1009
+ return;
1010
+ }
967
1011
  if (!allowTag(providers)) {
968
1012
  console.log('No consent');
969
1013
  return;
@@ -1028,7 +1072,12 @@
1028
1072
  handleData({ [`${provider}::${key}`]: value });
1029
1073
  };
1030
1074
  const handleCaptureQuery = (provider, key, persistType) => {
1031
- if (!window) {
1075
+ try {
1076
+ if (!window) {
1077
+ return;
1078
+ }
1079
+ }
1080
+ catch (_a) {
1032
1081
  return;
1033
1082
  }
1034
1083
  const params = new URLSearchParams(window.location.search);
@@ -1047,19 +1096,24 @@
1047
1096
  };
1048
1097
  const handleCaptureStorage = (provider, key, persistType, location) => {
1049
1098
  let data;
1050
- switch (location) {
1051
- case 'cookie': {
1052
- data = getCookieValue(key);
1053
- break;
1054
- }
1055
- case 'local': {
1056
- data = getData$1('local', key);
1057
- break;
1058
- }
1059
- case 'session': {
1060
- data = getData$1('session', key);
1099
+ try {
1100
+ switch (location) {
1101
+ case 'cookie': {
1102
+ data = getCookieValue(key);
1103
+ break;
1104
+ }
1105
+ case 'local': {
1106
+ data = localStorage.getItem(key);
1107
+ break;
1108
+ }
1109
+ case 'session': {
1110
+ data = sessionStorage.getItem(key);
1111
+ }
1061
1112
  }
1062
1113
  }
1114
+ catch (_a) {
1115
+ return;
1116
+ }
1063
1117
  if (!data) {
1064
1118
  return;
1065
1119
  }
@@ -1125,6 +1179,7 @@
1125
1179
  saveConsent({ all: true });
1126
1180
  }
1127
1181
  if (preferences.userId) {
1182
+ setUserId(preferences.userId);
1128
1183
  url.searchParams.set('userId', preferences.userId);
1129
1184
  }
1130
1185
  getRequest(url.href)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@blotoutio/edgetag-sdk-browser",
3
- "version": "0.6.8",
3
+ "version": "0.7.0",
4
4
  "description": "Browser SDK for EdgeTag",
5
5
  "author": "Blotout",
6
6
  "license": "MIT",