Skip to content

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

wip

Bill Yang6f46abd12ff3
diff --git a/docker-compose.yml b/docker-compose.yml
index e131ee4c6..3c8e9178a 100644
--- a/docker-compose.yml
+++ b/docker-compose.yml
@@ -19,6 +19,19 @@ services:
# retries: 3
# start_period: 10s
+ postgres:
+ image: postgres:latest
+ container_name: postgres
+ environment:
+ POSTGRES_USER: frog
+ POSTGRES_PASSWORD: admin
+ POSTGRES_DB: analytics
+ ports:
+ - "5432:5432"
+ volumes:
+ - postgres-data:/var/lib/postgresql/data
+
+
backend:
container_name: backend
build:
@@ -31,11 +44,18 @@ services:
- CLICKHOUSE_HOST=http://clickhouse:8123
- CLICKHOUSE_PORT=8123
- CLICKHOUSE_DB=analytics
+ - POSTGRES_HOST=postgres
+ - POSTGRES_PORT=5432
+ - POSTGRES_DB=analytics
+ - POSTGRES_USER=frog
+ - POSTGRES_PASSWORD=admin
depends_on:
- clickhouse
+ - postgres
# volumes:
# - ./server:/app
# - /app/node_modules
volumes:
clickhouse-data:
+ postgres-data:
diff --git a/server/package-lock.json b/server/package-lock.json
index a725f5ea9..e1243e289 100644
--- a/server/package-lock.json
+++ b/server/package-lock.json
@@ -12,26 +12,18 @@
"@fastify/cors": "^10.0.2",
"@fastify/one-line-logger": "^1.4.0",
"@fastify/static": "^8.0.4",
- "date-fns": "^2.30.0",
- "fastify": "^5.1.0"
+ "fastify": "^5.1.0",
+ "luxon": "^3.5.0",
+ "postgres": "^3.4.5",
+ "ua-parser-js": "^2.0.0"
},
"devDependencies": {
+ "@types/luxon": "^3.4.2",
"@types/node": "^20.10.0",
"ts-node-dev": "^2.0.0",
"typescript": "^5.7.3"
}
},
- "node_modules/@babel/runtime": {
- "version": "7.26.0",
- "resolved": "https://registry.npmjs.org/@babel/runtime/-/runtime-7.26.0.tgz",
- "integrity": "sha512-FDSOghenHTiToteC/QRlv2q3DhPZ/oOXTBoirfWNx1Cx3TMVcGWQtMMmQcSvb/JjpNeGzx8Pq/b4fKEJuWm1sw==",
- "dependencies": {
- "regenerator-runtime": "^0.14.0"
- },
- "engines": {
- "node": ">=6.9.0"
- }
- },
"node_modules/@clickhouse/client": {
"version": "0.2.7",
"resolved": "https://registry.npmjs.org/@clickhouse/client/-/client-0.2.7.tgz",
@@ -295,6 +287,12 @@
"integrity": "sha512-vxhUy4J8lyeyinH7Azl1pdd43GJhZH/tP2weN8TntQblOY+A0XbT8DJk1/oCPuOOyg/Ja757rG0CgHcWC8OfMA==",
"dev": true
},
+ "node_modules/@types/luxon": {
+ "version": "3.4.2",
+ "resolved": "https://registry.npmjs.org/@types/luxon/-/luxon-3.4.2.tgz",
+ "integrity": "sha512-TifLZlFudklWlMBfhubvgqTXRzLDI5pCbGa4P8a3wPyUQSW+1xQ5eDsreP9DWHX3tjq1ke96uYG/nwundroWcA==",
+ "dev": true
+ },
"node_modules/@types/node": {
"version": "20.10.0",
"resolved": "https://registry.npmjs.org/@types/node/-/node-20.10.0.tgz",
@@ -619,21 +617,6 @@
"node": ">= 8"
}
},
- "node_modules/date-fns": {
- "version": "2.30.0",
- "resolved": "https://registry.npmjs.org/date-fns/-/date-fns-2.30.0.tgz",
- "integrity": "sha512-fnULvOpxnC5/Vg3NCiWelDsLiUc9bRwAPs/+LfTLNvetFCtCTN+yQz15C/fs4AwX1R9K5GLtLfn8QW+dWisaAw==",
- "dependencies": {
- "@babel/runtime": "^7.21.0"
- },
- "engines": {
- "node": ">=0.11"
- },
- "funding": {
- "type": "opencollective",
- "url": "https://opencollective.com/date-fns"
- }
- },
"node_modules/dateformat": {
"version": "4.6.3",
"resolved": "https://registry.npmjs.org/dateformat/-/dateformat-4.6.3.tgz",
@@ -658,6 +641,25 @@
"node": ">=6"
}
},
+ "node_modules/detect-europe-js": {
+ "version": "0.1.2",
+ "resolved": "https://registry.npmjs.org/detect-europe-js/-/detect-europe-js-0.1.2.tgz",
+ "integrity": "sha512-lgdERlL3u0aUdHocoouzT10d9I89VVhk0qNRmll7mXdGfJT1/wqZ2ZLA4oJAjeACPY5fT1wsbq2AT+GkuInsow==",
+ "funding": [
+ {
+ "type": "github",
+ "url": "https://github.com/sponsors/faisalman"
+ },
+ {
+ "type": "opencollective",
+ "url": "https://opencollective.com/ua-parser-js"
+ },
+ {
+ "type": "paypal",
+ "url": "https://paypal.me/faisalman"
+ }
+ ]
+ },
"node_modules/diff": {
"version": "4.0.2",
"resolved": "https://registry.npmjs.org/diff/-/diff-4.0.2.tgz",
@@ -1077,6 +1079,25 @@
"node": ">=0.12.0"
}
},
+ "node_modules/is-standalone-pwa": {
+ "version": "0.1.1",
+ "resolved": "https://registry.npmjs.org/is-standalone-pwa/-/is-standalone-pwa-0.1.1.tgz",
+ "integrity": "sha512-9Cbovsa52vNQCjdXOzeQq5CnCbAcRk05aU62K20WO372NrTv0NxibLFCK6lQ4/iZEFdEA3p3t2VNOn8AJ53F5g==",
+ "funding": [
+ {
+ "type": "github",
+ "url": "https://github.com/sponsors/faisalman"
+ },
+ {
+ "type": "opencollective",
+ "url": "https://opencollective.com/ua-parser-js"
+ },
+ {
+ "type": "paypal",
+ "url": "https://paypal.me/faisalman"
+ }
+ ]
+ },
"node_modules/isexe": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/isexe/-/isexe-2.0.0.tgz",
@@ -1155,6 +1176,14 @@
"node": "20 || >=22"
}
},
+ "node_modules/luxon": {
+ "version": "3.5.0",
+ "resolved": "https://registry.npmjs.org/luxon/-/luxon-3.5.0.tgz",
+ "integrity": "sha512-rh+Zjr6DNfUYR3bPwJEnuwDdqMbxZW7LOQfUN4B54+Cl+0o5zaU9RJ6bcidfDtC1cWCZXQ+nvX8bf6bAji37QQ==",
+ "engines": {
+ "node": ">=12"
+ }
+ },
"node_modules/make-error": {
"version": "1.3.6",
"resolved": "https://registry.npmjs.org/make-error/-/make-error-1.3.6.tgz",
@@ -1381,6 +1410,18 @@
"resolved": "https://registry.npmjs.org/pino-std-serializers/-/pino-std-serializers-7.0.0.tgz",
"integrity": "sha512-e906FRY0+tV27iq4juKzSYPbUj2do2X2JX4EzSca1631EB2QJQUqGbDuERal7LCtOpxl6x3+nvo9NPZcmjkiFA=="
},
+ "node_modules/postgres": {
+ "version": "3.4.5",
+ "resolved": "https://registry.npmjs.org/postgres/-/postgres-3.4.5.tgz",
+ "integrity": "sha512-cDWgoah1Gez9rN3H4165peY9qfpEo+SA61oQv65O3cRUE1pOEoJWwddwcqKE8XZYjbblOJlYDlLV4h67HrEVDg==",
+ "engines": {
+ "node": ">=12"
+ },
+ "funding": {
+ "type": "individual",
+ "url": "https://github.com/sponsors/porsager"
+ }
+ },
"node_modules/process": {
"version": "0.11.10",
"resolved": "https://registry.npmjs.org/process/-/process-0.11.10.tgz",
@@ -1453,11 +1494,6 @@
"node": ">= 12.13.0"
}
},
- "node_modules/regenerator-runtime": {
- "version": "0.14.1",
- "resolved": "https://registry.npmjs.org/regenerator-runtime/-/regenerator-runtime-0.14.1.tgz",
- "integrity": "sha512-dYnhHh0nJoMfnkZs6GmmhFknAGRrLznOu5nc9ML+EJxGvrx6H7teuevqVqCuPcPK//3eDrrjQhehXVx9cnkGdw=="
- },
"node_modules/require-from-string": {
"version": "2.0.2",
"resolved": "https://registry.npmjs.org/require-from-string/-/require-from-string-2.0.2.tgz",
@@ -1991,6 +2027,55 @@
"node": ">=14.17"
}
},
+ "node_modules/ua-is-frozen": {
+ "version": "0.1.2",
+ "resolved": "https://registry.npmjs.org/ua-is-frozen/-/ua-is-frozen-0.1.2.tgz",
+ "integrity": "sha512-RwKDW2p3iyWn4UbaxpP2+VxwqXh0jpvdxsYpZ5j/MLLiQOfbsV5shpgQiw93+KMYQPcteeMQ289MaAFzs3G9pw==",
+ "funding": [
+ {
+ "type": "github",
+ "url": "https://github.com/sponsors/faisalman"
+ },
+ {
+ "type": "opencollective",
+ "url": "https://opencollective.com/ua-parser-js"
+ },
+ {
+ "type": "paypal",
+ "url": "https://paypal.me/faisalman"
+ }
+ ]
+ },
+ "node_modules/ua-parser-js": {
+ "version": "2.0.0",
+ "resolved": "https://registry.npmjs.org/ua-parser-js/-/ua-parser-js-2.0.0.tgz",
+ "integrity": "sha512-SASgD4RlB7+SCMmlVNqrhPw0f/2pGawWBzJ2+LwGTD0GgNnrKGzPJDiraGHJDwW9Zm5DH2lTmUpqDpbZjJY4+Q==",
+ "funding": [
+ {
+ "type": "opencollective",
+ "url": "https://opencollective.com/ua-parser-js"
+ },
+ {
+ "type": "paypal",
+ "url": "https://paypal.me/faisalman"
+ },
+ {
+ "type": "github",
+ "url": "https://github.com/sponsors/faisalman"
+ }
+ ],
+ "dependencies": {
+ "detect-europe-js": "^0.1.2",
+ "is-standalone-pwa": "^0.1.1",
+ "ua-is-frozen": "^0.1.2"
+ },
+ "bin": {
+ "ua-parser-js": "script/cli.js"
+ },
+ "engines": {
+ "node": "*"
+ }
+ },
"node_modules/undici-types": {
"version": "5.26.5",
"resolved": "https://registry.npmjs.org/undici-types/-/undici-types-5.26.5.tgz",
diff --git a/server/package.json b/server/package.json
index a31928436..56bd5e537 100644
--- a/server/package.json
+++ b/server/package.json
@@ -11,12 +11,15 @@
"dependencies": {
"@clickhouse/client": "^0.2.7",
"@fastify/cors": "^10.0.2",
- "@fastify/static": "^8.0.4",
"@fastify/one-line-logger": "^1.4.0",
+ "@fastify/static": "^8.0.4",
"fastify": "^5.1.0",
- "date-fns": "^2.30.0"
+ "luxon": "^3.5.0",
+ "postgres": "^3.4.5",
+ "ua-parser-js": "^2.0.0"
},
"devDependencies": {
+ "@types/luxon": "^3.4.2",
"@types/node": "^20.10.0",
"ts-node-dev": "^2.0.0",
"typescript": "^5.7.3"
diff --git a/server/public/analytics.js b/server/public/analytics.js
index a84943b96..a1c5fc4a7 100644
--- a/server/public/analytics.js
+++ b/server/public/analytics.js
@@ -4,29 +4,6 @@
const scriptTag = document.currentScript;
const ANALYTICS_URL = scriptTag.getAttribute("src").split("/analytics.js")[0];
- // Generate a unique session ID
- const generateSessionId = () => {
- const existingId = localStorage.getItem("frogstats_session_id");
- const lastActivity = localStorage.getItem("frogstats_last_activity");
- const now = Date.now();
-
- // Check if we have an existing session that's less than 30 minutes old
- if (
- existingId &&
- lastActivity &&
- now - parseInt(lastActivity) < 30 * 60 * 1000
- ) {
- localStorage.setItem("frogstats_last_activity", now.toString());
- return existingId;
- }
-
- // Generate new session ID if none exists or if expired
- const newId = Math.random().toString(36).substring(2) + now.toString(36);
- localStorage.setItem("frogstats_session_id", newId);
- localStorage.setItem("frogstats_last_activity", now.toString());
- return newId;
- };
-
// Track pageview
const trackPageview = () => {
const url = new URL(window.location.href);
@@ -36,7 +13,6 @@
querystring: url.search,
referrer: document.referrer,
timestamp: new Date().toISOString(),
- sessionId: generateSessionId(),
userAgent: navigator.userAgent,
screenWidth: window.innerWidth,
screenHeight: window.innerHeight,
@@ -61,7 +37,6 @@
eventData,
url: window.location.href,
timestamp: new Date().toISOString(),
- sessionId: generateSessionId(),
};
fetch(`${ANALYTICS_URL}/track/event`, {
@@ -82,7 +57,6 @@
const payload = {
url: window.location.href,
timestamp: new Date().toISOString(),
- sessionId: generateSessionId(),
duration,
};
diff --git a/server/src/actions/trackPageView.ts b/server/src/actions/trackPageView.ts
new file mode 100644
index 000000000..e27d63a58
--- /dev/null
+++ b/server/src/actions/trackPageView.ts
@@ -0,0 +1,118 @@
+import { FastifyRequest } from "fastify";
+import { TrackingPayload } from "../types";
+import { generateUserId, getDeviceType, getIpAddress } from "../utils";
+import crypto from "crypto";
+import clickhouse from "../db/clickhouse";
+import { DateTime } from "luxon";
+import { sql } from "../db/postgres";
+import UAParser, { UAParser as userAgentParser } from "ua-parser-js";
+
+const insertPageview = async (
+ pageview: TrackingPayload & {
+ userId: string;
+ timestamp: string;
+ sessionId: string;
+ ua: UAParser.IResult;
+ }
+) => {
+ try {
+ const formattedTimestamp = DateTime.fromISO(pageview.timestamp).toFormat(
+ "yyyy-MM-dd HH:mm:ss"
+ );
+ await clickhouse.insert({
+ table: "pageviews",
+ values: [
+ {
+ timestamp: formattedTimestamp,
+ session_id: pageview.sessionId,
+ user_id: pageview.userId,
+ hostname: pageview.hostname || "",
+ pathname: pageview.pathname || "",
+ querystring: pageview.querystring || "",
+ referrer: pageview.referrer || "",
+ user_agent: pageview.userAgent || "",
+ browser: pageview.ua.browser.name,
+ operating_system: pageview.ua.os.name,
+ language: pageview.language || "",
+ screen_width: pageview.screenWidth || 0,
+ screen_height: pageview.screenHeight || 0,
+ device_type: getDeviceType(
+ pageview.screenWidth,
+ pageview.screenHeight
+ ),
+ },
+ ],
+ format: "JSONEachRow",
+ });
+ return true;
+ } catch (error) {
+ console.error("Error inserting pageview:", error);
+ return false;
+ }
+};
+
+const updateSession = async (
+ pageview: TrackingPayload & {
+ userId: string;
+ timestamp: string;
+ sessionId: string;
+ ua: UAParser.IResult;
+ }
+) => {
+ const [existingSession] = await sql`
+ SELECT * FROM active_sessions WHERE user_id = ${pageview.userId}
+ `;
+ if (existingSession) {
+ await sql`
+ UPDATE active_sessions SET last_activity = ${pageview.timestamp}, pageviews = pageviews + 1 WHERE user_id = ${pageview.userId}
+ `;
+ return;
+ }
+
+ const inserts = {
+ session_id: pageview.sessionId,
+ user_id: pageview.userId,
+ hostname: pageview.hostname,
+ start_time: pageview.timestamp,
+ last_activity: pageview.timestamp,
+ pageviews: 1,
+ entry_page: pageview.pathname,
+ // exit_page: pageview.pathname,
+ device_type: getDeviceType(pageview.screenWidth, pageview.screenHeight),
+ screen_width: pageview.screenWidth,
+ screen_height: pageview.screenHeight,
+ browser: pageview.ua.browser.name,
+ operating_system: pageview.ua.os.name,
+ language: pageview.language || "",
+ referrer: pageview.referrer || "",
+ };
+
+ await sql`
+ INSERT INTO active_sessions ${sql(inserts)}
+ `;
+};
+
+export function trackPageView(
+ request: FastifyRequest<{ Body: TrackingPayload }>
+) {
+ const payload = {
+ ...request.body,
+ ip_address: getIpAddress(request),
+ timestamp: new Date().toISOString(),
+ ua: userAgentParser(request.body.userAgent),
+ };
+
+ const userId = generateUserId(payload.ip_address, payload.userAgent);
+ const timestamp = new Date().toISOString();
+ const sessionId = crypto.randomUUID();
+
+ const insertPayload = {
+ ...payload,
+ userId,
+ timestamp,
+ sessionId,
+ };
+
+ insertPageview(insertPayload);
+ updateSession(insertPayload);
+}
diff --git a/server/src/db/clickhouse.ts b/server/src/db/clickhouse.ts
index a45e191b7..567d4ee45 100644
--- a/server/src/db/clickhouse.ts
+++ b/server/src/db/clickhouse.ts
@@ -1,29 +1,29 @@
import { createClient } from "@clickhouse/client";
-import { format } from "date-fns";
-import { getBrowserDetails } from "../utils";
-const client = createClient({
+export const clickhouse = createClient({
host: process.env.CLICKHOUSE_HOST,
database: process.env.CLICKHOUSE_DB,
});
-export const initializeDatabase = async () => {
+export const initializeClickhouse = async () => {
// Create pageviews table
- await client.exec({
+ await clickhouse.exec({
query: `
CREATE TABLE IF NOT EXISTS pageviews (
timestamp DateTime,
session_id String,
+ user_id String,
hostname String,
pathname String,
querystring String,
referrer String,
user_agent String,
- ip_address String,
- browser String,
- language String,
+ browser LowCardinality(String),
+ operating_system LowCardinality(String),
+ language LowCardinality(String),
screen_width UInt16,
screen_height UInt16,
+ device_type LowCardinality(String)
)
ENGINE = MergeTree()
PARTITION BY toYYYYMM(timestamp)
@@ -32,7 +32,7 @@ export const initializeDatabase = async () => {
});
// Create events table
- await client.exec({
+ await clickhouse.exec({
query: `
CREATE TABLE IF NOT EXISTS events (
timestamp DateTime,
@@ -49,7 +49,7 @@ export const initializeDatabase = async () => {
});
// Create sessions table
- await client.exec({
+ await clickhouse.exec({
query: `
CREATE TABLE IF NOT EXISTS sessions (
hostname String,
@@ -75,43 +75,9 @@ export const initializeDatabase = async () => {
});
};
-export const insertPageview = async (pageview: Record<string, any>) => {
- try {
- const formattedTimestamp = format(
- new Date(pageview.timestamp),
- "yyyy-MM-dd HH:mm:ss"
- );
- await client.insert({
- table: "pageviews",
- values: [
- {
- timestamp: formattedTimestamp,
- session_id: pageview.sessionId,
- hostname: pageview.hostname || "",
- pathname: pageview.pathname || "",
- querystring: pageview.querystring || "",
- referrer: pageview.referrer || "",
- user_agent: pageview.userAgent || "",
- ip_address: pageview.ip_address || "",
- browser: getBrowserDetails(pageview.userAgent || ""),
- language: pageview.language || "",
- screen_width: pageview.screenWidth || 0,
- screen_height: pageview.screenHeight || 0,
- duration: pageview.duration || 0,
- },
- ],
- format: "JSONEachRow",
- });
- return true;
- } catch (error) {
- console.error("Error inserting pageview:", error);
- return false;
- }
-};
-
export const insertEvent = async (event: Record<string, any>) => {
try {
- await client.insert({
+ await clickhouse.insert({
table: "events",
values: [
{
@@ -135,7 +101,7 @@ export const insertEvent = async (event: Record<string, any>) => {
// Function to insert session data
export const insertSession = async (session: Record<string, any>) => {
try {
- await client.insert({
+ await clickhouse.insert({
table: "sessions",
values: [
{
@@ -164,4 +130,4 @@ export const insertSession = async (session: Record<string, any>) => {
}
};
-export default client;
+export default clickhouse;
diff --git a/server/src/db/postgres.ts b/server/src/db/postgres.ts
new file mode 100644
index 000000000..4d1f0644f
--- /dev/null
+++ b/server/src/db/postgres.ts
@@ -0,0 +1,36 @@
+import postgres from "postgres";
+
+export const sql = postgres({
+ host: process.env.POSTGRES_HOST || "postgres",
+ port: parseInt(process.env.POSTGRES_PORT || "5432", 10),
+ database: process.env.POSTGRES_DB,
+ username: process.env.POSTGRES_USER,
+ password: process.env.POSTGRES_PASSWORD,
+});
+
+export async function initializePostgres() {
+ try {
+ await sql`
+ CREATE TABLE IF NOT EXISTS active_sessions (
+ session_id TEXT PRIMARY KEY,
+ user_id TEXT,
+ hostname TEXT,
+ start_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
+ last_activity TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
+ pageviews INT DEFAULT 0,
+ entry_page TEXT,
+ exit_page TEXT,
+ device_type TEXT,
+ screen_width INT,
+ screen_height INT,
+ browser TEXT,
+ operating_system TEXT,
+ language TEXT,
+ referrer TEXT
+ );
+ `;
+ console.log("Tables created successfully.");
+ } catch (err) {
+ console.error("Error creating tables:", err);
+ }
+}
diff --git a/server/src/index.ts b/server/src/index.ts
index 490795c4c..9e01ce59c 100644
--- a/server/src/index.ts
+++ b/server/src/index.ts
@@ -1,12 +1,13 @@
-import Fastify, { FastifyRequest, FastifyReply } from "fastify";
import cors from "@fastify/cors";
import fastifyStatic from "@fastify/static";
+import Fastify, { FastifyReply, FastifyRequest } from "fastify";
import path from "path";
-import {
- initializeDatabase,
- insertPageview,
- insertEvent,
-} from "./db/clickhouse";
+import { trackPageView } from "./actions/trackPageView";
+import { initializeClickhouse, insertEvent } from "./db/clickhouse";
+import { TrackingPayload } from "./types";
+import { getIpAddress } from "./utils";
+import { initializePostgres } from "./db/postgres";
+import { UAParser } from "ua-parser-js";
const server = Fastify({
logger: {
@@ -29,41 +30,6 @@ server.register(fastifyStatic, {
prefix: "/", // optional: default '/'
});
-interface TrackingPayload {
- url: string;
- pathname?: string;
- querystring?: string;
- timestamp: string;
- sessionId: string;
- referrer?: string;
- userAgent?: string;
- screenWidth?: number;
- screenHeight?: number;
- language?: string;
- browser?: string;
- eventName?: string;
- eventData?: Record<string, unknown>;
- [key: string]: unknown;
-}
-
-// 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();
- }
-
- // Check for Cloudflare
- const cfConnectingIp = request.headers["cf-connecting-ip"];
- if (cfConnectingIp && typeof cfConnectingIp === "string") {
- return cfConnectingIp;
- }
-
- // Fallback to direct IP
- return request.ip;
-};
-
// Health check endpoint
server.get("/health", async () => {
return { status: "ok" };
@@ -77,18 +43,7 @@ server.post<{ Body: TrackingPayload }>(
reply: FastifyReply
) => {
try {
- console.info(request.body);
- const payload = {
- ...request.body,
- ip_address: getIpAddress(request),
- timestamp: new Date().toISOString(),
- };
-
- const success = await insertPageview(payload);
- if (!success) {
- throw new Error("Failed to insert pageview");
- }
-
+ await trackPageView(request);
return { success: true };
} catch (error) {
request.log.error(error);
@@ -100,6 +55,8 @@ server.post<{ Body: TrackingPayload }>(
}
);
+const uaParser = new UAParser();
+
// Track event endpoint
server.post<{ Body: TrackingPayload }>(
"/track/event",
@@ -133,7 +90,7 @@ server.post<{ Body: TrackingPayload }>(
const start = async () => {
try {
// Initialize the database
- await initializeDatabase();
+ await Promise.allSettled([initializeClickhouse(), initializePostgres()]);
// Start the server
await server.listen({ port: 3001, host: "0.0.0.0" });
diff --git a/server/src/types.ts b/server/src/types.ts
new file mode 100644
index 000000000..7581b6488
--- /dev/null
+++ b/server/src/types.ts
@@ -0,0 +1,11 @@
+export interface TrackingPayload {
+ hostname: string;
+ pathname: string;
+ querystring: string;
+ referrer: string;
+ timestamp: string;
+ userAgent: string;
+ screenWidth: number;
+ screenHeight: number;
+ language: string;
+}
diff --git a/server/src/utils.ts b/server/src/utils.ts
index 7d68a47db..b3e7c0010 100644
--- a/server/src/utils.ts
+++ b/server/src/utils.ts
@@ -1,17 +1,40 @@
-// Get browser details
-export const getBrowserDetails = (userAgent: string) => {
- let browser = "Unknown";
- if (userAgent.indexOf("Chrome") > -1) {
- browser = "Chrome";
- } else if (userAgent.indexOf("Safari") > -1) {
- browser = "Safari";
- } else if (userAgent.indexOf("Firefox") > -1) {
- browser = "Firefox";
- } else if (
- userAgent.indexOf("MSIE") > -1 ||
- userAgent.indexOf("Trident/") > -1
- ) {
- browser = "Internet Explorer";
+import { FastifyRequest } from "fastify";
+import crypto from "crypto";
+
+export function generateUserId(ip: string, userAgent: string) {
+ return crypto
+ .createHash("sha256")
+ .update(ip + userAgent)
+ .digest("hex");
+}
+
+export function getDeviceType(
+ screenWidth: number,
+ screenHeight: number
+): string {
+ if (screenWidth < 768) {
+ return "Mobile";
+ } else if (screenWidth >= 768 && screenWidth < 1024) {
+ return "Tablet";
+ } else {
+ return "Desktop";
}
- return browser;
+}
+
+// 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;
};
diff --git a/test/about.html b/test/about.html
new file mode 100644
index 000000000..20a3b9eb3
--- /dev/null
+++ b/test/about.html
@@ -0,0 +1,28 @@
+<!DOCTYPE html>
+<html lang="en">
+<head>
+ <meta charset="UTF-8">
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
+ <title>Test Site - About</title>
+ <link rel="stylesheet" href="styles.css">
+</head>
+<body>
+ <header>
+ <h1>About Us</h1>
+ <nav>
+ <ul>
+ <li><a href="index.html">Home</a></li>
+ <li><a href="about.html">About</a></li>
+ <li><a href="contact.html">Contact</a></li>
+ </ul>
+ </nav>
+ </header>
+ <main>
+ <p>This is the about page of the test site. Here you can learn more about us.</p>
+ </main>
+ <footer>
+ <p>&copy; 2025 Test Site</p>
+ </footer>
+ <script src="http://localhost:3001/analytics.js"></script>
+</body>
+</html>
\ No newline at end of file
diff --git a/test/contact.html b/test/contact.html
new file mode 100644
index 000000000..d40caee85
--- /dev/null
+++ b/test/contact.html
@@ -0,0 +1,28 @@
+<!DOCTYPE html>
+<html lang="en">
+<head>
+ <meta charset="UTF-8">
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
+ <title>Test Site - Contact</title>
+ <link rel="stylesheet" href="styles.css">
+</head>
+<body>
+ <header>
+ <h1>Contact Us</h1>
+ <nav>
+ <ul>
+ <li><a href="index.html">Home</a></li>
+ <li><a href="about.html">About</a></li>
+ <li><a href="contact.html">Contact</a></li>
+ </ul>
+ </nav>
+ </header>
+ <main>
+ <p>This is the contact page of the test site. Feel free to reach out to us.</p>
+ </main>
+ <footer>
+ <p>&copy; 2025 Test Site</p>
+ </footer>
+ <script src="http://localhost:3001/analytics.js"></script>
+</body>
+</html>
\ No newline at end of file
diff --git a/test/index.html b/test/index.html
new file mode 100644
index 000000000..e088fd27d
--- /dev/null
+++ b/test/index.html
@@ -0,0 +1,28 @@
+<!DOCTYPE html>
+<html lang="en">
+<head>
+ <meta charset="UTF-8">
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
+ <title>Test Site - Home</title>
+ <link rel="stylesheet" href="styles.css">
+</head>
+<body>
+ <header>
+ <h1>Welcome to the Test Site</h1>
+ <nav>
+ <ul>
+ <li><a href="index.html">Home</a></li>
+ <li><a href="about.html">About</a></li>
+ <li><a href="contact.html">Contact</a></li>
+ </ul>
+ </nav>
+ </header>
+ <main>
+ <p>This is the home page of the test site. Use the navigation links to explore other pages.</p>
+ </main>
+ <footer>
+ <p>&copy; 2025 Test Site</p>
+ </footer>
+ <script src="http://localhost:3001/analytics.js"></script>
+</body>
+</html>
\ No newline at end of file
diff --git a/test/styles.css b/test/styles.css
new file mode 100644
index 000000000..ffd7a8197
--- /dev/null
+++ b/test/styles.css
@@ -0,0 +1,42 @@
+body {
+ font-family: Arial, sans-serif;
+ margin: 0;
+ padding: 0;
+ line-height: 1.6;
+}
+
+header {
+ background: #333;
+ color: #fff;
+ padding: 10px 0;
+ text-align: center;
+}
+
+nav ul {
+ list-style: none;
+ padding: 0;
+}
+
+nav ul li {
+ display: inline;
+ margin-right: 10px;
+}
+
+nav ul li a {
+ color: #fff;
+ text-decoration: none;
+}
+
+main {
+ padding: 20px;
+}
+
+footer {
+ background: #333;
+ color: #fff;
+ text-align: center;
+ padding: 10px 0;
+ position: fixed;
+ bottom: 0;
+ width: 100%;
+}
\ No newline at end of file