🐸 Rybbit - open-source and privacy-friendly alternative to Google Analytics that is 10x more intuitive.
Add site usage limit checks to session replay and event tracking
Bill Yang30ae5aaf3781
diff --git a/server/src/api/sessionReplay/recordSessionReplay.ts b/server/src/api/sessionReplay/recordSessionReplay.tsindex e23021e00..a09869108 100644--- a/server/src/api/sessionReplay/recordSessionReplay.ts+++ b/server/src/api/sessionReplay/recordSessionReplay.ts@@ -1,9 +1,10 @@import { FastifyReply, FastifyRequest } from "fastify";import { z } from "zod";import { SessionReplayIngestService } from "../../services/replay/sessionReplayIngestService.js";+import { validateApiKey, validateOrigin } from "../../services/shared/requestValidation.js";+import { usageService } from "../../services/usageService.js";import { RecordSessionReplayRequest } from "../../types/sessionReplay.js";import { getIpAddress } from "../../utils.js";-import { validateApiKey, validateOrigin, checkApiKeyRateLimit } from "../../services/shared/requestValidation.js";const recordSessionReplaySchema = z.object({userId: z.string(),@@ -34,6 +35,13 @@ export async function recordSessionReplay() {try {const siteId = Number(request.params.site);++ // Check if the site has exceeded its monthly limit+ if (usageService.isSiteOverLimit(Number(siteId))) {+ console.log(`[SessionReplay] Skipping event for site ${siteId} - over monthly limit`);+ return reply.status(200).send("Site over monthly limit, event not tracked");+ }+const body = recordSessionReplaySchema.parse(request.body) as RecordSessionReplayRequest;// First check if API key is provided and validdiff --git a/server/src/services/tracker/trackEvent.ts b/server/src/services/tracker/trackEvent.tsindex 28a5e892a..4fbb58539 100644--- a/server/src/services/tracker/trackEvent.ts+++ b/server/src/services/tracker/trackEvent.ts@@ -3,9 +3,10 @@ import { isbot } from "isbot";import { z, ZodError } from "zod";import { siteConfig } from "../../lib/siteConfig.js";import { sessionsService } from "../sessions/sessionsService.js";-import { validateApiKey, validateOrigin, checkApiKeyRateLimit } from "../shared/requestValidation.js";-import { createBasePayload, isSiteOverLimit } from "./utils.js";+import { checkApiKeyRateLimit, validateApiKey, validateOrigin } from "../shared/requestValidation.js";+import { usageService } from "../usageService.js";import { pageviewQueue } from "./pageviewQueue.js";+import { createBasePayload } from "./utils.js";// Define Zod schema for validationexport const trackingPayloadSchema = z.discriminatedUnion("type", [@@ -146,7 +147,6 @@ export const trackingPayloadSchema = z.discriminatedUnion("type", [.strict(),]);-// Unified handler for all events (pageviews and custom events)export async function trackEvent(request: FastifyRequest, reply: FastifyReply) {try {@@ -222,7 +222,7 @@ export async function trackEvent(request: FastifyRequest, reply: FastifyReply) {}// Check if the site has exceeded its monthly limit- if (isSiteOverLimit(validatedPayload.site_id)) {+ if (usageService.isSiteOverLimit(Number(validatedPayload.site_id))) {console.log(`[Tracking] Skipping event for site ${validatedPayload.site_id} - over monthly limit`);return reply.status(200).send("Site over monthly limit, event not tracked");}diff --git a/server/src/services/tracker/utils.ts b/server/src/services/tracker/utils.tsindex 4a23a1bfc..f9a3f35ee 100644--- a/server/src/services/tracker/utils.ts+++ b/server/src/services/tracker/utils.ts@@ -1,11 +1,10 @@import { FastifyRequest } from "fastify";import UAParser, { UAParser as userAgentParser } from "ua-parser-js";import { z } from "zod";-import { usageService } from "../usageService.js";+import { getIpAddress } from "../../utils.js";import { userIdService } from "../userId/userIdService.js";import { trackingPayloadSchema } from "./trackEvent.js";import { TrackingPayload } from "./types.js";-import { getIpAddress } from "../../utils.js";export type TotalTrackingPayload = TrackingPayload & {type?: string;@@ -82,11 +81,6 @@ export function clearSelfReferrer(referrer: string, hostname: string): string {return referrer;}-// Check if site is over the monthly limit-export function isSiteOverLimit(siteId: number | string): boolean {- return usageService.getSitesOverLimit().has(Number(siteId));-}-// Create base tracking payload from requestexport function createBasePayload(request: FastifyRequest,diff --git a/server/src/services/usageService.ts b/server/src/services/usageService.tsindex 87ffae494..ce37737b5 100644--- a/server/src/services/usageService.ts+++ b/server/src/services/usageService.ts@@ -45,6 +45,13 @@ class UsageService {return this.sitesOverLimit;}+ /**+ * Checks if a site is over its monthly limit+ */+ public isSiteOverLimit(siteId: number): boolean {+ return this.sitesOverLimit.has(siteId);+ }+/*** Gets the first day of the current month in YYYY-MM-DD format using Luxon*/