@snowplow/react-native-tracker 0.2.0 → 1.1.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/dist/index.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import { NativeModules } from 'react-native';
2
2
 
3
3
  /*
4
- * Copyright (c) 2020-2021 Snowplow Analytics Ltd. All rights reserved.
4
+ * Copyright (c) 2020-2022 Snowplow Analytics Ltd. All rights reserved.
5
5
  *
6
6
  * This program is licensed to you under the Apache License Version 2.0,
7
7
  * and you may not use this file except in compliance with the Apache License Version 2.0.
@@ -13,112 +13,388 @@ import { NativeModules } from 'react-native';
13
13
  * See the Apache License Version 2.0 for the specific language governing permissions and limitations there under.
14
14
  */
15
15
  const { RNSnowplowTracker } = NativeModules;
16
+
16
17
  /*
17
- * Initialize a tracker from specified argmap.
18
+ * Copyright (c) 2020-2022 Snowplow Analytics Ltd. All rights reserved.
18
19
  *
19
- * @param argmap - The configuration to use for the tracker instance
20
- * @returns - A promise fullfilled if the tracker is initialized
20
+ * This program is licensed to you under the Apache License Version 2.0,
21
+ * and you may not use this file except in compliance with the Apache License Version 2.0.
22
+ * You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0.
23
+ *
24
+ * Unless required by applicable law or agreed to in writing,
25
+ * software distributed under the Apache License Version 2.0 is distributed on an
26
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
27
+ * See the Apache License Version 2.0 for the specific language governing permissions and limitations there under.
28
+ */
29
+ /**
30
+ * Returns a function that accepts a side-effect function as its argument and subscribes
31
+ * that function to aPromise's fullfillment,
32
+ * and errHandle to aPromise's rejection.
33
+ *
34
+ * @param aPromise - A void Promise
35
+ * @param errHandle - A function to handle the promise being rejected
36
+ * @returns - A function subscribed to the Promise's fullfillment
21
37
  */
