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-client.js b/src/amplitude-client.js index 607975e8..46b08eb0 100644 --- a/src/amplitude-client.js +++ b/src/amplitude-client.js @@ -166,47 +166,49 @@ 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.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) { @@ -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 */ @@ -702,7 +732,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)); } @@ -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)); } 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);