1
0
Fork 0
mirror of synced 2024-06-16 09:25:12 +12:00
budibase/packages/backend-core/src/events/processors/posthog/PosthogProcessor.ts
2022-08-08 15:44:29 +01:00

108 lines
2.7 KiB
TypeScript

import PostHog from "posthog-node"
import { Event, Identity, Group, BaseEvent } from "@budibase/types"
import { EventProcessor } from "../types"
import env from "../../../environment"
import * as context from "../../../context"
import * as rateLimiting from "./rateLimiting"
const pkg = require("../../../../package.json")
const EXCLUDED_EVENTS: Event[] = [
Event.USER_UPDATED,
Event.EMAIL_SMTP_UPDATED,
Event.AUTH_SSO_UPDATED,
Event.APP_UPDATED,
Event.ROLE_UPDATED,
Event.DATASOURCE_UPDATED,
Event.QUERY_UPDATED,
Event.TABLE_UPDATED,
Event.VIEW_UPDATED,
Event.VIEW_FILTER_UPDATED,
Event.VIEW_CALCULATION_UPDATED,
Event.AUTOMATION_TRIGGER_UPDATED,
Event.USER_GROUP_UPDATED,
]
export default class PosthogProcessor implements EventProcessor {
posthog: PostHog
constructor(token: string | undefined) {
if (!token) {
throw new Error("Posthog token is not defined")
}
this.posthog = new PostHog(token)
}
async processEvent(
event: Event,
identity: Identity,
properties: BaseEvent,
timestamp?: string | number
): Promise<void> {
// don't send excluded events
if (EXCLUDED_EVENTS.includes(event)) {
return
}
if (await rateLimiting.limited(event)) {
return
}
properties.version = pkg.version
properties.service = env.SERVICE
properties.environment = identity.environment
properties.hosting = identity.hosting
const appId = context.getAppId()
if (appId) {
properties.appId = appId
}
const payload: any = { distinctId: identity.id, event, properties }
if (timestamp) {
payload.timestamp = new Date(timestamp)
}
// add groups to the event
if (identity.installationId || identity.tenantId) {
payload.groups = {}
if (identity.installationId) {
payload.groups.installation = identity.installationId
payload.properties.installationId = identity.installationId
}
if (identity.tenantId) {
payload.groups.tenant = identity.tenantId
payload.properties.tenantId = identity.tenantId
}
}
this.posthog.capture(payload)
}
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)
}
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)
}
shutdown() {
this.posthog.shutdown()
}
}