♻️ Add abstractions for tracking server-side events (#1143)

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
This commit is contained in:
Luke Vella 2024-06-10 20:23:49 +01:00 committed by GitHub
parent 81d2f2c0bd
commit d43bc631f1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 69 additions and 12 deletions

View file

@ -12,7 +12,6 @@
"@rallly/database": "*",
"@rallly/emails": "*",
"@rallly/utils": "*",
"@vercel/functions": "^1.0.2",
"@trpc/server": "^10.13.0",
"iron-session": "^6.3.1",
"spacetime": "^7.4.7",

View file

@ -1,6 +1,7 @@
import { EmailClient } from "@rallly/emails";
import { inferAsyncReturnType, TRPCError } from "@trpc/server";
import { CreateNextContextOptions } from "@trpc/server/adapters/next";
import type { PostHog } from "posthog-node";
export type GetUserFn = (opts: CreateNextContextOptions) => Promise<{
id: string;
@ -12,6 +13,7 @@ export interface TRPCContextParams {
emailClient: EmailClient;
isSelfHosted: boolean;
isEmailBlocked?: (email: string) => boolean;
posthogClient?: PostHog;
/**
* Takes a relative path and returns an absolute URL to the app
* @param path

View file

@ -626,6 +626,7 @@ export const polls = router({
},
select: {
id: true,
createdAt: true,
timeZone: true,
title: true,
location: true,
@ -859,7 +860,23 @@ export const polls = router({
});
});
waitUntil(Promise.all([emailToHost, ...emailsToParticipants]));
ctx.posthogClient?.capture({
distinctId: ctx.user.id,
event: "finalize poll",
properties: {
number_of_participants: poll.participants.length,
number_of_attendees: attendees.length,
dayjs_since_created: dayjs().diff(poll.createdAt, "day"),
},
});
waitUntil(
Promise.all([
emailToHost,
...emailsToParticipants,
ctx.posthogClient?.flushAsync(),
]),
);
}
}),
reopen: possiblyPublicProcedure