forked from skillrecordings/egghead-next
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanalytics.ts
More file actions
92 lines (75 loc) · 1.82 KB
/
analytics.ts
File metadata and controls
92 lines (75 loc) · 1.82 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import {isFunction, isUndefined} from 'lodash'
import {Viewer} from 'types'
import Auth from './auth'
export const track = (
event: string,
paramsOrCallback?: any,
callback?: any,
) => {
const auth = new Auth()
return new Promise(async (resolve) => {
const ahoy = window.ahoy
let wasCalled = false
const viewer: Viewer = auth.getLocalUser()
function politelyExit() {
if (isFunction(callback) && !wasCalled) {
wasCalled = true
callback.apply(null, [event, wasCalled])
}
resolve(true)
}
const params = isFunction(paramsOrCallback) ? {} : paramsOrCallback
const timeout = 1250
if (isUndefined(callback) && isFunction(paramsOrCallback)) {
callback = paramsOrCallback
}
const store = console.error
console.error = () => {}
setTimeout(politelyExit, timeout)
console.error = store
if (ahoy && isFunction(ahoy.track)) {
ahoy.track(event, params)
}
if (window.fbq) {
window.fbq('trackCustom', event, params)
}
if (
viewer &&
!viewer.opted_out &&
viewer.contact_id &&
viewer.email &&
window._cio &&
isFunction(window._cio.track)
) {
identify(viewer)
window._cio.track(event, params)
}
politelyExit()
})
}
export const identify = (data: Viewer) => {
if (
!data.opted_out &&
data.email &&
data.contact_id &&
window._cio &&
isFunction(window._cio.identify)
) {
window._cio.identify({
id: data.contact_id,
email: data.email,
first_name: data.name,
pro: data.is_pro,
instructor: data.is_instructor,
created_at: data.created_at,
discord_id: data.discord_id,
timezone: data.timezone,
})
}
return Promise.resolve(data)
}
const analytics = {
track,
identify,
}
export default analytics