Skip to content

Commit b06134c

Browse files
authored
Merge pull request #204 from sputh/better-react-native-async-storage
Append this._storageSuffix (which includes API key) to unsentKey
2 parents e609a65 + fe025f0 commit b06134c

File tree

5 files changed

+78
-45
lines changed

5 files changed

+78
-45
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
### 5.7.0 (November 22, 2019)
2+
* Namespace AsyncStorage with api key to prevent cross domain contamination
3+
14
### 5.6.0 (October 21, 2019)
25

36
* Drop esm module from package.json to prevent it from being the default build.

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ Please see our [installation guide](https://amplitude.zendesk.com/hc/en-us/artic
1111
[![npm version](https://badge.fury.io/js/amplitude-js.svg)](https://badge.fury.io/js/amplitude-js)
1212
[![Bower version](https://badge.fury.io/bo/amplitude-js.svg)](https://badge.fury.io/bo/amplitude-js)
1313

14-
[5.6.0 - Released on October 21, 2019](https://github.com/amplitude/Amplitude-JavaScript/releases/latest)
14+
[5.7.0 - Released on November 22, 2019](https://github.com/amplitude/Amplitude-JavaScript/releases/latest)
1515

1616

1717
# JavaScript SDK Reference #

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "amplitude-js",
33
"author": "Amplitude <support@amplitude.com>",
4-
"version": "5.6.0",
4+
"version": "5.7.0",
55
"license": "MIT",
66
"description": "Javascript library for Amplitude Analytics",
77
"keywords": [

src/amplitude-client.js

Lines changed: 71 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -166,47 +166,49 @@ 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),
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) {
@@ -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
*/
@@ -702,15 +732,15 @@ AmplitudeClient.prototype._saveReferrer = function _saveReferrer(referrer) {
702732
AmplitudeClient.prototype.saveEvents = function saveEvents() {
703733
try {
704734
if (AsyncStorage) {
705-
AsyncStorage.setItem(this.options.unsentKey, JSON.stringify(this._unsentEvents));
735+
AsyncStorage.setItem(this.options.unsentKey + this._storageSuffix, JSON.stringify(this._unsentEvents));
706736
} else {
707737
this._setInStorage(localStorage, this.options.unsentKey, JSON.stringify(this._unsentEvents));
708738
}
709739
} catch (e) {}
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
}

src/amplitude-snippet.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
var amplitude = window.amplitude || {'_q':[],'_iq':{}};
33
var as = document.createElement('script');
44
as.type = 'text/javascript';
5-
as.integrity = 'sha384-t5vT47el2d0e6uQ1h75P9Lbzo8by6pbk+Rg41Gm4xuTGR+eDLpbWslKUtZMDe9Bj';
5+
as.integrity = 'sha384-rSEVPt+HsYVwBs0EY4dB3fOcSZOW9cbAQV2CqsLFDjNbdiNyoXcGruquK0IyWxAZ';
66
as.crossOrigin = 'anonymous';
77
as.async = true;
8-
as.src = 'https://cdn.amplitude.com/libs/amplitude-5.6.0-min.gz.js';
8+
as.src = 'https://cdn.amplitude.com/libs/amplitude-5.7.0-min.gz.js';
99
as.onload = function() {if(!window.amplitude.runQueuedFunctions) {console.log('[Amplitude] Error: could not load SDK');}};
1010
var s = document.getElementsByTagName('script')[0];
1111
s.parentNode.insertBefore(as, s);

0 commit comments

Comments
 (0)