📈 Send onboarding replies to PostHog

This commit is contained in:
Baptiste Arnaud
2024-02-02 11:58:32 +01:00
parent ce79e897a7
commit fd4867f3ae
12 changed files with 99 additions and 36 deletions

View File

@@ -2,17 +2,19 @@ import prisma from '@typebot.io/lib/prisma'
import { NextApiRequest, NextApiResponse } from 'next'
import { getAuthenticatedUser } from '@/features/auth/helpers/getAuthenticatedUser'
import { methodNotAllowed, notAuthenticated } from '@typebot.io/lib/api'
import { Prisma, User } from '@typebot.io/prisma'
import { User } from '@typebot.io/schemas'
import { trackEvents } from '@typebot.io/lib/telemetry/trackEvents'
import { Prisma } from '@typebot.io/prisma'
const handler = async (req: NextApiRequest, res: NextApiResponse) => {
const user = await getAuthenticatedUser(req, res)
if (!user) return notAuthenticated(res)
const id = req.query.userId as string
if (req.method === 'PUT') {
if (req.method === 'PATCH') {
const data = (
typeof req.body === 'string' ? JSON.parse(req.body) : req.body
) as User
) as Partial<User>
const typebots = await prisma.user.update({
where: { id },
data: {
@@ -22,6 +24,19 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
data.displayedInAppNotifications ?? Prisma.DbNull,
},
})
if (data.onboardingCategories || data.referral || data.company || data.name)
await trackEvents([
{
name: 'User updated',
userId: user.id,
data: {
name: data.name ?? undefined,
onboardingCategories: data.onboardingCategories ?? undefined,
referral: data.referral ?? undefined,
company: data.company ?? undefined,
},
},
])
return res.send({ typebots })
}
return methodNotAllowed(res)