📈 Track user upgrades in posthog (#1018)

This commit is contained in:
Luke Vella 2024-02-07 15:17:48 +07:00 committed by GitHub
parent 1b509a0750
commit 8ea2cafbc4
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 33 additions and 2 deletions

View file

@ -88,6 +88,11 @@ export default async function handler(
metadata: {
userId: userSession.user.id,
},
subscription_data: {
metadata: {
userId: userSession.user.id,
},
},
line_items: [
{
price:

View file

@ -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({