2
0

♻️ Re-organize telemetry package

This commit is contained in:
Baptiste Arnaud
2024-02-01 14:19:24 +01:00
parent 3fcb0081e5
commit 92a1d672fd
26 changed files with 102 additions and 206 deletions

View File

@ -40,6 +40,7 @@
"google-auth-library": "8.9.0",
"got": "12.6.0",
"minio": "7.1.3",
"posthog-node": "3.1.1",
"remark-parse": "11.0.0",
"stripe": "12.13.0",
"unified": "11.0.4",

View File

@ -1,29 +0,0 @@
import got from 'got'
import { TelemetryEvent } from '@typebot.io/schemas/features/telemetry'
import { env } from '@typebot.io/env'
export const sendTelemetryEvents = async (events: TelemetryEvent[]) => {
if (events.length === 0) return { message: 'No events to send' }
if (!env.TELEMETRY_WEBHOOK_URL) return { message: 'Telemetry not enabled' }
try {
await got.post(env.TELEMETRY_WEBHOOK_URL, {
json: { events },
headers: {
authorization: env.TELEMETRY_WEBHOOK_BEARER_TOKEN
? `Bearer ${env.TELEMETRY_WEBHOOK_BEARER_TOKEN}`
: undefined,
},
})
} catch (err) {
console.error('Failed to send event', err)
return {
message: 'Failed to send event',
error: err instanceof Error ? err.message : 'Unknown error',
}
}
return {
message: 'Event sent',
}
}

View File

@ -0,0 +1,54 @@
import { env } from '@typebot.io/env'
import { TelemetryEvent } from '@typebot.io/schemas/features/telemetry'
import { PostHog } from 'posthog-node'
import ky from 'ky'
export const trackEvents = async (events: TelemetryEvent[]) => {
if (!env.NEXT_PUBLIC_POSTHOG_KEY) return
const client = new PostHog(env.NEXT_PUBLIC_POSTHOG_KEY, {
host: env.NEXT_PUBLIC_POSTHOG_HOST,
})
events.forEach(async (event) => {
if (event.name === 'User created') {
client.identify({
distinctId: event.userId,
properties: event.data,
})
if (env.USER_CREATED_WEBHOOK_URL) {
await ky.post(env.USER_CREATED_WEBHOOK_URL, {
json: {
email: event.data.email,
name: event.data.name ? event.data.name.split(' ')[0] : undefined,
},
})
}
}
if (
event.name === 'Workspace created' ||
event.name === 'Subscription updated'
)
client.groupIdentify({
groupType: 'workspace',
groupKey: event.workspaceId,
properties: event.data,
})
if (event.name === 'Typebot created' || event.name === 'Typebot published')
client.groupIdentify({
groupType: 'typebot',
groupKey: event.typebotId,
properties: { name: event.data.name },
})
const groups: { workspace?: string; typebot?: string } = {}
if ('workspaceId' in event) groups['workspace'] = event.workspaceId
if ('typebotId' in event) groups['typebot'] = event.typebotId
client.capture({
distinctId: event.userId,
event: event.name,
properties: 'data' in event ? event.data : undefined,
groups,
})
})
await client.shutdownAsync()
}