Skip to content

Commit

Permalink
📈 add "User logged out" telemetry event
Browse files Browse the repository at this point in the history
  • Loading branch information
baptisteArno committed Dec 7, 2024
1 parent 9d7651d commit ddec835
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 9 deletions.
16 changes: 8 additions & 8 deletions apps/builder/src/features/typebot/api/publishTypebot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -135,7 +136,7 @@ export const publishTypebot = authenticatedProcedure
}
}

const publishEvents = await parseTypebotPublishEvents({
const publishEvents: TelemetryEvent[] = await parseTypebotPublishEvents({
existingTypebot,
userId: user.id,
hasFileUploadBlocks,
Expand All @@ -162,7 +163,7 @@ export const publishTypebot = authenticatedProcedure
theme: themeSchema.parse(existingTypebot.theme),
},
});
else
else {
await prisma.publicTypebot.createMany({
data: {
version: existingTypebot.version,
Expand All @@ -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,
Expand All @@ -193,8 +191,10 @@ export const publishTypebot = authenticatedProcedure
name: existingTypebot.name,
isFirstPublish: existingTypebot.publishedTypebot ? undefined : true,
},
},
]);
});
}

await trackEvents(publishEvents);

return { message: "success" };
});
8 changes: 7 additions & 1 deletion apps/builder/src/pages/api/auth/[...nextauth].ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down
7 changes: 7 additions & 0 deletions packages/telemetry/src/schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down Expand Up @@ -192,6 +198,7 @@ export const eventSchema = z.discriminatedUnion("name", [
workspaceCreatedEventSchema,
userCreatedEventSchema,
userLoggedInEventSchema,
userLoggedOutEventSchema,
typebotCreatedEventSchema,
publishedTypebotEventSchema,
subscriptionUpdatedEventSchema,
Expand Down

0 comments on commit ddec835

Please sign in to comment.