Skip to content

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

add countries

Bill Yanga5d5dd34de75
diff --git a/client/package-lock.json b/client/package-lock.json
index c63b4dc32..3c8a72f5a 100644
--- a/client/package-lock.json
+++ b/client/package-lock.json
@@ -17,10 +17,13 @@
"@radix-ui/react-slot": "^1.1.1",
"@tanstack/react-query": "^5.64.2",
"@tanstack/react-query-devtools": "^5.64.2",
+ "@types/country-flag-icons": "^1.2.2",
"@types/luxon": "^3.4.2",
"@types/pg": "^8.11.11",
"class-variance-authority": "^0.7.1",
"clsx": "^2.1.1",
+ "countries-list": "^3.1.1",
+ "country-flag-icons": "^1.5.14",
"date-fns": "^4.1.0",
"drizzle-orm": "^0.38.4",
"lucide-react": "^0.474.0",
@@ -2209,6 +2212,11 @@
"react": "^18 || ^19"
}
},
+ "node_modules/@types/country-flag-icons": {
+ "version": "1.2.2",
+ "resolved": "https://registry.npmjs.org/@types/country-flag-icons/-/country-flag-icons-1.2.2.tgz",
+ "integrity": "sha512-CefEn/J336TBDp7NX8JqzlDtCBOsm8M3r1Li0gEOt0HOMHF1XemNyrx9lSHjsafcb1yYWybU0N8ZAXuyCaND0w=="
+ },
"node_modules/@types/d3-array": {
"version": "3.2.1",
"resolved": "https://registry.npmjs.org/@types/d3-array/-/d3-array-3.2.1.tgz",
@@ -2610,6 +2618,16 @@
"node": ">= 6"
}
},
+ "node_modules/countries-list": {
+ "version": "3.1.1",
+ "resolved": "https://registry.npmjs.org/countries-list/-/countries-list-3.1.1.tgz",
+ "integrity": "sha512-nPklKJ5qtmY5MdBKw1NiBAoyx5Sa7p2yPpljZyQ7gyCN1m+eMFs9I6CT37Mxt8zvR5L3VzD3DJBE4WQzX3WF4A=="
+ },
+ "node_modules/country-flag-icons": {
+ "version": "1.5.14",
+ "resolved": "https://registry.npmjs.org/country-flag-icons/-/country-flag-icons-1.5.14.tgz",
+ "integrity": "sha512-GAFsVzHDu3bdAhbQ1LwBRqk/Ad8+ZzS5zU49P+lRla0KGy/V1V8ywNa1SxBOAmI/lyEOT9dfH3Q++q1lqJlvBA=="
+ },
"node_modules/cross-spawn": {
"version": "7.0.6",
"resolved": "https://registry.npmjs.org/cross-spawn/-/cross-spawn-7.0.6.tgz",
diff --git a/client/package.json b/client/package.json
index 8ee7effc6..2b4556d31 100644
--- a/client/package.json
+++ b/client/package.json
@@ -18,10 +18,13 @@
"@radix-ui/react-slot": "^1.1.1",
"@tanstack/react-query": "^5.64.2",
"@tanstack/react-query-devtools": "^5.64.2",
+ "@types/country-flag-icons": "^1.2.2",
"@types/luxon": "^3.4.2",
"@types/pg": "^8.11.11",
"class-variance-authority": "^0.7.1",
"clsx": "^2.1.1",
+ "countries-list": "^3.1.1",
+ "country-flag-icons": "^1.5.14",
"date-fns": "^4.1.0",
"drizzle-orm": "^0.38.4",
"lucide-react": "^0.474.0",
diff --git a/client/src/app/actions/getCountries.ts b/client/src/app/actions/getCountries.ts
new file mode 100644
index 000000000..10ef83828
--- /dev/null
+++ b/client/src/app/actions/getCountries.ts
@@ -0,0 +1,44 @@
+"use server";
+
+import { clickhouse } from "@/lib/clickhouse";
+import { getTimeStatement } from "./utils";
+
+type GetCountriesResponse = {
+ country: string;
+ count: number;
+ percentage: number;
+}[];
+
+export async function getCountries({
+ startDate,
+ endDate,
+ timezone = "America/Los_Angeles",
+}: {
+ startDate: string;
+ endDate: string;
+ timezone: string;
+}): Promise<{ data?: GetCountriesResponse; error?: string }> {
+ const query = `
+ SELECT
+ country,
+ COUNT(*) as count,
+ ROUND(COUNT(*) * 100.0 / SUM(COUNT(*)) OVER (), 2) as percentage
+ FROM pageviews
+ WHERE
+ ${getTimeStatement(startDate, endDate, timezone)}
+ GROUP BY country ORDER BY count desc;
+ `;
+
+ try {
+ const result = await clickhouse.query({
+ query,
+ format: "JSONEachRow",
+ });
+
+ const data: GetCountriesResponse = await result.json();
+ return { data };
+ } catch (error) {
+ console.error("Error fetching countries:", error);
+ return { error: "Failed to fetch countries" };
+ }
+}
diff --git a/client/src/app/components/sections/Countries/Countries.tsx b/client/src/app/components/sections/Countries/Countries.tsx
new file mode 100644
index 000000000..23bb98903
--- /dev/null
+++ b/client/src/app/components/sections/Countries/Countries.tsx
@@ -0,0 +1,40 @@
+"use client";
+
+import React from "react";
+import { useGetCountries } from "../../../hooks/useGetCountries";
+import { StandardCard } from "../../shared/StandardCard";
+import { countries } from "countries-list";
+import * as CountryFlags from "country-flag-icons/react/3x2";
+
+export function Countries() {
+ const { data, isLoading } = useGetCountries();
+
+ return (
+ <StandardCard
+ title="Countries"
+ data={data}
+ getKey={(e) => e.country}
+ getLabel={(e) => (
+ <span className="flex items-center gap-2">
+ {e.country && countries[e.country as keyof typeof countries] ? (
+ <>
+ {CountryFlags[e.country as keyof typeof CountryFlags]
+ ? React.createElement(
+ CountryFlags[e.country as keyof typeof CountryFlags],
+ {
+ title:
+ countries[e.country as keyof typeof countries].name,
+ className: "w-5",
+ }
+ )
+ : null}
+ {countries[e.country as keyof typeof countries].name}
+ </>
+ ) : (
+ "Unknown"
+ )}
+ </span>
+ )}
+ />
+ );
+}
diff --git a/client/src/app/components/shared/StandardCard.tsx b/client/src/app/components/shared/StandardCard.tsx
index 76e1deea1..199ed15bf 100644
--- a/client/src/app/components/shared/StandardCard.tsx
+++ b/client/src/app/components/shared/StandardCard.tsx
@@ -1,5 +1,6 @@
"use client";
+import { ReactNode } from "react";
import { Button } from "../../../components/ui/button";
import {
Card,
@@ -30,7 +31,7 @@ export function StandardCard<T extends { percentage: number; count: number }>({
}
| undefined;
getKey: (item: T) => string;
- getLabel: (item: T) => string;
+ getLabel: (item: T) => ReactNode;
}) {
return (
<Card>
@@ -47,7 +48,7 @@ export function StandardCard<T extends { percentage: number; count: number }>({
<div className="relative z-10 ml-1 flex justify-between items-center">
<div>{getLabel(e)}</div>
<div className="text-sm flex">
- <div>{e.count}</div>
+ <div>{e.count.toLocaleString()}</div>
<div className="ml-1 text-neutral-500">|</div>
<div className="ml-1 w-10 text-neutral-500">
{e.percentage}%
@@ -75,7 +76,7 @@ export function StandardCard<T extends { percentage: number; count: number }>({
<div className="relative z-10 ml-1 flex justify-between items-center pr-3">
<div>{getLabel(e)}</div>
<div className="text-sm flex">
- <div>{e.count}</div>
+ <div>{e.count.toLocaleString()}</div>
<div className="ml-1 text-neutral-500">|</div>
<div className="ml-1 w-10 text-neutral-500">
{e.percentage}%
diff --git a/client/src/app/hooks/useGetCountries.ts b/client/src/app/hooks/useGetCountries.ts
new file mode 100644
index 000000000..e72296f50
--- /dev/null
+++ b/client/src/app/hooks/useGetCountries.ts
@@ -0,0 +1,22 @@
+import { useQuery } from "@tanstack/react-query";
+import { useTimeSelection } from "../../lib/timeSelectionStore";
+import { getCountries } from "../actions/getCountries";
+import { getStartAndEndDate } from "./utils";
+
+export function useGetCountries() {
+ const { time } = useTimeSelection();
+
+ const { startDate, endDate } = getStartAndEndDate(time);
+
+ return useQuery({
+ queryKey: ["countries", time],
+ queryFn: () => {
+ const timezone = Intl.DateTimeFormat().resolvedOptions().timeZone;
+ return getCountries({
+ startDate,
+ endDate,
+ timezone,
+ });
+ },
+ });
+}
diff --git a/client/src/app/page.tsx b/client/src/app/page.tsx
index 15ed653b3..e33e7db3b 100644
--- a/client/src/app/page.tsx
+++ b/client/src/app/page.tsx
@@ -1,6 +1,7 @@
import { Header } from "./components/Header/Header";
import { MainSection } from "./components/MainSection/MainSection";
import { Browsers } from "./components/sections/Browsers/Browsers";
+import { Countries } from "./components/sections/Countries/Countries";
import { Devices } from "./components/sections/Devices/Devices";
import { OperatingSystems } from "./components/sections/OperatingSystems/OperatingSystems";
import { Pages } from "./components/sections/Pages/Pages";
@@ -18,6 +19,7 @@ export default function Home() {
<Devices />
<Pages />
<Referrers />
+ <Countries />
</div>
</div>
</main>