Skip to content

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

Add overview

Bill Yangcc7cb63cc115
diff --git a/client/src/app/components/MainSection/MainSection.tsx b/client/src/app/components/MainSection/MainSection.tsx
index de3771941..a9a41c9c0 100644
--- a/client/src/app/components/MainSection/MainSection.tsx
+++ b/client/src/app/components/MainSection/MainSection.tsx
@@ -1,17 +1,50 @@
"use client";
import { Card, CardContent, CardLoader } from "@/components/ui/card";
-import { useGetPageviews } from "../../../hooks/api";
+import { useGetOverview, useGetPageviews } from "../../../hooks/api";
import { BucketSelection } from "./BucketSelection";
import { Chart } from "./Chart";
export function MainSection() {
const { data, isFetching, error } = useGetPageviews();
+ const {
+ data: overviewData,
+ isFetching: isOverviewFetching,
+ error: overviewError,
+ } = useGetOverview();
return (
<Card>
- {isFetching && <CardLoader />}
+ {(isFetching || isOverviewFetching) && <CardLoader />}
<CardContent className="pt-4 w-full">
- <BucketSelection />
+ <div className="flex justify-between items-center">
+ <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">
+ {overviewData?.data?.users.toLocaleString()}
+ </div>
+ </div>
+ <div className="flex flex-col gap-1">
+ <div className="text-sm font-medium text-muted-foreground">
+ Sessions
+ </div>
+ <div className="text-3xl font-medium">
+ {overviewData?.data?.sessions.toLocaleString()}
+ </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">
+ {overviewData?.data?.pageviews.toLocaleString()}
+ </div>
+ </div>
+ </div>
+ <BucketSelection />
+ </div>
<div className="h-[350px]">
<Chart data={data} />
</div>
diff --git a/client/src/hooks/api.ts b/client/src/hooks/api.ts
index d09be03d2..0c136fe31 100644
--- a/client/src/hooks/api.ts
+++ b/client/src/hooks/api.ts
@@ -25,6 +25,7 @@ export function useGenericQuery<T>(
`${process.env.BACKEND_URL}/${endpoint}?startDate=${startDate}&endDate=${endDate}&timezone=${timezone}`
).then((res) => res.json());
},
+ staleTime: Infinity,
});
}
@@ -109,5 +110,16 @@ export function useGetPageviews(): UseQueryResult<
).then((res) => res.json());
},
placeholderData: keepPreviousData,
+ staleTime: Infinity,
});
}
+
+export type GetOverviewResponse = {
+ sessions: number;
+ pageviews: number;
+ users: number;
+};
+
+export function useGetOverview() {
+ return useGenericQuery<GetOverviewResponse>("overview");
+}
diff --git a/docker-compose.yml b/docker-compose.yml
index 2f80c8463..e7ca43ee2 100644
--- a/docker-compose.yml
+++ b/docker-compose.yml
@@ -59,7 +59,7 @@ services:
- NODE_ENV=production
- CLICKHOUSE_HOST=http://clickhouse:8123
- CLICKHOUSE_DB=analytics
- - BACKEND_URL=http://backend:3001
+ - BACKEND_URL=http://localhost:3001
depends_on:
- backend
diff --git a/server/src/api/getOverview.ts b/server/src/api/getOverview.ts
new file mode 100644
index 000000000..840000374
--- /dev/null
+++ b/server/src/api/getOverview.ts
@@ -0,0 +1,39 @@
+import { FastifyReply, FastifyRequest } from "fastify";
+import clickhouse from "../db/clickhouse/clickhouse";
+import { GenericRequest } from "./types";
+import { getTimeStatement, processResults } from "./utils";
+
+type GetOverviewResponse = {
+ sessions: number;
+ pageviews: number;
+ users: number;
+};
+
+export async function getOverview(
+ { query: { startDate, endDate, timezone } }: FastifyRequest<GenericRequest>,
+ res: FastifyReply
+) {
+ const query = `
+ SELECT
+ COUNT(DISTINCT(session_id)) AS sessions,
+ COUNT(*) AS pageviews,
+ COUNT(DISTINCT(user_id)) AS users
+ FROM
+ pageviews
+ WHERE
+ ${getTimeStatement(startDate, endDate, timezone)}
+ `;
+
+ try {
+ const result = await clickhouse.query({
+ query,
+ format: "JSONEachRow",
+ });
+
+ const data = await processResults<GetOverviewResponse>(result);
+ return res.send({ data: data[0] });
+ } catch (error) {
+ console.error("Error fetching overview:", error);
+ return res.status(500).send({ error: "Failed to fetch overview" });
+ }
+}
diff --git a/server/src/index.ts b/server/src/index.ts
index 336054baf..e97fb929b 100644
--- a/server/src/index.ts
+++ b/server/src/index.ts
@@ -16,6 +16,7 @@ import { getDevices } from "./api/getDevices";
import { getReferrers } from "./api/getReferrers";
import { getPages } from "./api/getPages";
import { getPageViews } from "./api/getPageViews";
+import { getOverview } from "./api/getOverview";
const server = Fastify({
logger: {
@@ -46,6 +47,8 @@ server.get("/health", async () => {
server.get("/live-user-count", async () => {
return { count: await getLiveUsercount() };
});
+
+server.get("/overview", getOverview);
server.get("/countries", getCountries);
server.get("/browsers", getBrowsers);
server.get("/operating-systems", getOperatingSystems);