2022-05-05 19:32:14 +12:00
|
|
|
import PostHog from "posthog-node"
|
2022-06-01 08:04:41 +12:00
|
|
|
import { Event, Identity, Group, BaseEvent } from "@budibase/types"
|
2022-05-10 21:33:59 +12:00
|
|
|
import { EventProcessor } from "./types"
|
2022-05-29 08:38:22 +12:00
|
|
|
import env from "../../environment"
|
2022-06-01 08:04:41 +12:00
|
|
|
import context from "../../context"
|
2022-05-29 08:38:22 +12:00
|
|
|
const pkg = require("../../../package.json")
|
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-24 09:14:44 +12:00
|
|
|
async processEvent(
|
|
|
|
event: Event,
|
|
|
|
identity: Identity,
|
2022-06-01 08:04:41 +12:00
|
|
|
properties: BaseEvent,
|
2022-05-25 11:15:52 +12:00
|
|
|
timestamp?: string | number
|
2022-05-24 09:14:44 +12:00
|
|
|
): Promise<void> {
|
2022-05-29 08:38:22 +12:00
|
|
|
properties.version = pkg.version
|
|
|
|
properties.service = env.SERVICE
|
2022-06-02 01:10:00 +12:00
|
|
|
properties.environment = env.DEPLOYMENT_ENVIRONMENT
|
2022-06-01 08:04:41 +12:00
|
|
|
|
|
|
|
const appId = context.getAppId()
|
|
|
|
if (appId) {
|
|
|
|
properties.appId = appId
|
|
|
|
}
|
|
|
|
|
2022-05-25 11:15:52 +12:00
|
|
|
const payload: any = { distinctId: identity.id, event, properties }
|
2022-06-01 08:04:41 +12:00
|
|
|
|
2022-05-25 11:15:52 +12:00
|
|
|
if (timestamp) {
|
|
|
|
payload.timestamp = new Date(timestamp)
|
|
|
|
}
|
2022-05-29 08:38:22 +12:00
|
|
|
|
|
|
|
// add groups to the event
|
|
|
|
if (identity.installationId || identity.tenantId) {
|
|
|
|
payload.groups = {}
|
|
|
|
if (identity.installationId) {
|
|
|
|
payload.groups.installation = identity.installationId
|
2022-06-01 08:04:41 +12:00
|
|
|
payload.properties.installationId = identity.installationId
|
2022-05-29 08:38:22 +12:00
|
|
|
}
|
|
|
|
if (identity.tenantId) {
|
|
|
|
payload.groups.tenant = identity.tenantId
|
2022-06-01 08:04:41 +12:00
|
|
|
payload.properties.tenantId = identity.tenantId
|
2022-05-29 08:38:22 +12:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-25 11:15:52 +12:00
|
|
|
this.posthog.capture(payload)
|
2022-05-05 19:32:14 +12:00
|
|
|
}
|
|
|
|
|
2022-05-26 08:32:08 +12:00
|
|
|
async identify(identity: Identity, timestamp?: string | number) {
|
|
|
|
const payload: any = { distinctId: identity.id, properties: identity }
|
|
|
|
if (timestamp) {
|
|
|
|
payload.timestamp = new Date(timestamp)
|
|
|
|
}
|
|
|
|
this.posthog.identify(payload)
|
2022-05-05 19:32:14 +12:00
|
|
|
}
|
|
|
|
|
2022-05-29 08:38:22 +12:00
|
|
|
async identifyGroup(group: Group, timestamp?: string | number) {
|
|
|
|
const payload: any = {
|
|
|
|
distinctId: group.id,
|
|
|
|
groupType: group.type,
|
|
|
|
groupKey: group.id,
|
|
|
|
properties: group,
|
|
|
|
}
|
|
|
|
|
|
|
|
if (timestamp) {
|
|
|
|
payload.timestamp = new Date(timestamp)
|
|
|
|
}
|
|
|
|
this.posthog.groupIdentify(payload)
|
|
|
|
}
|
|
|
|
|
2022-05-05 19:32:14 +12:00
|
|
|
shutdown() {
|
|
|
|
this.posthog.shutdown()
|
|
|
|
}
|
|
|
|
}
|