🐸 Rybbit - open-source and privacy-friendly alternative to Google Analytics that is 10x more intuitive.
Rename Frogstats to Rybbit in analytics script and refactor tracking utilities for improved session handling. Updated imports and adjusted session query logic to enhance performance and maintainability.
Bill Yang83d644d9ac6c
diff --git a/server/public/script.js b/server/public/script.jsindex 3c976e3fd..14cba06e5 100644--- a/server/public/script.js+++ b/server/public/script.js@@ -1,4 +1,4 @@-// Frogstats Analytics Script+// Rybbit Analytics Script(function () {// Get the script tag that loaded this scriptconst scriptTag = document.currentScript;diff --git a/server/src/tracker/trackingUtils.ts b/server/src/tracker/trackingUtils.tsindex 61b8f14b9..68d08f31c 100644--- a/server/src/tracker/trackingUtils.ts+++ b/server/src/tracker/trackingUtils.ts@@ -1,13 +1,13 @@import crypto from "crypto";-import { eq } from "drizzle-orm";+import { and, eq } from "drizzle-orm";import { FastifyRequest } from "fastify";import UAParser, { UAParser as userAgentParser } from "ua-parser-js";-import { db, sql } from "../db/postgres/postgres.js";+import { sitesOverLimit } from "../cron/monthly-usage-checker.js";+import { db } from "../db/postgres/postgres.js";import { activeSessions } from "../db/postgres/schema.js";import { TrackingPayload } from "../types.js";import { getDeviceType, getIpAddress, getUserId } from "../utils.js";import { pageviewQueue } from "./pageviewQueue.js";-import { sitesOverLimit } from "../cron/monthly-usage-checker.js";// Define extended payload typesexport type BaseTrackingPayload = TrackingPayload & {@@ -25,26 +25,6 @@ export type TotalTrackingPayload = BaseTrackingPayload & {ipAddress: string;};-// Extended type for database active sessions-export type ActiveSession = {- session_id: string;- site_id: number | null;- user_id: string;- pageviews: number;- hostname: string | null;- start_time: Date | null;- last_activity: Date | null;- entry_page: string | null;- exit_page: string | null;- device_type: string | null;- screen_width: number | null;- screen_height: number | null;- browser: string | null;- operating_system: string | null;- language: string | null;- referrer: string | null;-};-// UTM and URL parameter parsing utilitiesexport function getUTMParams(querystring: string): Record<string, string> {const params: Record<string, string> = {};@@ -90,14 +70,21 @@ export function isSiteOverLimit(siteId: number | string): boolean {}// Get existing user session-export async function getExistingSession(- userId: string,- siteId: string-): Promise<ActiveSession | null> {- const [existingSession] = await sql<ActiveSession[]>`- SELECT * FROM active_sessions WHERE user_id = ${userId} AND site_id = ${siteId}- `;- return existingSession;+export async function getExistingSession(userId: string, siteId: string) {+ const siteIdNumber = parseInt(siteId, 10); // Ensure siteId is a number for the query++ const [existingSession] = await db+ .select()+ .from(activeSessions)+ .where(+ and(+ eq(activeSessions.userId, userId),+ eq(activeSessions.siteId, siteIdNumber)+ )+ )+ .limit(1);++ return existingSession || null; // Return the session or null if not found}// Create base tracking payload from request@@ -123,7 +110,7 @@ export function createBasePayload(// Update session for both pageviews and eventsexport async function updateSession(payload: TotalTrackingPayload,- existingSession: ActiveSession | null,+ existingSession: any | null,isPageview: boolean = true): Promise<void> {if (existingSession) {@@ -140,7 +127,7 @@ export async function updateSession(await db.update(activeSessions).set(updateData)- .where(eq(activeSessions.userId, payload.userId));+ .where(eq(activeSessions.userId, existingSession.userId));return;}@@ -178,12 +165,12 @@ export async function updateSession(// Process tracking event and add to queueexport async function processTrackingEvent(payload: TotalTrackingPayload,- existingSession: ActiveSession | null,+ existingSession: any | null,isPageview: boolean = true): Promise<void> {// If session exists, use its ID instead of generated oneif (existingSession) {- payload.sessionId = existingSession.session_id;+ payload.sessionId = existingSession.sessionId;}// Add to queue for processing