Skip to content

Commit

Permalink
feat: Clear Storage API (#396)
Browse files Browse the repository at this point in the history
* Add API to clear storage

* Add tests

* Fix clear storage logic

* Fix public docs

* Improve tests
  • Loading branch information
jooohhn authored May 26, 2021
1 parent 7a8ce81 commit e8cb8b7
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/amplitude-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,16 @@ AmplitudeClient.prototype._sendEventsIfReady = function _sendEventsIfReady() {
return false; // an upload was scheduled, no events were uploaded
};

/**
* Clears any stored events and metadata. Storage is then re-created on next event sending.
* @constructor AmplitudeClient
* @public
* @return {boolean} True if metadata was cleared, false if none existed
*/
AmplitudeClient.prototype.clearStorage = function clearStorage() {
return this._metadataStorage.clear();
};

/**
* Helper function to fetch values from storage
* Storage argument allows for localStoraoge and sessionStoraoge
Expand Down
33 changes: 33 additions & 0 deletions src/metadata-storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,39 @@ class MetadataStorage {
sequenceNumber: parseInt(values[Constants.SEQUENCE_NUMBER_INDEX], 32),
};
}

/**
* Clears any saved metadata storage
* @constructor AmplitudeClient
* @public
* @return {boolean} True if metadata was cleared, false if none existed
*/
clear() {
let str;
if (this.storage === Constants.STORAGE_COOKIES) {
str = baseCookie.get(this.getCookieStorageKey() + '=');
baseCookie.set(this.getCookieStorageKey(), null, {
domain: this.cookieDomain,
secure: this.secure,
sameSite: this.sameSite,
expirationDays: 0,
});
}
if (!str) {
str = ampLocalStorage.getItem(this.storageKey);
ampLocalStorage.clear();
}
if (!str) {
try {
str = window.sessionStorage && window.sessionStorage.getItem(this.storageKey);
window.sessionStorage.clear();
} catch (e) {
utils.log.info(`window.sessionStorage unavailable. Reason: "${e}"`);
}
}
if (!str) false;
return true;
}
}

export default MetadataStorage;
30 changes: 30 additions & 0 deletions test/amplitude-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -3804,4 +3804,34 @@ describe('AmplitudeClient', function () {
});
});
});

describe('clearStorage', function () {
afterEach(() => {
reset();
});

it('should clear cookies', function () {
amplitude.init(apiKey, null, { storage: constants.STORAGE_COOKIES });
assert.isNotNull(amplitude._metadataStorage.load());
assert.equal(amplitude._metadataStorage.storage, constants.STORAGE_COOKIES);
assert.equal(amplitude.clearStorage(), true);
assert.isNull(amplitude._metadataStorage.load());
});

it('should clear localStorage', function () {
amplitude.init(apiKey, null, { storage: constants.STORAGE_LOCAL });
assert.isNotNull(amplitude._metadataStorage.load());
assert.equal(amplitude._metadataStorage.storage, constants.STORAGE_LOCAL);
assert.equal(amplitude.clearStorage(), true);
assert.isNull(amplitude._metadataStorage.load());
});

it('should clear sessionStorage', function () {
amplitude.init(apiKey, null, { storage: constants.STORAGE_SESSION });
assert.isNotNull(amplitude._metadataStorage.load());
assert.equal(amplitude._metadataStorage.storage, constants.STORAGE_SESSION);
assert.equal(amplitude.clearStorage(), true);
assert.isNull(amplitude._metadataStorage.load());
});
});
});

0 comments on commit e8cb8b7

Please sign in to comment.