Papermark is the open-source DocSend alternative and secure data rooms with built-in analytics and custom domains.
feat: add group analytics
Marc Seitz74847f64e43d
diff --git a/components/providers/posthog-group-sync.tsx b/components/providers/posthog-group-sync.tsxnew file mode 100644index 00000000..1435ae56--- /dev/null+++ b/components/providers/posthog-group-sync.tsx@@ -0,0 +1,41 @@+import { useEffect, useRef } from "react";++import posthog from "posthog-js";++import { useTeam } from "@/context/team-context";+import { getPostHogConfig } from "@/lib/posthog";++/**+ * Syncs the current team to PostHog as a `team` group so that events+ * captured during the session are aggregated by team in Group Analytics.+ *+ * Must be rendered inside `<TeamProvider>` (which itself lives inside+ * `<PostHogCustomProvider>`).+ */+export function PostHogGroupSync() {+ const { currentTeam } = useTeam();+ const lastGroupKeyRef = useRef<string | null>(null);++ useEffect(() => {+ if (typeof window === "undefined") return;+ if (!getPostHogConfig()) return;+ if (!currentTeam?.id) return;++ const properties: Record<string, unknown> = {};+ if (currentTeam.name) properties.name = currentTeam.name;+ if (currentTeam.plan) properties.plan = currentTeam.plan;+ if (currentTeam.createdAt) {+ properties.date_joined = new Date(currentTeam.createdAt).toISOString();+ }++ posthog.group("team", currentTeam.id, properties);+ lastGroupKeyRef.current = currentTeam.id;+ }, [+ currentTeam?.id,+ currentTeam?.name,+ currentTeam?.plan,+ currentTeam?.createdAt,+ ]);++ return null;+}diff --git a/pages/_app.tsx b/pages/_app.tsxindex 38bc43d8..626ee61d 100644--- a/pages/_app.tsx+++ b/pages/_app.tsx@@ -10,6 +10,7 @@ import { NuqsAdapter } from "nuqs/adapters/next/pages";import { EXCLUDED_PATHS } from "@/lib/constants";+import { PostHogGroupSync } from "@/components/providers/posthog-group-sync";import { PostHogCustomProvider } from "@/components/providers/posthog-provider";import { ThemeProvider } from "@/components/theme-provider";import { Toaster } from "@/components/ui/sonner";@@ -82,6 +83,7 @@ export default function App({<Component {...pageProps} />) : (<TeamProvider>+ <PostHogGroupSync /><UploadProgressProvider><Component {...pageProps} /></UploadProgressProvider>diff --git a/pages/api/teams/[teamId]/billing/manage.ts b/pages/api/teams/[teamId]/billing/manage.tsindex 5aae391e..de627a45 100644--- a/pages/api/teams/[teamId]/billing/manage.ts+++ b/pages/api/teams/[teamId]/billing/manage.ts@@ -102,7 +102,7 @@ export default async function handle(const isUnlimitedPlan = plan?.slug === "datarooms-unlimited";const stripe = stripeInstance(isOldAccount(team.plan));-+// Apply 30% discount for yearly plans before redirecting to billing portal// Same logic as retention flow: apply coupon directly to subscriptionif (applyYearlyDiscount && upgradePlan) {@@ -112,7 +112,7 @@ export default async function handle(// team.plan format is "pro", "business", "pro+old", "business+old", etc.const planString = `${plan.slug}${isOldAccount(team.plan) ? "+old" : ""}`;const couponId = getCouponFromPlan(planString, true);-+// Verify coupon exists before applying (coupons might only exist in production, not test mode)try {await stripe.coupons.retrieve(couponId);@@ -125,7 +125,7 @@ export default async function handle(if (error.code === "resource_missing") {console.warn(`[Manage] Coupon "${couponId}" not found in ${process.env.NEXT_PUBLIC_VERCEL_ENV || "test"} mode. ` +- `Skipping discount application. This is expected if the coupon only exists in production.`+ `Skipping discount application. This is expected if the coupon only exists in production.`,);} else {// Re-throw other errorsdiff --git a/pages/api/teams/[teamId]/billing/upgrade.ts b/pages/api/teams/[teamId]/billing/upgrade.tsindex 1b695745..2983d808 100644--- a/pages/api/teams/[teamId]/billing/upgrade.ts+++ b/pages/api/teams/[teamId]/billing/upgrade.ts@@ -91,7 +91,7 @@ export default async function handle(// team.plan format is "pro", "business", "pro+old", "business+old", etc.const planString = oldAccount ? `${plan.slug}+old` : plan.slug;couponId = getCouponFromPlan(planString, true);-+// Verify coupon exists in Stripe (coupons might only exist in production, not test mode)const stripe = stripeInstance(oldAccount);try {@@ -101,7 +101,7 @@ export default async function handle(if (error.code === "resource_missing") {console.warn(`[Upgrade] Coupon "${couponId}" not found in ${process.env.NEXT_PUBLIC_VERCEL_ENV || "test"} mode. ` +- `Continuing without discount. This is expected if the coupon only exists in production.`+ `Continuing without discount. This is expected if the coupon only exists in production.`,);couponId = undefined;} else {diff --git a/pages/api/teams/[teamId]/domains/[domain]/verify.ts b/pages/api/teams/[teamId]/domains/[domain]/verify.tsindex ad5f5f9e..c4107f03 100644--- a/pages/api/teams/[teamId]/domains/[domain]/verify.ts+++ b/pages/api/teams/[teamId]/domains/[domain]/verify.ts@@ -1,9 +1,9 @@import { NextApiRequest, NextApiResponse } from "next";+import { checkRateLimit, rateLimiters } from "@/ee/features/security";import { waitUntil } from "@vercel/functions";import { getServerSession } from "next-auth/next";-import { checkRateLimit, rateLimiters } from "@/ee/features/security";import { trackAnalytics } from "@/lib/analytics";import {getConfigResponse,