Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit, here you can use ``return !!str` for the same effect

}
}

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());
});
});
});