@snowplow/react-native-tracker 1.2.1 → 1.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/dist/index.js CHANGED
@@ -1,21 +1,9 @@
1
1
  import { NativeModules } from 'react-native';
2
+ import { trackerCore, buildSelfDescribingEvent, buildStructEvent, buildPageView, buildConsentGranted, buildConsentWithdrawn, buildEcommerceTransactionItem, buildEcommerceTransaction } from '@snowplow/tracker-core';
3
+ import { v4 } from 'uuid';
2
4
 
3
5
  /*
4
- * Copyright (c) 2020-2022 Snowplow Analytics Ltd. All rights reserved.
5
- *
6
- * This program is licensed to you under the Apache License Version 2.0,
7
- * and you may not use this file except in compliance with the Apache License Version 2.0.
8
- * You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0.
9
- *
10
- * Unless required by applicable law or agreed to in writing,
11
- * software distributed under the Apache License Version 2.0 is distributed on an
12
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
- * See the Apache License Version 2.0 for the specific language governing permissions and limitations there under.
14
- */
15
- const { RNSnowplowTracker } = NativeModules;
16
-
17
- /*
18
- * Copyright (c) 2020-2022 Snowplow Analytics Ltd. All rights reserved.
6
+ * Copyright (c) 2020-2023 Snowplow Analytics Ltd. All rights reserved.
19
7
  *
20
8
  * This program is licensed to you under the Apache License Version 2.0,
21
9
  * and you may not use this file except in compliance with the Apache License Version 2.0.
@@ -81,7 +69,7 @@ function isObject(x) {
81
69
  }
82
70
 
83
71
  /*
84
- * Copyright (c) 2020-2022 Snowplow Analytics Ltd. All rights reserved.
72
+ * Copyright (c) 2020-2023 Snowplow Analytics Ltd. All rights reserved.
85
73
  *
86
74
  * This program is licensed to you under the Apache License Version 2.0,
87
75
  * and you may not use this file except in compliance with the Apache License Version 2.0.
@@ -148,9 +136,397 @@ const logMessages = {
148
136
  setColorDepth: 'setColorDepth: colorDepth can only be a number(integer) or null',
149
137
  setSubjectData: 'setSubjectData:',
150
138
  };
139
+ const schemas = {
140
+ payloadData: 'iglu:com.snowplowanalytics.snowplow/payload_data/jsonschema/1-0-4',
141
+ timingSchema: 'iglu:com.snowplowanalytics.snowplow/timing/jsonschema/1-0-0',
142
+ deepLinkReceivedSchema: 'iglu:com.snowplowanalytics.mobile/deep_link_received/jsonschema/1-0-0',
143
+ messageNotificationSchema: 'iglu:com.snowplowanalytics.mobile/message_notification/jsonschema/1-0-0',
144
+ screenViewSchema: 'iglu:com.snowplowanalytics.mobile/screen_view/jsonschema/1-0-0',
145
+ };
151
146
 
152
147
  /*
153
- * Copyright (c) 2020-2022 Snowplow Analytics Ltd. All rights reserved.
148
+ * Copyright (c) 2020-2023 Snowplow Analytics Ltd. All rights reserved.
149
+ *
150
+ * This program is licensed to you under the Apache License Version 2.0,
151
+ * and you may not use this file except in compliance with the Apache License Version 2.0.
152
+ * You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0.
153
+ *
154
+ * Unless required by applicable law or agreed to in writing,
155
+ * software distributed under the Apache License Version 2.0 is distributed on an
156
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
157
+ * See the Apache License Version 2.0 for the specific language governing permissions and limitations there under.
158
+ */
159
+ // Tracker version added to the events
160
+ const trackerVersion = 'rn-1.4.0';
161
+ let trackers = {};
162
+ function preparePayload(payload) {
163
+ const stringifiedPayload = {};
164
+ payload['stm'] = new Date().getTime().toString();
165
+ for (const key in payload) {
166
+ if (Object.prototype.hasOwnProperty.call(payload, key)) {
167
+ stringifiedPayload[key] = String(payload[key]);
168
+ }
169
+ }
170
+ return stringifiedPayload;
171
+ }
172
+ function createEmitCallback(networkConfig) {
173
+ return (e) => {
174
+ const postJson = {
175
+ schema: schemas.payloadData,
176
+ data: [preparePayload(e)],
177
+ };
178
+ const endpoint = networkConfig.endpoint;
179
+ const postPath = networkConfig.customPostPath ?? '/com.snowplowanalytics.snowplow/tp2';
180
+ const headers = networkConfig.requestHeaders ?? {};
181
+ fetch(endpoint + postPath, {
182
+ method: 'POST',
183
+ body: JSON.stringify(postJson),
184
+ headers: {
185
+ 'Content-Type': 'application/json',
186
+ ...headers,
187
+ },
188
+ }).catch(errorHandler);
189
+ };
190
+ }
191
+ function updateTrackerProperties(tracker, configuration) {
192
+ tracker.setPlatform(configuration.trackerConfig?.devicePlatform ?? 'mob');
193
+ tracker.setTrackerVersion(trackerVersion);
194
+ tracker.setTrackerNamespace(configuration.namespace);
195
+ if (configuration.trackerConfig?.appId != null) {
196
+ tracker.setAppId(configuration.trackerConfig.appId);
197
+ }
198
+ if (configuration.subjectConfig?.colorDepth != null) {
199
+ tracker.setColorDepth(String(configuration.subjectConfig.colorDepth));
200
+ }
201
+ if (configuration.subjectConfig?.domainUserId != null) {
202
+ tracker.setDomainUserId(configuration.subjectConfig.domainUserId);
203
+ }
204
+ if (configuration.subjectConfig?.ipAddress != null) {
205
+ tracker.setIpAddress(configuration.subjectConfig.ipAddress);
206
+ }
207
+ if (configuration.subjectConfig?.language != null) {
208
+ tracker.setLang(configuration.subjectConfig.language);
209
+ }
210
+ if (configuration.subjectConfig?.screenResolution != null) {
211
+ tracker.setScreenResolution(String(configuration.subjectConfig.screenResolution[0]), String(configuration.subjectConfig.screenResolution[1]));
212
+ }
213
+ if (configuration.subjectConfig?.screenViewport != null) {
214
+ tracker.setViewport(String(configuration.subjectConfig.screenViewport[0]), String(configuration.subjectConfig.screenViewport[1]));
215
+ }
216
+ if (configuration.subjectConfig?.timezone != null) {
217
+ tracker.setTimezone(configuration.subjectConfig.timezone);
218
+ }
219
+ if (configuration.subjectConfig?.userId != null) {
220
+ tracker.setUserId(configuration.subjectConfig.userId);
221
+ }
222
+ if (configuration.subjectConfig?.useragent != null) {
223
+ tracker.setUseragent(configuration.subjectConfig.useragent);
224
+ }
225
+ }
226
+ function createTracker$2(configuration, emitCallback) {
227
+ // create an emit callback if not given
228
+ const emitter = emitCallback ?? createEmitCallback(configuration.networkConfig);
229
+ // the tracker core does not provide an option to set the duid, so we need to add custom
230
+ let domainUserId;
231
+ const setDomainUserId = (userId) => {
232
+ domainUserId = userId;
233
+ };
234
+ let networkUserId;
235
+ const setNetworkUserId = (userId) => {
236
+ networkUserId = userId;
237
+ };
238
+ // initialize the tracker core
239
+ const core = trackerCore({
240
+ base64: configuration.trackerConfig?.base64Encoding ?? true,
241
+ callback: (payload) => {
242
+ if (domainUserId != null) {
243
+ payload.add('duid', domainUserId);
244
+ }
245
+ if (networkUserId != null) {
246
+ payload.add('tnuid', networkUserId);
247
+ }
248
+ const builtPayload = payload.build();
249
+ emitter(builtPayload);
250
+ },
251
+ });
252
+ const tracker = { ...core, setDomainUserId, setNetworkUserId };
253
+ trackers[configuration.namespace] = tracker;
254
+ // update tracker properties to reflect subject and tracker info
255
+ updateTrackerProperties(tracker, configuration);
256
+ return Promise.resolve();
257
+ }
258
+ function removeTracker$2(details) {
259
+ delete trackers[details.tracker];
260
+ return Promise.resolve(true);
261
+ }
262
+ function removeAllTrackers$2() {
263
+ trackers = {};
264
+ return Promise.resolve(true);
265
+ }
266
+ function forTracker(namespace, callback) {
267
+ const tracker = namespace != null ? trackers[namespace] : Object.values(trackers)[0];
268
+ if (tracker) {
269
+ callback(tracker);
270
+ }
271
+ else {
272
+ errorHandler(new Error('No such tracker found.'));
273
+ }
274
+ }
275
+ function trackSelfDescribingEvent$2(details) {
276
+ forTracker(details.tracker, (tracker) => {
277
+ tracker.track(buildSelfDescribingEvent({
278
+ event: details.eventData,
279
+ }), details.contexts);
280
+ });
281
+ return Promise.resolve();
282
+ }
283
+ function trackStructuredEvent$2(details) {
284
+ forTracker(details.tracker, (tracker) => {
285
+ tracker.track(buildStructEvent(details.eventData), details.contexts);
286
+ });
287
+ return Promise.resolve();
288
+ }
289
+ function trackScreenViewEvent$2(details) {
290
+ const data = {
291
+ name: details.eventData.name,
292
+ id: details.eventData.id ?? v4()
293
+ };
294
+ if (details.eventData.type != null) {
295
+ data.type = details.eventData.type;
296
+ }
297
+ if (details.eventData.previousName != null) {
298
+ data.previousName = details.eventData.previousName;
299
+ }
300
+ if (details.eventData.previousId != null) {
301
+ data.previousId = details.eventData.previousId;
302
+ }
303
+ if (details.eventData.previousType != null) {
304
+ data.previousType = details.eventData.previousType;
305
+ }
306
+ if (details.eventData.transitionType != null) {
307
+ data.transitionType = details.eventData.transitionType;
308
+ }
309
+ return trackSelfDescribingEvent$2({
310
+ tracker: details.tracker,
311
+ eventData: {
312
+ schema: schemas.screenViewSchema,
313
+ data: data
314
+ },
315
+ contexts: details.contexts,
316
+ });
317
+ }
318
+ function trackPageViewEvent$2(details) {
319
+ forTracker(details.tracker, (tracker) => {
320
+ tracker.track(buildPageView(details.eventData), details.contexts);
321
+ });
322
+ return Promise.resolve();
323
+ }
324
+ function trackTimingEvent$2(details) {
325
+ return trackSelfDescribingEvent$2({
326
+ tracker: details.tracker,
327
+ eventData: {
328
+ schema: schemas.timingSchema,
329
+ data: details.eventData,
330
+ },
331
+ contexts: details.contexts,
332
+ });
333
+ }
334
+ function trackConsentGrantedEvent$2(details) {
335
+ forTracker(details.tracker, (tracker) => {
336
+ const built = buildConsentGranted({
337
+ id: details.eventData.documentId,
338
+ version: details.eventData.version,
339
+ name: details.eventData.name,
340
+ description: details.eventData.documentDescription,
341
+ expiry: details.eventData.expiry,
342
+ });
343
+ tracker.track(built.event, details.contexts.concat(built.context));
344
+ });
345
+ return Promise.resolve();
346
+ }
347
+ function trackConsentWithdrawnEvent$2(details) {
348
+ forTracker(details.tracker, (tracker) => {
349
+ const built = buildConsentWithdrawn({
350
+ all: details.eventData.all,
351
+ id: details.eventData.documentId,
352
+ version: details.eventData.version,
353
+ name: details.eventData.name,
354
+ description: details.eventData.documentDescription,
355
+ });
356
+ tracker.track(built.event, details.contexts.concat(built.context));
357
+ });
358
+ return Promise.resolve();
359
+ }
360
+ function trackEcommerceTransactionEvent$2(details) {
361
+ forTracker(details.tracker, (tracker) => {
362
+ details.eventData.items.forEach(item => {
363
+ tracker.track(buildEcommerceTransactionItem({
364
+ ...item,
365
+ orderId: details.eventData.orderId
366
+ }), details.contexts);
367
+ });
368
+ tracker.track(buildEcommerceTransaction({
369
+ orderId: details.eventData.orderId,
370
+ total: details.eventData.totalValue,
371
+ affiliation: details.eventData.affiliation,
372
+ tax: details.eventData.taxValue,
373
+ shipping: details.eventData.shipping,
374
+ city: details.eventData.city,
375
+ state: details.eventData.state,
376
+ country: details.eventData.country,
377
+ currency: details.eventData.currency,
378
+ }), details.contexts);
379
+ });
380
+ return Promise.resolve();
381
+ }
382
+ function trackDeepLinkReceivedEvent$2(details) {
383
+ return trackSelfDescribingEvent$2({
384
+ tracker: details.tracker,
385
+ eventData: {
386
+ schema: schemas.deepLinkReceivedSchema,
387
+ data: details.eventData,
388
+ },
389
+ contexts: details.contexts,
390
+ });
391
+ }
392
+ function trackMessageNotificationEvent$2(details) {
393
+ return trackSelfDescribingEvent$2({
394
+ tracker: details.tracker,
395
+ eventData: {
396
+ schema: schemas.messageNotificationSchema,
397
+ data: details.eventData,
398
+ },
399
+ contexts: details.contexts,
400
+ });
401
+ }
402
+ function removeGlobalContexts$1() {
403
+ return Promise.reject(new Error('Not implemented'));
404
+ }
405
+ function addGlobalContexts$1() {
406
+ return Promise.reject(new Error('Not implemented'));
407
+ }
408
+ function setUserId$2(details) {
409
+ trackers[details.tracker]?.setUserId(details.userId ?? '');
410
+ return Promise.resolve();
411
+ }
412
+ function setNetworkUserId$2(details) {
413
+ trackers[details.tracker]?.setNetworkUserId(details.networkUserId ?? '');
414
+ return Promise.resolve();
415
+ }
416
+ function setDomainUserId$2(details) {
417
+ trackers[details.tracker]?.setDomainUserId(details.domainUserId ?? '');
418
+ return Promise.resolve();
419
+ }
420
+ function setIpAddress$2(details) {
421
+ trackers[details.tracker]?.setIpAddress(details.ipAddress ?? '');
422
+ return Promise.resolve();
423
+ }
424
+ function setUseragent$2(details) {
425
+ trackers[details.tracker]?.setUseragent(details.useragent ?? '');
426
+ return Promise.resolve();
427
+ }
428
+ function setTimezone$2(details) {
429
+ trackers[details.tracker]?.setTimezone(details.timezone ?? '');
430
+ return Promise.resolve();
431
+ }
432
+ function setLanguage$2(details) {
433
+ trackers[details.tracker]?.setLang(details.language ?? '');
434
+ return Promise.resolve();
435
+ }
436
+ function setScreenResolution$2(details) {
437
+ if (details.screenResolution) {
438
+ trackers[details.tracker]?.setScreenResolution(String(details.screenResolution[0]), String(details.screenResolution[1]));
439
+ }
440
+ else {
441
+ trackers[details.tracker]?.addPayloadPair('res', '');
442
+ }
443
+ return Promise.resolve();
444
+ }
445
+ function setScreenViewport$2(details) {
446
+ if (details.screenViewport) {
447
+ trackers[details.tracker]?.setViewport(String(details.screenViewport[0]), String(details.screenViewport[1]));
448
+ }
449
+ else {
450
+ trackers[details.tracker]?.addPayloadPair('vp', '');
451
+ }
452
+ return Promise.resolve();
453
+ }
454
+ function setColorDepth$2(details) {
455
+ trackers[details.tracker]?.setColorDepth(String(details.colorDepth ?? ''));
456
+ return Promise.resolve();
457
+ }
458
+ function getSessionUserId$1() {
459
+ return Promise.reject(new Error('Not implemented'));
460
+ }
461
+ function getSessionId$1() {
462
+ return Promise.reject(new Error('Not implemented'));
463
+ }
464
+ function getSessionIndex$1() {
465
+ return Promise.reject(new Error('Not implemented'));
466
+ }
467
+ function getIsInBackground$1() {
468
+ return Promise.reject(new Error('Not implemented'));
469
+ }
470
+ function getBackgroundIndex$1() {
471
+ return Promise.reject(new Error('Not implemented'));
472
+ }
473
+ function getForegroundIndex$1() {
474
+ return Promise.reject(new Error('Not implemented'));
475
+ }
476
+ const JSSnowplowTracker = Object.freeze({
477
+ createTracker: createTracker$2,
478
+ removeTracker: removeTracker$2,
479
+ removeAllTrackers: removeAllTrackers$2,
480
+ trackSelfDescribingEvent: trackSelfDescribingEvent$2,
481
+ trackStructuredEvent: trackStructuredEvent$2,
482
+ trackScreenViewEvent: trackScreenViewEvent$2,
483
+ trackPageViewEvent: trackPageViewEvent$2,
484
+ trackTimingEvent: trackTimingEvent$2,
485
+ trackConsentGrantedEvent: trackConsentGrantedEvent$2,
486
+ trackConsentWithdrawnEvent: trackConsentWithdrawnEvent$2,
487
+ trackEcommerceTransactionEvent: trackEcommerceTransactionEvent$2,
488
+ trackDeepLinkReceivedEvent: trackDeepLinkReceivedEvent$2,
489
+ trackMessageNotificationEvent: trackMessageNotificationEvent$2,
490
+ removeGlobalContexts: removeGlobalContexts$1,
491
+ addGlobalContexts: addGlobalContexts$1,
492
+ setUserId: setUserId$2,
493
+ setNetworkUserId: setNetworkUserId$2,
494
+ setDomainUserId: setDomainUserId$2,
495
+ setIpAddress: setIpAddress$2,
496
+ setUseragent: setUseragent$2,
497
+ setTimezone: setTimezone$2,
498
+ setLanguage: setLanguage$2,
499
+ setScreenResolution: setScreenResolution$2,
500
+ setScreenViewport: setScreenViewport$2,
501
+ setColorDepth: setColorDepth$2,
502
+ getSessionUserId: getSessionUserId$1,
503
+ getSessionId: getSessionId$1,
504
+ getSessionIndex: getSessionIndex$1,
505
+ getIsInBackground: getIsInBackground$1,
506
+ getBackgroundIndex: getBackgroundIndex$1,
507
+ getForegroundIndex: getForegroundIndex$1,
508
+ });
509
+
510
+ /*
511
+ * Copyright (c) 2020-2023 Snowplow Analytics Ltd. All rights reserved.
512
+ *
513
+ * This program is licensed to you under the Apache License Version 2.0,
514
+ * and you may not use this file except in compliance with the Apache License Version 2.0.
515
+ * You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0.
516
+ *
517
+ * Unless required by applicable law or agreed to in writing,
518
+ * software distributed under the Apache License Version 2.0 is distributed on an
519
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
520
+ * See the Apache License Version 2.0 for the specific language governing permissions and limitations there under.
521
+ */
522
+ const isAvailable = NativeModules.RNSnowplowTracker != null;
523
+ if (!isAvailable) {
524
+ errorHandler(new Error('Unable to access the native iOS/Android Snowplow tracker, a tracker implementation with very limited functionality is used.'));
525
+ }
526
+ const RNSnowplowTracker = isAvailable ? NativeModules.RNSnowplowTracker : JSSnowplowTracker;
527
+
528
+ /*
529
+ * Copyright (c) 2020-2023 Snowplow Analytics Ltd. All rights reserved.
154
530
  *
155
531
  * This program is licensed to you under the Apache License Version 2.0,
156
532
  * and you may not use this file except in compliance with the Apache License Version 2.0.
@@ -394,7 +770,7 @@ function validateEcommerceTransaction(argmap) {
394
770
  }
395
771
 
396
772
  /*
397
- * Copyright (c) 2020-2022 Snowplow Analytics Ltd. All rights reserved.
773
+ * Copyright (c) 2020-2023 Snowplow Analytics Ltd. All rights reserved.
398
774
  *
399
775
  * This program is licensed to you under the Apache License Version 2.0,
400
776
  * and you may not use this file except in compliance with the Apache License Version 2.0.
@@ -408,7 +784,12 @@ function validateEcommerceTransaction(argmap) {
408
784
  /**
409
785
  * Configuration properties
410
786
  */