22
- function initializeTracker(argmap) {
23
- const defaults = {
24
- method: 'post',
25
- protocol: 'https',
26
- base64Encoded: true,
27
- platformContext: true,
28
- applicationContext: false,
29
- lifecycleEvents: false,
30
- screenContext: true,
31
- sessionContext: true,
32
- foregroundTimeout: 600,
33
- backgroundTimeout: 300,
34
- checkInterval: 15,
35
- installTracking: false
36
- };
37
- const ok = typeof argmap.endpoint !== 'undefined' && typeof argmap.appId !== 'undefined' && typeof argmap.namespace !== 'undefined';
38
- if (ok) {
39
- return Promise.resolve(RNSnowplowTracker.initialize({ ...defaults, ...argmap }));
40
- }
41
- else {
42
- const initReason = 'SnowplowTracker: initialize() requires endpoint, namespace and appId parameter to be set';
43
- return Promise.reject(new Error(initReason));
38
+ function safeWait(aPromise, errHandle) {
39
+ return ((func) => {
40
+ return (...args) => {
41
+ return aPromise.then(() => func(...args)).catch((err) => errHandle(err));
42
+ };
43
+ });
44
+ }
45
+ /**
46
+ * Returns a function that accepts a callback function as its argument and subscribes
47
+ * that function to aPromise's fullfillment,
48
+ * and errHandle to aPromise's rejection.
49
+ *
50
+ * @param aPromise - A void Promise
51
+ * @param errHandle - A function to handle the promise being rejected
52
+ * @returns - A function subscribed to the Promise's fullfillment
53
+ */
54
+ function safeWaitCallback(callPromise, errHandle) {
55
+ return ((func) => {
56
+ return (...args) => {
57
+ return callPromise.then(() => func(...args)).catch((err) => errHandle(err));
58
+ };
59
+ });
60
+ }
61
+ /**
62
+ * Handles an error.
63
+ *
64
+ * @param err - The error to be handled.
65
+ */
66
+ function errorHandler(err) {
67
+ if (__DEV__) {
68
+ console.warn('SnowplowTracker:' + err.message);
69
+ return undefined;
44
70
  }
71
+ return undefined;
45
72
  }
46
- /*
47
- * Sets or updates data for the Subject
73
+ /**
74
+ * Helper to check whether its argument is of object type
48
75
  *
49
- * @param argmap - The subject data to be used
50
- * @returns - A promise that is fullfilled if the Subject data is set
76
+ * @param x - The argument to check.
77
+ * @returns - A boolean
51
78
  */
52
- function setSubjectData(argmap) {
53
- return Promise.resolve(RNSnowplowTracker.setSubjectData(argmap));
79
+ function isObject(x) {
80
+ return Object.prototype.toString.call(x) === '[object Object]';
54
81
  }
82
+
55
83
  /*
56
- * Tracks a ScreenView event
84
+ * Copyright (c) 2020-2022 Snowplow Analytics Ltd. All rights reserved.
57
85
  *
58
- * @param argmap - The ScreenView event's parameters
59
- * @param ctxt - An array of contexts to be attached to the event
60
- * @returns - A promise that is fullfilled if the event is tracked successfully
86
+ * This program is licensed to you under the Apache License Version 2.0,
87
+ * and you may not use this file except in compliance with the Apache License Version 2.0.
88
+ * You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0.
89
+ *
90
+ * Unless required by applicable law or agreed to in writing,
91
+ * software distributed under the Apache License Version 2.0 is distributed on an
92
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
93
+ * See the Apache License Version 2.0 for the specific language governing permissions and limitations there under.
61
94
  */
62
- function trackScreenViewEvent(argmap, ctxt = []) {
63
- if (typeof argmap.screenName !== 'undefined') {
64
- return Promise.resolve(RNSnowplowTracker.trackScreenViewEvent(argmap, ctxt));
95
+ const logMessages = {
96
+ // configuration errors
97
+ namespace: 'namespace parameter is required to be set',
98
+ endpoint: 'endpoint parameter is required to be set',
99
+ network: 'networkConfig is invalid',
100
+ tracker: 'trackerConfig is invalid',
101
+ session: 'sessionConfig is invalid',
102
+ emitter: 'emitterConfig is invalid',
103
+ subject: 'subjectConfig is invalid',
104
+ gdpr: 'gdprConfig is invalid',
105
+ gc: 'gcConfig is invalid',
106
+ // event errors
107
+ context: 'invalid contexts parameter',
108
+ selfDesc: 'selfDescribing event requires schema and data parameters to be set',
109
+ evType: 'event argument can only be an object',
110
+ screenViewReq: 'screenView event requires name as string parameter to be set',
111
+ structuredReq: 'structured event requires category and action parameters to be set',
112
+ pageviewReq: 'pageView event requires pageUrl parameter to be set',
113
+ timingReq: 'timing event requires category, variable and timing parameters to be set',
114
+ consentGReq: 'consentGranted event requires expiry, documentId and version parameters to be set',
115
+ consentWReq: 'consentWithdrawn event requires all, documentId and version parameters to be set',
116
+ ecomReq: 'ecommerceTransaction event requires orderId, totalValue to be set and items to be an array of valid ecommerceItems',
117
+ deepLinkReq: 'deepLinkReceived event requires the url parameter to be set',
118
+ messageNotificationReq: 'messageNotification event requires title, body, and trigger parameters to be set',
119
+ // global contexts errors
120
+ gcTagType: 'tag argument is required to be a string',
121
+ gcType: 'global context argument is invalid',
122
+ // api error prefix
123
+ createTracker: 'createTracker:',
124
+ removeTracker: 'removeTracker: trackerNamespace can only be a string',
125
+ // methods
126
+ trackSelfDesc: 'trackSelfDescribingEvent:',
127
+ trackScreenView: 'trackScreenViewEvent:',
128
+ trackStructured: 'trackStructuredEvent:',
129
+ trackPageView: 'trackPageViewEvent:',
130
+ trackTiming: 'trackTimingEvent:',
131
+ trackConsentGranted: 'trackConsentGranted:',
132
+ trackConsentWithdrawn: 'trackConsentWithdrawn:',
133
+ trackEcommerceTransaction: 'trackEcommerceTransaction:',
134
+ trackDeepLinkReceived: 'trackDeepLinkReceivedEvent:',
135
+ trackMessageNotification: 'trackMessageNotificationEvent:',
136
+ removeGlobalContexts: 'removeGlobalContexts:',
137
+ addGlobalContexts: 'addGlobalContexts:',
138
+ // setters
139
+ setUserId: 'setUserId: userId can only be a string or null',
140
+ setNetworkUserId: 'setNetworkUserId: networkUserId can only be a string(UUID) or null',
141
+ setDomainUserId: 'setDomainUserId: domainUserId can only be a string(UUID) or null',
142
+ setIpAddress: 'setIpAddress: ipAddress can only be a string or null',
143
+ setUseragent: 'setUseragent: useragent can only be a string or null',
144
+ setTimezone: 'setTimezone: timezone can only be a string or null',
145
+ setLanguage: 'setLanguage: language can only be a string or null',
146
+ setScreenResolution: 'setScreenResolution: screenResolution can only be of ScreenSize type or null',
147
+ setScreenViewport: 'setScreenViewport: screenViewport can only be of ScreenSize type or null',
148
+ setColorDepth: 'setColorDepth: colorDepth can only be a number(integer) or null',
149
+ setSubjectData: 'setSubjectData:',
150
+ };
151
+
152
+ /*
153
+ * Copyright (c) 2020-2022 Snowplow Analytics Ltd. All rights reserved.
154
+ *
155
+ * This program is licensed to you under the Apache License Version 2.0,
156
+ * and you may not use this file except in compliance with the Apache License Version 2.0.
157
+ * You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0.
158
+ *
159
+ * Unless required by applicable law or agreed to in writing,
160
+ * software distributed under the Apache License Version 2.0 is distributed on an
161
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
162
+ * See the Apache License Version 2.0 for the specific language governing permissions and limitations there under.
163
+ */
164
+ /**
165
+ * Validates whether an object is valid self-describing
166
+ *
167
+ * @param sd {Object} - the object to validate
168
+ * @returns - boolean
169
+ */
170
+ function isValidSD(sd) {
171
+ return isObject(sd)
172
+ && typeof sd.schema === 'string'
173
+ && isObject(sd.data);
174
+ }
175
+ /**
176
+ * Validates whether an object is a valid array of contexts
177
+ *
178
+ * @param contexts {Object} - the object to validate
179
+ * @returns - boolean promise
180
+ */
181
+ function validateContexts(contexts) {
182
+ const isValid = Object.prototype.toString.call(contexts) === '[object Array]'
183
+ && contexts
184
+ .map((c) => isValidSD(c))
185
+ .reduce((acc, curr) => acc !== false && curr, true);
186
+ if (!isValid) {
187
+ return Promise.reject(new Error(logMessages.context));
65
188
  }
66
- else {
67
- const reason = 'SnowplowTracker: trackScreenViewEvent() requires screenName parameter to be set';
68
- return Promise.reject(new Error(reason));
189
+ return Promise.resolve(true);
190
+ }
191
+ /**
192
+ * Validates whether an object is valid self describing
193
+ *
194
+ * @param argmap {Object} - the object to validate
195
+ * @returns - boolean promise
196
+ */
197
+ function validateSelfDesc(argmap) {
198
+ if (!isValidSD(argmap)) {
199
+ return Promise.reject(new Error(logMessages.selfDesc));
69
200
  }
201
+ return Promise.resolve(true);
70
202
  }
71
- /*
72
- * Tracks a SelfDescribing event
203
+ /**
204
+ * Validates a screen view event
73
205
  *
74
- * @param argmap - The SelfDescribing event's parameters
75
- * @param ctxt - An array of contexts to be attached to the event
76
- * @returns - A promise that is fullfilled if the event is tracked successfully
206
+ * @param argmap {Object} - the object to validate
207
+ * @returns - boolean promise
77
208
  */
78
- function trackSelfDescribingEvent(argmap, ctxt = []) {
79
- if (typeof argmap.schema !== 'undefined' && typeof argmap.data !== 'undefined') {
80
- return Promise.resolve(RNSnowplowTracker.trackSelfDescribingEvent(argmap, ctxt));
209
+ function validateScreenView(argmap) {
210
+ // validate type
211
+ if (!isObject(argmap)) {
212
+ return Promise.reject(new Error(logMessages.evType));
81
213
  }
82
- else {
83
- const reason = 'SnowplowTracker: trackSelfDescribingEvent() requires schema and data parameters to be set';
84
- return Promise.reject(new Error(reason));
214
+ // validate required props
215
+ if (typeof argmap.name !== 'string') {
216
+ return Promise.reject(new Error(logMessages.screenViewReq));
85
217
  }
218
+ return Promise.resolve(true);
86
219
  }
87
- /*
88
- * Tracks a Structured event
220
+ /**
221
+ * Validates a structured event
89
222
  *
90
- * @param argmap - The Structured event's parameters
91
- * @param ctxt - An array of contexts to be attached to the event
92
- * @returns - A promise that is fullfilled if the event is tracked successfully
223
+ * @param argmap {Object} - the object to validate
224
+ * @returns - boolean promise
93
225
  */
94
- function trackStructuredEvent(argmap, ctxt = []) {
95
- if (typeof argmap.category !== 'undefined' && typeof argmap.action !== 'undefined') {
96
- return Promise.resolve(RNSnowplowTracker.trackStructuredEvent(argmap, ctxt));
226
+ function validateStructured(argmap) {
227
+ // validate type
228
+ if (!isObject(argmap)) {
229
+ return Promise.reject(new Error(logMessages.evType));
97
230
  }
98
- else {
99
- const reason = 'SnowplowTracker: trackStructuredEvent() requires category and action parameters to be set';
100
- return Promise.reject(new Error(reason));
231
+ // validate required props
232
+ if (typeof argmap.category !== 'string'
233
+ || typeof argmap.action !== 'string') {
234
+ return Promise.reject(new Error(logMessages.structuredReq));
101
235
  }
236
+ return Promise.resolve(true);
102
237
  }
103
- /*
104
- * Tracks a PageView event
238
+ /**
239
+ * Validates a page-view event
240
+ *
241
+ * @param argmap {Object} - the object to validate
242
+ * @returns - boolean promise
243
+ */
244
+ function validatePageView(argmap) {
245
+ // validate type
246
+ if (!isObject(argmap)) {
247
+ return Promise.reject(new Error(logMessages.evType));
248
+ }
249
+ // validate required props
250
+ if (typeof argmap.pageUrl !== 'string') {
251
+ return Promise.reject(new Error(logMessages.pageviewReq));
252
+ }
253
+ return Promise.resolve(true);
254
+ }
255
+ /**
256
+ * Validates a timing event
257
+ *
258
+ * @param argmap {Object} - the object to validate
259
+ * @returns - boolean promise
260
+ */
261
+ function validateTiming(argmap) {
262
+ // validate type
263
+ if (!isObject(argmap)) {
264
+ return Promise.reject(new Error(logMessages.evType));
265
+ }
266
+ // validate required props
267
+ if (typeof argmap.category !== 'string'
268
+ || typeof argmap.variable !== 'string'
269
+ || typeof argmap.timing !== 'number') {
270
+ return Promise.reject(new Error(logMessages.timingReq));
271
+ }
272
+ return Promise.resolve(true);
273
+ }
274
+ /**
275
+ * Validates a consent-granted event
276
+ *
277
+ * @param argmap {Object} - the object to validate
278
+ * @returns - boolean promise
279
+ */
280
+ function validateConsentGranted(argmap) {
281
+ // validate type
282
+ if (!isObject(argmap)) {
283
+ return Promise.reject(new Error(logMessages.evType));
284
+ }
285
+ // validate required props
286
+ if (typeof argmap.expiry !== 'string'
287
+ || typeof argmap.documentId !== 'string'
288
+ || typeof argmap.version !== 'string') {
289
+ return Promise.reject(new Error(logMessages.consentGReq));
290
+ }
291
+ return Promise.resolve(true);
292
+ }
293
+ /**
294
+ * Validates a consent-withdrawn event
295
+ *
296
+ * @param argmap {Object} - the object to validate
297
+ * @returns - boolean promise
298
+ */
299
+ function validateConsentWithdrawn(argmap) {
300
+ // validate type
301
+ if (!isObject(argmap)) {
302
+ return Promise.reject(new Error(logMessages.evType));
303
+ }
304
+ // validate required props
305
+ if (typeof argmap.all !== 'boolean'
306
+ || typeof argmap.documentId !== 'string'
307
+ || typeof argmap.version !== 'string') {
308
+ return Promise.reject(new Error(logMessages.consentWReq));
309
+ }
310
+ return Promise.resolve(true);
311
+ }
312
+ /**
313
+ * Validates a deep link received event
105
314
  *
106
- * @param argmap - The PageView event's parameters
107
- * @param ctxt - An array of contexts to be attached to the event
108
- * @returns - A promise that is fullfilled if the event is tracked successfully
315
+ * @param argmap {Object} - the object to validate
316
+ * @returns - boolean promise
109
317
  */
110
- function trackPageViewEvent(argmap, ctxt = []) {
111
- if (typeof argmap.pageUrl !== 'undefined') {
112
- return Promise.resolve(RNSnowplowTracker.trackPageViewEvent(argmap, ctxt));
318
+ function validateDeepLinkReceived(argmap) {
319
+ // validate type
320
+ if (!isObject(argmap)) {
321
+ return Promise.reject(new Error(logMessages.evType));
113
322
  }
114
- else {
115
- const reason = 'SnowplowTracker: trackPageViewEvent() requires pageUrl parameter to be set';
116
- return Promise.reject(new Error(reason));
323
+ // validate required props
324
+ if (typeof argmap.url !== 'string') {
325
+ return Promise.reject(new Error(logMessages.deepLinkReq));
117
326
  }
327
+ return Promise.resolve(true);
328
+ }
329
+ /**
330
+ * Validates a message notification event
331
+ *
332
+ * @param argmap {Object} - the object to validate
333
+ * @returns - boolean promise
334
+ */
335
+ function validateMessageNotification(argmap) {
336
+ // validate type
337
+ if (!isObject(argmap)) {
338
+ return Promise.reject(new Error(logMessages.evType));
339
+ }
340
+ // validate required props
341
+ if (typeof argmap.title !== 'string'
342
+ || typeof argmap.body !== 'string'
343
+ || typeof argmap.trigger !== 'string'
344
+ || !['push', 'location', 'calendar', 'timeInterval', 'other'].includes(argmap.trigger)) {
345
+ return Promise.reject(new Error(logMessages.messageNotificationReq));
346
+ }
347
+ return Promise.resolve(true);
348
+ }
349
+ /**
350
+ * Validates whether an object is valid ecommerce-item
351
+ *
352
+ * @param item {Object} - the object to validate
353
+ * @returns - boolean
354
+ */
355
+ function isValidEcomItem(item) {
356
+ if (isObject(item)
357
+ && typeof item.sku === 'string'
358
+ && typeof item.price === 'number'
359
+ && typeof item.quantity === 'number') {
360
+ return true;
361
+ }
362
+ return false;
363
+ }
364
+ /**
365
+ * Validates an array of ecommerce-items
366
+ *
367
+ * @param items {Object} - the object to validate
368
+ * @returns - boolean promise
369
+ */
370
+ function validItemsArg(items) {
371
+ return Object.prototype.toString.call(items) === '[object Array]'
372
+ && items
373
+ .map((i) => isValidEcomItem(i))
374
+ .reduce((acc, curr) => acc !== false && curr, true);
375
+ }
376
+ /**
377
+ * Validates an ecommerce-transaction event
378
+ *
379
+ * @param argmap {Object} - the object to validate
380
+ * @returns - boolean promise
381
+ */
382
+ function validateEcommerceTransaction(argmap) {
383
+ // validate type
384
+ if (!isObject(argmap)) {
385
+ return Promise.reject(new Error(logMessages.evType));
386
+ }
387
+ // validate required props
388
+ if (typeof argmap.orderId !== 'string'
389
+ || typeof argmap.totalValue !== 'number'
390
+ || !validItemsArg(argmap.items)) {
391
+ return Promise.reject(new Error(logMessages.ecomReq));
392
+ }
393
+ return Promise.resolve(true);
118
394
  }
119
395
 
120
396
  /*
121
- * Copyright (c) 2020-2021 Snowplow Analytics Ltd. All rights reserved.
397
+ * Copyright (c) 2020-2022 Snowplow Analytics Ltd. All rights reserved.
122
398
  *
123
399
  * This program is licensed to you under the Apache License Version 2.0,
124
400
  * and you may not use this file except in compliance with the Apache License Version 2.0.
@@ -129,35 +405,679 @@ function trackPageViewEvent(argmap, ctxt = []) {
129
405
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
130
406
  * See the Apache License Version 2.0 for the specific language governing permissions and limitations there under.
131
407
  */
408
+ /**
409
+ * Configuration properties
410
+ */
411
+ const networkProps = ['endpoint', 'method'];
412
+ const trackerProps = [
413
+ 'appId',
414
+ 'devicePlatform',
415
+ 'base64Encoding',
416
+ 'logLevel',
417
+ 'applicationContext',
418
+ 'platformContext',
419
+ 'geoLocationContext',
420
+ 'sessionContext',
421
+ 'deepLinkContext',
422
+ 'screenContext',
423
+ 'screenViewAutotracking',
424
+ 'lifecycleAutotracking',
425
+ 'installAutotracking',
426
+ 'exceptionAutotracking',
427
+ 'diagnosticAutotracking'
428
+ ];
429
+ const sessionProps = [
430
+ 'foregroundTimeout',
431
+ 'backgroundTimeout'
432
+ ];
433
+ const emitterProps = [
434
+ 'bufferOption',
435
+ 'emitRange',
436
+ 'threadPoolSize',
437
+ 'byteLimitPost',
438
+ 'byteLimitGet',
439
+ ];
440
+ const subjectProps = [
441
+ 'userId',
442
+ 'networkUserId',
443
+ 'domainUserId',
444
+ 'useragent',
445
+ 'ipAddress',
446
+ 'timezone',
447
+ 'language',
448
+ 'screenResolution',
449
+ 'screenViewport',
450
+ 'colorDepth'
451
+ ];
452
+ const gdprProps = [
453
+ 'basisForProcessing',
454
+ 'documentId',
455
+ 'documentVersion',
456
+ 'documentDescription'
457
+ ];
458
+ const gcProps = [
459
+ 'tag',
460
+ 'globalContexts'
461
+ ];
462
+ /**
463
+ * Validates whether an object is of valid configuration given its default keys
464
+ *
465
+ * @param config {Object} - the object to validate
466
+ * @param defaultKeys {Array} - the default keys to validate against
467
+ * @returns - boolean
468
+ */
469
+ function isValidConfig(config, defaultKeys) {
470
+ return Object.keys(config).every(key => defaultKeys.includes(key));
471
+ }
472
+ /**
473
+ * Validates the networkConfig
474
+ *
475
+ * @param config {Object} - the config to validate
476
+ * @returns - boolean
477
+ */
478
+ function isValidNetworkConf(config) {
479
+ if (!isObject(config)
480
+ || !isValidConfig(config, networkProps)
481
+ || typeof config.endpoint !== 'string'
482
+ || !config.endpoint) {
483
+ return false;
484
+ }
485
+ return true;
486
+ }
487
+ /**
488
+ * Validates the trackerConfig
489
+ *
490
+ * @param config {Object} - the config to validate
491
+ * @returns - boolean
492
+ */
493
+ function isValidTrackerConf(config) {
494
+ if (!isObject(config) || !isValidConfig(config, trackerProps)) {
495
+ return false;
496
+ }
497
+ return true;
498
+ }
499
+ /**
500
+ * Validates the sessionConfig
501
+ *
502
+ * @param config {Object} - the config to validate
503
+ * @returns - boolean
504
+ */
505
+ function isValidSessionConf(config) {
506
+ if (!isObject(config)
507
+ || !isValidConfig(config, sessionProps)
508
+ || !sessionProps.every(key => Object.keys(config).includes(key))) {
509
+ return false;
510
+ }
511
+ return true;
512
+ }
513
+ /**
514
+ * Validates the emitterConfig
515
+ *
516
+ * @param config {Object} - the config to validate
517
+ * @returns - boolean
518
+ */
519
+ function isValidEmitterConf(config) {
520
+ if (!isObject(config) || !isValidConfig(config, emitterProps)) {
521
+ return false;
522
+ }
523
+ return true;
524
+ }
525
+ /**
526
+ * Validates whether an object is of ScreenSize type
527
+ *
528
+ * @param arr {Object} - the object to validate
529
+ * @returns - boolean
530
+ */
531
+ function isScreenSize(arr) {
532
+ return Array.isArray(arr)
533
+ && arr.length === 2
534
+ && arr.every((n) => typeof n === 'number');
535
+ }
536
+ /**
537
+ * Validates the subjectConfig
538
+ *
539
+ * @param config {Object} - the config to validate
540
+ * @returns - boolean
541
+ */
542
+ function isValidSubjectConf(config) {
543
+ if (!isObject(config) || !isValidConfig(config, subjectProps)) {
544
+ return false;
545
+ }
546
+ // validating ScreenSize here to simplify array handling in bridge
547
+ if (Object.prototype.hasOwnProperty.call(config, 'screenResolution')
548
+ && config.screenResolution !== null
549
+ && !isScreenSize(config.screenResolution)) {
550
+ return false;
551
+ }
552
+ if (Object.prototype.hasOwnProperty.call(config, 'screenViewport')
553
+ && config.screenViewport !== null
554
+ && !isScreenSize(config.screenViewport)) {
555
+ return false;
556
+ }
557
+ return true;
558
+ }
559
+ /**
560
+ * Validates the gdprConfig
561
+ *
562
+ * @param config {Object} - the config to validate
563
+ * @returns - boolean
564
+ */
565
+ function isValidGdprConf(config) {
566
+ if (!isObject(config)
567
+ || !isValidConfig(config, gdprProps)
568
+ || !gdprProps.every(key => Object.keys(config).includes(key))
569
+ || !['consent', 'contract', 'legal_obligation', 'legitimate_interests', 'public_task', 'vital_interests'].includes(config.basisForProcessing)) {
570
+ return false;
571
+ }
572
+ return true;
573
+ }
574
+ /**
575
+ * Validates whether an object is of GlobalContext type
576
+ *
577
+ * @param gc {Object} - the object to validate
578
+ * @returns - boolean
579
+ */
580
+ function isValidGC(gc) {
581
+ return isObject(gc)
582
+ && isValidConfig(gc, gcProps)
583
+ && typeof gc.tag === 'string'
584
+ && Array.isArray(gc.globalContexts)
585
+ && gc.globalContexts.every(c => isValidSD(c));
586
+ }
587
+ /**
588
+ * Validates the GCConfig (global contexts)
589
+ *
590
+ * @param config {Object} - the config to validate
591
+ * @returns - boolean
592
+ */
593
+ function isValidGCConf(config) {
594
+ if (!Array.isArray(config)) {
595
+ return false;
596
+ }
597
+ if (!config.every(gc => isValidGC(gc))) {
598
+ return false;
599
+ }
600
+ return true;
601
+ }
602
+ /**
603
+ * Validates the initTrackerConfiguration
604
+ *
605
+ * @param init {Object} - the config to validate
606
+ * @returns - boolean promise
607
+ */
608
+ function initValidate(init) {
609
+ if (typeof init.namespace !== 'string' || !init.namespace) {
610
+ return Promise.reject(new Error(logMessages.namespace));
611
+ }
612
+ if (!Object.prototype.hasOwnProperty.call(init, 'networkConfig')
613
+ || !isValidNetworkConf(init.networkConfig)) {
614
+ return Promise.reject(new Error(logMessages.network));
615
+ }
616
+ if (Object.prototype.hasOwnProperty.call(init, 'trackerConfig')
617
+ && !isValidTrackerConf(init.trackerConfig)) {
618
+ return Promise.reject(new Error(logMessages.tracker));
619
+ }
620
+ if (Object.prototype.hasOwnProperty.call(init, 'sessionConfig')
621
+ && (!isValidSessionConf(init.sessionConfig))) {
622
+ return Promise.reject(new Error(logMessages.session));
623
+ }
624
+ if (Object.prototype.hasOwnProperty.call(init, 'emitterConfig')
625
+ && !isValidEmitterConf(init.emitterConfig)) {
626
+ return Promise.reject(new Error(logMessages.emitter));
627
+ }
628
+ if (Object.prototype.hasOwnProperty.call(init, 'subjectConfig')
629
+ && !isValidSubjectConf(init.subjectConfig)) {
630
+ return Promise.reject(new Error(logMessages.subject));
631
+ }
632
+ if (Object.prototype.hasOwnProperty.call(init, 'gdprConfig')
633
+ && !isValidGdprConf(init.gdprConfig)) {
634
+ return Promise.reject(new Error(logMessages.gdpr));
635
+ }
636
+ if (Object.prototype.hasOwnProperty.call(init, 'gcConfig')
637
+ && !isValidGCConf(init.gcConfig)) {
638
+ return Promise.reject(new Error(logMessages.gc));
639
+ }
640
+ return Promise.resolve(true);
641
+ }
642
+
132
643
  /*
133
- * Returns a function that accepts a function as its argument and subscribes
134
- * that function to aPromise's fullfillment,
135
- * and errHandle to aPromise's rejection.
644
+ * Copyright (c) 2020-2022 Snowplow Analytics Ltd. All rights reserved.
136
645
  *
137
- * @param aPromise - A Promise
138
- * @param errHandle - A function to handle the promise being rejected
139
- * @returns - A function subscribed to the Promise's fullfillment
646
+ * This program is licensed to you under the Apache License Version 2.0,
647
+ * and you may not use this file except in compliance with the Apache License Version 2.0.
648
+ * You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0.
649
+ *
650
+ * Unless required by applicable law or agreed to in writing,
651
+ * software distributed under the Apache License Version 2.0 is distributed on an
652
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
653
+ * See the Apache License Version 2.0 for the specific language governing permissions and limitations there under.
140
654
  */
141
- function safeWait(aPromise, errHandle) {
142
- return ((func) => {
143
- return (...args) => {
144
- return aPromise.then(() => func(...args)).catch((err) => errHandle(err));
145
- };
655
+ /**
656
+ * Tracks a self-describing event
657
+ *
658
+ * @param namespace {string} - the tracker namespace
659
+ * @param argmap {Object} - the event data
660
+ * @param contexts {Array}- the event contexts
661
+ * @returns {Promise}
662
+ */
663
+ function trackSelfDescribingEvent$1(namespace, argmap, contexts = []) {
664
+ return validateSelfDesc(argmap)
665
+ .then(() => validateContexts(contexts))
666
+ .then(() => RNSnowplowTracker.trackSelfDescribingEvent({
667
+ tracker: namespace,
668
+ eventData: argmap,
669
+ contexts: contexts
670
+ }))
671
+ .catch((error) => {
672
+ throw new Error(`${logMessages.trackSelfDesc} ${error.message}`);
673
+ });
674
+ }
675
+ /**
676
+ * Tracks a screen-view event
677
+ *
678
+ * @param namespace {string} - the tracker namespace
679
+ * @param argmap {Object} - the event data
680
+ * @param contexts {Array}- the event contexts
681
+ * @returns {Promise}
682
+ */
683
+ function trackScreenViewEvent$1(namespace, argmap, contexts = []) {
684
+ return validateScreenView(argmap)
685
+ .then(() => validateContexts(contexts))
686
+ .then(() => RNSnowplowTracker.trackScreenViewEvent({
687
+ tracker: namespace,
688
+ eventData: argmap,
689
+ contexts: contexts
690
+ }))
691
+ .catch((error) => {
692
+ throw new Error(`${logMessages.trackScreenView} ${error.message}`);
693
+ });
694
+ }
695
+ /**
696
+ * Tracks a structured event
697
+ *
698
+ * @param namespace {string} - the tracker namespace
699
+ * @param argmap {Object} - the event data
700
+ * @param contexts {Array}- the event contexts
701
+ * @returns {Promise}
702
+ */
703
+ function trackStructuredEvent$1(namespace, argmap, contexts = []) {
704
+ return validateStructured(argmap)
705
+ .then(() => validateContexts(contexts))
706
+ .then(() => RNSnowplowTracker.trackStructuredEvent({
707
+ tracker: namespace,
708
+ eventData: argmap,
709
+ contexts: contexts
710
+ }))
711
+ .catch((error) => {
712
+ throw new Error(`${logMessages.trackStructured} ${error.message}`);
146
713
  });
147
714
  }
715
+ /**
716
+ * Tracks a page-view event
717
+ *
718
+ * @param namespace {string} - the tracker namespace
719
+ * @param argmap {Object} - the event data
720
+ * @param contexts {Array}- the event contexts
721
+ * @returns {Promise}
722
+ */
723
+ function trackPageViewEvent$1(namespace, argmap, contexts = []) {
724
+ return validatePageView(argmap)
725
+ .then(() => validateContexts(contexts))
726
+ .then(() => RNSnowplowTracker.trackPageViewEvent({
727
+ tracker: namespace,
728
+ eventData: argmap,
729
+ contexts: contexts
730
+ }))
731
+ .catch((error) => {
732
+ throw new Error(`${logMessages.trackPageView} ${error.message}`);
733
+ });
734
+ }
735
+ /**
736
+ * Tracks a timing event
737
+ *
738
+ * @param namespace {string} - the tracker namespace
739
+ * @param argmap {Object} - the event data
740
+ * @param contexts {Array}- the event contexts
741
+ * @returns {Promise}
742
+ */
743
+ function trackTimingEvent$1(namespace, argmap, contexts = []) {
744
+ return validateTiming(argmap)
745
+ .then(() => validateContexts(contexts))
746
+ .then(() => RNSnowplowTracker.trackTimingEvent({
747
+ tracker: namespace,
748
+ eventData: argmap,
749
+ contexts: contexts
750
+ }))
751
+ .catch((error) => {
752
+ throw new Error(`${logMessages.trackTiming} ${error.message}`);
753
+ });
754
+ }
755
+ /**
756
+ * Tracks a consent-granted event
757
+ *
758
+ * @param namespace {string} - the tracker namespace
759
+ * @param argmap {Object} - the event data
760
+ * @param contexts {Array}- the event contexts
761
+ * @returns {Promise}
762
+ */
763
+ function trackConsentGrantedEvent$1(namespace, argmap, contexts = []) {
764
+ return validateConsentGranted(argmap)
765
+ .then(() => validateContexts(contexts))
766
+ .then(() => RNSnowplowTracker.trackConsentGrantedEvent({
767
+ tracker: namespace,
768
+ eventData: argmap,
769
+ contexts: contexts
770
+ }))
771
+ .catch((error) => {
772
+ throw new Error(`${logMessages.trackConsentGranted} ${error.message}`);
773
+ });
774
+ }
775
+ /**
776
+ * Tracks a consent-withdrawn event
777
+ *
778
+ * @param namespace {string} - the tracker namespace
779
+ * @param argmap {Object} - the event data
780
+ * @param contexts {Array}- the event contexts
781
+ * @returns {Promise}
782
+ */
783
+ function trackConsentWithdrawnEvent$1(namespace, argmap, contexts = []) {
784
+ return validateConsentWithdrawn(argmap)
785
+ .then(() => validateContexts(contexts))
786
+ .then(() => RNSnowplowTracker.trackConsentWithdrawnEvent({
787
+ tracker: namespace,
788
+ eventData: argmap,
789
+ contexts: contexts
790
+ }))
791
+ .catch((error) => {
792
+ throw new Error(`${logMessages.trackConsentWithdrawn} ${error.message}`);
793
+ });
794
+ }
795
+ /**
796
+ * Tracks an ecommerce-transaction event
797
+ *
798
+ * @param namespace {string} - the tracker namespace
799
+ * @param argmap {Object} - the event data
800
+ * @param contexts {Array}- the event contexts
801
+ * @returns {Promise}
802
+ */
803
+ function trackEcommerceTransactionEvent$1(namespace, argmap, contexts = []) {
804
+ return validateEcommerceTransaction(argmap)
805
+ .then(() => validateContexts(contexts))
806
+ .then(() => RNSnowplowTracker.trackEcommerceTransactionEvent({
807
+ tracker: namespace,
808
+ eventData: argmap,
809
+ contexts: contexts
810
+ }))
811
+ .catch((error) => {
812
+ throw new Error(`${logMessages.trackEcommerceTransaction} ${error.message}`);
813
+ });
814
+ }
815
+ /**
816
+ * Tracks a deep link received event
817
+ *
818
+ * @param namespace {string} - the tracker namespace
819
+ * @param argmap {Object} - the event data
820
+ * @param contexts {Array}- the event contexts
821
+ * @returns {Promise}
822
+ */
823
+ function trackDeepLinkReceivedEvent$1(namespace, argmap, contexts = []) {
824
+ return validateDeepLinkReceived(argmap)
825
+ .then(() => validateContexts(contexts))
826
+ .then(() => RNSnowplowTracker.trackDeepLinkReceivedEvent({
827
+ tracker: namespace,
828
+ eventData: argmap,
829
+ contexts: contexts
830
+ }))
831
+ .catch((error) => {
832
+ throw new Error(`${logMessages.trackDeepLinkReceived} ${error.message}`);
833
+ });
834
+ }
835
+ /**
836
+ * Tracks a message notification event
837
+ *
838
+ * @param namespace {string} - the tracker namespace
839
+ * @param argmap {Object} - the event data
840
+ * @param contexts {Array}- the event contexts
841
+ * @returns {Promise}
842
+ */
843
+ function trackMessageNotificationEvent$1(namespace, argmap, contexts = []) {
844
+ return validateMessageNotification(argmap)
845
+ .then(() => validateContexts(contexts))
846
+ .then(() => RNSnowplowTracker.trackMessageNotificationEvent({
847
+ tracker: namespace,
848
+ eventData: argmap,
849
+ contexts: contexts
850
+ }))
851
+ .catch((error) => {
852
+ throw new Error(`${logMessages.trackMessageNotification} ${error.message}`);
853
+ });
854
+ }
855
+
148
856
  /*
149
- * Handles an error.
857
+ * Copyright (c) 2020-2022 Snowplow Analytics Ltd. All rights reserved.
150
858
  *
151
- * @param err - The error to be handled.
859
+ * This program is licensed to you under the Apache License Version 2.0,
860
+ * and you may not use this file except in compliance with the Apache License Version 2.0.
861
+ * You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0.
862
+ *
863
+ * Unless required by applicable law or agreed to in writing,
864
+ * software distributed under the Apache License Version 2.0 is distributed on an
865
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
866
+ * See the Apache License Version 2.0 for the specific language governing permissions and limitations there under.
152
867
  */
153
- function errorHandler(err) {
154
- if (__DEV__) {
155
- console.warn(err.message);
868
+ /**
869
+ * Sets the userId of the tracker subject
870
+ *
871
+ * @param namespace {string} - the tracker namespace
872
+ * @param newUid {string | null} - the new userId
873
+ * @returns - Promise
874
+ */
875
+ function setUserId$1(namespace, newUid) {
876
+ if (!(newUid === null || typeof newUid === 'string')) {
877
+ return Promise.reject(new Error(logMessages.setUserId));
878
+ }
879
+ return Promise.resolve(RNSnowplowTracker.setUserId({
880
+ tracker: namespace,
881
+ userId: newUid
882
+ }));
883
+ }
884
+ /**
885
+ * Sets the networkUserId of the tracker subject
886
+ *
887
+ * @param namespace {string} - the tracker namespace
888
+ * @param newNuid {string | null} - the new networkUserId
889
+ * @returns - Promise
890
+ */
891
+ function setNetworkUserId$1(namespace, newNuid) {
892
+ if (!(newNuid === null || typeof newNuid === 'string')) {
893
+ return Promise.reject(new Error(logMessages.setNetworkUserId));
894
+ }
895
+ return Promise.resolve(RNSnowplowTracker.setNetworkUserId({
896
+ tracker: namespace,
897
+ networkUserId: newNuid
898
+ }));
899
+ }
900
+ /**
901
+ * Sets the domainUserId of the tracker subject
902
+ *
903
+ * @param namespace {string} - the tracker namespace
904
+ * @param newDuid {string | null} - the new domainUserId
905
+ * @returns - Promise
906
+ */
907
+ function setDomainUserId$1(namespace, newDuid) {
908
+ if (!(newDuid === null || typeof newDuid === 'string')) {
909
+ return Promise.reject(new Error(logMessages.setDomainUserId));
910
+ }
911
+ return Promise.resolve(RNSnowplowTracker.setDomainUserId({
912
+ tracker: namespace,
913
+ domainUserId: newDuid
914
+ }));
915
+ }
916
+ /**
917
+ * Sets the ipAddress of the tracker subject
918
+ *
919
+ * @param namespace {string} - the tracker namespace
920
+ * @param newIp {string | null} - the new ipAddress
921
+ * @returns - Promise
922
+ */
923
+ function setIpAddress$1(namespace, newIp) {
924
+ if (!(newIp === null || typeof newIp === 'string')) {
925
+ return Promise.reject(new Error(logMessages.setIpAddress));
926
+ }
927
+ return Promise.resolve(RNSnowplowTracker.setIpAddress({
928
+ tracker: namespace,
929
+ ipAddress: newIp
930
+ }));
931
+ }
932
+ /**
933
+ * Sets the useragent of the tracker subject
934
+ *
935
+ * @param namespace {string} - the tracker namespace
936
+ * @param newUagent {string | null} - the new useragent
937
+ * @returns - Promise
938
+ */
939
+ function setUseragent$1(namespace, newUagent) {
940
+ if (!(newUagent === null || typeof newUagent === 'string')) {
941
+ return Promise.reject(new Error(logMessages.setUseragent));
942
+ }
943
+ return Promise.resolve(RNSnowplowTracker.setUseragent({
944
+ tracker: namespace,
945
+ useragent: newUagent
946
+ }));
947
+ }
948
+ /**
949
+ * Sets the timezone of the tracker subject
950
+ *
951
+ * @param namespace {string} - the tracker namespace
952
+ * @param newTz {string | null} - the new timezone
953
+ * @returns - Promise
954
+ */
955
+ function setTimezone$1(namespace, newTz) {
956
+ if (!(newTz === null || typeof newTz === 'string')) {
957
+ return Promise.reject(new Error(logMessages.setTimezone));
958
+ }
959
+ return Promise.resolve(RNSnowplowTracker.setTimezone({
960
+ tracker: namespace,
961
+ timezone: newTz
962
+ }));
963
+ }
964
+ /**
965
+ * Sets the language of the tracker subject
966
+ *
967
+ * @param namespace {string} - the tracker namespace
968
+ * @param newLang {string | null} - the new language
969
+ * @returns - Promise
970
+ */
971
+ function setLanguage$1(namespace, newLang) {
972
+ if (!(newLang === null || typeof newLang === 'string')) {
973
+ return Promise.reject(new Error(logMessages.setLanguage));
974
+ }
975
+ return Promise.resolve(RNSnowplowTracker.setLanguage({
976
+ tracker: namespace,
977
+ language: newLang
978
+ }));
979
+ }
980
+ /**
981
+ * Sets the screenResolution of the tracker subject
982
+ *
983
+ * @param namespace {string} - the tracker namespace
984
+ * @param newRes {ScreenSize | null} - the new screenResolution
985
+ * @returns - Promise
986
+ */
987
+ function setScreenResolution$1(namespace, newRes) {
988
+ if (!(newRes === null || isScreenSize(newRes))) {
989
+ return Promise.reject(new Error(logMessages.setScreenResolution));
990
+ }
991
+ return Promise.resolve(RNSnowplowTracker.setScreenResolution({
992
+ tracker: namespace,
993
+ screenResolution: newRes
994
+ }));
995
+ }
996
+ /**
997
+ * Sets the screenViewport of the tracker subject
998
+ *
999
+ * @param namespace {string} - the tracker namespace
1000
+ * @param newView {ScreenSize | null} - the new screenViewport
1001
+ * @returns - Promise
1002
+ */
1003
+ function setScreenViewport$1(namespace, newView) {
1004
+ if (!(newView === null || isScreenSize(newView))) {
1005
+ return Promise.reject(new Error(logMessages.setScreenViewport));
1006
+ }
1007
+ return Promise.resolve(RNSnowplowTracker.setScreenViewport({
1008
+ tracker: namespace,
1009
+ screenViewport: newView
1010
+ }));
1011
+ }
1012
+ /**
1013
+ * Sets the colorDepth of the tracker subject
1014
+ *
1015
+ * @param namespace {string} - the tracker namespace
1016
+ * @param newColorD {number | null} - the new colorDepth
1017
+ * @returns - Promise
1018
+ */
1019
+ function setColorDepth$1(namespace, newColorD) {
1020
+ if (!(newColorD === null || typeof newColorD === 'number')) {
1021
+ return Promise.reject(new Error(logMessages.setColorDepth));
156
1022
  }
1023
+ return Promise.resolve(RNSnowplowTracker.setColorDepth({
1024
+ tracker: namespace,
1025
+ colorDepth: newColorD
1026
+ }));
1027
+ }
1028
+ const setterMap = {
1029
+ userId: setUserId$1,
1030
+ networkUserId: setNetworkUserId$1,
1031
+ domainUserId: setDomainUserId$1,
1032
+ ipAddress: setIpAddress$1,
1033
+ useragent: setUseragent$1,
1034
+ timezone: setTimezone$1,
1035
+ language: setLanguage$1,
1036
+ screenResolution: setScreenResolution$1,
1037
+ screenViewport: setScreenViewport$1,
1038
+ colorDepth: setColorDepth$1
1039
+ };
1040
+ /**
1041
+ * Sets the tracker subject
1042
+ *
1043
+ * @param namespace {string} - the tracker namespace
1044
+ * @param config {SubjectConfiguration} - the new subject data
1045
+ * @returns - Promise
1046
+ */
1047
+ function setSubjectData$1(namespace, config) {
1048
+ if (!isValidSubjectConf(config)) {
1049
+ return Promise.reject(new Error(`${logMessages.setSubjectData} ${logMessages.subject}`));
1050
+ }
1051
+ const promises = Object.keys(config)
1052
+ .map((k) => {
1053
+ const fun = setterMap[k];
1054
+ return fun ? fun(namespace, config[k]) : undefined;
1055
+ })
1056
+ .filter((f) => f !== undefined);
1057
+ // to use Promise.all (Promise.allSettled not supported in all RN versions)
1058
+ const safePromises = promises
1059
+ .map((p) => p
1060
+ .then((x) => Object.assign({
1061
+ status: 'fulfilled',
1062
+ value: x
1063
+ }))
1064
+ .catch((err) => Object.assign({
1065
+ status: 'rejected',
1066
+ reason: err.message
1067
+ })));
1068
+ return Promise.all(safePromises).then((outcomes) => {
1069
+ const anyReasons = outcomes.filter((res) => res.status === 'rejected');
1070
+ if (anyReasons.length > 0) {
1071
+ const allReasons = anyReasons
1072
+ .reduce((acc, curr) => acc + ':' + curr.reason, logMessages.setSubjectData);
1073
+ throw new Error(allReasons);
1074
+ }
1075
+ return true;
1076
+ });
157
1077
  }
158
1078
 
159
1079
  /*
160
- * Copyright (c) 2020-2021 Snowplow Analytics Ltd. All rights reserved.
1080
+ * Copyright (c) 2020-2022 Snowplow Analytics Ltd. All rights reserved.
161
1081
  *
162
1082
  * This program is licensed to you under the Apache License Version 2.0,
163
1083
  * and you may not use this file except in compliance with the Apache License Version 2.0.
@@ -168,34 +1088,486 @@ function errorHandler(err) {
168
1088
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
169
1089
  * See the Apache License Version 2.0 for the specific language governing permissions and limitations there under.
170
1090
  */
1091
+ /**
1092
+ * Create a tracker from specified initial configuration.
1093
+ *
1094
+ * @param initConfig {Object} - The initial tracker configuration
1095
+ * @returns - A promise fullfilled if the tracker is initialized
1096
+ */
1097
+ function createTracker$1(initConfig) {
1098
+ return initValidate(initConfig)
1099
+ .then(() => RNSnowplowTracker.createTracker(initConfig))
1100
+ .catch((error) => {
1101
+ throw new Error(`${logMessages.createTracker} ${error.message}.`);
1102
+ });
1103
+ }
1104
+ /**
1105
+ * Removes the tracker with given namespace
1106
+ *
1107
+ * @param trackerNamespace {string} - The tracker namespace
1108
+ * @returns - A boolean promise
1109
+ */
1110
+ function removeTracker$1(trackerNamespace) {
1111
+ if (typeof trackerNamespace !== 'string') {
1112
+ return Promise.reject(new Error(logMessages.removeTracker));
1113
+ }
1114
+ return Promise.resolve(RNSnowplowTracker.removeTracker({ tracker: trackerNamespace }));
1115
+ }
1116
+ /**
1117
+ * Removes all existing trackers
1118
+ *
1119
+ * @returns - A void promise
1120
+ */
1121
+ function removeAllTrackers$1() {
1122
+ return Promise.resolve(RNSnowplowTracker.removeAllTrackers());
1123
+ }
1124
+ /**
1125
+ * Returns a function to track a SelfDescribing event by a tracker
1126
+ *
1127
+ * @param namespace {string} - The tracker namespace
1128
+ * @returns - A function to track a SelfDescribing event
1129
+ */
1130
+ function trackSelfDescribingEvent(namespace) {
1131
+ return function (argmap, contexts = []) {
1132
+ return trackSelfDescribingEvent$1(namespace, argmap, contexts);
1133
+ };
1134
+ }
1135
+ /**
1136
+ * Returns a function to track a ScreenView event by a tracker
1137
+ *
1138
+ * @param namespace {string} - The tracker namespace
1139
+ * @returns - A function to track a ScreenView event
1140
+ */
1141
+ function trackScreenViewEvent(namespace) {
1142
+ return function (argmap, contexts = []) {
1143
+ return trackScreenViewEvent$1(namespace, argmap, contexts);
1144
+ };
1145
+ }
1146
+ /**
1147
+ * Returns a function to track a Structured event by a tracker
1148
+ *
1149
+ * @param namespace {string} - The tracker namespace
1150
+ * @returns - A function to track a Structured event
1151
+ */
1152
+ function trackStructuredEvent(namespace) {
1153
+ return function (argmap, contexts = []) {
1154
+ return trackStructuredEvent$1(namespace, argmap, contexts);
1155
+ };
1156
+ }
1157
+ /**
1158
+ * Returns a function to track a PageView event by a tracker
1159
+ *
1160
+ * @param namespace {string} - The tracker namespace
1161
+ * @returns - A function to track a PageView event
1162
+ */
1163
+ function trackPageViewEvent(namespace) {
1164
+ return function (argmap, contexts = []) {
1165
+ return trackPageViewEvent$1(namespace, argmap, contexts);
1166
+ };
1167
+ }
1168
+ /**
1169
+ * Returns a function to track a Timing event by a tracker
1170
+ *
1171
+ * @param namespace {string} - The tracker namespace
1172
+ * @returns - A function to track a Timing event
1173
+ */
1174
+ function trackTimingEvent(namespace) {
1175
+ return function (argmap, contexts = []) {
1176
+ return trackTimingEvent$1(namespace, argmap, contexts);
1177
+ };
1178
+ }
1179
+ /**
1180
+ * Returns a function to track a ConsentGranted event by a tracker
1181
+ *
1182
+ * @param namespace {string} - The tracker namespace
1183
+ * @returns - A function to track a ConsentGranted event
1184
+ */
1185
+ function trackConsentGrantedEvent(namespace) {
1186
+ return function (argmap, contexts = []) {
1187
+ return trackConsentGrantedEvent$1(namespace, argmap, contexts);
1188
+ };
1189
+ }
1190
+ /**
1191
+ * Returns a function to track a ConsentWithdrawn event by a tracker
1192
+ *
1193
+ * @param namespace {string} - The tracker namespace
1194
+ * @returns - A function to track a ConsentWithdrawn event
1195
+ */
1196
+ function trackConsentWithdrawnEvent(namespace) {
1197
+ return function (argmap, contexts = []) {
1198
+ return trackConsentWithdrawnEvent$1(namespace, argmap, contexts);
1199
+ };
1200
+ }
1201
+ /**
1202
+ * Returns a function to track an EcommerceTransaction event by a tracker
1203
+ *
1204
+ * @param namespace {string} - The tracker namespace
1205
+ * @returns - A function to track an EcommerceTransaction event
1206
+ */
1207
+ function trackEcommerceTransactionEvent(namespace) {
1208
+ return function (argmap, contexts = []) {
1209
+ return trackEcommerceTransactionEvent$1(namespace, argmap, contexts);
1210
+ };
1211
+ }
1212
+ /**
1213
+ * Returns a function to track an DeepLinkReceived event by a tracker
1214
+ *
1215
+ * @param namespace {string} - The tracker namespace
1216
+ * @returns - A function to track an DeepLinkReceived event
1217
+ */
1218
+ function trackDeepLinkReceivedEvent(namespace) {
1219
+ return function (argmap, contexts = []) {
1220
+ return trackDeepLinkReceivedEvent$1(namespace, argmap, contexts);
1221
+ };
1222
+ }
1223
+ /**
1224
+ * Returns a function to track an MessageNotification event by a tracker
1225
+ *
1226
+ * @param namespace {string} - The tracker namespace
1227
+ * @returns - A function to track an MessageNotification event
1228
+ */
1229
+ function trackMessageNotificationEvent(namespace) {
1230
+ return function (argmap, contexts = []) {
1231
+ return trackMessageNotificationEvent$1(namespace, argmap, contexts);
1232
+ };
1233
+ }
1234
+ /**
1235
+ * Returns a function to remove global contexts by a tracker
1236
+ *
1237
+ * @param namespace {string} - The tracker namespace
1238
+ * @returns - A function to remove global contexts
1239
+ */
1240
+ function removeGlobalContexts(namespace) {
1241
+ return function (tag) {
1242
+ if (typeof tag !== 'string') {
1243
+ return Promise.reject(new Error(`${logMessages.removeGlobalContexts} ${logMessages.gcTagType}`));
1244
+ }
1245
+ return Promise.resolve(RNSnowplowTracker.removeGlobalContexts({ tracker: namespace, removeTag: tag }));
1246
+ };
1247
+ }
1248
+ /**
1249
+ * Returns a function to add global contexts by a tracker
1250
+ *
1251
+ * @param namespace {string} - The tracker namespace
1252
+ * @returns - A function to add global contexts
1253
+ */
1254
+ function addGlobalContexts(namespace) {
1255
+ return function (gc) {
1256
+ if (!isValidGC(gc)) {
1257
+ return Promise.reject(new Error(`${logMessages.addGlobalContexts} ${logMessages.gcType}`));
1258
+ }
1259
+ return Promise.resolve(RNSnowplowTracker.addGlobalContexts({ tracker: namespace, addGlobalContext: gc }));
1260
+ };
1261
+ }
1262
+ /**
1263
+ * Returns a function to set the subject userId
1264
+ *
1265
+ * @param namespace {string} - The tracker namespace
1266
+ * @returns - A function to set the userId
1267
+ */
1268
+ function setUserId(namespace) {
1269
+ return function (newUid) {
1270
+ return setUserId$1(namespace, newUid);
1271
+ };
1272
+ }
1273
+ /**
1274
+ * Returns a function to set the subject networkUserId
1275
+ *
1276
+ * @param namespace {string} - The tracker namespace
1277
+ * @returns - A function to set the networkUserId
1278
+ */
1279
+ function setNetworkUserId(namespace) {
1280
+ return function (newNuid) {
1281
+ return setNetworkUserId$1(namespace, newNuid);
1282
+ };
1283
+ }
1284
+ /**
1285
+ * Returns a function to set the subject domainUserId
1286
+ *
1287
+ * @param namespace {string} - The tracker namespace
1288
+ * @returns - A function to set the domainUserId
1289
+ */
1290
+ function setDomainUserId(namespace) {
1291
+ return function (newDuid) {
1292
+ return setDomainUserId$1(namespace, newDuid);
1293
+ };
1294
+ }
1295
+ /**
1296
+ * Returns a function to set the subject ipAddress
1297
+ *
1298
+ * @param namespace {string} - The tracker namespace
1299
+ * @returns - A function to set the ipAddress
1300
+ */
1301
+ function setIpAddress(namespace) {
1302
+ return function (newIp) {
1303
+ return setIpAddress$1(namespace, newIp);
1304
+ };
1305
+ }
1306
+ /**
1307
+ * Returns a function to set the subject useragent
1308
+ *
1309
+ * @param namespace {string} - The tracker namespace
1310
+ * @returns - A function to set the useragent
1311
+ */
1312
+ function setUseragent(namespace) {
1313
+ return function (newUagent) {
1314
+ return setUseragent$1(namespace, newUagent);
1315
+ };
1316
+ }
1317
+ /**
1318
+ * Returns a function to set the subject timezone
1319
+ *
1320
+ * @param namespace {string} - The tracker namespace
1321
+ * @returns - A function to set the timezone
1322
+ */
1323
+ function setTimezone(namespace) {
1324
+ return function (newTz) {
1325
+ return setTimezone$1(namespace, newTz);
1326
+ };
1327
+ }
1328
+ /**
1329
+ * Returns a function to set the subject language
1330
+ *
1331
+ * @param namespace {string} - The tracker namespace
1332
+ * @returns - A function to set the language
1333
+ */
1334
+ function setLanguage(namespace) {
1335
+ return function (newLang) {
1336
+ return setLanguage$1(namespace, newLang);
1337
+ };
1338
+ }
1339
+ /**
1340
+ * Returns a function to set the subject screenResolution
1341
+ *
1342
+ * @param namespace {string} - The tracker namespace
1343
+ * @returns - A function to set the screenResolution
1344
+ */
1345
+ function setScreenResolution(namespace) {
1346
+ return function (newRes) {
1347
+ return setScreenResolution$1(namespace, newRes);
1348
+ };
1349
+ }
1350
+ /**
1351
+ * Returns a function to set the subject screenViewport
1352
+ *
1353
+ * @param namespace {string} - The tracker namespace
1354
+ * @returns - A function to set the screenViewport
1355
+ */
1356
+ function setScreenViewport(namespace) {
1357
+ return function (newView) {
1358
+ return setScreenViewport$1(namespace, newView);
1359
+ };
1360
+ }
1361
+ /**
1362
+ * Returns a function to set the subject colorDepth
1363
+ *
1364
+ * @param namespace {string} - The tracker namespace
1365
+ * @returns - A function to set the colorDepth
1366
+ */
1367
+ function setColorDepth(namespace) {
1368
+ return function (newColorD) {
1369
+ return setColorDepth$1(namespace, newColorD);
1370
+ };
1371
+ }
1372
+ /**
1373
+ * Returns a function to set subject data
1374
+ *
1375
+ * @param namespace {string} - The tracker namespace
1376
+ * @returns - A function to set subject data
1377
+ */
1378
+ function setSubjectData(namespace) {
1379
+ return function (config) {
1380
+ return setSubjectData$1(namespace, config);
1381
+ };
1382
+ }
1383
+ /**
1384
+ * Returns a function to get the current session userId
1385
+ *
1386
+ * @param namespace {string} - The tracker namespace
1387
+ * @returns - A function to get the session userId
1388
+ */
1389
+ function getSessionUserId(namespace) {
1390
+ return function () {
1391
+ return Promise
1392
+ .resolve(RNSnowplowTracker.getSessionUserId({ tracker: namespace }));
1393
+ };
1394
+ }
1395
+ /**
1396
+ * Returns a function to get the current sessionId
1397
+ *
1398
+ * @param namespace {string} - The tracker namespace
1399
+ * @returns - A function to get the sessionId
1400
+ */
1401
+ function getSessionId(namespace) {
1402
+ return function () {
1403
+ return Promise
1404
+ .resolve(RNSnowplowTracker.getSessionId({ tracker: namespace }));
1405
+ };
1406
+ }
1407
+ /**
1408
+ * Returns a function to get the current session index
1409
+ *
1410
+ * @param namespace {string} - The tracker namespace
1411
+ * @returns - A function to get the session index
1412
+ */
1413
+ function getSessionIndex(namespace) {
1414
+ return function () {
1415
+ return Promise
1416
+ .resolve(RNSnowplowTracker.getSessionIndex({ tracker: namespace }));
1417
+ };
1418
+ }
1419
+ /**
1420
+ * Returns a function to get whether the app is in background
1421
+ *
1422
+ * @param namespace {string} - The tracker namespace
1423
+ * @returns - A function to get whether the app isInBackground
1424
+ */
1425
+ function getIsInBackground(namespace) {
1426
+ return function () {
1427
+ return Promise
1428
+ .resolve(RNSnowplowTracker.getIsInBackground({ tracker: namespace }));
1429
+ };
1430
+ }
1431
+ /**
1432
+ * Returns a function to get the background index
1433
+ *
1434
+ * @param namespace {string} - The tracker namespace
1435
+ * @returns - A function to get the backgroundIndex
1436
+ */
1437
+ function getBackgroundIndex(namespace) {
1438
+ return function () {
1439
+ return Promise
1440
+ .resolve(RNSnowplowTracker.getBackgroundIndex({ tracker: namespace }));
1441
+ };
1442
+ }
1443
+ /**
1444
+ * Returns a function to get the foreground index
1445
+ *
1446
+ * @param namespace {string} - The tracker namespace
1447
+ * @returns - A function to get the foregroundIndex
1448
+ */
1449
+ function getForegroundIndex(namespace) {
1450
+ return function () {
1451
+ return Promise
1452
+ .resolve(RNSnowplowTracker.getForegroundIndex({ tracker: namespace }));
1453
+ };
1454
+ }
1455
+
171
1456
  /*
1457
+ * Copyright (c) 2020-2022 Snowplow Analytics Ltd. All rights reserved.
1458
+ *
1459
+ * This program is licensed to you under the Apache License Version 2.0,
1460
+ * and you may not use this file except in compliance with the Apache License Version 2.0.
1461
+ * You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0.
1462
+ *
1463
+ * Unless required by applicable law or agreed to in writing,
1464
+ * software distributed under the Apache License Version 2.0 is distributed on an
1465
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1466
+ * See the Apache License Version 2.0 for the specific language governing permissions and limitations there under.
1467
+ */
1468
+ /**
172
1469
  * Creates a React Native Tracker object
173
1470
  *
174
- * @param namespace - The tracker namespace
175
- * @param config - The configuration to be used for the tracker
1471
+ * @param namespace {string} - The tracker namespace
1472
+ * @param networkConfig {Object} - The network configuration
1473
+ * @param control {Array} - The tracker controller configuration
176
1474
  * @returns The tracker object
177
1475
  */
178
- function createTracker(namespace, config) {
1476
+ function createTracker(namespace, networkConfig, controllerConfig = {}) {
179
1477
  // initTrackerPromise
180
- const initTrackerPromise = Promise.resolve(initializeTracker({
181
- ...config,
1478
+ const initTrackerPromise = Promise.resolve(createTracker$1({
182
1479
  namespace,
1480
+ networkConfig,
1481
+ ...controllerConfig
183
1482
  }));
184
1483
  // mkMethod creates methods subscribed to the initTrackerPromise
185
1484
  const mkMethod = safeWait(initTrackerPromise, errorHandler);
186
- // tracker methods
187
- const setSubjectData$1 = mkMethod(setSubjectData);
188
- const trackScreenViewEvent$1 = mkMethod(trackScreenViewEvent);
189
- const trackSelfDescribingEvent$1 = mkMethod(trackSelfDescribingEvent);
190
- const trackStructuredEvent$1 = mkMethod(trackStructuredEvent);
191
- const trackPageViewEvent$1 = mkMethod(trackPageViewEvent);
1485
+ // mkCallback creates callbacks subscribed to the initTrackerPromise
1486
+ const mkCallback = safeWaitCallback(initTrackerPromise, errorHandler);
1487
+ // track methods
1488
+ const trackSelfDescribingEvent$1 = mkMethod(trackSelfDescribingEvent(namespace));
1489
+ const trackScreenViewEvent$1 = mkMethod(trackScreenViewEvent(namespace));
1490
+ const trackStructuredEvent$1 = mkMethod(trackStructuredEvent(namespace));
1491
+ const trackPageViewEvent$1 = mkMethod(trackPageViewEvent(namespace));
1492
+ const trackTimingEvent$1 = mkMethod(trackTimingEvent(namespace));
1493
+ const trackConsentGrantedEvent$1 = mkMethod(trackConsentGrantedEvent(namespace));
1494
+ const trackConsentWithdrawnEvent$1 = mkMethod(trackConsentWithdrawnEvent(namespace));
1495
+ const trackEcommerceTransactionEvent$1 = mkMethod(trackEcommerceTransactionEvent(namespace));
1496
+ const trackDeepLinkReceivedEvent$1 = mkMethod(trackDeepLinkReceivedEvent(namespace));
1497
+ const trackMessageNotificationEvent$1 = mkMethod(trackMessageNotificationEvent(namespace));
1498
+ // Global Contexts
1499
+ const removeGlobalContexts$1 = mkMethod(removeGlobalContexts(namespace));
1500
+ const addGlobalContexts$1 = mkMethod(addGlobalContexts(namespace));
1501
+ // setters
1502
+ const setUserId$1 = mkMethod(setUserId(namespace));
1503
+ const setNetworkUserId$1 = mkMethod(setNetworkUserId(namespace));
1504
+ const setDomainUserId$1 = mkMethod(setDomainUserId(namespace));
1505
+ const setIpAddress$1 = mkMethod(setIpAddress(namespace));
1506
+ const setUseragent$1 = mkMethod(setUseragent(namespace));
1507
+ const setTimezone$1 = mkMethod(setTimezone(namespace));
1508
+ const setLanguage$1 = mkMethod(setLanguage(namespace));
1509
+ const setScreenResolution$1 = mkMethod(setScreenResolution(namespace));
1510
+ const setScreenViewport$1 = mkMethod(setScreenViewport(namespace));
1511
+ const setColorDepth$1 = mkMethod(setColorDepth(namespace));
1512
+ const setSubjectData$1 = mkMethod(setSubjectData(namespace));
1513
+ // callbacks
1514
+ const getSessionUserId$1 = mkCallback(getSessionUserId(namespace));
1515
+ const getSessionId$1 = mkCallback(getSessionId(namespace));
1516
+ const getSessionIndex$1 = mkCallback(getSessionIndex(namespace));
1517
+ const getIsInBackground$1 = mkCallback(getIsInBackground(namespace));
1518
+ const getBackgroundIndex$1 = mkCallback(getBackgroundIndex(namespace));
1519
+ const getForegroundIndex$1 = mkCallback(getForegroundIndex(namespace));
192
1520
  return Object.freeze({
193
- setSubjectData: setSubjectData$1,
194
- trackScreenViewEvent: trackScreenViewEvent$1,
195
1521
  trackSelfDescribingEvent: trackSelfDescribingEvent$1,
1522
+ trackScreenViewEvent: trackScreenViewEvent$1,
196
1523
  trackStructuredEvent: trackStructuredEvent$1,
197
1524
  trackPageViewEvent: trackPageViewEvent$1,
1525
+ trackTimingEvent: trackTimingEvent$1,
1526
+ trackConsentGrantedEvent: trackConsentGrantedEvent$1,
1527
+ trackConsentWithdrawnEvent: trackConsentWithdrawnEvent$1,
1528
+ trackEcommerceTransactionEvent: trackEcommerceTransactionEvent$1,
1529
+ trackDeepLinkReceivedEvent: trackDeepLinkReceivedEvent$1,
1530
+ trackMessageNotificationEvent: trackMessageNotificationEvent$1,
1531
+ removeGlobalContexts: removeGlobalContexts$1,
1532
+ addGlobalContexts: addGlobalContexts$1,
1533
+ setUserId: setUserId$1,
1534
+ setNetworkUserId: setNetworkUserId$1,
1535
+ setDomainUserId: setDomainUserId$1,
1536
+ setIpAddress: setIpAddress$1,
1537
+ setUseragent: setUseragent$1,
1538
+ setTimezone: setTimezone$1,
1539
+ setLanguage: setLanguage$1,
1540
+ setScreenResolution: setScreenResolution$1,
1541
+ setScreenViewport: setScreenViewport$1,
1542
+ setColorDepth: setColorDepth$1,
1543
+ setSubjectData: setSubjectData$1,
1544
+ getSessionUserId: getSessionUserId$1,
1545
+ getSessionId: getSessionId$1,
1546
+ getSessionIndex: getSessionIndex$1,
1547
+ getIsInBackground: getIsInBackground$1,
1548
+ getBackgroundIndex: getBackgroundIndex$1,
1549
+ getForegroundIndex: getForegroundIndex$1,
198
1550
  });
199
1551
  }
1552
+ /**
1553
+ * Removes a tracker given its namespace
1554
+ *
1555
+ * @param trackerNamespace {string}
1556
+ * @returns - A boolean promise
1557
+ */
1558
+ function removeTracker(trackerNamespace) {
1559
+ return removeTracker$1(trackerNamespace)
1560
+ .catch((e) => errorHandler(e));
1561
+ }
1562
+ /**
1563
+ * Removes all trackers
1564
+ *
1565
+ * @returns - A boolean promise
1566
+ */
1567
+ function removeAllTrackers() {
1568
+ return removeAllTrackers$1()
1569
+ .catch((e) => errorHandler(e));
1570
+ }
200
1571
 
201
- export { createTracker };
1572
+ export { createTracker, removeAllTrackers, removeTracker };
1573
+ //# sourceMappingURL=index.js.map