Skip to content

Commit

Permalink
[bugfix] update analytics.reset() to clear group data (#611)
Browse files Browse the repository at this point in the history
* adds analytics.reset() test

* updates analytics.reset() to clear group data

* add changelog
chrisradek authored Oct 4, 2022
1 parent a23ccee commit 29c3bd7
Showing 4 changed files with 70 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/dull-parents-breathe.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@segment/analytics-next': patch
---

Fixes analytics.reset() so that it clears group data.
35 changes: 35 additions & 0 deletions packages/browser/src/core/analytics/__tests__/integration.test.ts
Original file line number Diff line number Diff line change
@@ -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({})
})
})
})
1 change: 1 addition & 0 deletions packages/browser/src/core/analytics/index.ts
Original file line number Diff line number Diff line change
@@ -328,6 +328,7 @@ export class Analytics

reset(): void {
this._user.reset()
this._group.reset()
}

timeout(timeout: number): void {
29 changes: 29 additions & 0 deletions packages/browser/src/test-helpers/retrieve-stored-data.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import cookie from 'js-cookie'

export interface RetrieveStoredDataProps {
cookieNames: string[]
localStorageKeys: string[]
}

export function retrieveStoredData({
cookieNames,
localStorageKeys,
}: RetrieveStoredDataProps): Record<string, string | {}> {
const result: ReturnType<typeof retrieveStoredData> = {}

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
}

0 comments on commit 29c3bd7

Please sign in to comment.