From a17479b009f17c6a533cad4b53c6dfb75fb7b7e7 Mon Sep 17 00:00:00 2001 From: Christopher Radek Date: Mon, 3 Oct 2022 11:41:07 -0700 Subject: [PATCH 1/3] adds analytics.reset() test --- .../analytics/__tests__/integration.test.ts | 35 +++++++++++++++++++ .../src/test-helpers/retrieve-stored-data.ts | 29 +++++++++++++++ 2 files changed, 64 insertions(+) create mode 100644 packages/browser/src/test-helpers/retrieve-stored-data.ts 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/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 +} From 6df1c21f5cf46883579493ebd81d82e6c67b0ca7 Mon Sep 17 00:00:00 2001 From: Christopher Radek Date: Mon, 3 Oct 2022 11:42:56 -0700 Subject: [PATCH 2/3] updates analytics.reset() to clear group data --- packages/browser/src/core/analytics/index.ts | 1 + 1 file changed, 1 insertion(+) 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 { From 6bce99c725026fd6ac1453da8c7b67e32d778bc8 Mon Sep 17 00:00:00 2001 From: Christopher Radek Date: Mon, 3 Oct 2022 11:45:03 -0700 Subject: [PATCH 3/3] add changelog --- .changeset/dull-parents-breathe.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/dull-parents-breathe.md 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.