2024-12-18 10:47:27 +01:00
|
|
|
import { API } from "@/api"
|
2021-09-21 12:47:14 +02:00
|
|
|
import PosthogClient from "./PosthogClient"
|
2022-04-20 12:31:54 +02:00
|
|
|
import { Events, EventSource } from "./constants"
|
2021-09-21 12:47:14 +02:00
|
|
|
|
2022-06-06 16:27:29 +02:00
|
|
|
const posthog = new PosthogClient(process.env.POSTHOG_TOKEN)
|
2021-09-21 12:47:14 +02:00
|
|
|
|
|
|
|
class AnalyticsHub {
|
|
|
|
constructor() {
|
2024-04-09 16:24:03 +02:00
|
|
|
this.clients = [posthog]
|
2024-02-15 16:25:07 +01:00
|
|
|
this.initialised = false
|
2021-09-21 12:47:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
async activate() {
|
2022-01-24 19:58:22 +01:00
|
|
|
// Check analytics are enabled
|
|
|
|
const analyticsStatus = await API.getAnalyticsStatus()
|
2024-02-15 16:25:07 +01:00
|
|
|
if (analyticsStatus.enabled && !this.initialised) {
|
|
|
|
this.clients.forEach(client => {
|
|
|
|
client.init()
|
|
|
|
})
|
|
|
|
this.initialised = true
|
2022-01-20 19:42:30 +01:00
|
|
|
}
|
2021-09-21 12:47:14 +02:00
|
|
|
}
|
|
|
|
|
2022-06-06 13:51:22 +02:00
|
|
|
identify(id) {
|
2021-09-21 12:47:14 +02:00
|
|
|
posthog.identify(id)
|
|
|
|
}
|
|
|
|
|
2023-10-18 10:54:37 +02:00
|
|
|
captureException(_err) {}
|
2021-09-21 12:47:14 +02:00
|
|
|
|
|
|
|
captureEvent(eventName, props = {}) {
|
|
|
|
posthog.captureEvent(eventName, props)
|
2024-04-08 10:58:59 +02:00
|
|
|
}
|
|
|
|
|
2025-03-04 18:49:18 +01:00
|
|
|
enableSessionRecording() {
|
|
|
|
posthog.enableSessionRecording()
|
|
|
|
}
|
|
|
|
|
2021-09-21 21:21:15 +02:00
|
|
|
async logout() {
|
|
|
|
posthog.logout()
|
|
|
|
}
|
2021-09-21 12:47:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const analytics = new AnalyticsHub()
|
|
|
|
|
2022-04-20 12:31:54 +02:00
|
|
|
export { Events, EventSource }
|
2021-09-21 21:21:15 +02:00
|
|
|
export default analytics
|