Skip to content

Commit e99863f

Browse files
committed
Append storageSuffix to unsentIdentifyKeys as well, add migration to add suffix to current events keyname
1 parent 7fb0749 commit e99863f

File tree

1 file changed

+72
-42
lines changed

1 file changed

+72
-42
lines changed

src/amplitude-client.js

Lines changed: 72 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -166,52 +166,54 @@ AmplitudeClient.prototype.init = function init(apiKey, opt_userId, opt_config, o
166166
};
167167

168168
if (AsyncStorage) {
169-
Promise.all([
170-
AsyncStorage.getItem(this._storageSuffix),
171-
AsyncStorage.getItem(this.options.unsentKey + this._storageSuffix),
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);
169+
this._migrateUnsentEvents(() => {
170+
Promise.all([
171+
AsyncStorage.getItem(this._storageSuffix),
172+
AsyncStorage.getItem(this.options.unsentKey + this._storageSuffix),
173+
AsyncStorage.getItem(this.options.unsentIdentifyKey + this._storageSuffix),
174+
]).then((values) => {
175+
if (values[0]) {
176+
const cookieData = JSON.parse(values[0]);
177+
if (cookieData) {
178+
_loadCookieDataProps(this, cookieData);
179+
}
178180
}
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);
183-
}
184-
if (DeviceInfo) {
185-
Promise.all([
186-
DeviceInfo.getCarrier(),
187-
DeviceInfo.getModel(),
188-
DeviceInfo.getManufacturer(),
189-
DeviceInfo.getUniqueId(),
190-
]).then(values => {
191-
this.deviceInfo = {
192-
carrier: values[0],
193-
model: values[1],
194-
manufacturer: values[2]
195-
};
196-
initFromStorage(values[3]);
181+
if (this.options.saveEvents) {
182+
this._unsentEvents = this._parseSavedUnsentEventsString(values[1]).concat(this._unsentEvents);
183+
this._unsentIdentifys = this._parseSavedUnsentEventsString(values[2]).concat(this._unsentIdentifys);
184+
}
185+
if (DeviceInfo) {
186+
Promise.all([
187+
DeviceInfo.getCarrier(),
188+
DeviceInfo.getModel(),
189+
DeviceInfo.getManufacturer(),
190+
DeviceInfo.getUniqueId(),
191+
]).then(values => {
192+
this.deviceInfo = {
193+
carrier: values[0],
194+
model: values[1],
195+
manufacturer: values[2]
196+
};
197+
initFromStorage(values[3]);
198+
this.runQueuedFunctions();
199+
if (type(opt_callback) === 'function') {
200+
opt_callback(this);
201+
}
202+
}).catch((err) => {
203+
this.options.onError(err);
204+
});
205+
} else {
206+
initFromStorage();
197207
this.runQueuedFunctions();
198-
if (type(opt_callback) === 'function') {
199-
opt_callback(this);
200-
}
201-
}).catch((err) => {
202-
this.options.onError(err);
203-
});
204-
} else {
205-
initFromStorage();
206-
this.runQueuedFunctions();
207-
}
208-
}).catch((err) => {
209-
this.options.onError(err);
208+
}
209+
}).catch((err) => {
210+
this.options.onError(err);
211+
});
210212
});
211213
} else {
212214
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+
this._unsentEvents = this._loadSavedUnsentEvents(this.options.unsentKey + this._storageSuffix).concat(this._unsentEvents);
216+
this._unsentIdentifys = this._loadSavedUnsentEvents(this.options.unsentIdentifyKey + this._storageSuffix).concat(this._unsentIdentifys);
215217
}
216218
initFromStorage();
217219
this.runQueuedFunctions();
@@ -225,6 +227,34 @@ AmplitudeClient.prototype.init = function init(apiKey, opt_userId, opt_config, o
225227
}
226228
};
227229

230+
/**
231+
* @private
232+
*/
233+
AmplitudeClient.prototype._migrateUnsentEvents = function _migrateUnsentEvents(cb) {
234+
Promise.all([
235+
AsyncStorage.getItem(this.options.unsentKey),
236+
AsyncStorage.getItem(this.options.unsentIdentifyKey),
237+
]).then((values) => {
238+
if (this.options.saveEvents) {
239+
var unsentEventsString = values[0];
240+
var unsentIdentifyKey = values[1];
241+
Promise.all([
242+
AsyncStorage.setItem(this.options.unsentKey + this._storageSuffix, unsentEventsString),
243+
AsyncStorage.setItem(this.options.unsentIdentifyKey + this._storageSuffix, unsentIdentifyKey ),
244+
]).then(() => {
245+
Promise.all([
246+
AsyncStorage.removeItem(this.options.unsentKey),
247+
AsyncStorage.removeItem(this.options.unsentIdentifyKey),
248+
]).then(cb);
249+
}).catch((err) => {
250+
this.options.onError(err);
251+
});;
252+
}
253+
}).catch((err) => {
254+
this.options.onError(err);
255+
});
256+
};
257+
228258
/**
229259
* @private
230260
*/
@@ -710,7 +740,7 @@ AmplitudeClient.prototype.saveEvents = function saveEvents() {
710740

711741
try {
712742
if (AsyncStorage) {
713-
AsyncStorage.setItem(this.options.unsentIdentifyKey, JSON.stringify(this._unsentIdentifys));
743+
AsyncStorage.setItem(this.options.unsentIdentifyKey + this._storageSuffix, JSON.stringify(this._unsentIdentifys));
714744
} else {
715745
this._setInStorage(localStorage, this.options.unsentIdentifyKey, JSON.stringify(this._unsentIdentifys));
716746
}

0 commit comments

Comments
 (0)