Skip to content
This repository has been archived by the owner on Jun 27, 2023. It is now read-only.

Commit

Permalink
feat: add anonymousId logic to reset (segmentio#475)
Browse files Browse the repository at this point in the history
* feat: add anonymousId logic to reset

* refactor: add Oscar's changes

Co-authored-by: Alan Charles <alancharles@Alans-MacBook-Pro-2.local>
  • Loading branch information
alanjcharles and Alan Charles authored Mar 11, 2022
1 parent 02167e0 commit 23baab4
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 6 deletions.
13 changes: 13 additions & 0 deletions packages/core/src/__tests__/analytics.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,19 @@ describe('SegmentClient', () => {
});

describe('#reset', () => {
it('resets all userInfo except anonymousId', () => {
client = new SegmentClient(clientArgs);
const setUserInfo = jest.spyOn(store.userInfo, 'set');

client.reset(false);

expect(setUserInfo).toHaveBeenCalledWith({
anonymousId: 'anonymousId',
userId: undefined,
traits: undefined,
});
});

it('resets user data, identity, traits', () => {
client = new SegmentClient(clientArgs);
const setUserInfo = jest.spyOn(store.userInfo, 'set');
Expand Down
10 changes: 8 additions & 2 deletions packages/core/src/analytics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -651,12 +651,18 @@ export class SegmentClient {
this.appState = nextAppState;
}

reset() {
reset(resetAnonymousId: boolean = true) {
const anonymousId =
resetAnonymousId === true
? getUUID()
: this.store.userInfo.get().anonymousId;

this.store.userInfo.set({
anonymousId: getUUID(),
anonymousId,
userId: undefined,
traits: undefined,
});

this.logger.info('Client has been reset');
}
}
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2113,10 +2113,10 @@
conventional-recommended-bump "^6.1.0"
prepend-file "^2.0.0"

"@segment/sovran-react-native@^0.2.4":
version "0.2.4"
resolved "https://registry.yarnpkg.com/@segment/sovran-react-native/-/sovran-react-native-0.2.4.tgz#8f73ba41febf8cb2f711a0d15d38a79384b0b767"
integrity sha512-+r98+pDMUQj8Siw4gG3KRSH7pIt4Q6omu4vwBfT0oBRpZ0dsSz6a17xTlEZkUU3ytgDj1c/fmJQg6V8tLgkbWw==
"@segment/sovran-react-native@^0.2.6":
version "0.2.6"
resolved "https://registry.yarnpkg.com/@segment/sovran-react-native/-/sovran-react-native-0.2.6.tgz#895ef37b71c299f56c89515cf8d200f13fce2251"
integrity sha512-SxqKvMvgu9PZo0jSkZ0yys08H9qJbI1uGbaeBvJFZfs92TIvQmcW0PYQZsspqt85RPzrL4J9KDio6xoLXmHurw==
dependencies:
"@react-native-async-storage/async-storage" "^1.15.15"
ansi-regex "5.0.1"
Expand Down

0 comments on commit 23baab4

Please sign in to comment.