Rallly is an open-source scheduling and collaboration tool designed to make organizing events and meetings easier.
TypeScript i18nextnext-authnextjspostgresqlprismareact-emailt3-stacktailwindcsstrpcturborepotypescriptzod
🐛 Don't create PostHog person profiles for guest users (#2676)
Luke Vella4d30465a52d7
diff --git a/apps/web/src/trpc/routers/polls.ts b/apps/web/src/trpc/routers/polls.tsindex e09460152..9944f1455 100644--- a/apps/web/src/trpc/routers/polls.ts+++ b/apps/web/src/trpc/routers/polls.ts@@ -165,6 +165,9 @@ export const polls = router({action: "create_poll",verdict: moderation.verdict,reason: moderation.reason,+ // Guests are transient and rarely convert, so don't create a+ // PostHog person profile for them.+ $process_person_profile: !ctx.user.isGuest,},});}@@ -293,6 +296,9 @@ export const polls = router({hideScores: poll.hideScores,requireParticipantEmail: poll.requireParticipantEmail,isGuest: ctx.user.isGuest,+ // Guests are transient and rarely convert, so don't create a+ // PostHog person profile for them (keeps them as anonymous events).+ $process_person_profile: !ctx.user.isGuest,},groups: {poll: poll.id,@@ -358,6 +364,9 @@ export const polls = router({action: "update_poll",verdict: moderation.verdict,reason: moderation.reason,+ // Guests are transient and rarely convert, so don't create a+ // PostHog person profile for them.+ $process_person_profile: !ctx.user.isGuest,},});}@@ -511,6 +520,9 @@ export const polls = router({has_location: !!updatedPoll.location,has_description: !!updatedPoll.description,is_guest: ctx.user.isGuest,+ // Guests are transient and rarely convert, so don't create a+ // PostHog person profile for them (keeps them as anonymous events).+ $process_person_profile: !ctx.user.isGuest,},groups: {poll: pollId,@@ -524,6 +536,9 @@ export const polls = router({distinctId: ctx.user.id,properties: {option_count: updatedPoll._count.options,+ // Guests are transient and rarely convert, so don't create a+ // PostHog person profile for them (keeps them as anonymous events).+ $process_person_profile: !ctx.user.isGuest,},groups: {poll: pollId,@@ -540,6 +555,9 @@ export const polls = router({hide_participants: !!updatedPoll.hideParticipants,hide_scores: !!updatedPoll.hideScores,require_participant_email: !!updatedPoll.requireParticipantEmail,+ // Guests are transient and rarely convert, so don't create a+ // PostHog person profile for them (keeps them as anonymous events).+ $process_person_profile: !ctx.user.isGuest,},groups: {poll: pollId,@@ -575,6 +593,11 @@ export const polls = router({posthog()?.capture({event: "poll_delete",distinctId: ctx.user.id,+ properties: {+ // Guests are transient and rarely convert, so don't create a+ // PostHog person profile for them (keeps them as anonymous events).+ $process_person_profile: !ctx.user.isGuest,+ },groups: {poll: pollId,},@@ -1168,6 +1191,11 @@ export const polls = router({posthog()?.capture({event: "poll_close",distinctId: ctx.user.id,+ properties: {+ // Guests are transient and rarely convert, so don't create a+ // PostHog person profile for them (keeps them as anonymous events).+ $process_person_profile: !ctx.user.isGuest,+ },groups: {poll: input.pollId,},diff --git a/apps/web/src/trpc/routers/polls/comments.ts b/apps/web/src/trpc/routers/polls/comments.tsindex a095f476e..f9303f3ec 100644--- a/apps/web/src/trpc/routers/polls/comments.ts+++ b/apps/web/src/trpc/routers/polls/comments.ts@@ -15,7 +15,7 @@ import {requireUserMiddleware,router,} from "../../trpc";-import { resolveUserId } from "./utils";+import { resolveActor } from "./utils";const logger = createLogger("comments");@@ -171,6 +171,9 @@ export const comments = router({event: "poll_comment_add",properties: {is_guest: ctx.user.isGuest,+ // Guests are transient and rarely convert, so don't create a+ // PostHog person profile for them (keeps them as anonymous events).+ $process_person_profile: !ctx.user.isGuest,},groups: {poll: pollId,@@ -187,7 +190,7 @@ export const comments = router({}),).mutation(async ({ input: { commentId, token }, ctx }) => {- const userId = await resolveUserId(token, ctx.user);+ const actor = await resolveActor(token, ctx.user);const comment = await prisma.comment.findUnique({where: { id: commentId },@@ -201,9 +204,9 @@ export const comments = router({});}- const isAuthor = comment.userId === userId;+ const isAuthor = comment.userId === actor.id;- if (!isAuthor && !(await hasPollAdminAccess(comment.pollId, userId))) {+ if (!isAuthor && !(await hasPollAdminAccess(comment.pollId, actor.id))) {throw new TRPCError({code: "FORBIDDEN",message: "You are not allowed to delete this comment",@@ -217,14 +220,17 @@ export const comments = router({});// Track comment deletion analytics- if (comment) {- posthog()?.capture({- distinctId: userId,- event: "poll_comment_delete",- groups: {- poll: comment.pollId,- },- });- }+ posthog()?.capture({+ distinctId: actor.id,+ event: "poll_comment_delete",+ properties: {+ // Guests are transient and rarely convert, so don't create a+ // PostHog person profile for them (keeps them as anonymous events).+ $process_person_profile: !actor.isGuest,+ },+ groups: {+ poll: comment.pollId,+ },+ });}),});diff --git a/apps/web/src/trpc/routers/polls/participants.ts b/apps/web/src/trpc/routers/polls/participants.tsindex 45cdba0ac..b4e4a0d75 100644--- a/apps/web/src/trpc/routers/polls/participants.ts+++ b/apps/web/src/trpc/routers/polls/participants.ts@@ -20,7 +20,7 @@ import {} from "../../trpc";import {createParticipantEditToken,- resolveUserId,+ resolveActor,tryResolveUserId,} from "./utils";@@ -196,9 +196,9 @@ export const participants = router({}),).mutation(async ({ input: { participantId, token }, ctx }) => {- const userId = await resolveUserId(token, ctx.user);+ const actor = await resolveActor(token, ctx.user);- const participant = await canModifyParticipant(participantId, userId);+ const participant = await canModifyParticipant(participantId, actor.id);await prisma.participant.update({where: {@@ -211,10 +211,13 @@ export const participants = router({});posthog()?.capture({- distinctId: userId,+ distinctId: actor.id,event: "poll_response_delete",properties: {participant_id: participant.id,+ // Guests are transient and rarely convert, so don't create a+ // PostHog person profile for them (keeps them as anonymous events).+ $process_person_profile: !actor.isGuest,},groups: {poll: participant.pollId,@@ -355,6 +358,9 @@ export const participants = router({participant_id: participant.id,has_email: !!email,total_responses: totalResponses,+ // Guests are transient and rarely convert, so don't create a+ // PostHog person profile for them (keeps them as anonymous events).+ $process_person_profile: !ctx.user.isGuest,},groups: {poll: pollId,@@ -373,7 +379,7 @@ export const participants = router({}),).mutation(async ({ input: { participantId, newName, token }, ctx }) => {- const userId = await resolveUserId(token, ctx.user);+ const { id: userId } = await resolveActor(token, ctx.user);await canModifyParticipant(participantId, userId);@@ -402,11 +408,11 @@ export const participants = router({}),).mutation(async ({ input: { participantId, votes, token }, ctx }) => {- const userId = await resolveUserId(token, ctx.user);+ const actor = await resolveActor(token, ctx.user);const existingParticipant = await canModifyParticipant(participantId,- userId,+ actor.id,);const pollId = existingParticipant.pollId;@@ -469,8 +475,13 @@ export const participants = router({});posthog()?.capture({- distinctId: userId,+ distinctId: actor.id,event: "poll_response_update",+ properties: {+ // Guests are transient and rarely convert, so don't create a+ // PostHog person profile for them (keeps them as anonymous events).+ $process_person_profile: !actor.isGuest,+ },groups: {poll: pollId,},diff --git a/apps/web/src/trpc/routers/polls/utils.ts b/apps/web/src/trpc/routers/polls/utils.tsindex b7c6129ff..a20e6cdb2 100644--- a/apps/web/src/trpc/routers/polls/utils.ts+++ b/apps/web/src/trpc/routers/polls/utils.ts@@ -23,24 +23,48 @@ export async function createParticipantEditToken();}-export async function resolveUserId(+export async function tryResolveUserId(token: string | undefined,ctxUser: { id: string } | undefined,-): Promise<string> {- const userId = await tryResolveUserId(token, ctxUser);- if (!userId) {+): Promise<string | null> {+ const userIdFromToken = await getUserIdFromToken(token);+ return userIdFromToken ?? ctxUser?.id ?? null;+}++type Actor = { id: string; isGuest: boolean };++/**+ * Resolve the acting user along with whether they should be treated as a guest+ * for analytics (person-processing) purposes.+ *+ * Participant/comment edit tokens are only ever issued to guest participants,+ * so a token-resolved actor is treated as a guest. A signed-in real user always+ * acts through their session (`ctxUser`), never a token.+ */+export async function tryResolveActor(+ token: string | undefined,+ ctxUser: Actor | undefined,+): Promise<Actor | null> {+ const userIdFromToken = await getUserIdFromToken(token);+ if (userIdFromToken) {+ return { id: userIdFromToken, isGuest: true };+ }+ if (ctxUser) {+ return { id: ctxUser.id, isGuest: ctxUser.isGuest };+ }+ return null;+}++export async function resolveActor(+ token: string | undefined,+ ctxUser: Actor | undefined,+): Promise<Actor> {+ const actor = await tryResolveActor(token, ctxUser);+ if (!actor) {throw new TRPCError({code: "UNAUTHORIZED",message: "This method requires a user or valid token",});}- return userId;-}--export async function tryResolveUserId(- token: string | undefined,- ctxUser: { id: string } | undefined,-): Promise<string | null> {- const userIdFromToken = await getUserIdFromToken(token);- return userIdFromToken ?? ctxUser?.id ?? null;+ return actor;}