Skip to content

Commit e8cb8b7

Browse files
authored
feat: Clear Storage API (#396)
* Add API to clear storage * Add tests * Fix clear storage logic * Fix public docs * Improve tests
1 parent 7a8ce81 commit e8cb8b7

File tree

3 files changed

+73
-0
lines changed

3 files changed

+73
-0
lines changed

src/amplitude-client.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -521,6 +521,16 @@ AmplitudeClient.prototype._sendEventsIfReady = function _sendEventsIfReady() {
521521
return false; // an upload was scheduled, no events were uploaded
522522
};
523523

524+
/**
525+
* Clears any stored events and metadata. Storage is then re-created on next event sending.
526+
* @constructor AmplitudeClient
527+
* @public
528+
* @return {boolean} True if metadata was cleared, false if none existed
529+
*/
530+
AmplitudeClient.prototype.clearStorage = function clearStorage() {
531+
return this._metadataStorage.clear();
532+
};
533+
524534
/**
525535
* Helper function to fetch values from storage
526536
* Storage argument allows for localStoraoge and sessionStoraoge

src/metadata-storage.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,39 @@ class MetadataStorage {
163163
sequenceNumber: parseInt(values[Constants.SEQUENCE_NUMBER_INDEX], 32),
164164
};
165165
}
166+
167+
/**
168+
* Clears any saved metadata storage
169+
* @constructor AmplitudeClient
170+
* @public
171+
* @return {boolean} True if metadata was cleared, false if none existed
172+
*/
173+
clear() {
174+
let str;
175+
if (this.storage === Constants.STORAGE_COOKIES) {
176+
str = baseCookie.get(this.getCookieStorageKey() + '=');
177+
baseCookie.set(this.getCookieStorageKey(), null, {
178+
domain: this.cookieDomain,
179+
secure: this.secure,
180+
sameSite: this.sameSite,
181+
expirationDays: 0,
182+
});
183+
}
184+
if (!str) {
185+
str = ampLocalStorage.getItem(this.storageKey);
186+
ampLocalStorage.clear();
187+
}
188+
if (!str) {
189+
try {
190+
str = window.sessionStorage && window.sessionStorage.getItem(this.storageKey);
191+
window.sessionStorage.clear();
192+
} catch (e) {
193+
utils.log.info(`window.sessionStorage unavailable. Reason: "${e}"`);
194+
}
195+
}
196+
if (!str) false;
197+
return true;
198+
}
166199
}
167200

168201
export default MetadataStorage;

test/amplitude-client.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3804,4 +3804,34 @@ describe('AmplitudeClient', function () {
38043804
});
38053805
});
38063806
});
3807+
3808+
describe('clearStorage', function () {
3809+
afterEach(() => {
3810+
reset();
3811+
});
3812+
3813+
it('should clear cookies', function () {
3814+
amplitude.init(apiKey, null, { storage: constants.STORAGE_COOKIES });
3815+
assert.isNotNull(amplitude._metadataStorage.load());
3816+
assert.equal(amplitude._metadataStorage.storage, constants.STORAGE_COOKIES);
3817+
assert.equal(amplitude.clearStorage(), true);
3818+
assert.isNull(amplitude._metadataStorage.load());
3819+
});
3820+
3821+
it('should clear localStorage', function () {
3822+
amplitude.init(apiKey, null, { storage: constants.STORAGE_LOCAL });
3823+
assert.isNotNull(amplitude._metadataStorage.load());
3824+
assert.equal(amplitude._metadataStorage.storage, constants.STORAGE_LOCAL);
3825+
assert.equal(amplitude.clearStorage(), true);
3826+
assert.isNull(amplitude._metadataStorage.load());
3827+
});
3828+
3829+
it('should clear sessionStorage', function () {
3830+
amplitude.init(apiKey, null, { storage: constants.STORAGE_SESSION });
3831+
assert.isNotNull(amplitude._metadataStorage.load());
3832+
assert.equal(amplitude._metadataStorage.storage, constants.STORAGE_SESSION);
3833+
assert.equal(amplitude.clearStorage(), true);
3834+
assert.isNull(amplitude._metadataStorage.load());
3835+
});
3836+
});
38073837
});

0 commit comments

Comments
 (0)