@@ -5673,7 +5673,7 @@ var DEFAULT_OPTIONS = {
5673
5673
*/
5674
5674
var AmplitudeClient = function AmplitudeClient ( instanceName ) {
5675
5675
this . _instanceName = utils . isEmptyString ( instanceName ) ? constants . DEFAULT_INSTANCE : instanceName . toLowerCase ( ) ;
5676
- this . _storageSuffix = this . _instanceName === constants . DEFAULT_INSTANCE ? '' : '_' + this . _instanceName ;
5676
+ this . _legacyStorageSuffix = this . _instanceName === constants . DEFAULT_INSTANCE ? '' : '_' + this . _instanceName ;
5677
5677
this . _unsentEvents = [ ] ;
5678
5678
this . _unsentIdentifys = [ ] ;
5679
5679
this . _ua = new uaParser ( navigator . userAgent ) . getResult ( ) ;
@@ -5716,6 +5716,7 @@ AmplitudeClient.prototype.init = function init(apiKey, opt_userId, opt_config, o
5716
5716
5717
5717
try {
5718
5718
this . options . apiKey = apiKey ;
5719
+ this . _storageSuffix = '_' + apiKey + this . _legacyStorageSuffix ;
5719
5720
_parseConfig ( this . options , opt_config ) ;
5720
5721
this . cookieStorage . options ( {
5721
5722
expirationDays : this . options . cookieExpiration ,
@@ -5864,6 +5865,29 @@ AmplitudeClient.prototype._apiKeySet = function _apiKeySet(methodName) {
5864
5865
*/
5865
5866
AmplitudeClient . prototype . _loadSavedUnsentEvents = function _loadSavedUnsentEvents ( unsentKey ) {
5866
5867
var savedUnsentEventsString = this . _getFromStorage ( localStorage$1 , unsentKey ) ;
5868
+ var events = this . _parseSavedUnsentEventsString ( savedUnsentEventsString , unsentKey ) ;
5869
+
5870
+ var savedUnsentEventsStringLegacy = this . _getFromStorageLegacy ( localStorage$1 , unsentKey ) ;
5871
+ var legacyEvents = this . _parseSavedUnsentEventsString ( savedUnsentEventsStringLegacy , unsentKey ) ;
5872
+
5873
+ var unsentEvents = legacyEvents . concat ( events ) ;
5874
+
5875
+ // Migrate legacy events out of storage
5876
+ this . _removeFromLegacyStorage ( localStorage$1 , unsentKey ) ;
5877
+ this . _setInStorage ( localStorage$1 , unsentKey , JSON . stringify ( unsentEvents ) ) ;
5878
+
5879
+ return unsentEvents ;
5880
+ } ;
5881
+
5882
+ AmplitudeClient . prototype . _removeFromLegacyStorage = function _removeFromLegacyStorage ( storage , key ) {
5883
+ storage . removeItem ( key + this . _legacyStorageSuffix ) ;
5884
+ } ;
5885
+
5886
+ /**
5887
+ * Load saved events from localStorage. JSON deserializes event array. Handles case where string is corrupted.
5888
+ * @private
5889
+ */
5890
+ AmplitudeClient . prototype . _parseSavedUnsentEventsString = function _parseSavedUnsentEventsString ( savedUnsentEventsString , unsentKey ) {
5867
5891
if ( utils . isEmptyString ( savedUnsentEventsString ) ) {
5868
5892
return [ ] ; // new app, does not have any saved events
5869
5893
}
@@ -5977,6 +6001,15 @@ AmplitudeClient.prototype._getFromStorage = function _getFromStorage(storage, ke
5977
6001
return storage . getItem ( key + this . _storageSuffix ) ;
5978
6002
} ;
5979
6003
6004
+ /**
6005
+ * Helper function to fetch values from storage
6006
+ * Storage argument allows for localStoraoge and sessionStoraoge
6007
+ * @private
6008
+ */
6009
+ AmplitudeClient . prototype . _getFromStorageLegacy = function _getFromStorageLegacy ( storage , key ) {
6010
+ return storage . getItem ( key + this . _legacyStorageSuffix ) ;
6011
+ } ;
6012
+
5980
6013
/**
5981
6014
* Helper function to set values in storage
5982
6015
* Storage argument allows for localStoraoge and sessionStoraoge
@@ -5994,7 +6027,7 @@ AmplitudeClient.prototype._setInStorage = function _setInStorage(storage, key, v
5994
6027
*/
5995
6028
var _upgradeCookeData = function _upgradeCookeData ( scope ) {
5996
6029
// skip if migration already happened
5997
- var cookieData = scope . cookieStorage . get ( scope . options . cookieName ) ;
6030
+ var cookieData = scope . cookieStorage . get ( scope . options . cookieName + scope . _storageSuffix ) ;
5998
6031
if ( type ( cookieData ) === 'object' && cookieData . deviceId && cookieData . sessionId && cookieData . lastEventTime ) {
5999
6032
return ;
6000
6033
}
@@ -6047,34 +6080,45 @@ var _upgradeCookeData = function _upgradeCookeData(scope) {
6047
6080
*/
6048
6081
var _loadCookieData = function _loadCookieData ( scope ) {
6049
6082
var cookieData = scope . cookieStorage . get ( scope . options . cookieName + scope . _storageSuffix ) ;
6083
+
6050
6084
if ( type ( cookieData ) === 'object' ) {
6051
- if ( cookieData . deviceId ) {
6052
- scope . options . deviceId = cookieData . deviceId ;
6053
- }
6054
- if ( cookieData . userId ) {
6055
- scope . options . userId = cookieData . userId ;
6056
- }
6057
- if ( cookieData . optOut !== null && cookieData . optOut !== undefined ) {
6058
- scope . options . optOut = cookieData . optOut ;
6059
- }
6060
- if ( cookieData . sessionId ) {
6061
- scope . _sessionId = parseInt ( cookieData . sessionId ) ;
6062
- }
6063
- if ( cookieData . lastEventTime ) {
6064
- scope . _lastEventTime = parseInt ( cookieData . lastEventTime ) ;
6065
- }
6066
- if ( cookieData . eventId ) {
6067
- scope . _eventId = parseInt ( cookieData . eventId ) ;
6068
- }
6069
- if ( cookieData . identifyId ) {
6070
- scope . _identifyId = parseInt ( cookieData . identifyId ) ;
6071
- }
6072
- if ( cookieData . sequenceNumber ) {
6073
- scope . _sequenceNumber = parseInt ( cookieData . sequenceNumber ) ;
6085
+ _loadCookieDataProps ( scope , cookieData ) ;
6086
+ } else {
6087
+ var legacyCookieData = scope . cookieStorage . get ( scope . options . cookieName + scope . _legacyStorageSuffix ) ;
6088
+ if ( type ( legacyCookieData ) === 'object' ) {
6089
+ scope . cookieStorage . remove ( scope . options . cookieName + scope . _legacyStorageSuffix ) ;
6090
+ _loadCookieDataProps ( scope , legacyCookieData ) ;
6074
6091
}
6075
6092
}
6076
6093
} ;
6077
6094
6095
+ var _loadCookieDataProps = function _loadCookieDataProps ( scope , cookieData ) {
6096
+ if ( cookieData . deviceId ) {
6097
+ scope . options . deviceId = cookieData . deviceId ;
6098
+ }
6099
+ if ( cookieData . userId ) {
6100
+ scope . options . userId = cookieData . userId ;
6101
+ }
6102
+ if ( cookieData . optOut !== null && cookieData . optOut !== undefined ) {
6103
+ scope . options . optOut = cookieData . optOut ;
6104
+ }
6105
+ if ( cookieData . sessionId ) {
6106
+ scope . _sessionId = parseInt ( cookieData . sessionId ) ;
6107
+ }
6108
+ if ( cookieData . lastEventTime ) {
6109
+ scope . _lastEventTime = parseInt ( cookieData . lastEventTime ) ;
6110
+ }
6111
+ if ( cookieData . eventId ) {
6112
+ scope . _eventId = parseInt ( cookieData . eventId ) ;
6113
+ }
6114
+ if ( cookieData . identifyId ) {
6115
+ scope . _identifyId = parseInt ( cookieData . identifyId ) ;
6116
+ }
6117
+ if ( cookieData . sequenceNumber ) {
6118
+ scope . _sequenceNumber = parseInt ( cookieData . sequenceNumber ) ;
6119
+ }
6120
+ } ;
6121
+
6078
6122
/**
6079
6123
* Saves deviceId, userId, event meta data to amplitude cookie
6080
6124
* @private
0 commit comments