Skip to content

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

add cards

Bill Yangf5105bbce665
diff --git a/client/src/app/actions/getBrowsers.ts b/client/src/app/actions/getBrowsers.ts
new file mode 100644
index 000000000..207f2a63c
--- /dev/null
+++ b/client/src/app/actions/getBrowsers.ts
@@ -0,0 +1,44 @@
+"use server";
+
+import { clickhouse } from "@/lib/clickhouse";
+import { getTimeStatement } from "./utils";
+
+type Response = {
+ browser: string;
+ count: number;
+ percentage: number;
+}[];
+
+export async function getBrowsers({
+ startDate,
+ endDate,
+ timezone = "America/Los_Angeles",
+}: {
+ startDate: string;
+ endDate: string;
+ timezone: string;
+}): Promise<{ data?: Response; error?: string }> {
+ const query = `
+ SELECT
+ browser,
+ COUNT(*) as count,
+ ROUND(COUNT(*) * 100.0 / SUM(COUNT(*)) OVER (), 2) as percentage
+ FROM pageviews
+ WHERE
+ ${getTimeStatement(startDate, endDate, timezone)}
+ GROUP BY browser ORDER BY count desc;
+ `;
+
+ try {
+ const result = await clickhouse.query({
+ query,
+ format: "JSONEachRow",
+ });
+
+ const data: Response = await result.json();
+ return { data };
+ } catch (error) {
+ console.error("Error fetching browsers:", error);
+ return { error: "Failed to fetch browsers" };
+ }
+}
diff --git a/client/src/app/actions/getDevices.ts b/client/src/app/actions/getDevices.ts
new file mode 100644
index 000000000..abf6b8ff6
--- /dev/null
+++ b/client/src/app/actions/getDevices.ts
@@ -0,0 +1,44 @@
+"use server";
+
+import { clickhouse } from "@/lib/clickhouse";
+import { getTimeStatement } from "./utils";
+
+type Response = {
+ device_type: string;
+ count: number;
+ percentage: number;
+}[];
+
+export async function getDevices({
+ startDate,
+ endDate,
+ timezone = "America/Los_Angeles",
+}: {
+ startDate: string;
+ endDate: string;
+ timezone: string;
+}): Promise<{ data?: Response; error?: string }> {
+ const query = `
+ SELECT
+ device_type,
+ COUNT(*) as count,
+ ROUND(COUNT(*) * 100.0 / SUM(COUNT(*)) OVER (), 2) as percentage
+ FROM pageviews
+ WHERE
+ ${getTimeStatement(startDate, endDate, timezone)}
+ GROUP BY device_type ORDER BY count desc;
+ `;
+
+ try {
+ const result = await clickhouse.query({
+ query,
+ format: "JSONEachRow",
+ });
+
+ const data: Response = await result.json();
+ return { data };
+ } catch (error) {
+ console.error("Error fetching devices:", error);
+ return { error: "Failed to fetch devices" };
+ }
+}
diff --git a/client/src/app/actions/getOperatingSystems.ts b/client/src/app/actions/getOperatingSystems.ts
new file mode 100644
index 000000000..115c481fd
--- /dev/null
+++ b/client/src/app/actions/getOperatingSystems.ts
@@ -0,0 +1,44 @@
+"use server";
+
+import { clickhouse } from "@/lib/clickhouse";
+import { getTimeStatement } from "./utils";
+
+type Response = {
+ operating_system: string;
+ count: number;
+ percentage: number;
+}[];
+
+export async function getOperatingSystems({
+ startDate,
+ endDate,
+ timezone = "America/Los_Angeles",
+}: {
+ startDate: string;
+ endDate: string;
+ timezone: string;
+}): Promise<{ data?: Response; error?: string }> {
+ const query = `
+ SELECT
+ operating_system,
+ COUNT(*) as count,
+ ROUND(COUNT(*) * 100.0 / SUM(COUNT(*)) OVER (), 2) as percentage
+ FROM pageviews
+ WHERE
+ ${getTimeStatement(startDate, endDate, timezone)}
+ GROUP BY operating_system ORDER BY count desc;
+ `;
+
+ try {
+ const result = await clickhouse.query({
+ query,
+ format: "JSONEachRow",
+ });
+
+ const data: Response = await result.json();
+ return { data };
+ } catch (error) {
+ console.error("Error fetching operating systems:", error);
+ return { error: "Failed to fetch operating systems" };
+ }
+}
diff --git a/client/src/app/actions/getPages.ts b/client/src/app/actions/getPages.ts
new file mode 100644
index 000000000..a3f59f8eb
--- /dev/null
+++ b/client/src/app/actions/getPages.ts
@@ -0,0 +1,46 @@
+"use server";
+
+import { clickhouse } from "@/lib/clickhouse";
+import { getTimeStatement } from "./utils";
+
+type Response = {
+ pathname: string;
+ count: number;
+ percentage: number;
+}[];
+
+export async function getPages({
+ startDate,
+ endDate,
+ timezone = "America/Los_Angeles",
+}: {
+ startDate: string;
+ endDate: string;
+ timezone: string;
+}): Promise<{ data?: Response; error?: string }> {
+ const query = `
+ SELECT
+ pathname,
+ COUNT(*) as count,
+ ROUND(COUNT(*) * 100.0 / SUM(COUNT(*)) OVER (), 2) as percentage
+ FROM pageviews
+ WHERE
+ ${getTimeStatement(startDate, endDate, timezone)}
+ GROUP BY pathname
+ ORDER BY count desc
+ LIMIT 100;
+ `;
+
+ try {
+ const result = await clickhouse.query({
+ query,
+ format: "JSONEachRow",
+ });
+
+ const data: Response = await result.json();
+ return { data };
+ } catch (error) {
+ console.error("Error fetching pages:", error);
+ return { error: "Failed to fetch pages" };
+ }
+}
diff --git a/client/src/app/actions/getPageviews.ts b/client/src/app/actions/getPageviews.ts
index 108e340b5..ba9d16fbf 100644
--- a/client/src/app/actions/getPageviews.ts
+++ b/client/src/app/actions/getPageviews.ts
@@ -1,6 +1,7 @@
"use server";
import { clickhouse } from "@/lib/clickhouse";
+import { getTimeStatement } from "./utils";
type Response = { time: string; pageviews: number }[];
@@ -19,26 +20,13 @@ export async function getPageViews({
count() AS pageviews
FROM pageviews
WHERE
- timestamp >= toTimeZone(
- toStartOfDay(toDateTime('${startDate}', '${timezone}')),
- 'UTC'
- )
- AND timestamp < if(
- toDate('${endDate}') = toDate(now(), '${timezone}'),
- now(),
- toTimeZone(
- toStartOfDay(toDateTime('${endDate}', '${timezone}')) + INTERVAL 1 DAY,
- 'UTC'
- )
- )
+ ${getTimeStatement(startDate, endDate, timezone)}
GROUP BY
time
ORDER BY
time ASC
`;
- console.info(query);
-
try {
const result = await clickhouse.query({
query,
diff --git a/client/src/app/actions/utils.ts b/client/src/app/actions/utils.ts
new file mode 100644
index 000000000..217432836
--- /dev/null
+++ b/client/src/app/actions/utils.ts
@@ -0,0 +1,20 @@
+export function getTimeStatement(
+ startDate: string,
+ endDate: string,
+ timezone: string
+) {
+ return `
+timestamp >= toTimeZone(
+ toStartOfDay(toDateTime('${startDate}', '${timezone}')),
+ 'UTC'
+)
+AND timestamp < if(
+ toDate('${endDate}') = toDate(now(), '${timezone}'),
+ now(),
+ toTimeZone(
+ toStartOfDay(toDateTime('${endDate}', '${timezone}')) + INTERVAL 1 DAY,
+ 'UTC'
+ )
+)
+ `;
+}
diff --git a/client/src/app/components/sections/Browsers/Browsers.tsx b/client/src/app/components/sections/Browsers/Browsers.tsx
new file mode 100644
index 000000000..21f046544
--- /dev/null
+++ b/client/src/app/components/sections/Browsers/Browsers.tsx
@@ -0,0 +1,41 @@
+"use client";
+
+import {
+ Card,
+ CardContent,
+ CardHeader,
+ CardTitle,
+} from "../../../../components/ui/card";
+import { useGetBrowsers } from "../../../hooks/useGetBrowsers";
+
+export function Browsers() {
+ const { data, isLoading } = useGetBrowsers();
+
+ return (
+ <Card>
+ <CardHeader>
+ <CardTitle>Browsers</CardTitle>
+ </CardHeader>
+ <CardContent className="pt-4 sm:px-6 sm:pt-6 flex flex-col gap-2">
+ {data?.data?.slice(0, 10).map((e) => (
+ <div key={e.browser} className="relative">
+ <div
+ className="absolute inset-0 bg-red-500 py-2 opacity-50"
+ style={{ width: `${e.percentage}%` }}
+ ></div>
+ <div className="relative z-10 ml-1 flex justify-between items-center">
+ <div>{e.browser || "Other"}</div>
+ <div className="text-sm flex">
+ <div>{e.count}</div>
+ <div className="ml-1 text-neutral-500">|</div>
+ <div className="ml-1 w-10 text-neutral-500">
+ {e.percentage}%
+ </div>
+ </div>
+ </div>
+ </div>
+ ))}
+ </CardContent>
+ </Card>
+ );
+}
diff --git a/client/src/app/components/sections/Devices/Devices.tsx b/client/src/app/components/sections/Devices/Devices.tsx
new file mode 100644
index 000000000..2ec00615a
--- /dev/null
+++ b/client/src/app/components/sections/Devices/Devices.tsx
@@ -0,0 +1,41 @@
+"use client";
+
+import {
+ Card,
+ CardContent,
+ CardHeader,
+ CardTitle,
+} from "../../../../components/ui/card";
+import { useGetDevices } from "../../../hooks/useGetDevices";
+
+export function Devices() {
+ const { data, isLoading } = useGetDevices();
+
+ return (
+ <Card>
+ <CardHeader>
+ <CardTitle>Devices</CardTitle>
+ </CardHeader>
+ <CardContent className="pt-4 sm:px-6 sm:pt-6 flex flex-col gap-2">
+ {data?.data?.slice(0, 10).map((e) => (
+ <div key={e.device_type} className="relative">
+ <div
+ className="absolute inset-0 bg-red-500 py-2 opacity-50"
+ style={{ width: `${e.percentage}%` }}
+ ></div>
+ <div className="relative z-10 ml-1 flex justify-between items-center">
+ <div>{e.device_type || "Other"}</div>
+ <div className="text-sm flex">
+ <div>{e.count}</div>
+ <div className="ml-1 text-neutral-500">|</div>
+ <div className="ml-1 w-10 text-neutral-500">
+ {e.percentage}%
+ </div>
+ </div>
+ </div>
+ </div>
+ ))}
+ </CardContent>
+ </Card>
+ );
+}
diff --git a/client/src/app/components/sections/OperatingSystems/OperatingSystems.tsx b/client/src/app/components/sections/OperatingSystems/OperatingSystems.tsx
new file mode 100644
index 000000000..5c3f7beae
--- /dev/null
+++ b/client/src/app/components/sections/OperatingSystems/OperatingSystems.tsx
@@ -0,0 +1,41 @@
+"use client";
+
+import {
+ Card,
+ CardContent,
+ CardHeader,
+ CardTitle,
+} from "../../../../components/ui/card";
+import { useGetOperatingSystems } from "../../../hooks/useGetOperatingSystems";
+
+export function OperatingSystems() {
+ const { data, isLoading } = useGetOperatingSystems();
+
+ return (
+ <Card>
+ <CardHeader>
+ <CardTitle>Operating Systems</CardTitle>
+ </CardHeader>
+ <CardContent className="pt-4 sm:px-6 sm:pt-6 flex flex-col gap-2">
+ {data?.data?.map((e) => (
+ <div key={e.operating_system} className="relative">
+ <div
+ className="absolute inset-0 bg-red-500 py-2 opacity-50"
+ style={{ width: `${e.percentage}%` }}
+ ></div>
+ <div className="relative z-10 ml-1 flex justify-between items-center">
+ <div>{e.operating_system || "Other"}</div>
+ <div className="text-sm flex">
+ <div>{e.count}</div>
+ <div className="ml-1 text-neutral-500">|</div>
+ <div className="ml-1 w-10 text-neutral-500">
+ {e.percentage}%
+ </div>
+ </div>
+ </div>
+ </div>
+ ))}
+ </CardContent>
+ </Card>
+ );
+}
diff --git a/client/src/app/components/sections/Pages/Pages.tsx b/client/src/app/components/sections/Pages/Pages.tsx
new file mode 100644
index 000000000..0cfcde525
--- /dev/null
+++ b/client/src/app/components/sections/Pages/Pages.tsx
@@ -0,0 +1,41 @@
+"use client";
+
+import {
+ Card,
+ CardContent,
+ CardHeader,
+ CardTitle,
+} from "../../../../components/ui/card";
+import { useGetPages } from "../../../hooks/useGetPages";
+
+export function Pages() {
+ const { data, isLoading } = useGetPages();
+
+ return (
+ <Card>
+ <CardHeader>
+ <CardTitle>Pages</CardTitle>
+ </CardHeader>
+ <CardContent className="pt-4 sm:px-6 sm:pt-6 flex flex-col gap-2">
+ {data?.data?.slice(0, 10).map((e) => (
+ <div key={e.pathname} className="relative">
+ <div
+ className="absolute inset-0 bg-red-500 py-2 opacity-50"
+ style={{ width: `${e.percentage}%` }}
+ ></div>
+ <div className="relative z-10 ml-1 flex justify-between items-center">
+ <div>{e.pathname || "Other"}</div>
+ <div className="text-sm flex">
+ <div>{e.count}</div>
+ <div className="ml-1 text-neutral-500">|</div>
+ <div className="ml-1 w-10 text-neutral-500">
+ {e.percentage}%
+ </div>
+ </div>
+ </div>
+ </div>
+ ))}
+ </CardContent>
+ </Card>
+ );
+}
diff --git a/client/src/app/hooks/useGetBrowsers.ts b/client/src/app/hooks/useGetBrowsers.ts
new file mode 100644
index 000000000..3f64c795d
--- /dev/null
+++ b/client/src/app/hooks/useGetBrowsers.ts
@@ -0,0 +1,22 @@
+import { useQuery } from "@tanstack/react-query";
+import { useTimeSelection } from "../../lib/timeSelectionStore";
+import { getBrowsers } from "../actions/getBrowsers";
+import { getStartAndEndDate } from "./utils";
+
+export function useGetBrowsers() {
+ const { time } = useTimeSelection();
+
+ const { startDate, endDate } = getStartAndEndDate(time);
+
+ return useQuery({
+ queryKey: ["browsers", time],
+ queryFn: () => {
+ const timezone = Intl.DateTimeFormat().resolvedOptions().timeZone;
+ return getBrowsers({
+ startDate,
+ endDate,
+ timezone,
+ });
+ },
+ });
+}
diff --git a/client/src/app/hooks/useGetDevices.ts b/client/src/app/hooks/useGetDevices.ts
new file mode 100644
index 000000000..5d60c014e
--- /dev/null
+++ b/client/src/app/hooks/useGetDevices.ts
@@ -0,0 +1,22 @@
+import { useQuery } from "@tanstack/react-query";
+import { useTimeSelection } from "../../lib/timeSelectionStore";
+import { getDevices } from "../actions/getDevices";
+import { getStartAndEndDate } from "./utils";
+
+export function useGetDevices() {
+ const { time } = useTimeSelection();
+
+ const { startDate, endDate } = getStartAndEndDate(time);
+
+ return useQuery({
+ queryKey: ["devices", time],
+ queryFn: () => {
+ const timezone = Intl.DateTimeFormat().resolvedOptions().timeZone;
+ return getDevices({
+ startDate,
+ endDate,
+ timezone,
+ });
+ },
+ });
+}
diff --git a/client/src/app/hooks/useGetOperatingSystems.ts b/client/src/app/hooks/useGetOperatingSystems.ts
new file mode 100644
index 000000000..8f9a033f6
--- /dev/null
+++ b/client/src/app/hooks/useGetOperatingSystems.ts
@@ -0,0 +1,22 @@
+import { useQuery } from "@tanstack/react-query";
+import { useTimeSelection } from "../../lib/timeSelectionStore";
+import { getStartAndEndDate } from "./utils";
+import { getOperatingSystems } from "../actions/getOperatingSystems";
+
+export function useGetOperatingSystems() {
+ const { time } = useTimeSelection();
+
+ const { startDate, endDate } = getStartAndEndDate(time);
+
+ return useQuery({
+ queryKey: ["operating-systems", time],
+ queryFn: () => {
+ const timezone = Intl.DateTimeFormat().resolvedOptions().timeZone;
+ return getOperatingSystems({
+ startDate,
+ endDate,
+ timezone,
+ });
+ },
+ });
+}
diff --git a/client/src/app/hooks/useGetPages.ts b/client/src/app/hooks/useGetPages.ts
new file mode 100644
index 000000000..088d045d6
--- /dev/null
+++ b/client/src/app/hooks/useGetPages.ts
@@ -0,0 +1,22 @@
+import { useQuery } from "@tanstack/react-query";
+import { useTimeSelection } from "../../lib/timeSelectionStore";
+import { getStartAndEndDate } from "./utils";
+import { getPages } from "../actions/getPages";
+
+export function useGetPages() {
+ const { time } = useTimeSelection();
+
+ const { startDate, endDate } = getStartAndEndDate(time);
+
+ return useQuery({
+ queryKey: ["pages", time],
+ queryFn: () => {
+ const timezone = Intl.DateTimeFormat().resolvedOptions().timeZone;
+ return getPages({
+ startDate,
+ endDate,
+ timezone,
+ });
+ },
+ });
+}
diff --git a/client/src/app/page.tsx b/client/src/app/page.tsx
index fdff35a55..7d2f89b18 100644
--- a/client/src/app/page.tsx
+++ b/client/src/app/page.tsx
@@ -1,5 +1,9 @@
import { Header } from "./components/Header/Header";
import { MainSection } from "./components/MainSection/MainSection";
+import { Browsers } from "./components/sections/Browsers/Browsers";
+import { Devices } from "./components/sections/Devices/Devices";
+import { OperatingSystems } from "./components/sections/OperatingSystems/OperatingSystems";
+import { Pages } from "./components/sections/Pages/Pages";
export default function Home() {
return (
@@ -7,6 +11,12 @@ export default function Home() {
<div className="w-full max-w-6xl">
<Header />
<MainSection />
+ <div className="grid grid-cols-2 gap-4 mt-4">
+ <OperatingSystems />
+ <Browsers />
+ <Devices />
+ <Pages />
+ </div>
</div>
</main>
);
diff --git a/client/src/lib/timeSelectionStore.ts b/client/src/lib/timeSelectionStore.ts
index e1c000d77..df71680ff 100644
--- a/client/src/lib/timeSelectionStore.ts
+++ b/client/src/lib/timeSelectionStore.ts
@@ -45,6 +45,18 @@ export const goBack = () => {
mode: "date",
date: DateTime.fromISO(time.date).minus({ days: 1 }).toISODate() ?? "",
});
+ } else if (time.mode === "range") {
+ const startDate = DateTime.fromISO(time.startDate);
+ const endDate = DateTime.fromISO(time.endDate);
+
+ const daysBetweenStartAndEnd = endDate.diff(startDate, "days").days;
+
+ setTime({
+ mode: "range",
+ startDate:
+ startDate.minus({ days: daysBetweenStartAndEnd }).toISODate() ?? "",
+ endDate: startDate.toISODate() ?? "",
+ });
}
};
@@ -60,5 +72,25 @@ export const goForward = () => {
? DateTime.now().toISODate()
: DateTime.fromISO(time.date).plus({ days: 1 }).toISODate() ?? "",
});
+ } else if (time.mode === "range") {
+ const startDate = DateTime.fromISO(time.startDate);
+ const endDate = DateTime.fromISO(time.endDate);
+ const now = DateTime.now();
+
+ const daysBetweenStartAndEnd = endDate.diff(startDate, "days").days;
+ const proposedEndDate = endDate.plus({ days: daysBetweenStartAndEnd });
+
+ // Don't allow moving forward if it would put the entire range in the future
+ if (startDate.plus({ days: daysBetweenStartAndEnd }) > now) {
+ return;
+ }
+
+ setTime({
+ mode: "range",
+ startDate:
+ startDate.plus({ days: daysBetweenStartAndEnd }).toISODate() ?? "",
+ // Cap the end date at today
+ endDate: proposedEndDate.toISODate() ?? "",
+ });
}
};