From 7fb07498da41a92f8accf51872aeb5fca1812540 Mon Sep 17 00:00:00 2001 From: Samantha Puth Date: Thu, 7 Nov 2019 12:56:07 -0800 Subject: [PATCH 1/4] Append this._storageSuffix (which includes API key) to unsentKey before storing in AsyncStorage Currently, when saving events to AsyncStorage, we we are only using unsentKey as the identifier. This can cause events from multiple projects to get mixed up. By appending this._storageSuffix, we are adding more specificity to keep unsent events within the correct project scope. --- src/amplitude-client.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/amplitude-client.js b/src/amplitude-client.js index 607975e8..fc7dafcf 100644 --- a/src/amplitude-client.js +++ b/src/amplitude-client.js @@ -168,7 +168,7 @@ AmplitudeClient.prototype.init = function init(apiKey, opt_userId, opt_config, o if (AsyncStorage) { Promise.all([ AsyncStorage.getItem(this._storageSuffix), - AsyncStorage.getItem(this.options.unsentKey), + AsyncStorage.getItem(this.options.unsentKey + this._storageSuffix), AsyncStorage.getItem(this.options.unsentIdentifyKey), ]).then((values) => { if (values[0]) { @@ -702,7 +702,7 @@ AmplitudeClient.prototype._saveReferrer = function _saveReferrer(referrer) { AmplitudeClient.prototype.saveEvents = function saveEvents() { try { if (AsyncStorage) { - AsyncStorage.setItem(this.options.unsentKey, JSON.stringify(this._unsentEvents)); + AsyncStorage.setItem(this.options.unsentKey + this._storageSuffix, JSON.stringify(this._unsentEvents)); } else { this._setInStorage(localStorage, this.options.unsentKey, JSON.stringify(this._unsentEvents)); } From e99863fc07a78713bf811e597e218cffe203b005 Mon Sep 17 00:00:00 2001 From: Samantha Puth Date: Fri, 15 Nov 2019 11:52:04 -0800 Subject: [PATCH 2/4] Append storageSuffix to unsentIdentifyKeys as well, add migration to add suffix to current events keyname --- src/amplitude-client.js | 114 +++++++++++++++++++++++++--------------- 1 file changed, 72 insertions(+), 42 deletions(-) diff --git a/src/amplitude-client.js b/src/amplitude-client.js index fc7dafcf..ff809d79 100644 --- a/src/amplitude-client.js +++ b/src/amplitude-client.js @@ -166,52 +166,54 @@ AmplitudeClient.prototype.init = function init(apiKey, opt_userId, opt_config, o }; if (AsyncStorage) { - Promise.all([ - AsyncStorage.getItem(this._storageSuffix), - AsyncStorage.getItem(this.options.unsentKey + this._storageSuffix), - AsyncStorage.getItem(this.options.unsentIdentifyKey), - ]).then((values) => { - if (values[0]) { - const cookieData = JSON.parse(values[0]); - if (cookieData) { - _loadCookieDataProps(this, cookieData); + this._migrateUnsentEvents(() => { + Promise.all([ + AsyncStorage.getItem(this._storageSuffix), + AsyncStorage.getItem(this.options.unsentKey + this._storageSuffix), + AsyncStorage.getItem(this.options.unsentIdentifyKey + this._storageSuffix), + ]).then((values) => { + if (values[0]) { + const cookieData = JSON.parse(values[0]); + if (cookieData) { + _loadCookieDataProps(this, cookieData); + } } - } - if (this.options.saveEvents) { - this._unsentEvents = this._parseSavedUnsentEventsString(values[1]).concat(this._unsentEvents); - this._unsentIdentifys = this._parseSavedUnsentEventsString(values[2]).concat(this._unsentIdentifys); - } - if (DeviceInfo) { - Promise.all([ - DeviceInfo.getCarrier(), - DeviceInfo.getModel(), - DeviceInfo.getManufacturer(), - DeviceInfo.getUniqueId(), - ]).then(values => { - this.deviceInfo = { - carrier: values[0], - model: values[1], - manufacturer: values[2] - }; - initFromStorage(values[3]); + if (this.options.saveEvents) { + this._unsentEvents = this._parseSavedUnsentEventsString(values[1]).concat(this._unsentEvents); + this._unsentIdentifys = this._parseSavedUnsentEventsString(values[2]).concat(this._unsentIdentifys); + } + if (DeviceInfo) { + Promise.all([ + DeviceInfo.getCarrier(), + DeviceInfo.getModel(), + DeviceInfo.getManufacturer(), + DeviceInfo.getUniqueId(), + ]).then(values => { + this.deviceInfo = { + carrier: values[0], + model: values[1], + manufacturer: values[2] + }; + initFromStorage(values[3]); + this.runQueuedFunctions(); + if (type(opt_callback) === 'function') { + opt_callback(this); + } + }).catch((err) => { + this.options.onError(err); + }); + } else { + initFromStorage(); this.runQueuedFunctions(); - if (type(opt_callback) === 'function') { - opt_callback(this); - } - }).catch((err) => { - this.options.onError(err); - }); - } else { - initFromStorage(); - this.runQueuedFunctions(); - } - }).catch((err) => { - this.options.onError(err); + } + }).catch((err) => { + this.options.onError(err); + }); }); } else { if (this.options.saveEvents) { - this._unsentEvents = this._loadSavedUnsentEvents(this.options.unsentKey).concat(this._unsentEvents); - this._unsentIdentifys = this._loadSavedUnsentEvents(this.options.unsentIdentifyKey).concat(this._unsentIdentifys); + this._unsentEvents = this._loadSavedUnsentEvents(this.options.unsentKey + this._storageSuffix).concat(this._unsentEvents); + this._unsentIdentifys = this._loadSavedUnsentEvents(this.options.unsentIdentifyKey + this._storageSuffix).concat(this._unsentIdentifys); } initFromStorage(); this.runQueuedFunctions(); @@ -225,6 +227,34 @@ AmplitudeClient.prototype.init = function init(apiKey, opt_userId, opt_config, o } }; +/** + * @private + */ +AmplitudeClient.prototype._migrateUnsentEvents = function _migrateUnsentEvents(cb) { + Promise.all([ + AsyncStorage.getItem(this.options.unsentKey), + AsyncStorage.getItem(this.options.unsentIdentifyKey), + ]).then((values) => { + if (this.options.saveEvents) { + var unsentEventsString = values[0]; + var unsentIdentifyKey = values[1]; + Promise.all([ + AsyncStorage.setItem(this.options.unsentKey + this._storageSuffix, unsentEventsString), + AsyncStorage.setItem(this.options.unsentIdentifyKey + this._storageSuffix, unsentIdentifyKey ), + ]).then(() => { + Promise.all([ + AsyncStorage.removeItem(this.options.unsentKey), + AsyncStorage.removeItem(this.options.unsentIdentifyKey), + ]).then(cb); + }).catch((err) => { + this.options.onError(err); + });; + } + }).catch((err) => { + this.options.onError(err); + }); +}; + /** * @private */ @@ -710,7 +740,7 @@ AmplitudeClient.prototype.saveEvents = function saveEvents() { try { if (AsyncStorage) { - AsyncStorage.setItem(this.options.unsentIdentifyKey, JSON.stringify(this._unsentIdentifys)); + AsyncStorage.setItem(this.options.unsentIdentifyKey + this._storageSuffix, JSON.stringify(this._unsentIdentifys)); } else { this._setInStorage(localStorage, this.options.unsentIdentifyKey, JSON.stringify(this._unsentIdentifys)); } From 86479c48b4abaed6bf002c5b6a0d775c2c80adc3 Mon Sep 17 00:00:00 2001 From: Samantha Puth Date: Mon, 18 Nov 2019 15:08:00 -0800 Subject: [PATCH 3/4] Fix Tests. TODO: add tests for react native --- src/amplitude-client.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/amplitude-client.js b/src/amplitude-client.js index ff809d79..46b08eb0 100644 --- a/src/amplitude-client.js +++ b/src/amplitude-client.js @@ -212,8 +212,8 @@ AmplitudeClient.prototype.init = function init(apiKey, opt_userId, opt_config, o }); } else { if (this.options.saveEvents) { - this._unsentEvents = this._loadSavedUnsentEvents(this.options.unsentKey + this._storageSuffix).concat(this._unsentEvents); - this._unsentIdentifys = this._loadSavedUnsentEvents(this.options.unsentIdentifyKey + this._storageSuffix).concat(this._unsentIdentifys); + this._unsentEvents = this._loadSavedUnsentEvents(this.options.unsentKey).concat(this._unsentEvents); + this._unsentIdentifys = this._loadSavedUnsentEvents(this.options.unsentIdentifyKey).concat(this._unsentIdentifys); } initFromStorage(); this.runQueuedFunctions(); @@ -240,7 +240,7 @@ AmplitudeClient.prototype._migrateUnsentEvents = function _migrateUnsentEvents(c var unsentIdentifyKey = values[1]; Promise.all([ AsyncStorage.setItem(this.options.unsentKey + this._storageSuffix, unsentEventsString), - AsyncStorage.setItem(this.options.unsentIdentifyKey + this._storageSuffix, unsentIdentifyKey ), + AsyncStorage.setItem(this.options.unsentIdentifyKey + this._storageSuffix, unsentIdentifyKey), ]).then(() => { Promise.all([ AsyncStorage.removeItem(this.options.unsentKey), @@ -248,7 +248,7 @@ AmplitudeClient.prototype._migrateUnsentEvents = function _migrateUnsentEvents(c ]).then(cb); }).catch((err) => { this.options.onError(err); - });; + }); } }).catch((err) => { this.options.onError(err); From fe025f04c5075d4f828158d63f725f50623a1c47 Mon Sep 17 00:00:00 2001 From: Samantha Puth Date: Fri, 22 Nov 2019 16:16:40 -0800 Subject: [PATCH 4/4] Bump Version v5.7.0 --- CHANGELOG.md | 3 +++ README.md | 2 +- package.json | 2 +- src/amplitude-snippet.js | 4 ++-- 4 files changed, 7 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index bd6bb088..912b616b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,6 @@ +### 5.7.0 (November 22, 2019) +* Namespace AsyncStorage with api key to prevent cross domain contamination + ### 5.6.0 (October 21, 2019) * Drop esm module from package.json to prevent it from being the default build. diff --git a/README.md b/README.md index 438ef299..0637bee8 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,7 @@ Please see our [installation guide](https://amplitude.zendesk.com/hc/en-us/artic [![npm version](https://badge.fury.io/js/amplitude-js.svg)](https://badge.fury.io/js/amplitude-js) [![Bower version](https://badge.fury.io/bo/amplitude-js.svg)](https://badge.fury.io/bo/amplitude-js) -[5.6.0 - Released on October 21, 2019](https://github.com/amplitude/Amplitude-JavaScript/releases/latest) +[5.7.0 - Released on November 22, 2019](https://github.com/amplitude/Amplitude-JavaScript/releases/latest) # JavaScript SDK Reference # diff --git a/package.json b/package.json index dcf2b803..3146c776 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "amplitude-js", "author": "Amplitude ", - "version": "5.6.0", + "version": "5.7.0", "license": "MIT", "description": "Javascript library for Amplitude Analytics", "keywords": [ diff --git a/src/amplitude-snippet.js b/src/amplitude-snippet.js index 00955a4f..613d0d18 100644 --- a/src/amplitude-snippet.js +++ b/src/amplitude-snippet.js @@ -2,10 +2,10 @@ var amplitude = window.amplitude || {'_q':[],'_iq':{}}; var as = document.createElement('script'); as.type = 'text/javascript'; - as.integrity = 'sha384-t5vT47el2d0e6uQ1h75P9Lbzo8by6pbk+Rg41Gm4xuTGR+eDLpbWslKUtZMDe9Bj'; + as.integrity = 'sha384-rSEVPt+HsYVwBs0EY4dB3fOcSZOW9cbAQV2CqsLFDjNbdiNyoXcGruquK0IyWxAZ'; as.crossOrigin = 'anonymous'; as.async = true; - as.src = 'https://cdn.amplitude.com/libs/amplitude-5.6.0-min.gz.js'; + as.src = 'https://cdn.amplitude.com/libs/amplitude-5.7.0-min.gz.js'; as.onload = function() {if(!window.amplitude.runQueuedFunctions) {console.log('[Amplitude] Error: could not load SDK');}}; var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(as, s);