Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[bugfix] update analytics.reset() to clear group data #611

Merged
merged 3 commits into from
Oct 4, 2022
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
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'
Expand Down Expand Up @@ -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
Expand Up @@ -328,6 +328,7 @@ export class Analytics

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

timeout(timeout: number): void {
Expand Down
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
}