@@ -33,38 +33,64 @@ import { newPlatformContextPlugin } from './plugins/platform_context';
3333import { newAppLifecyclePlugin } from './plugins/app_lifecycle' ;
3434import { newAppInstallPlugin } from './plugins/app_install' ;
3535import { newAppContextPlugin } from './plugins/app_context' ;
36+ import DefaultAsyncStorage from '@react-native-async-storage/async-storage' ;
3637
3738const initializedTrackers : Record < string , { tracker : ReactNativeTracker ; core : TrackerCore } > = { } ;
3839
40+ type SetPropertiesAsNonNullable < Obj , Properties extends keyof Obj > = Omit < Obj , Properties > & {
41+ [ K in Properties ] -?: NonNullable < Obj [ K ] > ;
42+ } ;
43+
44+ type Configuration = TrackerConfiguration &
45+ EmitterConfiguration &
46+ SessionConfiguration &
47+ SubjectConfiguration &
48+ EventStoreConfiguration &
49+ ScreenTrackingConfiguration &
50+ PlatformContextConfiguration &
51+ DeepLinkConfiguration &
52+ AppLifecycleConfiguration ;
53+
54+ type NormalizedConfiguration = SetPropertiesAsNonNullable <
55+ Configuration ,
56+ 'asyncStorage' | 'devicePlatform' | 'encodeBase64' | 'eventStore' | 'plugins'
57+ > ;
58+
59+ const normalizeTrackerConfiguration = async ( configuration : Configuration ) : Promise < NormalizedConfiguration > => {
60+ const eventStore = configuration . eventStore ?? ( await newReactNativeEventStore ( configuration ) ) ;
61+ const asyncStorage = configuration . asyncStorage ?? DefaultAsyncStorage ;
62+ const plugins = configuration . plugins ?? [ ] ;
63+ const devicePlatform = configuration . devicePlatform ?? 'mob' ;
64+ const encodeBase64 = configuration . encodeBase64 ?? false ;
65+
66+ return {
67+ ...configuration ,
68+ devicePlatform,
69+ encodeBase64,
70+ asyncStorage,
71+ eventStore,
72+ plugins,
73+ } ;
74+ } ;
75+
3976/**
4077 * Creates a new tracker instance with the given configuration
4178 * @param configuration - Configuration for the tracker
4279 * @returns Tracker instance
4380 */
44- export async function newTracker (
45- configuration : TrackerConfiguration &
46- EmitterConfiguration &
47- SessionConfiguration &
48- SubjectConfiguration &
49- EventStoreConfiguration &
50- ScreenTrackingConfiguration &
51- PlatformContextConfiguration &
52- DeepLinkConfiguration &
53- AppLifecycleConfiguration
54- ) : Promise < ReactNativeTracker > {
55- const { namespace, appId, encodeBase64 = false } = configuration ;
56- if ( configuration . eventStore === undefined ) {
57- configuration . eventStore = await newReactNativeEventStore ( configuration ) ;
58- }
81+ export async function newTracker ( configuration : Configuration ) : Promise < ReactNativeTracker > {
82+ const normalizedConfiguration = await normalizeTrackerConfiguration ( configuration ) ;
83+
84+ const { namespace, appId, encodeBase64 } = normalizedConfiguration ;
5985
60- const emitter = newEmitter ( configuration ) ;
86+ const emitter = newEmitter ( normalizedConfiguration ) ;
6187 const callback = ( payload : PayloadBuilder ) : void => {
6288 emitter . input ( payload . build ( ) ) ;
6389 } ;
6490
6591 const core = trackerCore ( { base64 : encodeBase64 , callback } ) ;
6692
67- core . setPlatform ( configuration . devicePlatform ?? 'mob' ) ;
93+ core . setPlatform ( normalizedConfiguration . devicePlatform ) ;
6894 core . setTrackerVersion ( 'rn-' + version ) ;
6995 core . setTrackerNamespace ( namespace ) ;
7096 if ( appId ) {
@@ -73,31 +99,31 @@ export async function newTracker(
7399
74100 const { addPlugin } = newPlugins ( namespace , core ) ;
75101
76- const sessionPlugin = await newSessionPlugin ( configuration ) ;
102+ const sessionPlugin = await newSessionPlugin ( normalizedConfiguration ) ;
77103 addPlugin ( sessionPlugin ) ;
78104
79- const deepLinksPlugin = await newDeepLinksPlugin ( configuration , core ) ;
105+ const deepLinksPlugin = newDeepLinksPlugin ( normalizedConfiguration , core ) ;
80106 addPlugin ( deepLinksPlugin ) ;
81107
82- const subject = newSubject ( core , configuration ) ;
108+ const subject = newSubject ( core , normalizedConfiguration ) ;
83109 addPlugin ( subject . subjectPlugin ) ;
84110
85- const screenPlugin = ScreenTrackingPlugin ( configuration ) ;
111+ const screenPlugin = ScreenTrackingPlugin ( normalizedConfiguration ) ;
86112 addPlugin ( { plugin : screenPlugin } ) ;
87113
88- const platformContextPlugin = await newPlatformContextPlugin ( configuration ) ;
114+ const platformContextPlugin = await newPlatformContextPlugin ( normalizedConfiguration ) ;
89115 addPlugin ( platformContextPlugin ) ;
90116
91- const lifecyclePlugin = await newAppLifecyclePlugin ( configuration , core ) ;
117+ const lifecyclePlugin = await newAppLifecyclePlugin ( normalizedConfiguration , core ) ;
92118 addPlugin ( lifecyclePlugin ) ;
93119
94- const installPlugin = newAppInstallPlugin ( configuration , core ) ;
120+ const installPlugin = newAppInstallPlugin ( normalizedConfiguration , core ) ;
95121 addPlugin ( installPlugin ) ;
96122
97- const appContextPlugin = newAppContextPlugin ( configuration ) ;
123+ const appContextPlugin = newAppContextPlugin ( normalizedConfiguration ) ;
98124 addPlugin ( appContextPlugin ) ;
99125
100- ( configuration . plugins ?? [ ] ) . forEach ( ( plugin ) => addPlugin ( { plugin } ) ) ;
126+ normalizedConfiguration . plugins . forEach ( ( plugin ) => addPlugin ( { plugin } ) ) ;
101127
102128 const tracker : ReactNativeTracker = {
103129 ...newTrackEventFunctions ( core ) ,
0 commit comments