MCPcopy Create free account
hub / github.com/DavidWells/analytics / userReducer

Function userReducer

packages/analytics-core/src/modules/user.js:7–47  ·  view source on GitHub ↗
(storage)

Source from the content-addressed store, hash-verified

5
6/* user reducer */
7export default function userReducer(storage) {
8 return function user(state = {}, action = {}) {
9
10 if (action.type === EVENTS.setItemEnd) {
11 // Set anonymousId if changed by storage.setItem
12 if (action.key === ANON_ID) {
13 return { ...state, ...{ anonymousId: action.value }}
14 }
15 // Set userId if changed by storage.setItem
16 if (action.key === USER_ID) {
17 return { ...state, ...{ userId: action.value }}
18 }
19 }
20
21 switch (action.type) {
22 case EVENTS.identify:
23 return Object.assign({}, state, {
24 userId: action.userId,
25 traits: {
26 ...state.traits,
27 ...action.traits
28 }
29 })
30 case EVENTS.reset:
31 // Side effect to fix race condition in Node. TODO refactor
32 // This is from default storage.removeItem: (key) => globalContext[key] = undefined
33 [ USER_ID, ANON_ID, USER_TRAITS ].forEach((key) => {
34 // sync storage, not instance.storage
35 storage.removeItem(key)
36 })
37 return Object.assign({}, state, {
38 userId: null,
39 // TODO reset anon id automatically?
40 anonymousId: null,
41 traits: {},
42 })
43 default:
44 return state
45 }
46 }
47}
48
49export function getPersistedUserData(storage) {
50 return {

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected