From ddec8351bb9b4ecf5c842d34d7986c0b567b2648 Mon Sep 17 00:00:00 2001 From: Baptiste Arnaud Date: Sat, 7 Dec 2024 10:49:06 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=93=88=20add=20"User=20logged=20out"=20te?= =?UTF-8?q?lemetry=20event?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/features/typebot/api/publishTypebot.ts | 16 ++++++++-------- apps/builder/src/pages/api/auth/[...nextauth].ts | 8 +++++++- packages/telemetry/src/schemas.ts | 7 +++++++ 3 files changed, 22 insertions(+), 9 deletions(-) diff --git a/apps/builder/src/features/typebot/api/publishTypebot.ts b/apps/builder/src/features/typebot/api/publishTypebot.ts index b9f2aaa2c1..23b764f37e 100644 --- a/apps/builder/src/features/typebot/api/publishTypebot.ts +++ b/apps/builder/src/features/typebot/api/publishTypebot.ts @@ -8,6 +8,7 @@ import prisma from "@typebot.io/prisma"; import { Plan } from "@typebot.io/prisma/enum"; import { computeRiskLevel } from "@typebot.io/radar"; import { settingsSchema } from "@typebot.io/settings/schemas"; +import type { TelemetryEvent } from "@typebot.io/telemetry/schemas"; import { trackEvents } from "@typebot.io/telemetry/trackEvents"; import { themeSchema } from "@typebot.io/theme/schemas"; import { edgeSchema } from "@typebot.io/typebot/schemas/edge"; @@ -135,7 +136,7 @@ export const publishTypebot = authenticatedProcedure } } - const publishEvents = await parseTypebotPublishEvents({ + const publishEvents: TelemetryEvent[] = await parseTypebotPublishEvents({ existingTypebot, userId: user.id, hasFileUploadBlocks, @@ -162,7 +163,7 @@ export const publishTypebot = authenticatedProcedure theme: themeSchema.parse(existingTypebot.theme), }, }); - else + else { await prisma.publicTypebot.createMany({ data: { version: existingTypebot.version, @@ -181,10 +182,7 @@ export const publishTypebot = authenticatedProcedure theme: themeSchema.parse(existingTypebot.theme), }, }); - - await trackEvents([ - ...publishEvents, - { + publishEvents.push({ name: "Typebot published", workspaceId: existingTypebot.workspaceId, typebotId: existingTypebot.id, @@ -193,8 +191,10 @@ export const publishTypebot = authenticatedProcedure name: existingTypebot.name, isFirstPublish: existingTypebot.publishedTypebot ? undefined : true, }, - }, - ]); + }); + } + + await trackEvents(publishEvents); return { message: "success" }; }); diff --git a/apps/builder/src/pages/api/auth/[...nextauth].ts b/apps/builder/src/pages/api/auth/[...nextauth].ts index 060716effe..8d26072c60 100644 --- a/apps/builder/src/pages/api/auth/[...nextauth].ts +++ b/apps/builder/src/pages/api/auth/[...nextauth].ts @@ -186,8 +186,14 @@ export const getAuthOptions = ({ signIn({ user }) { Sentry.setUser({ id: user.id }); }, - signOut() { + async signOut({ session }) { Sentry.setUser(null); + await trackEvents([ + { + name: "User logged out", + userId: (session as unknown as { userId: string }).userId, + }, + ]); }, }, callbacks: { diff --git a/packages/telemetry/src/schemas.ts b/packages/telemetry/src/schemas.ts index c35a53dd1c..87f97b4919 100644 --- a/packages/telemetry/src/schemas.ts +++ b/packages/telemetry/src/schemas.ts @@ -43,6 +43,12 @@ const userLoggedInEventSchema = userEvent.merge( }), ); +const userLoggedOutEventSchema = userEvent.merge( + z.object({ + name: z.literal("User logged out"), + }), +); + const userUpdatedEventSchema = userEvent.merge( z.object({ name: z.literal("User updated"), @@ -192,6 +198,7 @@ export const eventSchema = z.discriminatedUnion("name", [ workspaceCreatedEventSchema, userCreatedEventSchema, userLoggedInEventSchema, + userLoggedOutEventSchema, typebotCreatedEventSchema, publishedTypebotEventSchema, subscriptionUpdatedEventSchema,