411
- const networkProps = ['endpoint', 'method'];
787
+ const networkProps = [
788
+ 'endpoint',
789
+ 'method',
790
+ 'customPostPath',
791
+ 'requestHeaders',
792
+ ];
412
793
  const trackerProps = [
413
794
  'appId',
414
795
  'devicePlatform',
@@ -424,7 +805,8 @@ const trackerProps = [
424
805
  'lifecycleAutotracking',
425
806
  'installAutotracking',
426
807
  'exceptionAutotracking',
427
- 'diagnosticAutotracking'
808
+ 'diagnosticAutotracking',
809
+ 'userAnonymisation'
428
810
  ];
429
811
  const sessionProps = [
430
812
  'foregroundTimeout',
@@ -436,6 +818,7 @@ const emitterProps = [
436
818
  'threadPoolSize',
437
819
  'byteLimitPost',
438
820
  'byteLimitGet',
821
+ 'serverAnonymisation',
439
822
  ];
440
823
  const subjectProps = [
441
824
  'userId',
@@ -641,7 +1024,7 @@ function initValidate(init) {
641
1024
  }
642
1025
 
643
1026
  /*
644
- * Copyright (c) 2020-2022 Snowplow Analytics Ltd. All rights reserved.
1027
+ * Copyright (c) 2020-2023 Snowplow Analytics Ltd. All rights reserved.
645
1028
  *
646
1029
  * This program is licensed to you under the Apache License Version 2.0,
647
1030
  * and you may not use this file except in compliance with the Apache License Version 2.0.
@@ -854,7 +1237,7 @@ function trackMessageNotificationEvent$1(namespace, argmap, contexts = []) {
854
1237
  }
855
1238
 
856
1239
  /*
857
- * Copyright (c) 2020-2022 Snowplow Analytics Ltd. All rights reserved.
1240
+ * Copyright (c) 2020-2023 Snowplow Analytics Ltd. All rights reserved.
858
1241
  *
859
1242
  * This program is licensed to you under the Apache License Version 2.0,
860
1243
  * and you may not use this file except in compliance with the Apache License Version 2.0.
@@ -1077,7 +1460,7 @@ function setSubjectData$1(namespace, config) {
1077
1460
  }
1078
1461
 
1079
1462
  /*
1080
- * Copyright (c) 2020-2022 Snowplow Analytics Ltd. All rights reserved.
1463
+ * Copyright (c) 2020-2023 Snowplow Analytics Ltd. All rights reserved.
1081
1464
  *
1082
1465
  * This program is licensed to you under the Apache License Version 2.0,
1083
1466
  * and you may not use this file except in compliance with the Apache License Version 2.0.
@@ -1454,7 +1837,77 @@ function getForegroundIndex(namespace) {
1454
1837
  }
1455
1838
 
1456
1839
  /*
1457
- * Copyright (c) 2020-2022 Snowplow Analytics Ltd. All rights reserved.
1840
+ * Copyright (c) 2020-2023 Snowplow Analytics Ltd. All rights reserved.
1841
+ *
1842
+ * This program is licensed to you under the Apache License Version 2.0,
1843
+ * and you may not use this file except in compliance with the Apache License Version 2.0.
1844
+ * You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0.
1845
+ *
1846
+ * Unless required by applicable law or agreed to in writing,
1847
+ * software distributed under the Apache License Version 2.0 is distributed on an
1848
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1849
+ * See the Apache License Version 2.0 for the specific language governing permissions and limitations there under.
1850
+ */
1851
+ function forEachTracker(trackers, iterator) {
1852
+ if (trackers && trackers.length > 0) {
1853
+ trackers.forEach(iterator);
1854
+ }
1855
+ else {
1856
+ iterator(null);
1857
+ }
1858
+ }
1859
+ /**
1860
+ * Enables tracking events from apps rendered in react-native-webview components.
1861
+ * The apps need to use the Snowplow WebView tracker to track the events.
1862
+ *
1863
+ * To subscribe for the events, set the `onMessage` attribute:
1864
+ * <WebView onMessage={getWebViewCallback()} ... />
1865
+ *
1866
+ * @returns Callback to subscribe for events from Web views tracked using the Snowplow WebView tracker.
1867
+ */
1868
+ function getWebViewCallback() {
1869
+ return (message) => {
1870
+ const data = JSON.parse(message.nativeEvent.data);
1871
+ switch (data.command) {
1872
+ case 'trackSelfDescribingEvent':
1873
+ forEachTracker(data.trackers, (namespace) => {
1874
+ trackSelfDescribingEvent$1(namespace, data.event, data.context || []).catch((error) => {
1875
+ errorHandler(error);
1876
+ });
1877
+ });
1878
+ break;
1879
+ case 'trackStructEvent':
1880
+ forEachTracker(data.trackers, (namespace) => {
1881
+ trackStructuredEvent$1(namespace, data.event, data.context || []).catch((error) => {
1882
+ errorHandler(error);
1883
+ });
1884
+ });
1885
+ break;
1886
+ case 'trackPageView':
1887
+ forEachTracker(data.trackers, (namespace) => {
1888
+ const event = data.event;
1889
+ trackPageViewEvent$1(namespace, {
1890
+ pageTitle: event.title ?? '',
1891
+ pageUrl: event.url ?? '',
1892
+ referrer: event.referrer,
1893
+ }).catch((error) => {
1894
+ errorHandler(error);
1895
+ });
1896
+ });
1897
+ break;
1898
+ case 'trackScreenView':
1899
+ forEachTracker(data.trackers, (namespace) => {
1900
+ trackScreenViewEvent$1(namespace, data.event, data.context || []).catch((error) => {
1901
+ errorHandler(error);
1902
+ });
1903
+ });
1904
+ break;
1905
+ }
1906
+ };
1907
+ }
1908
+
1909
+ /*
1910
+ * Copyright (c) 2020-2023 Snowplow Analytics Ltd. All rights reserved.
1458
1911
  *
1459
1912
  * This program is licensed to you under the Apache License Version 2.0,
1460
1913
  * and you may not use this file except in compliance with the Apache License Version 2.0.
@@ -1569,5 +2022,5 @@ function removeAllTrackers() {
1569
2022
  .catch((e) => errorHandler(e));
1570
2023
  }
1571
2024
 
1572
- export { createTracker, removeAllTrackers, removeTracker };
2025
+ export { createTracker, getWebViewCallback, removeAllTrackers, removeTracker };
1573
2026
  //# sourceMappingURL=index.js.map