2022-05-05 19:32:14 +12:00
|
|
|
import PostHog from "posthog-node"
|
|
|
|
import { Event } from "@budibase/types"
|
2022-05-10 21:33:59 +12:00
|
|
|
import { EventProcessor } from "./types"
|
|
|
|
import { getTenantId } from "../../context"
|
2022-05-05 19:32:14 +12:00
|
|
|
|
2022-05-10 21:33:59 +12:00
|
|
|
export default class PosthogProcessor implements EventProcessor {
|
2022-05-05 19:32:14 +12:00
|
|
|
posthog: PostHog
|
|
|
|
|
|
|
|
constructor(token: string | undefined) {
|
|
|
|
if (!token) {
|
|
|
|
throw new Error("Posthog token is not defined")
|
|
|
|
}
|
|
|
|
this.posthog = new PostHog(token)
|
|
|
|
}
|
|
|
|
|
2022-05-10 21:33:59 +12:00
|
|
|
processEvent(event: Event, properties: any): void {
|
|
|
|
const userId = getTenantId() // TODO
|
|
|
|
this.posthog.capture({ distinctId: userId, event, properties })
|
2022-05-05 19:32:14 +12:00
|
|
|
}
|
|
|
|
|
2022-05-10 21:33:59 +12:00
|
|
|
identify(distinctId: string, properties: any) {
|
|
|
|
this.posthog.identify({ distinctId, properties })
|
2022-05-05 19:32:14 +12:00
|
|
|
}
|
|
|
|
|
|
|
|
shutdown() {
|
|
|
|
this.posthog.shutdown()
|
|
|
|
}
|
|
|
|
}
|