Skip to content

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

add more metrics to overview

Bill Yang0871ed0ef77a
diff --git a/client/src/app/components/MainSection/Overview.tsx b/client/src/app/components/MainSection/Overview.tsx
index 29d8bc86a..088247665 100644
--- a/client/src/app/components/MainSection/Overview.tsx
+++ b/client/src/app/components/MainSection/Overview.tsx
@@ -1,3 +1,4 @@
+import { Duration } from "luxon";
import { Badge } from "../../../components/ui/badge";
import { Skeleton } from "../../../components/ui/skeleton";
import { useGetOverview } from "../../../hooks/api";
@@ -28,6 +29,39 @@ const ChangePercentage = ({
);
};
+const Stat = ({
+ title,
+ value,
+ previous,
+ valueFormatter,
+ isLoading,
+}: {
+ title: string;
+ value: number;
+ previous: number;
+ valueFormatter?: (value: number) => string;
+ isLoading: boolean;
+}) => {
+ return (
+ <div className="flex flex-col gap-1">
+ <div className="text-sm font-medium text-muted-foreground">{title}</div>
+ <div className="text-3xl font-medium flex gap-2 items-center">
+ {isLoading ? (
+ <>
+ <Skeleton className="w-[60px] h-7 rounded-md" />
+ <Skeleton className="w-[30px] h-5 rounded-md" />
+ </>
+ ) : (
+ <>
+ {valueFormatter ? valueFormatter(value) : value}
+ <ChangePercentage current={value} previous={previous} />
+ </>
+ )}
+ </div>
+ </div>
+ );
+};
+
export function Overview() {
const {
data: overviewData,
@@ -49,73 +83,74 @@ export function Overview() {
const currentPageviews = overviewData?.data?.pageviews ?? 0;
const previousPageviews = overviewDataPrevious?.data?.pageviews ?? 0;
- return (
- <div className="flex gap-8 items-center">
- <div className="flex flex-col gap-1">
- <div className="text-sm font-medium text-muted-foreground">
- Unique Users
- </div>
- <div className="text-3xl font-medium flex gap-2 items-center">
- {isLoading ? (
- <>
- <Skeleton className="w-[60px] h-7 rounded-md" />
- <Skeleton className="w-[30px] h-5 rounded-md" />
- </>
- ) : (
- <>
- {formatter(currentUsers)}
+ const currentPagesPerSession = overviewData?.data?.pages_per_session ?? 0;
+ const previousPagesPerSession =
+ overviewDataPrevious?.data?.pages_per_session ?? 0;
- <ChangePercentage
- current={currentUsers}
- previous={previousUsers}
- />
- </>
- )}
- </div>
- </div>
+ const currentBounceRate = overviewData?.data?.bounce_rate ?? 0;
+ const previousBounceRate = overviewDataPrevious?.data?.bounce_rate ?? 0;
- <div className="flex flex-col gap-1">
- <div className="text-sm font-medium text-muted-foreground">
- Sessions
- </div>
- <div className="text-3xl font-medium flex gap-2 items-center">
- {isLoading ? (
- <>
- <Skeleton className="w-[60px] h-7 rounded-md" />
- <Skeleton className="w-[30px] h-5 rounded-md" />
- </>
- ) : (
- <>
- {formatter(currentSessions)}
- <ChangePercentage
- current={currentSessions}
- previous={previousSessions}
- />
- </>
- )}
- </div>
- </div>
- <div className="flex flex-col gap-1">
- <div className="text-sm font-medium text-muted-foreground">
- Pageviews
- </div>
- <div className="text-3xl font-medium flex gap-2 items-center">
- {isLoading ? (
- <>
- <Skeleton className="w-[60px] h-7 rounded-md" />
- <Skeleton className="w-[30px] h-5 rounded-md" />
- </>
- ) : (
- <>
- {formatter(currentPageviews)}
- <ChangePercentage
- current={currentPageviews}
- previous={previousPageviews}
- />
- </>
- )}
- </div>
- </div>
+ const currentSessionDuration = overviewData?.data?.session_duration ?? 0;
+ const previousSessionDuration =
+ overviewDataPrevious?.data?.session_duration ?? 0;
+
+ return (
+ <div className="flex gap-6 items-center">
+ <Stat
+ title="Unique Users"
+ value={currentUsers}
+ previous={previousUsers}
+ isLoading={isLoading}
+ valueFormatter={formatter}
+ />
+ <Stat
+ title="Sessions"
+ value={currentSessions}
+ previous={previousSessions}
+ isLoading={isLoading}
+ valueFormatter={formatter}
+ />
+ <Stat
+ title="Pageviews"
+ value={currentPageviews}
+ previous={previousPageviews}
+ isLoading={isLoading}
+ valueFormatter={formatter}
+ />
+ <Stat
+ title="Pages per Session"
+ value={currentPagesPerSession}
+ previous={previousPagesPerSession}
+ isLoading={isLoading}
+ valueFormatter={(value) => value.toFixed(1)}
+ />
+ <Stat
+ title="Bounce Rate"
+ value={currentBounceRate}
+ previous={previousBounceRate}
+ isLoading={isLoading}
+ valueFormatter={(value) => `${(value * 100).toFixed(1)}%`}
+ />
+ <Stat
+ title="Session Duration"
+ value={currentSessionDuration}
+ previous={previousSessionDuration}
+ isLoading={isLoading}
+ valueFormatter={(value) => {
+ const duration = Duration.fromMillis(value * 1000);
+ const hours = Math.floor(duration.as("hours"));
+ const minutes = Math.floor(duration.as("minutes") % 60);
+ const seconds = Math.floor(duration.as("seconds") % 60);
+
+ if (hours > 0) {
+ return `${hours}hr ${minutes}min`;
+ } else if (minutes > 0) {
+ return `${minutes}m ${seconds}s`;
+ } else {
+ return `${seconds}s`;
+ }
+ }}
+ />
</div>
);
}
diff --git a/client/src/app/page.tsx b/client/src/app/page.tsx
index f5b99e511..b3b3ebca7 100644
--- a/client/src/app/page.tsx
+++ b/client/src/app/page.tsx
@@ -11,7 +11,7 @@ import { Referrers } from "./components/sections/Referrers/Referrers";
export default function Home() {
return (
<main className="flex min-h-screen flex-col items-center p-4 pt-12">
- <div className="w-full max-w-5xl">
+ <div className="w-full max-w-6xl">
<Header />
<MainSection />
<div className="grid grid-cols-2 gap-4 mt-4">
diff --git a/client/src/hooks/api.ts b/client/src/hooks/api.ts
index 3ecf03eb0..1d3fb7e4d 100644
--- a/client/src/hooks/api.ts
+++ b/client/src/hooks/api.ts
@@ -124,6 +124,9 @@ export type GetOverviewResponse = {
sessions: number;
pageviews: number;
users: number;
+ pages_per_session: number;
+ bounce_rate: number;
+ session_duration: number;
};
export function useGetOverview(periodTime?: PeriodTime) {
diff --git a/server/src/api/getOverview.ts b/server/src/api/getOverview.ts
index 840000374..e3ba468d9 100644
--- a/server/src/api/getOverview.ts
+++ b/server/src/api/getOverview.ts
@@ -7,6 +7,9 @@ type GetOverviewResponse = {
sessions: number;
pageviews: number;
users: number;
+ pages_per_session: number;
+ bounce_rate: number;
+ session_duration: number;
};
export async function getOverview(
@@ -14,14 +17,45 @@ export async function getOverview(
res: FastifyReply
) {
const query = `
- SELECT
- COUNT(DISTINCT(session_id)) AS sessions,
- COUNT(*) AS pageviews,
- COUNT(DISTINCT(user_id)) AS users
+ SELECT
+ session_stats.sessions,
+ session_stats.pages_per_session,
+ session_stats.bounce_rate,
+ session_stats.session_duration,
+ page_stats.pageviews,
+ page_stats.users
FROM
- pageviews
- WHERE
- ${getTimeStatement(startDate, endDate, timezone)}
+ (
+ -- Session-level metrics
+ SELECT
+ COUNT() AS sessions,
+ AVG(pages_in_session) AS pages_per_session,
+ sumIf(1, pages_in_session = 1) / COUNT() AS bounce_rate,
+ AVG(end_time - start_time) AS session_duration
+ FROM
+ (
+ -- Build a summary row per session
+ SELECT
+ session_id,
+ MIN(timestamp) AS start_time,
+ MAX(timestamp) AS end_time,
+ COUNT(*) AS pages_in_session
+ FROM pageviews
+ WHERE
+ ${getTimeStatement(startDate, endDate, timezone)}
+ GROUP BY session_id
+ )
+ ) AS session_stats
+ CROSS JOIN
+ (
+ -- Page-level and user-level metrics
+ SELECT
+ COUNT(*) AS pageviews,
+ COUNT(DISTINCT user_id) AS users
+ FROM pageviews
+ WHERE
+ ${getTimeStatement(startDate, endDate, timezone)}
+ ) AS page_stats
`;
try {