Skip to content

Commit b2eb90e

Browse files
committed
setting null values in storage should remove from storage
1 parent 18e67df commit b2eb90e

File tree

4 files changed

+27
-4
lines changed

4 files changed

+27
-4
lines changed

amplitude.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,12 @@ Amplitude.prototype.runQueuedFunctions = function () {
261261
*/
262262
Amplitude.prototype.setInStorage = function(item, value) {
263263
var key = item + '_' + this.options.apiKey.slice(0, 6);
264-
this.storage.setItem(key, value);
264+
if (value) {
265+
this.storage.setItem(key, value);
266+
return;
267+
}
268+
// if value is null, then remove from storage
269+
this.storage.removeItem(key);
265270
};
266271

267272
/**

amplitude.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/amplitude.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,12 @@ Amplitude.prototype.runQueuedFunctions = function () {
155155
*/
156156
Amplitude.prototype.setInStorage = function(item, value) {
157157
var key = item + '_' + this.options.apiKey.slice(0, 6);
158-
this.storage.setItem(key, value);
158+
if (value) {
159+
this.storage.setItem(key, value);
160+
return;
161+
}
162+
// if value is null, then remove from storage
163+
this.storage.removeItem(key);
159164
};
160165

161166
/**

test/amplitude.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,18 @@ describe('Amplitude', function() {
4242
assert.equal(amplitude.options.userId, userId);
4343
});
4444

45+
it('should allow null userId', function() {
46+
var userIdKey = 'amplitude_userId_' + apiKey;
47+
amplitude.init(apiKey, userId);
48+
assert.equal(amplitude.options.userId, userId);
49+
assert.equal(window.localStorage.getItem(userIdKey), userId);
50+
assert.equal(amplitude.getFromStorage('amplitude_userId'), userId);
51+
amplitude.setUserId(null);
52+
assert.isNull(amplitude.options.userId);
53+
assert.isNull(window.localStorage.getItem(userIdKey));
54+
assert.isUndefined(amplitude.getFromStorage('amplitude_userId'));
55+
});
56+
4557
it('should not set cookie', function() {
4658
amplitude.init(apiKey, userId);
4759
var stored = cookie.get(amplitude.options.cookieName);
@@ -145,6 +157,7 @@ describe('Amplitude', function() {
145157
});
146158

147159
it('should change device id', function() {
160+
amplitude.options.apiKey = apiKey;
148161
amplitude.setDeviceId('deviceId');
149162
amplitude.init(apiKey);
150163
assert.equal(amplitude.options.deviceId, 'deviceId');

0 commit comments

Comments
 (0)