Skip to content

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

add more bucketed stats

Bill Yang93960074b74f
diff --git a/client/src/app/[site]/components/MainSection/Chart.tsx b/client/src/app/[site]/components/MainSection/Chart.tsx
index 37b331861..fc347bcf6 100644
--- a/client/src/app/[site]/components/MainSection/Chart.tsx
+++ b/client/src/app/[site]/components/MainSection/Chart.tsx
@@ -2,7 +2,7 @@
import { ResponsiveLine } from "@nivo/line";
import { round } from "lodash";
import { DateTime } from "luxon";
-import { APIResponse, GetPageViewsResponse } from "@/hooks/api";
+import { APIResponse, GetOverviewBucketedResponse } from "@/hooks/api";
import { nivoTheme } from "@/lib/nivo";
import { Time, TimeBucket, useStore } from "@/lib/store";
@@ -67,8 +67,8 @@ export function Chart({
previousData,
max,
}: {
- data: APIResponse<GetPageViewsResponse> | undefined;
- previousData: APIResponse<GetPageViewsResponse> | undefined;
+ data: APIResponse<GetOverviewBucketedResponse> | undefined;
+ previousData: APIResponse<GetOverviewBucketedResponse> | undefined;
max: number;
}) {
const { time, bucket } = useStore();
@@ -124,7 +124,7 @@ export function Chart({
min: 0,
stacked: false,
reverse: false,
- max: max,
+ max: Math.max(max, 10),
}}
enableGridX={false}
enableGridY={true}
diff --git a/client/src/app/[site]/components/MainSection/MainSection.tsx b/client/src/app/[site]/components/MainSection/MainSection.tsx
index 68ce6448a..e590f4565 100644
--- a/client/src/app/[site]/components/MainSection/MainSection.tsx
+++ b/client/src/app/[site]/components/MainSection/MainSection.tsx
@@ -1,18 +1,18 @@
"use client";
import { Card, CardContent, CardLoader } from "@/components/ui/card";
-import { useGetOverview, useGetPageviews } from "@/hooks/api";
+import { useGetOverview, useGetOverviewBucketed } from "@/hooks/api";
import { BucketSelection } from "./BucketSelection";
import { Chart } from "./Chart";
import { Overview } from "./Overview";
import { PreviousChart } from "./PreviousChart";
export function MainSection() {
- const { data, isFetching, error } = useGetPageviews();
+ const { data, isFetching, error } = useGetOverviewBucketed();
const {
data: previousData,
isFetching: isPreviousFetching,
error: previousError,
- } = useGetPageviews("previous");
+ } = useGetOverviewBucketed("previous");
const { isFetching: isOverviewFetching } = useGetOverview();
const { isFetching: isOverviewFetchingPrevious } = useGetOverview("previous");
diff --git a/client/src/app/[site]/components/MainSection/PreviousChart.tsx b/client/src/app/[site]/components/MainSection/PreviousChart.tsx
index 9ccea40c6..d882ce6c6 100644
--- a/client/src/app/[site]/components/MainSection/PreviousChart.tsx
+++ b/client/src/app/[site]/components/MainSection/PreviousChart.tsx
@@ -1,7 +1,7 @@
"use client";
import { ResponsiveLine } from "@nivo/line";
import { DateTime } from "luxon";
-import { APIResponse, GetPageViewsResponse } from "@/hooks/api";
+import { APIResponse, GetOverviewBucketedResponse } from "@/hooks/api";
import { nivoTheme } from "@/lib/nivo";
import { Time, useStore } from "@/lib/store";
import { useMemo } from "react";
@@ -31,7 +31,7 @@ export function PreviousChart({
data,
max,
}: {
- data: APIResponse<GetPageViewsResponse> | undefined;
+ data: APIResponse<GetOverviewBucketedResponse> | undefined;
max: number;
}) {
const { previousTime: time } = useStore();
@@ -65,7 +65,7 @@ export function PreviousChart({
min: 0,
stacked: false,
reverse: false,
- max,
+ max: Math.max(max, 10),
}}
enableGridX={false}
enableGridY={false}
diff --git a/client/src/app/[site]/components/shared/StandardCard.tsx b/client/src/app/[site]/components/shared/StandardCard.tsx
index f4a7a711c..e05842987 100644
--- a/client/src/app/[site]/components/shared/StandardCard.tsx
+++ b/client/src/app/[site]/components/shared/StandardCard.tsx
@@ -60,7 +60,7 @@ export function StandardCard<T extends { percentage: number; count: number }>({
</div>
</div>
))}
- {data?.data?.length && data?.data?.length > 10 && (
+ {data?.data?.length && data?.data?.length > 10 ? (
<Dialog>
<DialogTrigger asChild>
<Button variant="outline">View All</Button>
@@ -94,7 +94,7 @@ export function StandardCard<T extends { percentage: number; count: number }>({
</div>
</DialogContent>
</Dialog>
- )}
+ ) : null}
</CardContent>
</Card>
);
diff --git a/client/src/hooks/api.ts b/client/src/hooks/api.ts
index d53c72184..cde88153c 100644
--- a/client/src/hooks/api.ts
+++ b/client/src/hooks/api.ts
@@ -115,25 +115,30 @@ export function useGetReferrers() {
return useGenericQuery<GetReferrersResponse>("referrers");
}
-export type GetPageViewsResponse = {
+export type GetOverviewBucketedResponse = {
time: string;
pageviews: number;
+ sessions: number;
+ pages_per_session: number;
+ bounce_rate: number;
+ session_duration: number;
+ users: number;
}[];
-export function useGetPageviews(
+export function useGetOverviewBucketed(
periodTime?: PeriodTime
-): UseQueryResult<APIResponse<GetPageViewsResponse>> {
+): UseQueryResult<APIResponse<GetOverviewBucketedResponse>> {
const { time, previousTime, bucket, site } = useStore();
const timeToUse = periodTime === "previous" ? previousTime : time;
const { startDate, endDate } = getStartAndEndDate(timeToUse);
return useQuery({
- queryKey: ["pageviews", timeToUse, bucket, site],
+ queryKey: ["overview-bucketed", timeToUse, bucket, site],
queryFn: () => {
const timezone = Intl.DateTimeFormat().resolvedOptions().timeZone;
return authedFetch(
- `${BACKEND_URL}/pageviews?startDate=${startDate}&endDate=${endDate}&timezone=${timezone}&bucket=${bucket}&site=${site}`
+ `${BACKEND_URL}/overview-bucketed?startDate=${startDate}&endDate=${endDate}&timezone=${timezone}&bucket=${bucket}&site=${site}`
).then((res) => res.json());
},
placeholderData: (_, query: any) => {
diff --git a/server/src/api/getOverview.ts b/server/src/api/getOverview.ts
index a381e4751..1a210f8a2 100644
--- a/server/src/api/getOverview.ts
+++ b/server/src/api/getOverview.ts
@@ -18,8 +18,7 @@ export async function getOverview(
}: FastifyRequest<GenericRequest>,
res: FastifyReply
) {
- const query = `
- SELECT
+ const query = `SELECT
session_stats.sessions,
session_stats.pages_per_session,
session_stats.bounce_rate,
@@ -59,8 +58,10 @@ export async function getOverview(
WHERE
${getTimeStatement(startDate, endDate, timezone)}
AND site_id = ${site}
- ) AS page_stats
- `;
+ ) AS page_stats`;
+
+ console.info;
+ console.info(query);
try {
const result = await clickhouse.query({
diff --git a/server/src/api/getOverviewBucketed.ts b/server/src/api/getOverviewBucketed.ts
new file mode 100644
index 000000000..00a1c6e31
--- /dev/null
+++ b/server/src/api/getOverviewBucketed.ts
@@ -0,0 +1,130 @@
+import { FastifyReply, FastifyRequest } from "fastify";
+import clickhouse from "../db/clickhouse/clickhouse.js";
+import { getTimeStatement, processResults } from "./utils.js";
+
+const TimeBucketToFn = {
+ hour: "toStartOfHour",
+ day: "toStartOfDay",
+ week: "toStartOfWeek",
+ month: "toStartOfMonth",
+};
+
+const bucketIntervalMap = {
+ hour: "1 HOUR",
+ day: "1 DAY",
+ week: "7 DAY",
+ month: "1 MONTH",
+} as const;
+
+function getTimeStatementFill(
+ startDate: string,
+ endDate: string,
+ timezone: string,
+ bucket: TimeBucket
+) {
+ return `WITH FILL FROM toTimeZone(
+ toStartOfDay(toDateTime('${startDate}', '${timezone}')),
+ 'UTC'
+ )
+ TO if(
+ toDate('${endDate}') = toDate(now(), '${timezone}'),
+ now(),
+ toTimeZone(
+ toStartOfDay(toDateTime('${endDate}', '${timezone}')) + INTERVAL 1 DAY,
+ 'UTC'
+ )
+ ) STEP INTERVAL ${bucketIntervalMap[bucket]}`;
+}
+
+type TimeBucket = "hour" | "day" | "week" | "month";
+
+type getOverviewBucketed = { time: string; pageviews: number }[];
+
+export async function getOverviewBucketed(
+ {
+ query: { startDate, endDate, timezone, bucket, site },
+ }: FastifyRequest<{
+ Querystring: {
+ startDate: string;
+ endDate: string;
+ timezone: string;
+ bucket: TimeBucket;
+ site: string;
+ };
+ }>,
+ res: FastifyReply
+) {
+ const query = `SELECT
+ session_stats.time AS time,
+ session_stats.sessions,
+ session_stats.pages_per_session,
+ session_stats.bounce_rate,
+ session_stats.session_duration,
+ page_stats.pageviews,
+ page_stats.users
+FROM
+(
+ SELECT
+ ${
+ TimeBucketToFn[bucket]
+ }(toTimeZone(start_time, '${timezone}')) AS time,
+ 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
+ (
+ /* One 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)}
+ AND site_id = ${site}
+ GROUP BY session_id
+ )
+ GROUP BY time ORDER BY time ${getTimeStatementFill(
+ startDate,
+ endDate,
+ timezone,
+ bucket
+ )}
+) AS session_stats
+FULL JOIN
+(
+ SELECT
+ ${
+ TimeBucketToFn[bucket]
+ }(toTimeZone(timestamp, '${timezone}')) AS time,
+ COUNT(*) AS pageviews,
+ COUNT(DISTINCT user_id) AS users
+ FROM pageviews
+ WHERE
+ ${getTimeStatement(startDate, endDate, timezone)}
+ AND site_id = ${site}
+ GROUP BY time ORDER BY time ${getTimeStatementFill(
+ startDate,
+ endDate,
+ timezone,
+ bucket
+ )}
+) AS page_stats
+USING time
+ORDER BY time`;
+
+ try {
+ const result = await clickhouse.query({
+ query,
+ format: "JSONEachRow",
+ });
+
+ const data = await processResults<getOverviewBucketed[number]>(result);
+ return res.send({ data });
+ } catch (error) {
+ console.error("Error fetching pageviews:", error);
+ return res.status(500).send({ error: "Failed to fetch pageviews" });
+ }
+}
diff --git a/server/src/api/getPageViews.ts b/server/src/api/getPageViews.ts
deleted file mode 100644
index 8080fbbb2..000000000
--- a/server/src/api/getPageViews.ts
+++ /dev/null
@@ -1,85 +0,0 @@
-import { FastifyReply, FastifyRequest } from "fastify";
-import clickhouse from "../db/clickhouse/clickhouse.js";
-import { getTimeStatement, processResults } from "./utils.js";
-
-const TimeBucketToFn = {
- hour: "toStartOfHour",
- day: "toStartOfDay",
- week: "toStartOfWeek",
- month: "toStartOfMonth",
-};
-
-const bucketIntervalMap = {
- hour: "1 HOUR",
- day: "1 DAY",
- week: "7 DAY",
- month: "1 MONTH",
-} as const;
-
-function getTimeStatementFill(
- startDate: string,
- endDate: string,
- timezone: string,
- bucket: TimeBucket
-) {
- return `
- WITH FILL FROM toTimeZone(
- toStartOfDay(toDateTime('${startDate}', '${timezone}')),
- 'UTC'
- )
- TO if(
- toDate('${endDate}') = toDate(now(), '${timezone}'),
- now(),
- toTimeZone(
- toStartOfDay(toDateTime('${endDate}', '${timezone}')) + INTERVAL 1 DAY,
- 'UTC'
- )
- ) STEP INTERVAL ${bucketIntervalMap[bucket]}
- `;
-}
-
-type TimeBucket = "hour" | "day" | "week" | "month";
-
-type GetPageViewsResponse = { time: string; pageviews: number }[];
-
-export async function getPageViews(
- {
- query: { startDate, endDate, timezone, bucket, site },
- }: FastifyRequest<{
- Querystring: {
- startDate: string;
- endDate: string;
- timezone: string;
- bucket: TimeBucket;
- site: string;
- };
- }>,
- res: FastifyReply
-) {
- const query = `
- SELECT
- ${TimeBucketToFn[bucket]}(toTimeZone(timestamp, '${timezone}')) AS time,
- count() AS pageviews
- FROM pageviews
- WHERE
- ${getTimeStatement(startDate, endDate, timezone)}
- AND site_id = ${site}
- GROUP BY
- time
- ORDER BY
- time ${getTimeStatementFill(startDate, endDate, timezone, bucket)}
- `;
-
- try {
- const result = await clickhouse.query({
- query,
- format: "JSONEachRow",
- });
-
- const data = await processResults<GetPageViewsResponse[number]>(result);
- return res.send({ data });
- } catch (error) {
- console.error("Error fetching pageviews:", error);
- return res.status(500).send({ error: "Failed to fetch pageviews" });
- }
-}
diff --git a/server/src/api/utils.ts b/server/src/api/utils.ts
index 0d98347d7..cbcbbb5c3 100644
--- a/server/src/api/utils.ts
+++ b/server/src/api/utils.ts
@@ -5,8 +5,7 @@ export function getTimeStatement(
endDate: string,
timezone: string
) {
- return `
- timestamp >= toTimeZone(
+ return `timestamp >= toTimeZone(
toStartOfDay(toDateTime('${startDate}', '${timezone}')),
'UTC'
)
@@ -17,8 +16,7 @@ export function getTimeStatement(
toStartOfDay(toDateTime('${endDate}', '${timezone}')) + INTERVAL 1 DAY,
'UTC'
)
- )
- `;
+ )`;
}
export async function processResults<T>(
diff --git a/server/src/index.ts b/server/src/index.ts
index cadc7214b..20a80f4b7 100644
--- a/server/src/index.ts
+++ b/server/src/index.ts
@@ -17,7 +17,7 @@ import { getLiveUsercount } from "./api/getLiveUsercount.js";
import { getOperatingSystems } from "./api/getOperatingSystems.js";
import { getOverview } from "./api/getOverview.js";
import { getPages } from "./api/getPages.js";
-import { getPageViews } from "./api/getPageViews.js";
+import { getOverviewBucketed } from "./api/getOverviewBucketed.js";
import { getReferrers } from "./api/getReferrers.js";
import { initializeClickhouse } from "./db/clickhouse/clickhouse.js";
import { initializePostgres } from "./db/postgres/postgres.js";
@@ -121,13 +121,13 @@ server.addHook("onRequest", async (request, reply) => {
server.get("/live-user-count/:site", getLiveUsercount);
server.get("/overview", getOverview);
+server.get("/overview-bucketed", getOverviewBucketed);
server.get("/countries", getCountries);
server.get("/browsers", getBrowsers);
server.get("/operating-systems", getOperatingSystems);
server.get("/devices", getDevices);
server.get("/pages", getPages);
server.get("/referrers", getReferrers);
-server.get("/pageviews", getPageViews);
server.get("/site-has-data/:site", getSiteHasData);
// Administrative