Skip to content

🐸 Rybbit - open-source and privacy-friendly alternative to Google Analytics that is 10x more intuitive.

Refactor utils and tracking modules to streamline session management and device type detection

Bill Yang1c9b88a3339d
diff --git a/server/src/tracker/trackEvent.ts b/server/src/tracker/trackEvent.ts
index b531d48cb..227f1337e 100644
--- a/server/src/tracker/trackEvent.ts
+++ b/server/src/tracker/trackEvent.ts
@@ -1,10 +1,16 @@
import { FastifyReply, FastifyRequest } from "fastify";
import { z, ZodError } from "zod";
import {
+ clearSelfReferrer,
createBasePayload,
getExistingSession,
- processTrackingEvent,
+ TotalTrackingPayload,
} from "./trackingUtils.js";
+import { db } from "../db/postgres/postgres.js";
+import { activeSessions } from "../db/postgres/schema.js";
+import { eq } from "drizzle-orm";
+import { getDeviceType } from "../utils.js";
+import { pageviewQueue } from "./pageviewQueue.js";
// Define Zod schema for validation
export const trackingPayloadSchema = z.discriminatedUnion("type", [
@@ -52,6 +58,79 @@ export const trackingPayloadSchema = z.discriminatedUnion("type", [
}),
]);
+// Update session for both pageviews and events
+export async function updateSession(
+ payload: TotalTrackingPayload,
+ existingSession: any | null,
+ isPageview: boolean = true
+): Promise<void> {
+ if (existingSession) {
+ // Update session with Drizzle
+ const updateData: any = {
+ lastActivity: new Date(payload.timestamp),
+ };
+
+ // Only increment pageviews count for actual pageviews
+ if (isPageview) {
+ updateData.pageviews = (existingSession.pageviews || 0) + 1;
+ }
+
+ await db
+ .update(activeSessions)
+ .set(updateData)
+ .where(eq(activeSessions.userId, existingSession.userId));
+ return;
+ }
+
+ // Insert new session with Drizzle
+ const insertData = {
+ sessionId: payload.sessionId,
+ siteId:
+ typeof payload.site_id === "string"
+ ? parseInt(payload.site_id, 10)
+ : payload.site_id,
+ userId: payload.userId,
+ hostname: payload.hostname || null,
+ startTime: new Date(payload.timestamp || Date.now()),
+ lastActivity: new Date(payload.timestamp || Date.now()),
+ pageviews: isPageview ? 1 : 0,
+ entryPage: payload.pathname || null,
+ deviceType: getDeviceType(
+ payload.screenWidth,
+ payload.screenHeight,
+ payload.ua
+ ),
+ screenWidth: payload.screenWidth || null,
+ screenHeight: payload.screenHeight || null,
+ browser: payload.ua.browser.name || null,
+ operatingSystem: payload.ua.os.name || null,
+ language: payload.language || null,
+ referrer: payload.hostname
+ ? clearSelfReferrer(payload.referrer || "", payload.hostname)
+ : payload.referrer || null,
+ };
+
+ await db.insert(activeSessions).values(insertData);
+}
+
+// Process tracking event and add to queue
+export async function processTrackingEvent(
+ payload: TotalTrackingPayload,
+ existingSession: any | null,
+ isPageview: boolean = true
+): Promise<void> {
+ // If session exists, use its ID instead of generated one
+ if (existingSession) {
+ payload.sessionId = existingSession.sessionId;
+ }
+
+ // Add to queue for processing
+ await pageviewQueue.add(payload);
+
+ // Update session data
+ await updateSession(payload, existingSession, isPageview);
+}
+
// Unified handler for all events (pageviews and custom events)
export async function trackEvent(request: FastifyRequest, reply: FastifyReply) {
try {
diff --git a/server/src/tracker/trackingUtils.ts b/server/src/tracker/trackingUtils.ts
index 31d9db8ac..ead6d1817 100644
--- a/server/src/tracker/trackingUtils.ts
+++ b/server/src/tracker/trackingUtils.ts
@@ -7,8 +7,6 @@ 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 { trackingPayloadSchema } from "./trackEvent.js";
// Define extended payload types
@@ -121,75 +119,27 @@ export function createBasePayload(
};
}
-// Update session for both pageviews and events
-export async function updateSession(
- payload: TotalTrackingPayload,
- existingSession: any | null,
- isPageview: boolean = true
-): Promise<void> {
- if (existingSession) {
- // Update session with Drizzle
- const updateData: any = {
- lastActivity: new Date(payload.timestamp),
- };
-
- // Only increment pageviews count for actual pageviews
- if (isPageview) {
- updateData.pageviews = (existingSession.pageviews || 0) + 1;
- }
-
- await db
- .update(activeSessions)
- .set(updateData)
- .where(eq(activeSessions.userId, existingSession.userId));
- return;
- }
-
- // Insert new session with Drizzle
- const insertData = {
- sessionId: payload.sessionId,
- siteId:
- typeof payload.site_id === "string"
- ? parseInt(payload.site_id, 10)
- : payload.site_id,
- userId: payload.userId,
- hostname: payload.hostname || null,
- startTime: new Date(payload.timestamp || Date.now()),
- lastActivity: new Date(payload.timestamp || Date.now()),
- pageviews: isPageview ? 1 : 0,
- entryPage: payload.pathname || null,
- deviceType: getDeviceType(
- payload.screenWidth,
- payload.screenHeight,
- payload.ua
- ),
- screenWidth: payload.screenWidth || null,
- screenHeight: payload.screenHeight || null,
- browser: payload.ua.browser.name || null,
- operatingSystem: payload.ua.os.name || null,
- language: payload.language || null,
- referrer: payload.hostname
- ? clearSelfReferrer(payload.referrer || "", payload.hostname)
- : payload.referrer || null,
- };
-
- await db.insert(activeSessions).values(insertData);
+function getUserId(ip: string, userAgent: string) {
+ return crypto
+ .createHash("sha256")
+ .update(ip + userAgent)
+ .digest("hex");
}
-// Process tracking event and add to queue
-export async function processTrackingEvent(
- payload: TotalTrackingPayload,
- existingSession: any | null,
- isPageview: boolean = true
-): Promise<void> {
- // If session exists, use its ID instead of generated one
- if (existingSession) {
- payload.sessionId = existingSession.sessionId;
+// Helper function to get IP address
+const getIpAddress = (request: FastifyRequest): string => {
+ // Check for proxied IP addresses
+ const forwardedFor = request.headers["x-forwarded-for"];
+ if (forwardedFor && typeof forwardedFor === "string") {
+ return forwardedFor.split(",")[0].trim();
}
- // Add to queue for processing
- await pageviewQueue.add(payload);
+ // Check for Cloudflare
+ const cfConnectingIp = request.headers["cf-connecting-ip"];
+ if (cfConnectingIp && typeof cfConnectingIp === "string") {
+ return cfConnectingIp;
+ }
- // Update session data
- await updateSession(payload, existingSession, isPageview);
-}
+ // Fallback to direct IP
+ return request.ip;
+};
diff --git a/server/src/utils.ts b/server/src/utils.ts
index 8184561d7..ee801f128 100644
--- a/server/src/utils.ts
+++ b/server/src/utils.ts
@@ -1,111 +1,5 @@
-import { FastifyRequest } from "fastify";
-import crypto from "crypto";
-import { db } from "./db/postgres/postgres.js";
-import { sites } from "./db/postgres/schema.js";
-import { eq } from "drizzle-orm";
import { publicSites } from "./lib/publicSites.js";
-export function getUserId(ip: string, userAgent: string) {
- return crypto
- .createHash("sha256")
- .update(ip + userAgent)
- .digest("hex");
-}
-
-export const desktopOS = new Set([
- "Windows",
- "macOS",
- "Linux",
- "Ubuntu",
- "Fedora",
- "Debian",
- "Mint",
- "Arch",
- "CentOS",
- "elementary OS",
- "Gentoo",
- "Kubuntu",
- "Manjaro",
- "RedHat",
- "SUSE",
- "Slackware",
- "Deepin",
- "FreeBSD",
- "OpenBSD",
- "NetBSD",
- "GhostBSD",
- "PC-BSD",
- "Solaris",
- "AIX",
- "HP-UX",
- "OS/2",
- "BeOS",
- "Haiku",
- "Amiga OS",
- "Morph OS",
- "SerenityOS",
-]);
-
-export const mobileOS = new Set([
- "iOS",
- "Android",
- "Windows Phone",
- "Windows Mobile",
- "BlackBerry",
- "Symbian",
- "Firefox OS",
- "Ubuntu Touch",
- "Sailfish",
- "Tizen",
- "KaiOS",
- "HarmonyOS",
- "OpenHarmony",
- "watchOS",
- "Android-x86",
- "RIM Tablet OS",
- "Bada",
- "WebOS",
- "Maemo",
- "MeeGo",
-]);
-
-export const tvOS = new Set([
- "Chromecast",
- "Chromecast Android",
- "Chromecast Fuchsia",
- "Chromecast Linux",
- "Chromecast SmartSpeaker",
- "NetTV",
- "NetRange",
-]);
-
-export const gamingOS = new Set(["PlayStation", "Xbox", "Nintendo"]);
-
-export const otherOS = new Set([
- "Fuchsia",
- "GNU",
- "Hurd",
- "Plan9",
- "Contiki",
- "Pico",
- "Minix",
- "Unix",
- "OpenVMS",
- "RISC OS",
- "QNX",
- "Series40",
- "PCLinuxOS",
- "Linpus",
- "Linspire",
- "Mageia",
- "Mandriva",
- "Raspbian",
- "Sabayon",
- "VectorLinux",
- "Zenwalk",
- "DragonFly",
-]);
-
export function getDeviceType(
screenWidth: number,
screenHeight: number,
@@ -151,24 +45,6 @@ export function getDeviceType(
return "Mobile";
}
-// Helper function to get IP address
-export const getIpAddress = (request: FastifyRequest): string => {
- // Check for proxied IP addresses
- const forwardedFor = request.headers["x-forwarded-for"];
- if (forwardedFor && typeof forwardedFor === "string") {
- return forwardedFor.split(",")[0].trim();
- }
-
- // Check for Cloudflare
- const cfConnectingIp = request.headers["cf-connecting-ip"];
- if (cfConnectingIp && typeof cfConnectingIp === "string") {
- return cfConnectingIp;
- }
-
- // Fallback to direct IP
- return request.ip;
-};
-
// Check if a site is public
export const isSitePublic = async (siteId: string | number) => {
try {