diff --git a/.changeset/dull-parents-breathe.md b/.changeset/dull-parents-breathe.md new file mode 100644 index 000000000..0288d5d40 --- /dev/null +++ b/.changeset/dull-parents-breathe.md @@ -0,0 +1,5 @@ +--- +'@segment/analytics-next': patch +--- + +Fixes analytics.reset() so that it clears group data. diff --git a/packages/browser/src/core/analytics/__tests__/integration.test.ts b/packages/browser/src/core/analytics/__tests__/integration.test.ts index 03cb7a5c2..9dae7b1dc 100644 --- a/packages/browser/src/core/analytics/__tests__/integration.test.ts +++ b/packages/browser/src/core/analytics/__tests__/integration.test.ts @@ -1,5 +1,6 @@ import { PriorityQueue } from '../../../lib/priority-queue' import { MiddlewareParams } from '../../../plugins/middleware' +import { retrieveStoredData } from '../../../test-helpers/retrieve-stored-data' import { Context } from '../../context' import { Plugin } from '../../plugin' import { EventQueue } from '../../queue/event-queue' @@ -221,4 +222,38 @@ describe('Analytics', () => { }) }) }) + + describe('reset', () => { + it('clears user and group data', async () => { + const analytics = new Analytics({ writeKey: '' }) + + const cookieNames = ['ajs_user_id', 'ajs_anonymous_id', 'ajs_group_id'] + const localStorageKeys = ['ajs_user_traits', 'ajs_group_properties'] + + analytics.user().anonymousId('unknown-user') + analytics.user().id('known-user') + analytics.user().traits({ job: 'engineer' }) + analytics.group().id('known-group') + analytics.group().traits({ team: 'analytics' }) + + // Ensure all cookies/localstorage is written correctly first + let storedData = retrieveStoredData({ cookieNames, localStorageKeys }) + expect(storedData).toEqual({ + ajs_user_id: 'known-user', + ajs_anonymous_id: 'unknown-user', + ajs_group_id: 'known-group', + ajs_user_traits: { + job: 'engineer', + }, + ajs_group_properties: { + team: 'analytics', + }, + }) + + // Now make sure everything was cleared on reset + analytics.reset() + storedData = retrieveStoredData({ cookieNames, localStorageKeys }) + expect(storedData).toEqual({}) + }) + }) }) diff --git a/packages/browser/src/core/analytics/index.ts b/packages/browser/src/core/analytics/index.ts index 77ee77193..cd501e45e 100644 --- a/packages/browser/src/core/analytics/index.ts +++ b/packages/browser/src/core/analytics/index.ts @@ -328,6 +328,7 @@ export class Analytics reset(): void { this._user.reset() + this._group.reset() } timeout(timeout: number): void { diff --git a/packages/browser/src/test-helpers/retrieve-stored-data.ts b/packages/browser/src/test-helpers/retrieve-stored-data.ts new file mode 100644 index 000000000..91b5ce259 --- /dev/null +++ b/packages/browser/src/test-helpers/retrieve-stored-data.ts @@ -0,0 +1,29 @@ +import cookie from 'js-cookie' + +export interface RetrieveStoredDataProps { + cookieNames: string[] + localStorageKeys: string[] +} + +export function retrieveStoredData({ + cookieNames, + localStorageKeys, +}: RetrieveStoredDataProps): Record { + const result: ReturnType = {} + + const cookies = cookie.get() + cookieNames.forEach((name) => { + if (name in cookies) { + result[name] = cookies[name] + } + }) + + localStorageKeys.forEach((key) => { + const value = localStorage.getItem(key) + if (value !== null && typeof value !== 'undefined') { + result[key] = JSON.parse(value) + } + }) + + return result +}