-
Notifications
You must be signed in to change notification settings - Fork 141
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
215 additions
and
37 deletions.
There are no files selected for viewing
74 changes: 74 additions & 0 deletions
74
packages/browser/src/browser/__tests__/anon-id-and-reset.integration.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import unfetch from 'unfetch' | ||
import { AnalyticsBrowser } from '..' | ||
import { | ||
clearAjsBrowserStorage, | ||
getAnonId, | ||
} from '../../test-helpers/browser-storage' | ||
import { createSuccess } from '../../test-helpers/factories' | ||
|
||
jest.mock('unfetch') | ||
const helpers = { | ||
mockFetchSettingsSuccessResponse: () => { | ||
return jest | ||
.mocked(unfetch) | ||
.mockImplementation(() => createSuccess({ integrations: {} })) | ||
}, | ||
loadAnalytics() { | ||
return AnalyticsBrowser.load({ writeKey: 'foo' }) | ||
}, | ||
} | ||
|
||
beforeEach(() => { | ||
helpers.mockFetchSettingsSuccessResponse() | ||
}) | ||
|
||
describe('setting anonymousId', () => { | ||
it('is set if an event like track is called during pre-init period', async () => { | ||
const analytics = helpers.loadAnalytics() | ||
const ctx = await analytics.track('foo') | ||
const id = getAnonId() | ||
expect(id).toBeDefined() | ||
expect(ctx.event.anonymousId).toBe(id) | ||
}) | ||
|
||
it('sets the global anonymousId to the anonymousId set by the most recent event', async () => { | ||
const analytics = helpers.loadAnalytics() | ||
const trackCtx = await analytics.track( | ||
'add to cart', | ||
{}, | ||
{ anonymousId: 'foo' } | ||
) | ||
expect(getAnonId()).toBe('foo') | ||
expect(trackCtx.event.anonymousId).toBe('foo') | ||
const idCtx = await analytics.identify('john') | ||
expect(getAnonId()).toBe('foo') | ||
expect(idCtx.event.anonymousId).toBe('foo') | ||
}) | ||
}) | ||
|
||
describe('resetting', () => { | ||
beforeEach(() => { | ||
clearAjsBrowserStorage() | ||
}) | ||
|
||
describe('anonymousId', () => { | ||
it('clears anonId if reset is called during pre-init period', async () => { | ||
const analytics = helpers.loadAnalytics() | ||
const track = analytics.track('foo') | ||
const reset = analytics.reset() | ||
await Promise.all([track, reset]) | ||
expect(getAnonId()).toBeFalsy() | ||
}) | ||
|
||
it('clears anonId if reset is called after initialization is complete', async () => { | ||
const [analytics] = await helpers.loadAnalytics() | ||
expect(getAnonId()).toBeFalsy() | ||
const track = analytics.track('foo') | ||
expect(typeof getAnonId()).toBe('string') | ||
analytics.reset() | ||
expect(getAnonId()).toBeFalsy() | ||
await track | ||
expect(getAnonId()).toBeFalsy() | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import cookie from 'js-cookie' | ||
|
||
const ajsCookieNames = [ | ||
'ajs_user_id', | ||
'ajs_anonymous_id', | ||
'ajs_group_id', | ||
] as const | ||
const ajsLocalStorageKeys = ['ajs_user_traits', 'ajs_group_properties'] as const | ||
|
||
export const getAjsBrowserStorage = () => { | ||
return getBrowserStorage({ | ||
cookieNames: ajsCookieNames, | ||
localStorageKeys: ajsLocalStorageKeys, | ||
}) | ||
} | ||
|
||
export const getAnonId = () => getAjsBrowserStorage().ajs_anonymous_id | ||
|
||
export const clearAjsBrowserStorage = () => { | ||
return clearBrowserStorage({ | ||
cookieNames: ajsCookieNames, | ||
localStorageKeys: ajsLocalStorageKeys, | ||
}) | ||
} | ||
|
||
export function getBrowserStorage< | ||
CookieNames extends string, | ||
LSKeys extends string | ||
>({ | ||
cookieNames, | ||
localStorageKeys, | ||
}: { | ||
cookieNames: readonly CookieNames[] | ||
localStorageKeys: readonly LSKeys[] | ||
}): Record<CookieNames | LSKeys, string | {}> { | ||
const result = {} as ReturnType<typeof getBrowserStorage> | ||
|
||
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 | ||
} | ||
|
||
export function clearBrowserStorage({ | ||
cookieNames, | ||
localStorageKeys, // if no keys are passed, the entire thing is cleared | ||
}: { | ||
cookieNames: string[] | readonly string[] | ||
localStorageKeys?: string[] | readonly string[] | ||
}) { | ||
cookieNames.forEach((name) => { | ||
cookie.remove(name) | ||
}) | ||
if (!localStorageKeys) { | ||
localStorage.clear() | ||
} else { | ||
localStorageKeys.forEach((key) => { | ||
localStorage.removeItem(key) | ||
}) | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.