Skip to content

Commit 9083654

Browse files
committed
feat: New PostHog tracking events
1 parent 7b7764d commit 9083654

3 files changed

Lines changed: 57 additions & 18 deletions

File tree

apps/web/app/api/desktop/[...route]/root.ts

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { serverEnv } from "@cap/env";
1+
import { buildEnv, serverEnv } from "@cap/env";
22
import { Hono } from "hono";
33
import { zValidator } from "@hono/zod-validator";
44
import { z } from "zod";
@@ -8,6 +8,7 @@ import { users } from "@cap/database/schema";
88
import { eq } from "drizzle-orm";
99
import * as crypto from "node:crypto";
1010
import { withAuth } from "../../utils";
11+
import { PostHog } from "posthog-node";
1112

1213
export const app = new Hono().use(withAuth);
1314

@@ -148,10 +149,32 @@ app.post(
148149
success_url: `${serverEnv().WEB_URL}/dashboard/caps?upgrade=true`,
149150
cancel_url: `${serverEnv().WEB_URL}/pricing`,
150151
allow_promotion_codes: true,
152+
metadata: { platform: "desktop" },
151153
});
152154

153155
if (checkoutSession.url) {
154156
console.log("[POST] Checkout session created successfully");
157+
158+
try {
159+
const ph = new PostHog(buildEnv.NEXT_PUBLIC_POSTHOG_KEY || "", {
160+
host: buildEnv.NEXT_PUBLIC_POSTHOG_HOST || "",
161+
});
162+
163+
ph.capture({
164+
distinctId: user.id,
165+
event: "checkout_started",
166+
properties: {
167+
price_id: priceId,
168+
quantity: 1,
169+
platform: "desktop",
170+
},
171+
});
172+
173+
await ph.shutdown();
174+
} catch (e) {
175+
console.error("Failed to capture checkout_started in PostHog", e);
176+
}
177+
155178
return c.json({ url: checkoutSession.url });
156179
}
157180

apps/web/app/api/settings/billing/subscribe/route.ts

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import { NextRequest } from "next/server";
44
import { eq } from "drizzle-orm";
55
import { db } from "@cap/database";
66
import { users } from "@cap/database/schema";
7-
import { serverEnv } from "@cap/env";
8-
import posthog from "posthog-js";
7+
import { buildEnv, serverEnv } from "@cap/env";
8+
import { PostHog } from "posthog-node";
99

1010
export async function POST(request: NextRequest) {
1111
console.log("Starting subscription process");
@@ -36,15 +36,6 @@ export async function POST(request: NextRequest) {
3636
}
3737

3838
try {
39-
// Track subscription initiated event
40-
if (typeof window !== "undefined") {
41-
posthog.capture("subscription_initiated", {
42-
price_id: priceId,
43-
quantity: quantity,
44-
platform: "web",
45-
});
46-
}
47-
4839
if (!user.stripeCustomerId) {
4940
console.log("Creating new Stripe customer for user:", user.id);
5041
const customer = await stripe().customers.create({
@@ -75,10 +66,32 @@ export async function POST(request: NextRequest) {
7566
success_url: `${serverEnv().WEB_URL}/dashboard/caps?upgrade=true`,
7667
cancel_url: `${serverEnv().WEB_URL}/pricing`,
7768
allow_promotion_codes: true,
69+
metadata: { platform: "web" },
7870
});
7971

8072
if (checkoutSession.url) {
8173
console.log("Successfully created checkout session");
74+
75+
try {
76+
const ph = new PostHog(buildEnv.NEXT_PUBLIC_POSTHOG_KEY || "", {
77+
host: buildEnv.NEXT_PUBLIC_POSTHOG_HOST || "",
78+
});
79+
80+
ph.capture({
81+
distinctId: user.id,
82+
event: "checkout_started",
83+
properties: {
84+
price_id: priceId,
85+
quantity: quantity,
86+
platform: "web",
87+
},
88+
});
89+
90+
await ph.shutdown();
91+
} catch (e) {
92+
console.error("Failed to capture checkout_started in PostHog", e);
93+
}
94+
8295
return Response.json({ url: checkoutSession.url }, { status: 200 });
8396
}
8497

apps/web/app/api/webhooks/stripe/route.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -202,26 +202,29 @@ export const POST = async (req: Request) => {
202202
{ host: buildEnv.NEXT_PUBLIC_POSTHOG_HOST || "" }
203203
);
204204

205-
// Track subscription completed event
205+
const isFirstPurchase = !dbUser.stripeSubscriptionId;
206+
// Track purchase completed event
206207
serverPostHog.capture({
207208
distinctId: dbUser.id,
208-
event: "subscription_completed",
209+
event: "purchase_completed",
209210
properties: {
210211
subscription_id: subscription.id,
211212
subscription_status: subscription.status,
212213
invite_quota: inviteQuota,
213214
price_id: subscription.items.data[0]?.price.id,
214215
quantity: inviteQuota,
215-
platform: "web",
216-
is_first_subscription: true,
216+
platform:
217+
(session.metadata && (session.metadata as any).platform) ||
218+
"web",
219+
is_first_purchase: isFirstPurchase,
217220
},
218221
});
219222

220223
// Shutdown the client
221224
await serverPostHog.shutdown();
222-
console.log("Successfully tracked subscription event in PostHog");
225+
console.log("Successfully tracked purchase event in PostHog");
223226
} catch (error) {
224-
console.error("Error tracking subscription in PostHog:", error);
227+
console.error("Error tracking purchase in PostHog:", error);
225228
// Don't throw - we don't want to fail the webhook because of analytics
226229
}
227230
}

0 commit comments

Comments
 (0)