Skip to content

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

Track ip (#602)

Bill Yang0829c4730344
diff --git a/client/src/api/admin/sites.ts b/client/src/api/admin/sites.ts
index 0577ddd49..1b47746be 100644
--- a/client/src/api/admin/sites.ts
+++ b/client/src/api/admin/sites.ts
@@ -25,6 +25,7 @@ export type SiteResponse = {
trackUrlParams?: boolean;
trackInitialPageView?: boolean;
trackSpaNavigation?: boolean;
+ trackIp?: boolean;
};
export type GetSitesFromOrgResponse = {
diff --git a/client/src/api/analytics/userInfo.ts b/client/src/api/analytics/userInfo.ts
index 319a4ee34..d7660affd 100644
--- a/client/src/api/analytics/userInfo.ts
+++ b/client/src/api/analytics/userInfo.ts
@@ -19,6 +19,7 @@ export type UserInfo = {
first_seen: string;
pageviews: number;
events: number;
+ ip?: string;
};
export function useUserInfo(siteId: number, userId: string) {
diff --git a/client/src/api/analytics/userSessions.ts b/client/src/api/analytics/userSessions.ts
index 6145bd6bb..2e7d6cad1 100644
--- a/client/src/api/analytics/userSessions.ts
+++ b/client/src/api/analytics/userSessions.ts
@@ -34,6 +34,7 @@ export type GetSessionsResponse = {
events: number;
errors: number;
outbound: number;
+ ip: string;
}[];
export function useGetSessionsInfinite(userId?: string) {
@@ -103,6 +104,7 @@ export interface SessionDetails {
pageviews: number;
entry_page: string;
exit_page: string;
+ ip: string;
}
export interface SessionEventProps {
diff --git a/client/src/app/[site]/user/[userId]/page.tsx b/client/src/app/[site]/user/[userId]/page.tsx
index 16a1777ba..5ae085971 100644
--- a/client/src/app/[site]/user/[userId]/page.tsx
+++ b/client/src/app/[site]/user/[userId]/page.tsx
@@ -29,6 +29,7 @@ import { OperatingSystem } from "../../components/shared/icons/OperatingSystem";
import { VisitCalendar } from "./components/Calendar";
import { Avatar } from "../../../../components/Avatar";
import { MobileSidebar } from "../../components/Sidebar/MobileSidebar";
+import { Badge } from "../../../../components/ui/badge";
export default function UserPage() {
useSetPageTitle("Rybbit · User");
@@ -54,12 +55,20 @@ export default function UserPage() {
Back to Users
</Button>
<div className="mb-4">
- <h1 className="text-2xl font-bold mb-4 flex items-center gap-2">
- <div>
- <MobileSidebar />
+ <h1 className="mb-4 flex items-center gap-2 justify-between">
+ <div className="text-2xl font-bold flex items-center gap-2">
+ <div>
+ <MobileSidebar />
+ </div>
+
+ <Avatar size={40} name={userId as string} />
+ {userId?.slice(0, 10)}
</div>
- <Avatar size={40} name={userId as string} />
- {userId?.slice(0, 10)}
+ {data?.ip && (
+ <Badge variant="outline" className="flex gap-1 text-neutral-300">
+ IP: <span className="text-neutral-100">{data?.ip}</span>
+ </Badge>
+ )}
</h1>
<div className="bg-neutral-900 p-3 rounded-lg flex flex-col gap-1 border border-neutral-750 text-sm mb-3">
{isLoading ? (
@@ -124,7 +133,7 @@ export default function UserPage() {
{data?.operating_system}
{data?.operating_system_version && <span className="ml-1">v{data?.operating_system_version}</span>}
</div>
- <span className="text-neutral-100">Screen Size:</span>
+ <span className="text-neutral-100">Screen:</span>
<div className="flex gap-1 text-neutral-300">
{data?.screen_width} x {data?.screen_height}
</div>
diff --git a/client/src/components/Sessions/SessionDetails.tsx b/client/src/components/Sessions/SessionDetails.tsx
index fa2f84edc..17cb32bc2 100644
--- a/client/src/components/Sessions/SessionDetails.tsx
+++ b/client/src/components/Sessions/SessionDetails.tsx
@@ -512,6 +512,12 @@ export function SessionDetails({ session, userId }: SessionDetailsProps) {
</span>
</div>
) : null}
+ {sessionDetails?.ip && (
+ <div className="flex items-center gap-2 text-sm">
+ <span className="font-medium text-neutral-300 min-w-[80px]">IP:</span>
+ <span className="text-neutral-400">{sessionDetails.ip}</span>
+ </div>
+ )}
</div>
</div>
diff --git a/client/src/components/SiteSettings/SiteConfiguration.tsx b/client/src/components/SiteSettings/SiteConfiguration.tsx
index f90b8f713..c4e6406e7 100644
--- a/client/src/components/SiteSettings/SiteConfiguration.tsx
+++ b/client/src/components/SiteSettings/SiteConfiguration.tsx
@@ -66,6 +66,7 @@ export function SiteConfiguration({ siteMetadata, disabled = false, onClose }: S
trackUrlParams: siteMetadata.trackUrlParams ?? true,
trackInitialPageView: siteMetadata.trackInitialPageView ?? true,
trackSpaNavigation: siteMetadata.trackSpaNavigation ?? true,
+ trackIp: siteMetadata.trackIp ?? false,
});
const [loadingStates, setLoadingStates] = useState<Record<string, boolean>>({});
@@ -165,6 +166,15 @@ export function SiteConfiguration({ siteMetadata, disabled = false, onClose }: S
enabledMessage: "Bot blocking enabled",
disabledMessage: "Bot blocking disabled",
},
+ {
+ id: "trackIp",
+ label: "Track IP Address",
+ description: "Track the IP address of the user. This is definitely not GDPR compliant!",
+ value: toggleStates.trackIp,
+ key: "trackIp",
+ enabledMessage: "IP address tracking enabled",
+ disabledMessage: "IP address tracking disabled",
+ },
];
const { data: subscription } = useStripeSubscription();
diff --git a/docs-v2/src/app/layout.tsx b/docs-v2/src/app/layout.tsx
index 33f87b5cf..d9da5307b 100644
--- a/docs-v2/src/app/layout.tsx
+++ b/docs-v2/src/app/layout.tsx
@@ -102,10 +102,7 @@ j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
"data-api-key": process.env.NEXT_PUBLIC_RYBBIT_API_KEY,
})}
/>
- <Script
- src="https://scripts.simpleanalyticscdn.com/latest.js"
- strategy="afterInteractive"
- />
+ <Script src="https://scripts.simpleanalyticscdn.com/latest.js" strategy="afterInteractive" />
<body className={`flex flex-col min-h-screen ${inter.variable} font-sans`}>
<noscript>
<iframe
diff --git a/server/src/api/analytics/getSession.ts b/server/src/api/analytics/getSession.ts
index b2f87e27f..3b4d4a0ba 100644
--- a/server/src/api/analytics/getSession.ts
+++ b/server/src/api/analytics/getSession.ts
@@ -26,6 +26,7 @@ export interface SessionDetails {
events: number;
entry_page: string;
exit_page: string;
+ ip: string;
}
export interface Event {
@@ -105,7 +106,8 @@ SELECT
countIf(type = 'pageview') as pageviews,
count() as events,
argMinIf(pathname, timestamp, type = 'pageview') as entry_page,
- argMaxIf(pathname, timestamp, type = 'pageview') as exit_page
+ argMaxIf(pathname, timestamp, type = 'pageview') as exit_page,
+ any(ip) AS ip
FROM events
WHERE
site_id = {siteId:Int32}
diff --git a/server/src/api/analytics/getSessions.ts b/server/src/api/analytics/getSessions.ts
index cdbc195cd..2cfb73a10 100644
--- a/server/src/api/analytics/getSessions.ts
+++ b/server/src/api/analytics/getSessions.ts
@@ -34,6 +34,7 @@ export type GetSessionsResponse = {
events: number;
errors: number;
outbound: number;
+ ip: string;
}[];
export interface GetSessionsRequest {
@@ -89,7 +90,8 @@ export async function getSessions(req: FastifyRequest<GetSessionsRequest>, res:
countIf(type = 'pageview') AS pageviews,
countIf(type = 'custom_event') AS events,
countIf(type = 'error') AS errors,
- countIf(type = 'outbound') AS outbound
+ countIf(type = 'outbound') AS outbound,
+ argMax(ip, timestamp) AS ip
FROM events
WHERE
site_id = {siteId:Int32}
diff --git a/server/src/api/analytics/getUserInfo.ts b/server/src/api/analytics/getUserInfo.ts
index d2447ea87..7d76f1e52 100644
--- a/server/src/api/analytics/getUserInfo.ts
+++ b/server/src/api/analytics/getUserInfo.ts
@@ -20,6 +20,7 @@ interface UserPageviewData {
first_seen: string;
pageviews: number;
events: number;
+ ip: string;
}
export async function getUserInfo(
@@ -58,7 +59,8 @@ export async function getUserInfo(
argMinIf(pathname, timestamp, type = 'pageview') AS entry_page,
argMaxIf(pathname, timestamp, type = 'pageview') AS exit_page,
countIf(type = 'pageview') AS pageviews,
- countIf(type = 'custom_event') AS events
+ countIf(type = 'custom_event') AS events,
+ argMax(ip, timestamp) AS ip
FROM
events
WHERE
@@ -87,7 +89,8 @@ export async function getUserInfo(
MAX(session_end) AS last_seen,
MIN(session_start) AS first_seen,
SUM(pageviews) AS pageviews,
- SUM(events) AS events
+ SUM(events) AS events,
+ any(ip) AS ip
FROM
sessions
`,
diff --git a/server/src/api/sites/getSite.ts b/server/src/api/sites/getSite.ts
index 615696d44..90bdf7c88 100644
--- a/server/src/api/sites/getSite.ts
+++ b/server/src/api/sites/getSite.ts
@@ -45,6 +45,7 @@ export async function getSite(request: FastifyRequest<GetSiteParams>, reply: Fas
saltUserIds: site.saltUserIds,
public: site.public,
blockBots: site.blockBots,
+ trackIp: site.trackIp,
isOwner: isOwner,
// Analytics features
sessionReplay: site.sessionReplay,
diff --git a/server/src/api/sites/updateSiteConfig.ts b/server/src/api/sites/updateSiteConfig.ts
index ea56479fa..1c55fa051 100644
--- a/server/src/api/sites/updateSiteConfig.ts
+++ b/server/src/api/sites/updateSiteConfig.ts
@@ -10,13 +10,15 @@ import { validateIPPattern } from "../../lib/ipUtils.js";
// Schema for the update request - all fields are optional but validated when present
const updateSiteConfigSchema = z.object({
siteId: z.union([z.string(), z.number()]).transform(val => {
- const num = typeof val === 'number' ? val : parseInt(val as string, 10);
+ const num = typeof val === "number" ? val : parseInt(val as string, 10);
if (isNaN(num) || num <= 0) {
- throw new z.ZodError([{
- code: z.ZodIssueCode.custom,
- message: "Invalid site ID: must be a positive integer",
- path: ["siteId"]
- }]);
+ throw new z.ZodError([
+ {
+ code: z.ZodIssueCode.custom,
+ message: "Invalid site ID: must be a positive integer",
+ path: ["siteId"],
+ },
+ ]);
}
return num;
}),
@@ -24,9 +26,12 @@ const updateSiteConfigSchema = z.object({
public: z.boolean().optional(),
saltUserIds: z.boolean().optional(),
blockBots: z.boolean().optional(),
- domain: z.string()
- .regex(/^(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,}$/,
- "Invalid domain format. Must be a valid domain like example.com or sub.example.com")
+ domain: z
+ .string()
+ .regex(
+ /^(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,}$/,
+ "Invalid domain format. Must be a valid domain like example.com or sub.example.com"
+ )
.optional(),
excludedIPs: z.array(z.string().trim().min(1)).max(100).optional(),
@@ -38,6 +43,7 @@ const updateSiteConfigSchema = z.object({
trackUrlParams: z.boolean().optional(),
trackInitialPageView: z.boolean().optional(),
trackSpaNavigation: z.boolean().optional(),
+ trackIp: z.boolean().optional(),
});
type UpdateSiteConfigRequest = z.infer<typeof updateSiteConfigSchema>;
@@ -98,9 +104,19 @@ export async function updateSiteConfig(
// Map the fields that exist in both request and database
const directMappings = [
- 'public', 'saltUserIds', 'blockBots', 'domain', 'excludedIPs',
- 'sessionReplay', 'webVitals', 'trackErrors', 'trackOutbound',
- 'trackUrlParams', 'trackInitialPageView', 'trackSpaNavigation'
+ "public",
+ "saltUserIds",
+ "blockBots",
+ "domain",
+ "excludedIPs",
+ "sessionReplay",
+ "webVitals",
+ "trackErrors",
+ "trackOutbound",
+ "trackUrlParams",
+ "trackInitialPageView",
+ "trackSpaNavigation",
+ "trackIp",
];
for (const field of directMappings) {
@@ -121,10 +137,7 @@ export async function updateSiteConfig(
dbUpdateData.updatedAt = new Date().toISOString();
// Update the database
- await db
- .update(sites)
- .set(dbUpdateData)
- .where(eq(sites.siteId, siteId));
+ await db.update(sites).set(dbUpdateData).where(eq(sites.siteId, siteId));
// Update the site config cache
await siteConfig.updateConfig(siteId, updateData);
@@ -141,16 +154,16 @@ export async function updateSiteConfig(
console.error("Error updating site configuration:", error);
// Check for unique constraint violation on domain
- if (String(error).includes('duplicate key value violates unique constraint')) {
+ if (String(error).includes("duplicate key value violates unique constraint")) {
return reply.status(409).send({
success: false,
- error: "Domain already in use"
+ error: "Domain already in use",
});
}
return reply.status(500).send({
success: false,
- error: "Failed to update site configuration"
+ error: "Failed to update site configuration",
});
}
-}
\ No newline at end of file
+}
diff --git a/server/src/db/clickhouse/clickhouse.ts b/server/src/db/clickhouse/clickhouse.ts
index 41390a3c1..f57d0a01b 100644
--- a/server/src/db/clickhouse/clickhouse.ts
+++ b/server/src/db/clickhouse/clickhouse.ts
@@ -46,7 +46,7 @@ export const initializeClickhouse = async () => {
`,
});
- // Add performance metric columns if they don't exist
+ // Add columns to the events table
await clickhouse.exec({
query: `
ALTER TABLE events
@@ -54,7 +54,8 @@ export const initializeClickhouse = async () => {
ADD COLUMN IF NOT EXISTS cls Nullable(Float64),
ADD COLUMN IF NOT EXISTS inp Nullable(Float64),
ADD COLUMN IF NOT EXISTS fcp Nullable(Float64),
- ADD COLUMN IF NOT EXISTS ttfb Nullable(Float64)
+ ADD COLUMN IF NOT EXISTS ttfb Nullable(Float64),
+ ADD COLUMN IF NOT EXISTS ip Nullable(String)
`,
});
diff --git a/server/src/db/postgres/schema.ts b/server/src/db/postgres/schema.ts
index 7a942cb30..32bc6fcac 100644
--- a/server/src/db/postgres/schema.ts
+++ b/server/src/db/postgres/schema.ts
@@ -73,6 +73,7 @@ export const sites = pgTable(
trackUrlParams: boolean().default(true),
trackInitialPageView: boolean().default(true),
trackSpaNavigation: boolean().default(true),
+ trackIp: boolean().default(false),
apiKey: text("api_key"), // Format: rb_{32_hex_chars} = 35 chars total
},
table => [
diff --git a/server/src/lib/siteConfig.ts b/server/src/lib/siteConfig.ts
index d5e6fb13e..c05f4c004 100644
--- a/server/src/lib/siteConfig.ts
+++ b/server/src/lib/siteConfig.ts
@@ -1,11 +1,11 @@
-import { eq, or } from "drizzle-orm";
+import { eq } from "drizzle-orm";
import { db } from "../db/postgres/postgres.js";
import { sites } from "../db/postgres/schema.js";
-import { logger } from "./logger/logger.js";
import { matchesCIDR, matchesRange } from "./ipUtils.js";
+import { logger } from "./logger/logger.js";
// Site configuration interface
-interface SiteConfigData {
+export interface SiteConfigData {
id: string | null;
siteId: number;
public: boolean;
@@ -21,6 +21,7 @@ interface SiteConfigData {
trackUrlParams: boolean;
trackInitialPageView: boolean;
trackSpaNavigation: boolean;
+ trackIp: boolean;
}
class SiteConfig {
@@ -65,6 +66,7 @@ class SiteConfig {
trackUrlParams: sites.trackUrlParams,
trackInitialPageView: sites.trackInitialPageView,
trackSpaNavigation: sites.trackSpaNavigation,
+ trackIp: sites.trackIp,
})
.from(sites)
.where(isNumeric ? eq(sites.siteId, Number(siteIdOrId)) : eq(sites.id, String(siteIdOrId)))
@@ -90,6 +92,7 @@ class SiteConfig {
trackUrlParams: site.trackUrlParams || true,
trackInitialPageView: site.trackInitialPageView || true,
trackSpaNavigation: site.trackSpaNavigation || true,
+ trackIp: site.trackIp || false,
};
this.cache.set(cacheKey, {
diff --git a/server/src/services/shared/requestValidation.test.ts b/server/src/services/shared/requestValidation.test.ts
index d9830b0e4..015bd86fe 100644
--- a/server/src/services/shared/requestValidation.test.ts
+++ b/server/src/services/shared/requestValidation.test.ts
@@ -17,6 +17,7 @@ interface SiteConfigData {
trackUrlParams: boolean;
trackInitialPageView: boolean;
trackSpaNavigation: boolean;
+ trackIp: boolean;
}
// Mock dependencies
@@ -83,6 +84,7 @@ describe("validateApiKey", () => {
trackUrlParams: true,
trackInitialPageView: true,
trackSpaNavigation: true,
+ trackIp: false,
};
vi.mocked(siteConfig.getConfig).mockResolvedValue(mockSite);
@@ -115,6 +117,7 @@ describe("validateApiKey", () => {
trackUrlParams: true,
trackInitialPageView: true,
trackSpaNavigation: true,
+ trackIp: false,
};
vi.mocked(siteConfig.getConfig).mockResolvedValue(mockSite);
@@ -141,6 +144,7 @@ describe("validateApiKey", () => {
trackUrlParams: true,
trackInitialPageView: true,
trackSpaNavigation: true,
+ trackIp: false,
};
vi.mocked(siteConfig.getConfig).mockResolvedValue(mockSite);
@@ -183,6 +187,7 @@ describe("validateApiKey", () => {
trackUrlParams: true,
trackInitialPageView: true,
trackSpaNavigation: true,
+ trackIp: false,
}; // No apiKey property
vi.mocked(siteConfig.getConfig).mockResolvedValue(mockSite as SiteConfigData);
diff --git a/server/src/services/tracker/pageviewQueue.ts b/server/src/services/tracker/pageviewQueue.ts
index e4b7ece20..e5819cc97 100644
--- a/server/src/services/tracker/pageviewQueue.ts
+++ b/server/src/services/tracker/pageviewQueue.ts
@@ -4,25 +4,10 @@ import { getLocation } from "../../db/geolocation/geolocation.js";
import { createServiceLogger } from "../../lib/logger/logger.js";
import { getDeviceType } from "../../utils.js";
import { getChannel } from "./getChannel.js";
-import { clearSelfReferrer, getAllUrlParams } from "./utils.js";
-import { TrackingPayload } from "./types.js";
+import { clearSelfReferrer, getAllUrlParams, TotalTrackingPayload } from "./utils.js";
-type TotalPayload = TrackingPayload & {
- userId: string;
- timestamp: string;
+type TotalPayload = TotalTrackingPayload & {
sessionId: string;
- ua: UAParser.IResult;
- referrer: string;
- ipAddress: string;
- type?: string;
- event_name?: string;
- properties?: string;
- // Performance metrics
- lcp?: number;
- cls?: number;
- inp?: number;
- fcp?: number;
- ttfb?: number;
};
const getParsedProperties = (properties: string | undefined) => {
@@ -126,6 +111,7 @@ class PageviewQueue {
inp: pv.inp || null,
fcp: pv.fcp || null,
ttfb: pv.ttfb || null,
+ ip: pv.storeIp ? pv.ipAddress : null,
};
});
diff --git a/server/src/services/tracker/trackEvent.ts b/server/src/services/tracker/trackEvent.ts
index 9ec1b9f16..b8de474a4 100644
--- a/server/src/services/tracker/trackEvent.ts
+++ b/server/src/services/tracker/trackEvent.ts
@@ -293,7 +293,7 @@ export async function trackEvent(request: FastifyRequest, reply: FastifyReply) {
request, // Pass request for IP/UA
validatedPayload.type,
validatedPayload, // Pass original validated payload
- siteConfiguration.siteId // Pass numeric siteId separately
+ siteConfiguration
);
// Update session (use numeric siteId)
diff --git a/server/src/services/tracker/utils.ts b/server/src/services/tracker/utils.ts
index aad7605a6..11c5222d4 100644
--- a/server/src/services/tracker/utils.ts
+++ b/server/src/services/tracker/utils.ts
@@ -5,16 +5,23 @@ import { getIpAddress } from "../../utils.js";
import { userIdService } from "../userId/userIdService.js";
import { trackingPayloadSchema } from "./trackEvent.js";
import { TrackingPayload } from "./types.js";
+import { SiteConfigData } from "../../lib/siteConfig.js";
export type TotalTrackingPayload = TrackingPayload & {
+ userId: string;
+ timestamp: string;
type?: string;
event_name?: string;
properties?: string;
- userId: string;
- timestamp: string;
ua: UAParser.IResult;
referrer: string;
ipAddress: string;
+ storeIp?: boolean;
+ lcp?: number;
+ cls?: number;
+ inp?: number;
+ fcp?: number;
+ ttfb?: number;
};
// Infer type from Zod schema
@@ -86,7 +93,7 @@ export async function createBasePayload(
request: FastifyRequest,
eventType: "pageview" | "custom_event" | "performance" | "error" | "outbound" = "pageview",
validatedBody: ValidatedTrackingPayload,
- numericSiteId: number
+ siteConfiguration: SiteConfigData
): Promise<TotalTrackingPayload> {
// Use custom user agent if provided, otherwise fall back to header
const userAgent = validatedBody.user_agent || request.headers["user-agent"] || "";
@@ -96,11 +103,11 @@ export async function createBasePayload(
// Use custom user ID if provided, otherwise generate one
const userId = validatedBody.user_id
? validatedBody.user_id.trim()
- : await userIdService.generateUserId(ipAddress, userAgent, numericSiteId);
+ : await userIdService.generateUserId(ipAddress, userAgent, siteConfiguration.siteId);
return {
...validatedBody,
- site_id: numericSiteId, // Use the numeric site ID
+ site_id: siteConfiguration.siteId, // Use the numeric site ID
hostname: validatedBody.hostname || "",
pathname: validatedBody.pathname || "",
querystring: validatedBody.querystring || "",
@@ -114,5 +121,6 @@ export async function createBasePayload(
timestamp: new Date().toISOString(),
ua: userAgentParser(userAgent),
userId: userId,
- };
+ storeIp: siteConfiguration.trackIp,
+ } as any;
}