Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

feat: New PostHog tracking events

Richie McIlroy908365441850
diff --git a/apps/web/app/api/desktop/[...route]/root.ts b/apps/web/app/api/desktop/[...route]/root.ts
index 8b6f93a85..de4a8a937 100644
--- a/apps/web/app/api/desktop/[...route]/root.ts
+++ b/apps/web/app/api/desktop/[...route]/root.ts
@@ -1,4 +1,4 @@
-import { serverEnv } from "@cap/env";
+import { buildEnv, serverEnv } from "@cap/env";
import { Hono } from "hono";
import { zValidator } from "@hono/zod-validator";
import { z } from "zod";
@@ -8,6 +8,7 @@ import { users } from "@cap/database/schema";
import { eq } from "drizzle-orm";
import * as crypto from "node:crypto";
import { withAuth } from "../../utils";
+import { PostHog } from "posthog-node";
export const app = new Hono().use(withAuth);
@@ -148,10 +149,32 @@ app.post(
success_url: `${serverEnv().WEB_URL}/dashboard/caps?upgrade=true`,
cancel_url: `${serverEnv().WEB_URL}/pricing`,
allow_promotion_codes: true,
+ metadata: { platform: "desktop" },
});
if (checkoutSession.url) {
console.log("[POST] Checkout session created successfully");
+
+ try {
+ const ph = new PostHog(buildEnv.NEXT_PUBLIC_POSTHOG_KEY || "", {
+ host: buildEnv.NEXT_PUBLIC_POSTHOG_HOST || "",
+ });
+
+ ph.capture({
+ distinctId: user.id,
+ event: "checkout_started",
+ properties: {
+ price_id: priceId,
+ quantity: 1,
+ platform: "desktop",
+ },
+ });
+
+ await ph.shutdown();
+ } catch (e) {
+ console.error("Failed to capture checkout_started in PostHog", e);
+ }
+
return c.json({ url: checkoutSession.url });
}
diff --git a/apps/web/app/api/settings/billing/subscribe/route.ts b/apps/web/app/api/settings/billing/subscribe/route.ts
index 9f617aac5..ae02c6836 100644
--- a/apps/web/app/api/settings/billing/subscribe/route.ts
+++ b/apps/web/app/api/settings/billing/subscribe/route.ts
@@ -4,8 +4,8 @@ import { NextRequest } from "next/server";
import { eq } from "drizzle-orm";
import { db } from "@cap/database";
import { users } from "@cap/database/schema";
-import { serverEnv } from "@cap/env";
-import posthog from "posthog-js";
+import { buildEnv, serverEnv } from "@cap/env";
+import { PostHog } from "posthog-node";
export async function POST(request: NextRequest) {
console.log("Starting subscription process");
@@ -36,15 +36,6 @@ export async function POST(request: NextRequest) {
}
try {
- // Track subscription initiated event
- if (typeof window !== "undefined") {
- posthog.capture("subscription_initiated", {
- price_id: priceId,
- quantity: quantity,
- platform: "web",
- });
- }
-
if (!user.stripeCustomerId) {
console.log("Creating new Stripe customer for user:", user.id);
const customer = await stripe().customers.create({
@@ -75,10 +66,32 @@ export async function POST(request: NextRequest) {
success_url: `${serverEnv().WEB_URL}/dashboard/caps?upgrade=true`,
cancel_url: `${serverEnv().WEB_URL}/pricing`,
allow_promotion_codes: true,
+ metadata: { platform: "web" },
});
if (checkoutSession.url) {
console.log("Successfully created checkout session");
+
+ try {
+ const ph = new PostHog(buildEnv.NEXT_PUBLIC_POSTHOG_KEY || "", {
+ host: buildEnv.NEXT_PUBLIC_POSTHOG_HOST || "",
+ });
+
+ ph.capture({
+ distinctId: user.id,
+ event: "checkout_started",
+ properties: {
+ price_id: priceId,
+ quantity: quantity,
+ platform: "web",
+ },
+ });
+
+ await ph.shutdown();
+ } catch (e) {
+ console.error("Failed to capture checkout_started in PostHog", e);
+ }
+
return Response.json({ url: checkoutSession.url }, { status: 200 });
}
diff --git a/apps/web/app/api/webhooks/stripe/route.ts b/apps/web/app/api/webhooks/stripe/route.ts
index 350b12ad3..85719d287 100644
--- a/apps/web/app/api/webhooks/stripe/route.ts
+++ b/apps/web/app/api/webhooks/stripe/route.ts
@@ -202,26 +202,29 @@ export const POST = async (req: Request) => {
{ host: buildEnv.NEXT_PUBLIC_POSTHOG_HOST || "" }
);
- // Track subscription completed event
+ const isFirstPurchase = !dbUser.stripeSubscriptionId;
+ // Track purchase completed event
serverPostHog.capture({
distinctId: dbUser.id,
- event: "subscription_completed",
+ event: "purchase_completed",
properties: {
subscription_id: subscription.id,
subscription_status: subscription.status,
invite_quota: inviteQuota,
price_id: subscription.items.data[0]?.price.id,
quantity: inviteQuota,
- platform: "web",
- is_first_subscription: true,
+ platform:
+ (session.metadata && (session.metadata as any).platform) ||
+ "web",
+ is_first_purchase: isFirstPurchase,
},
});
// Shutdown the client
await serverPostHog.shutdown();
- console.log("Successfully tracked subscription event in PostHog");
+ console.log("Successfully tracked purchase event in PostHog");
} catch (error) {
- console.error("Error tracking subscription in PostHog:", error);
+ console.error("Error tracking purchase in PostHog:", error);
// Don't throw - we don't want to fail the webhook because of analytics
}
}