@@ -13,17 +13,14 @@ import UUID from './uuid';
13
13
import { version } from '../package.json' ;
14
14
import DEFAULT_OPTIONS from './options' ;
15
15
16
- let asyncStorage ;
16
+ let AsyncStorage ;
17
17
let Platform ;
18
18
let DeviceInfo ;
19
19
if ( BUILD_COMPAT_REACT_NATIVE ) {
20
20
const reactNative = require ( 'react-native' ) ;
21
+ AsyncStorage = require ( '@react-native-community/async-storage' ) . default ;
21
22
Platform = reactNative . Platform ;
22
- asyncStorage = reactNative . AsyncStorage ;
23
- try {
24
- DeviceInfo = require ( 'react-native-device-info' ) ;
25
- } catch ( e ) {
26
- }
23
+ DeviceInfo = require ( 'react-native-device-info' ) ;
27
24
}
28
25
29
26
/**
@@ -98,19 +95,20 @@ AmplitudeClient.prototype.init = function init(apiKey, opt_userId, opt_config, o
98
95
} ) ;
99
96
this . options . domain = this . cookieStorage . options ( ) . domain ;
100
97
101
- if ( this . _instanceName === Constants . DEFAULT_INSTANCE ) {
102
- _upgradeCookieData ( this ) ;
98
+ if ( ! BUILD_COMPAT_REACT_NATIVE ) {
99
+ if ( this . _instanceName === Constants . DEFAULT_INSTANCE ) {
100
+ _upgradeCookieData ( this ) ;
101
+ }
103
102
}
104
103
_loadCookieData ( this ) ;
105
104
this . _pendingReadStorage = true ;
106
105
107
-
108
- const initFromStorage = ( ) => {
106
+ const initFromStorage = ( deviceId ) => {
109
107
// load deviceId and userId from input, or try to fetch existing value from cookie
110
108
this . options . deviceId = ( type ( opt_config ) === 'object' && type ( opt_config . deviceId ) === 'string' &&
111
109
! utils . isEmptyString ( opt_config . deviceId ) && opt_config . deviceId ) ||
112
110
( this . options . deviceIdFromUrlParam && this . _getDeviceIdFromUrlParam ( this . _getUrlParams ( ) ) ) ||
113
- this . options . deviceId || UUID ( ) + 'R' ;
111
+ this . options . deviceId || deviceId || UUID ( ) + 'R' ;
114
112
this . options . userId =
115
113
( type ( opt_userId ) === 'string' && ! utils . isEmptyString ( opt_userId ) && opt_userId ) ||
116
114
( type ( opt_userId ) === 'number' && opt_userId . toString ( ) ) ||
@@ -136,9 +134,6 @@ AmplitudeClient.prototype.init = function init(apiKey, opt_userId, opt_config, o
136
134
137
135
// load unsent events and identifies before any attempt to log new ones
138
136
if ( this . options . saveEvents ) {
139
- this . _unsentEvents = this . _loadSavedUnsentEvents ( this . options . unsentKey ) . concat ( this . _unsentEvents ) ;
140
- this . _unsentIdentifys = this . _loadSavedUnsentEvents ( this . options . unsentIdentifyKey ) . concat ( this . _unsentIdentifys ) ;
141
-
142
137
// validate event properties for unsent events
143
138
for ( let i = 0 ; i < this . _unsentEvents . length ; i ++ ) {
144
139
var eventProperties = this . _unsentEvents [ i ] . event_properties ;
@@ -170,38 +165,63 @@ AmplitudeClient.prototype.init = function init(apiKey, opt_userId, opt_config, o
170
165
this . _isInitialized = true ;
171
166
} ;
172
167
173
- if ( asyncStorage ) {
174
- asyncStorage . getItem ( this . _storageSuffix ) . then ( ( json ) => {
175
- const cookieData = JSON . parse ( json ) ;
176
- if ( cookieData ) {
177
- _loadCookieDataProps ( this , cookieData ) ;
168
+ if ( AsyncStorage ) {
169
+ Promise . all ( [
170
+ AsyncStorage . getItem ( this . _storageSuffix ) ,
171
+ AsyncStorage . getItem ( this . options . unsentKey ) ,
172
+ AsyncStorage . getItem ( this . options . unsentIdentifyKey ) ,
173
+ ] ) . then ( ( values ) => {
174
+ if ( values [ 0 ] ) {
175
+ const cookieData = JSON . parse ( values [ 0 ] ) ;
176
+ if ( cookieData ) {
177
+ _loadCookieDataProps ( this , cookieData ) ;
178
+ }
179
+ }
180
+ if ( this . options . saveEvents ) {
181
+ this . _unsentEvents = this . _parseSavedUnsentEventsString ( values [ 1 ] ) . concat ( this . _unsentEvents ) ;
182
+ this . _unsentIdentifys = this . _parseSavedUnsentEventsString ( values [ 2 ] ) . concat ( this . _unsentIdentifys ) ;
178
183
}
179
184
if ( DeviceInfo ) {
180
- Promise . all ( [ DeviceInfo . getCarrier ( ) , DeviceInfo . getModel ( ) , DeviceInfo . getManufacturer ( ) ] ) . then ( values => {
185
+ Promise . all ( [
186
+ DeviceInfo . getCarrier ( ) ,
187
+ DeviceInfo . getModel ( ) ,
188
+ DeviceInfo . getManufacturer ( ) ,
189
+ DeviceInfo . getUniqueId ( ) ,
190
+ ] ) . then ( values => {
181
191
this . deviceInfo = {
182
192
carrier : values [ 0 ] ,
183
193
model : values [ 1 ] ,
184
194
manufacturer : values [ 2 ]
185
195
} ;
186
- initFromStorage ( ) ;
196
+ initFromStorage ( values [ 3 ] ) ;
187
197
this . runQueuedFunctions ( ) ;
198
+ if ( type ( opt_callback ) === 'function' ) {
199
+ opt_callback ( this ) ;
200
+ }
201
+ } ) . catch ( ( err ) => {
202
+ this . options . onError ( err ) ;
188
203
} ) ;
189
204
} else {
190
205
initFromStorage ( ) ;
191
206
this . runQueuedFunctions ( ) ;
192
207
}
208
+ } ) . catch ( ( err ) => {
209
+ this . options . onError ( err ) ;
193
210
} ) ;
194
211
} else {
212
+ if ( this . options . saveEvents ) {
213
+ this . _unsentEvents = this . _loadSavedUnsentEvents ( this . options . unsentKey ) . concat ( this . _unsentEvents ) ;
214
+ this . _unsentIdentifys = this . _loadSavedUnsentEvents ( this . options . unsentIdentifyKey ) . concat ( this . _unsentIdentifys ) ;
215
+ }
195
216
initFromStorage ( ) ;
196
217
this . runQueuedFunctions ( ) ;
218
+ if ( type ( opt_callback ) === 'function' ) {
219
+ opt_callback ( this ) ;
220
+ }
197
221
}
198
-
199
- } catch ( e ) {
200
- utils . log . error ( e ) ;
201
- } finally {
202
- if ( type ( opt_callback ) === 'function' ) {
203
- opt_callback ( this ) ;
204
- }
222
+ } catch ( err ) {
223
+ utils . log . error ( err ) ;
224
+ this . options . onError ( err ) ;
205
225
}
206
226
} ;
207
227
@@ -554,8 +574,8 @@ var _saveCookieData = function _saveCookieData(scope) {
554
574
identifyId : scope . _identifyId ,
555
575
sequenceNumber : scope . _sequenceNumber
556
576
} ;
557
- if ( asyncStorage ) {
558
- asyncStorage . setItem ( scope . _storageSuffix , JSON . stringify ( cookieData ) ) ;
577
+ if ( AsyncStorage ) {
578
+ AsyncStorage . setItem ( scope . _storageSuffix , JSON . stringify ( cookieData ) ) ;
559
579
}
560
580
scope . cookieStorage . set ( scope . options . cookieName + scope . _storageSuffix , cookieData ) ;
561
581
} ;
@@ -681,11 +701,19 @@ AmplitudeClient.prototype._saveReferrer = function _saveReferrer(referrer) {
681
701
*/
682
702
AmplitudeClient . prototype . saveEvents = function saveEvents ( ) {
683
703
try {
684
- this . _setInStorage ( localStorage , this . options . unsentKey , JSON . stringify ( this . _unsentEvents ) ) ;
704
+ if ( AsyncStorage ) {
705
+ AsyncStorage . setItem ( this . options . unsentKey , JSON . stringify ( this . _unsentEvents ) ) ;
706
+ } else {
707
+ this . _setInStorage ( localStorage , this . options . unsentKey , JSON . stringify ( this . _unsentEvents ) ) ;
708
+ }
685
709
} catch ( e ) { }
686
710
687
711
try {
688
- this . _setInStorage ( localStorage , this . options . unsentIdentifyKey , JSON . stringify ( this . _unsentIdentifys ) ) ;
712
+ if ( AsyncStorage ) {
713
+ AsyncStorage . setItem ( this . options . unsentIdentifyKey , JSON . stringify ( this . _unsentIdentifys ) ) ;
714
+ } else {
715
+ this . _setInStorage ( localStorage , this . options . unsentIdentifyKey , JSON . stringify ( this . _unsentIdentifys ) ) ;
716
+ }
689
717
} catch ( e ) { }
690
718
} ;
691
719
@@ -999,7 +1027,9 @@ AmplitudeClient.prototype.setVersionName = function setVersionName(versionName)
999
1027
* @private
1000
1028
*/
1001
1029
AmplitudeClient . prototype . _logEvent = function _logEvent ( eventType , eventProperties , apiProperties , userProperties , groups , groupProperties , timestamp , callback ) {
1002
- _loadCookieData ( this ) ; // reload cookie before each log event to sync event meta-data between windows and tabs
1030
+ if ( ! BUILD_COMPAT_REACT_NATIVE ) {
1031
+ _loadCookieData ( this ) ; // reload cookie before each log event to sync event meta-data between windows and tabs
1032
+ }
1003
1033
if ( ! eventType ) {
1004
1034
if ( type ( callback ) === 'function' ) {
1005
1035
callback ( 0 , 'No request sent' , { reason : 'Missing eventType' } ) ;
0 commit comments