Skip to content

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

📈 Track user upgrades in posthog (#1018)

Luke Vella8ea2cafbc414
diff --git a/apps/web/src/pages/api/stripe/checkout.ts b/apps/web/src/pages/api/stripe/checkout.ts
index 46bcc1ece..add865a59 100644
--- a/apps/web/src/pages/api/stripe/checkout.ts
+++ b/apps/web/src/pages/api/stripe/checkout.ts
@@ -88,6 +88,11 @@ export default async function handler(
metadata: {
userId: userSession.user.id,
},
+ subscription_data: {
+ metadata: {
+ userId: userSession.user.id,
+ },
+ },
line_items: [
{
price:
diff --git a/apps/web/src/pages/api/stripe/webhook.ts b/apps/web/src/pages/api/stripe/webhook.ts
index 1714f220f..33bfdcbc0 100644
--- a/apps/web/src/pages/api/stripe/webhook.ts
+++ b/apps/web/src/pages/api/stripe/webhook.ts
@@ -1,10 +1,13 @@
import type { Stripe } from "@rallly/backend/stripe";
import { stripe } from "@rallly/backend/stripe";
import { prisma } from "@rallly/database";
+import * as Sentry from "@sentry/node";
import { buffer } from "micro";
import { NextApiRequest, NextApiResponse } from "next";
import { z } from "zod";
+import { PostHogClient } from "@/app/posthog";
+
export const config = {
api: {
bodyParser: false,
@@ -50,7 +53,7 @@ export default async function handler(
}
switch (event.type) {
- case "checkout.session.completed":
+ case "checkout.session.completed": {
const checkoutSession = event.data.object as Stripe.Checkout.Session;
if (checkoutSession.subscription === null) {
@@ -74,10 +77,31 @@ export default async function handler(
subscriptionId: checkoutSession.subscription as string,
},
});
+
+ const subscription = await stripe.subscriptions.retrieve(
+ checkoutSession.subscription as string,
+ );
+
+ try {
+ const posthog = PostHogClient();
+
+ posthog?.capture({
+ distinctId: userId,
+ event: "upgrade",
+ properties: {
+ interval: subscription.items.data[0].plan.interval,
+ },
+ });
+ await posthog?.shutdownAsync();
+ } catch (e) {
+ Sentry.captureMessage("Failed to track upgrade event");
+ }
+
break;
+ }
case "customer.subscription.deleted":
case "customer.subscription.updated":
- case "customer.subscription.created":
+ case "customer.subscription.created": {
const { id } = event.data.object as Stripe.Subscription;
const subscription = await stripe.subscriptions.retrieve(id);
@@ -113,7 +137,9 @@ export default async function handler(
periodEnd: toDate(subscription.current_period_end),
},
});
+
break;
+ }
default:
// Unexpected event type
res.status(400).json({