🐸 Rybbit - open-source and privacy-friendly alternative to Google Analytics that is 10x more intuitive.
Add event limit
Bill Yang83e9c3d8f181
diff --git a/client/src/api/admin/sites.ts b/client/src/api/admin/sites.tsindex 102ad2b72..a5765e679 100644--- a/client/src/api/admin/sites.ts+++ b/client/src/api/admin/sites.ts@@ -11,6 +11,7 @@ export type GetSitesResponse = {createdBy: string;overMonthlyLimit?: boolean;monthlyEventCount?: number;+ eventLimit?: number;}[];export function useGetSites() {diff --git a/client/src/app/[site]/Header/Header.tsx b/client/src/app/[site]/Header/Header.tsxindex d37957a2f..b7d9901f8 100644--- a/client/src/app/[site]/Header/Header.tsx+++ b/client/src/app/[site]/Header/Header.tsx@@ -40,6 +40,11 @@ export function Header() {return route === tabName.toLowerCase();};+ // Format numbers with commas+ const formatNumber = (num: number = 0) => {+ return num.toLocaleString();+ };+return (<div className="flex flex-col">{/* Usage Limit Banner */}@@ -48,8 +53,9 @@ export function Header() {<AlertTriangle className="h-4 w-4" /><AlertTitle>Monthly Event Limit Exceeded</AlertTitle><AlertDescription>- This site's organization has exceeded its monthly event limit (- {site.monthlyEventCount?.toLocaleString()} events recorded). Please+ This site's organization has exceeded its monthly event limit.+ You've used {formatNumber(site.monthlyEventCount || 0)} events out+ of your {formatNumber(site.eventLimit || 20000)} event limit. Pleaseupgrade your plan to continue collecting analytics.</AlertDescription><Linkdiff --git a/server/src/api/sites/getSites.ts b/server/src/api/sites/getSites.tsindex d35752709..c57afaceb 100644--- a/server/src/api/sites/getSites.ts+++ b/server/src/api/sites/getSites.ts@@ -1,8 +1,42 @@-import { and, eq } from "drizzle-orm";+import { and, eq, inArray } from "drizzle-orm";import { FastifyReply, FastifyRequest } from "fastify";import { db } from "../../db/postgres/postgres.js";-import { member, user } from "../../db/postgres/schema.js";+import { member, user, subscription } from "../../db/postgres/schema.js";import { getSitesUserHasAccessTo } from "../../lib/auth-utils.js";+import { STRIPE_PLANS } from "../../lib/const.js";++// Default event limit for users without an active subscription+const DEFAULT_EVENT_LIMIT = 20_000;++/**+ * Get subscription event limit for a user+ */+async function getUserEventLimit(userId: string): Promise<number> {+ try {+ // Find active subscription+ const userSubscription = await db+ .select()+ .from(subscription)+ .where(+ and(+ eq(subscription.referenceId, userId),+ inArray(subscription.status, ["active", "trialing"])+ )+ )+ .limit(1);++ if (!userSubscription.length) {+ return DEFAULT_EVENT_LIMIT;+ }++ // Find the plan in STRIPE_PLANS+ const plan = STRIPE_PLANS.find((p) => p.name === userSubscription[0].plan);+ return plan ? plan.limits.events : DEFAULT_EVENT_LIMIT;+ } catch (error) {+ console.error(`Error getting event limit for user ${userId}:`, error);+ return DEFAULT_EVENT_LIMIT;+ }+}export async function getSites(req: FastifyRequest, reply: FastifyReply) {try {@@ -14,7 +48,11 @@ export async function getSites(req: FastifyRequest, reply: FastifyReply) {sitesData.map(async (site) => {// Skip if no organization IDif (!site.organizationId) {- return { ...site, overMonthlyLimit: false };+ return {+ ...site,+ overMonthlyLimit: false,+ eventLimit: DEFAULT_EVENT_LIMIT,+ };}// Get the organization owner@@ -30,7 +68,11 @@ export async function getSites(req: FastifyRequest, reply: FastifyReply) {.limit(1);if (!orgOwner.length) {- return { ...site, overMonthlyLimit: false };+ return {+ ...site,+ overMonthlyLimit: false,+ eventLimit: DEFAULT_EVENT_LIMIT,+ };}// Get the user data to check if they're over limit@@ -44,14 +86,22 @@ export async function getSites(req: FastifyRequest, reply: FastifyReply) {.limit(1);if (!userData.length) {- return { ...site, overMonthlyLimit: false };+ return {+ ...site,+ overMonthlyLimit: false,+ eventLimit: DEFAULT_EVENT_LIMIT,+ };}+ // Get the user's event limit from their subscription+ const eventLimit = await getUserEventLimit(orgOwner[0].userId);+// Return site with usage limit inforeturn {...site,overMonthlyLimit: userData[0].overMonthlyLimit || false,monthlyEventCount: userData[0].monthlyEventCount || 0,+ eventLimit,};}));