Skip to content

Rallly is an open-source scheduling and collaboration tool designed to make organizing events and meetings easier.

📈 Track poll creation through the API in PostHog (#2749)

Luke Vella51c29f1399cf
diff --git a/apps/web/src/app/api/private/[...route]/route.test.ts b/apps/web/src/app/api/private/[...route]/route.test.ts
index ac3b80d71..e3be38289 100644
--- a/apps/web/src/app/api/private/[...route]/route.test.ts
+++ b/apps/web/src/app/api/private/[...route]/route.test.ts
@@ -9,6 +9,8 @@ const mockClosePoll = vi.fn();
const mockGetPollResults = vi.fn();
const mockGetPollParticipants = vi.fn();
const mockListPolls = vi.fn();
+const mockTrack = vi.fn();
+const mockIdentifyGroup = vi.fn();
vi.mock("@/features/poll/mutations", () => ({
deletePoll: (...args: unknown[]) => mockDeletePoll(...args),
@@ -59,6 +61,12 @@ vi.mock("@/lib/kv", () => ({
redis: null,
}));
+vi.mock("@/lib/posthog", () => ({
+ track: (...args: unknown[]) => mockTrack(...args),
+ identifyGroup: (...args: unknown[]) => mockIdentifyGroup(...args),
+ flushPostHog: vi.fn(),
+}));
+
import { prisma } from "@rallly/database";
import { after } from "next/server";
import { hashApiKey, verifyApiKey } from "@/features/api-keys/utils";
@@ -472,6 +480,40 @@ describe("Private API - /polls", () => {
);
});
+ it("should track poll creation with source api and kind date", async () => {
+ const res = await app.request("/api/private/polls", {
+ method: "POST",
+ headers: {
+ "Content-Type": "application/json",
+ Authorization: `Bearer ${testApiKey}`,
+ },
+ body: JSON.stringify({
+ title: "Team offsite",
+ dates: ["2025-01-15"],
+ }),
+ });
+
+ expect(res.status).toBe(200);
+ expect(mockTrack).toHaveBeenCalledWith(
+ { id: "test-user-id", isGuest: false },
+ expect.objectContaining({
+ event: "poll_create",
+ properties: expect.objectContaining({
+ kind: "date",
+ source: "api",
+ }),
+ groups: { poll: "test-poll-id", space: "test-space-id" },
+ }),
+ );
+ expect(mockIdentifyGroup).toHaveBeenCalledWith(
+ expect.objectContaining({
+ groupType: "poll",
+ groupKey: "test-poll-id",
+ properties: expect.objectContaining({ kind: "date" }),
+ }),
+ );
+ });
+
it("should save location when provided", async () => {
const res = await app.request("/api/private/polls", {
method: "POST",
@@ -569,6 +611,59 @@ describe("Private API - /polls", () => {
);
});
+ it("should track poll creation with source api and kind time", async () => {
+ mockCreatePoll.mockResolvedValueOnce({
+ id: "test-poll-id",
+ title: "Team sync",
+ description: null,
+ location: null,
+ timeZone: "Europe/London",
+ status: "open",
+ createdAt: new Date("2025-01-10T12:00:00Z"),
+ user: { name: "Test User", image: null },
+ options: [
+ {
+ id: "opt-1",
+ startTime: new Date("2025-01-15T09:00:00Z"),
+ duration: 30,
+ },
+ ],
+ });
+
+ const res = await app.request("/api/private/polls", {
+ method: "POST",
+ headers: {
+ "Content-Type": "application/json",
+ Authorization: `Bearer ${testApiKey}`,
+ },
+ body: JSON.stringify({
+ title: "Team sync",
+ slots: {
+ duration: 30,
+ timezone: "Europe/London",
+ times: ["2025-01-15T09:00:00Z"],
+ },
+ }),
+ });
+
+ expect(res.status).toBe(200);
+ expect(mockTrack).toHaveBeenCalledWith(
+ { id: "test-user-id", isGuest: false },
+ expect.objectContaining({
+ event: "poll_create",
+ properties: expect.objectContaining({
+ kind: "time",
+ source: "api",
+ }),
+ }),
+ );
+ expect(mockIdentifyGroup).toHaveBeenCalledWith(
+ expect.objectContaining({
+ properties: expect.objectContaining({ kind: "time" }),
+ }),
+ );
+ });
+
it("should create poll without timezone when not provided in request", async () => {
const res = await app.request("/api/private/polls", {
method: "POST",
diff --git a/apps/web/src/app/api/private/[...route]/route.ts b/apps/web/src/app/api/private/[...route]/route.ts
index def4af933..38e51a50a 100644
--- a/apps/web/src/app/api/private/[...route]/route.ts
+++ b/apps/web/src/app/api/private/[...route]/route.ts
@@ -10,6 +10,7 @@ import {
resolver,
validator,
} from "hono-openapi";
+import { after } from "next/server";
import {
getPollParticipants,
getPollResults,
@@ -18,6 +19,7 @@ import {
import { closePoll, createPoll, deletePoll } from "@/features/poll/mutations";
import type { AuthorizedSpaceId } from "@/features/space/types";
import { isMaintenanceModeEnabled } from "@/lib/maintenance";
+import { flushPostHog, identifyGroup, track } from "@/lib/posthog";
import {
createPollRequestExamples,
patchPollRequestExamples,
@@ -269,6 +271,54 @@ app.post(
organizerUserId = spaceMember.user.id;
}
+ const trackPollCreated = (poll: Awaited<ReturnType<typeof createPoll>>) => {
+ const kind = poll.options.some((o) => o.duration > 0) ? "time" : "date";
+
+ identifyGroup({
+ groupType: "poll",
+ groupKey: poll.id,
+ properties: {
+ name: poll.title,
+ status: poll.status,
+ kind,
+ created_at: poll.createdAt,
+ comment_count: 0,
+ option_count: poll.options.length,
+ has_location: !!poll.location,
+ has_description: !!poll.description,
+ timezone: poll.timeZone,
+ muted: false,
+ },
+ });
+
+ track(
+ { id: organizerUserId, isGuest: false },
+ {
+ event: "poll_create",
+ properties: {
+ title: poll.title,
+ kind,
+ source: "api",
+ optionCount: poll.options.length,
+ hasLocation: !!poll.location,
+ hasDescription: !!poll.description,
+ timezone: poll.timeZone,
+ requireParticipantEmail: input.requireEmail,
+ hideParticipants: input.hideParticipants,
+ hideScores: input.hideScores,
+ disableComments: input.disableComments,
+ isGuest: false,
+ },
+ groups: {
+ poll: poll.id,
+ space: spaceId,
+ },
+ },
+ );
+
+ after(() => flushPostHog());
+ };
+
// Process dates (all-day options)
if (input.dates) {
if (input.dates.length > MAX_OPTIONS) {
@@ -311,6 +361,8 @@ app.post(
spaceId,
});
+ trackPollCreated(poll);
+
return c.json(
createPollSuccessResponseSchema.parse(toPollResponseBody(poll)),
);
@@ -381,6 +433,8 @@ app.post(
spaceId,
});
+ trackPollCreated(poll);
+
return c.json(
createPollSuccessResponseSchema.parse(toPollResponseBody(poll)),
);
diff --git a/apps/web/src/trpc/routers/polls.ts b/apps/web/src/trpc/routers/polls.ts
index c2f447359..972fd4b1b 100644
--- a/apps/web/src/trpc/routers/polls.ts
+++ b/apps/web/src/trpc/routers/polls.ts
@@ -267,6 +267,7 @@ export const polls = router({
properties: {
title: poll.title,
kind,
+ source: "web",
optionCount: poll.options.length,
hasLocation: !!location,
hasDescription: !!description,