mirror of
https://github.com/lukevella/rallly.git
synced 2025-08-06 09:59:00 +02:00
📈 Track user upgrades in posthog (#1018)
This commit is contained in:
parent
1b509a0750
commit
8ea2cafbc4
2 changed files with 33 additions and 2 deletions
|
@ -88,6 +88,11 @@ export default async function handler(
|
||||||
metadata: {
|
metadata: {
|
||||||
userId: userSession.user.id,
|
userId: userSession.user.id,
|
||||||
},
|
},
|
||||||
|
subscription_data: {
|
||||||
|
metadata: {
|
||||||
|
userId: userSession.user.id,
|
||||||
|
},
|
||||||
|
},
|
||||||
line_items: [
|
line_items: [
|
||||||
{
|
{
|
||||||
price:
|
price:
|
||||||
|
|
|
@ -1,10 +1,13 @@
|
||||||
import type { Stripe } from "@rallly/backend/stripe";
|
import type { Stripe } from "@rallly/backend/stripe";
|
||||||
import { stripe } from "@rallly/backend/stripe";
|
import { stripe } from "@rallly/backend/stripe";
|
||||||
import { prisma } from "@rallly/database";
|
import { prisma } from "@rallly/database";
|
||||||
|
import * as Sentry from "@sentry/node";
|
||||||
import { buffer } from "micro";
|
import { buffer } from "micro";
|
||||||
import { NextApiRequest, NextApiResponse } from "next";
|
import { NextApiRequest, NextApiResponse } from "next";
|
||||||
import { z } from "zod";
|
import { z } from "zod";
|
||||||
|
|
||||||
|
import { PostHogClient } from "@/app/posthog";
|
||||||
|
|
||||||
export const config = {
|
export const config = {
|
||||||
api: {
|
api: {
|
||||||
bodyParser: false,
|
bodyParser: false,
|
||||||
|
@ -50,7 +53,7 @@ export default async function handler(
|
||||||
}
|
}
|
||||||
|
|
||||||
switch (event.type) {
|
switch (event.type) {
|
||||||
case "checkout.session.completed":
|
case "checkout.session.completed": {
|
||||||
const checkoutSession = event.data.object as Stripe.Checkout.Session;
|
const checkoutSession = event.data.object as Stripe.Checkout.Session;
|
||||||
|
|
||||||
if (checkoutSession.subscription === null) {
|
if (checkoutSession.subscription === null) {
|
||||||
|
@ -74,10 +77,31 @@ export default async function handler(
|
||||||
subscriptionId: checkoutSession.subscription as string,
|
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;
|
break;
|
||||||
|
}
|
||||||
case "customer.subscription.deleted":
|
case "customer.subscription.deleted":
|
||||||
case "customer.subscription.updated":
|
case "customer.subscription.updated":
|
||||||
case "customer.subscription.created":
|
case "customer.subscription.created": {
|
||||||
const { id } = event.data.object as Stripe.Subscription;
|
const { id } = event.data.object as Stripe.Subscription;
|
||||||
|
|
||||||
const subscription = await stripe.subscriptions.retrieve(id);
|
const subscription = await stripe.subscriptions.retrieve(id);
|
||||||
|
@ -113,7 +137,9 @@ export default async function handler(
|
||||||
periodEnd: toDate(subscription.current_period_end),
|
periodEnd: toDate(subscription.current_period_end),
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
break;
|
break;
|
||||||
|
}
|
||||||
default:
|
default:
|
||||||
// Unexpected event type
|
// Unexpected event type
|
||||||
res.status(400).json({
|
res.status(400).json({
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue