forked from tracespace/tracespace
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
57 lines (47 loc) · 1.51 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// analytics module via mixpanel
import mixpanel from 'mixpanel-browser'
import log from '../logger'
import * as State from '../state'
import createEvent from './create-event'
const MIXPANEL_ID = process.env.MIXPANEL_ID
let userId: string | null = null
export function getAnalyticsUserId(): string | null {
return userId
}
export function createAnalyticsMiddleware(): State.Middleware {
if (MIXPANEL_ID) {
log.debug('initializing mixpanel')
mixpanel.init(MIXPANEL_ID, {
opt_out_tracking_by_default: true,
loaded: (mp): void => {
userId = mp.get_distinct_id()
},
})
} else {
log.debug('no mixpanel token found; not initializing')
}
return store => next => action => {
const prevState = store.getState()
const result = next(action)
const nextState = store.getState()
if (userId) {
const nextOptIn = nextState.appPreferences.analyticsOptIn
const event = createEvent(action, nextState, prevState)
// ensure opt-in preferences are communicated to mixpanel
if (nextOptIn && mixpanel.has_opted_out_tracking()) {
mixpanel.opt_in_tracking()
} else if (!nextOptIn && !mixpanel.has_opted_out_tracking) {
mixpanel.opt_out_tracking()
}
// only track anything if opted in
if (nextOptIn && event) {
const [name, payload] = event
log.debug('track', event)
mixpanel.track(name, payload)
} else if (event) {
log.debug('did not track', event)
}
}
return result
}
}