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

@ -16,7 +16,7 @@
"@stripe/stripe-js": "1.54.1",
"@udecode/plate-common": "21.1.5",
"dompurify": "3.0.6",
"ky": "^1.1.3",
"ky": "1.1.3",
"marked": "9.0.3",
"solid-element": "1.7.1",
"solid-js": "1.7.8"

6
packages/env/env.ts vendored
View File

@ -57,7 +57,11 @@ const baseEnv = {
z.string().url()
),
DISABLE_SIGNUP: boolean.optional().default('false'),
ADMIN_EMAIL: z.string().email().optional(),
ADMIN_EMAIL: z
.string()
.min(1)
.optional()
.transform((val) => val?.split(',')),
DEFAULT_WORKSPACE_PLAN: z
.enum(['FREE', 'STARTER', 'PRO', 'LIFETIME', 'UNLIMITED'])
.refine((str) =>

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()
}

View File

@ -5,7 +5,7 @@ import { promptAndSetEnvironment } from './utils'
import { Workspace } from '@typebot.io/schemas'
import { sendAlmostReachedChatsLimitEmail } from '@typebot.io/emails/src/emails/AlmostReachedChatsLimitEmail'
import { TelemetryEvent } from '@typebot.io/schemas/features/telemetry'
import { sendTelemetryEvents } from '@typebot.io/lib/telemetry/sendTelemetryEvent'
import { trackEvents } from '@typebot.io/lib/telemetry/trackEvents'
import Stripe from 'stripe'
import { createId } from '@paralleldrive/cuid2'
@ -222,7 +222,7 @@ export const checkAndReportChatsUsage = async () => {
`Send ${newResultsCollectedEvents.length} new results events and ${quarantineEvents.length} auto quarantine events...`
)
await sendTelemetryEvents(quarantineEvents.concat(newResultsCollectedEvents))
await trackEvents(quarantineEvents.concat(newResultsCollectedEvents))
}
const getSubscription = async (