Skip to content

Papermark is the open-source DocSend alternative and secure data rooms with built-in analytics and custom domains.

refactor(analytics): user upgraded

Marc Seitzd0db528fd2f0
diff --git a/pages/api/stripe/webhook.ts b/pages/api/stripe/webhook.ts
index de065ef9..4429abb4 100644
--- a/pages/api/stripe/webhook.ts
+++ b/pages/api/stripe/webhook.ts
@@ -6,7 +6,7 @@ import { stripe } from "@/lib/stripe";
import { getPlanFromPriceId, isNewCustomer } from "@/lib/stripe/utils";
import { log } from "@/lib/utils";
import { sendUpgradePlanEmail } from "@/lib/emails/send-upgrade-plan";
-import { identifyUser, trackAnalytics } from "@/lib/analytics";
+import { useAnalyticsServer } from "@/lib/analytics";
// Stripe requires the raw body to construct the event.
export const config = {
@@ -43,6 +43,8 @@ interface SubscriptionInfo {
const pendingSubscriptionUpdates = new Map<string, SubscriptionInfo>();
+const analytics = useAnalyticsServer();
+
export default async function webhookHandler(
req: NextApiRequest,
res: NextApiResponse,
@@ -69,7 +71,10 @@ export default async function webhookHandler(
checkoutSession.client_reference_id === null ||
checkoutSession.customer === null
) {
- await log({message: "Missing items in Stripe webhook callback", type: "error"});
+ await log({
+ message: "Missing items in Stripe webhook callback",
+ type: "error",
+ });
return;
}
@@ -115,11 +120,15 @@ export default async function webhookHandler(
});
}
- await identifyUser(team.users[0].user.id);
- await trackAnalytics({
- event: "User Upgraded",
- email: team.users[0].user.email,
- });
+ analytics.capture(
+ team.users[0].user.email ?? team.users[0].user.id,
+ "User Upgraded",
+ {
+ teamId: team.id,
+ teamPlan: plan.slug,
+ $set: { teamId: team.id, teamPlan: plan.slug },
+ },
+ );
// Remove the pending update from the store
pendingSubscriptionUpdates.delete(stripeId);
@@ -172,17 +181,19 @@ export default async function webhookHandler(
if (!team) {
await log({
- message: "Team with stripeId: `" +
+ message:
+ "Team with stripeId: `" +
stripeId +
"`not found in Stripe webhook `customer.subscription.deleted` callback",
- type: "error"
+ type: "error",
});
return;
}
await log({
- message: ":cry: Team *`" + team.id + "`* deleted their subscription",
- type: "info"
+ message:
+ ":cry: Team *`" + team.id + "`* deleted their subscription",
+ type: "info",
});
} else {
throw new Error("Unhandled relevant event!");
@@ -193,7 +204,7 @@ export default async function webhookHandler(
event.id
}_](https://dashboard.stripe.com/events/${event.id})) \n\n Error: ${(error as Error).message}`,
type: "error",
- mention: true
+ mention: true,
});
return;
}