2022-01-21 07:42:30 +13:00
|
|
|
import { API } from "api"
|
2021-09-21 22:47:14 +12:00
|
|
|
import PosthogClient from "./PosthogClient"
|
|
|
|
import IntercomClient from "./IntercomClient"
|
|
|
|
import SentryClient from "./SentryClient"
|
2022-04-20 22:31:54 +12:00
|
|
|
import { Events, EventSource } from "./constants"
|
2021-09-21 22:47:14 +12:00
|
|
|
|
2022-06-07 02:27:29 +12:00
|
|
|
const posthog = new PosthogClient(process.env.POSTHOG_TOKEN)
|
2021-09-22 07:21:15 +12:00
|
|
|
const sentry = new SentryClient(process.env.SENTRY_DSN)
|
|
|
|
const intercom = new IntercomClient(process.env.INTERCOM_TOKEN)
|
2021-09-21 22:47:14 +12:00
|
|
|
|
|
|
|
class AnalyticsHub {
|
|
|
|
constructor() {
|
|
|
|
this.clients = [posthog, sentry, intercom]
|
|
|
|
}
|
|
|
|
|
|
|
|
async activate() {
|
2022-01-25 07:58:22 +13:00
|
|
|
// Check analytics are enabled
|
|
|
|
const analyticsStatus = await API.getAnalyticsStatus()
|
|
|
|
if (analyticsStatus.enabled) {
|
|
|
|
this.clients.forEach(client => client.init())
|
2022-01-21 07:42:30 +13:00
|
|
|
}
|
2021-09-21 22:47:14 +12:00
|
|
|
}
|
|
|
|
|
2022-06-06 23:51:22 +12:00
|
|
|
identify(id) {
|
2021-09-21 22:47:14 +12:00
|
|
|
posthog.identify(id)
|
|
|
|
sentry.identify(id)
|
|
|
|
}
|
|
|
|
|
|
|
|
captureException(err) {
|
|
|
|
sentry.captureException(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
captureEvent(eventName, props = {}) {
|
|
|
|
posthog.captureEvent(eventName, props)
|
|
|
|
intercom.captureEvent(eventName, props)
|
|
|
|
}
|
|
|
|
|
|
|
|
showChat(user) {
|
|
|
|
intercom.show(user)
|
|
|
|
}
|
|
|
|
|
2021-09-22 07:21:15 +12:00
|
|
|
async logout() {
|
|
|
|
posthog.logout()
|
|
|
|
intercom.logout()
|
|
|
|
}
|
2021-09-21 22:47:14 +12:00
|
|
|
}
|
|
|
|
|
|
|
|
const analytics = new AnalyticsHub()
|
|
|
|
|
2022-04-20 22:31:54 +12:00
|
|
|
export { Events, EventSource }
|
2021-09-22 07:21:15 +12:00
|
|
|
export default analytics
|