Skip to content

Commit 29c3bd7

Browse files
authored
[bugfix] update analytics.reset() to clear group data (#611)
* adds analytics.reset() test * updates analytics.reset() to clear group data * add changelog
1 parent a23ccee commit 29c3bd7

File tree

4 files changed

+70
-0
lines changed

4 files changed

+70
-0
lines changed

.changeset/dull-parents-breathe.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@segment/analytics-next': patch
3+
---
4+
5+
Fixes analytics.reset() so that it clears group data.

packages/browser/src/core/analytics/__tests__/integration.test.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { PriorityQueue } from '../../../lib/priority-queue'
22
import { MiddlewareParams } from '../../../plugins/middleware'
3+
import { retrieveStoredData } from '../../../test-helpers/retrieve-stored-data'
34
import { Context } from '../../context'
45
import { Plugin } from '../../plugin'
56
import { EventQueue } from '../../queue/event-queue'
@@ -221,4 +222,38 @@ describe('Analytics', () => {
221222
})
222223
})
223224
})
225+
226+
describe('reset', () => {
227+
it('clears user and group data', async () => {
228+
const analytics = new Analytics({ writeKey: '' })
229+
230+
const cookieNames = ['ajs_user_id', 'ajs_anonymous_id', 'ajs_group_id']
231+
const localStorageKeys = ['ajs_user_traits', 'ajs_group_properties']
232+
233+
analytics.user().anonymousId('unknown-user')
234+
analytics.user().id('known-user')
235+
analytics.user().traits({ job: 'engineer' })
236+
analytics.group().id('known-group')
237+
analytics.group().traits({ team: 'analytics' })
238+
239+
// Ensure all cookies/localstorage is written correctly first
240+
let storedData = retrieveStoredData({ cookieNames, localStorageKeys })
241+
expect(storedData).toEqual({
242+
ajs_user_id: 'known-user',
243+
ajs_anonymous_id: 'unknown-user',
244+
ajs_group_id: 'known-group',
245+
ajs_user_traits: {
246+
job: 'engineer',
247+
},
248+
ajs_group_properties: {
249+
team: 'analytics',
250+
},
251+
})
252+
253+
// Now make sure everything was cleared on reset
254+
analytics.reset()
255+
storedData = retrieveStoredData({ cookieNames, localStorageKeys })
256+
expect(storedData).toEqual({})
257+
})
258+
})
224259
})

packages/browser/src/core/analytics/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,7 @@ export class Analytics
328328

329329
reset(): void {
330330
this._user.reset()
331+
this._group.reset()
331332
}
332333

333334
timeout(timeout: number): void {
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import cookie from 'js-cookie'
2+
3+
export interface RetrieveStoredDataProps {
4+
cookieNames: string[]
5+
localStorageKeys: string[]
6+
}
7+
8+
export function retrieveStoredData({
9+
cookieNames,
10+
localStorageKeys,
11+
}: RetrieveStoredDataProps): Record<string, string | {}> {
12+
const result: ReturnType<typeof retrieveStoredData> = {}
13+
14+
const cookies = cookie.get()
15+
cookieNames.forEach((name) => {
16+
if (name in cookies) {
17+
result[name] = cookies[name]
18+
}
19+
})
20+
21+
localStorageKeys.forEach((key) => {
22+
const value = localStorage.getItem(key)
23+
if (value !== null && typeof value !== 'undefined') {
24+
result[key] = JSON.parse(value)
25+
}
26+
})
27+
28+
return result
29+
}

0 commit comments

Comments
 (0)