Skip to content

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

Enhance user information and analytics components

Bill Yang46fdce73ddc8
diff --git a/client/src/api/analytics/userInfo.ts b/client/src/api/analytics/userInfo.ts
index 5281730cd..066cc0b7d 100644
--- a/client/src/api/analytics/userInfo.ts
+++ b/client/src/api/analytics/userInfo.ts
@@ -7,6 +7,7 @@ export type UserInfo = {
sessions: number;
country: string;
region: string;
+ city: string;
language: string;
device_type: string;
browser: string;
diff --git a/client/src/app/[site]/main/components/sections/Countries.tsx b/client/src/app/[site]/main/components/sections/Countries.tsx
index 1ab482506..52d976e9d 100644
--- a/client/src/app/[site]/main/components/sections/Countries.tsx
+++ b/client/src/app/[site]/main/components/sections/Countries.tsx
@@ -8,7 +8,7 @@ import {
TabsTrigger,
} from "../../../../../components/ui/basic-tabs";
import { Card, CardContent } from "../../../../../components/ui/card";
-import { getCountryName } from "../../../../../lib/utils";
+import { getCountryName, getLanguageName } from "../../../../../lib/utils";
import { StandardSection } from "../../../components/shared/StandardSection/StandardSection";
import { CountryFlag } from "../../../components/shared/icons/CountryFlag";
import { useSubdivisions } from "../../../../../lib/geo";
@@ -17,27 +17,6 @@ import { MapComponent } from "../../../components/shared/Map";
type Tab = "countries" | "regions" | "languages" | "cities" | "map";
-const regionNamesInEnglish = new Intl.DisplayNames(["en"], { type: "region" });
-const languageNamesInEnglish = new Intl.DisplayNames(["en"], {
- type: "language",
-});
-
-const getLanguageName = (languageCode: string) => {
- try {
- // Handle codes like "en-US" that have both language and region
- if (languageCode.includes("-")) {
- const [language, region] = languageCode.split("-");
- const languageName = languageNamesInEnglish.of(language);
- const regionName = regionNamesInEnglish.of(region);
- return `${languageName} (${regionName})`;
- }
- // Just a language code
- return languageNamesInEnglish.of(languageCode);
- } catch (error) {
- return languageCode;
- }
-};
-
// Helper to extract country code from language code
const getCountryFromLanguage = (languageCode: string): string | null => {
if (languageCode.includes("-")) {
diff --git a/client/src/app/[site]/user/[userId]/page.tsx b/client/src/app/[site]/user/[userId]/page.tsx
index 5ecf382df..18b87c1e3 100644
--- a/client/src/app/[site]/user/[userId]/page.tsx
+++ b/client/src/app/[site]/user/[userId]/page.tsx
@@ -19,7 +19,11 @@ import { useParams } from "next/navigation";
import { useUserInfo } from "../../../../api/analytics/userInfo";
import CopyText from "../../../../components/CopyText";
import { useGetRegionName } from "../../../../lib/geo";
-import { formatDuration, getCountryName } from "../../../../lib/utils";
+import {
+ formatDuration,
+ getCountryName,
+ getLanguageName,
+} from "../../../../lib/utils";
import { Browser } from "../../components/shared/icons/Browser";
import { CountryFlag } from "../../components/shared/icons/CountryFlag";
import { OperatingSystem } from "../../components/shared/icons/OperatingSystem";
@@ -55,14 +59,12 @@ export default function UserPage() {
<div className="bg-neutral-900 p-3 rounded-lg flex flex-col gap-1 border border-neutral-750 text-sm mb-3">
<div className="grid grid-cols-1 md:grid-cols-2 gap-2">
<div className="grid grid-cols-[100px_auto] gap-2">
- <span className=" text-neutral-100">User ID:</span>
+ {/* <span className=" text-neutral-100">User ID:</span>
<CopyText
text={userId as string}
maxLength={24}
className="inline-flex text-neutral-300"
- />
- <span className=" text-neutral-100">Language:</span>
- <div className="text-neutral-300">{data?.language}</div>
+ /> */}
<span className="text-neutral-100">Country:</span>
<div className="text-neutral-300 flex gap-1 items-center">
<CountryFlag country={data?.country || ""} />
@@ -72,6 +74,12 @@ export default function UserPage() {
<div className="text-neutral-300">
{data?.region ? getRegionName(data.region) : "N/A"}
</div>
+ <span className=" text-neutral-100">City:</span>
+ <div className="text-neutral-300">{data?.city ?? "N/A"}</div>
+ <span className=" text-neutral-100">Language:</span>
+ <div className="text-neutral-300">
+ {data?.language ? getLanguageName(data.language) : "N/A"}
+ </div>
</div>
<div className="grid grid-cols-[110px_1fr] gap-2">
<span className=" text-neutral-100">Device Type:</span>
diff --git a/client/src/components/Sessions/SessionDetails.tsx b/client/src/components/Sessions/SessionDetails.tsx
index 70b39528c..d94618e9c 100644
--- a/client/src/components/Sessions/SessionDetails.tsx
+++ b/client/src/components/Sessions/SessionDetails.tsx
@@ -2,7 +2,7 @@ import { DateTime } from "luxon";
import { Browser } from "../../app/[site]/components/shared/icons/Browser";
import { CountryFlag } from "../../app/[site]/components/shared/icons/CountryFlag";
import { OperatingSystem } from "../../app/[site]/components/shared/icons/OperatingSystem";
-import { getCountryName } from "../../lib/utils";
+import { getCountryName, getLanguageName } from "../../lib/utils";
import {
MonitorSmartphone,
Clock,
@@ -10,6 +10,9 @@ import {
MousePointerClick,
ArrowRight,
Loader2,
+ Smartphone,
+ Monitor,
+ Tablet,
} from "lucide-react";
import Avatar from "boring-avatars";
import {
@@ -415,7 +418,9 @@ export function SessionDetails({ session, userId }: SessionDetailsProps) {
Language:
</span>
<span className="text-gray-400">
- {sessionDetails.language}
+ {sessionDetails.language
+ ? getLanguageName(sessionDetails.language)
+ : "N/A"}
</span>
</div>
)}
@@ -449,7 +454,15 @@ export function SessionDetails({ session, userId }: SessionDetailsProps) {
Device:
</span>
<div className="flex items-center gap-1.5 text-gray-400">
- <MonitorSmartphone className="w-4 h-4 text-gray-500" />
+ {sessionDetails?.device_type === "Desktop" && (
+ <Monitor className="w-4 h-4" />
+ )}
+ {sessionDetails?.device_type === "Mobile" && (
+ <Smartphone className="w-4 h-4" />
+ )}
+ {sessionDetails?.device_type === "Tablet" && (
+ <Tablet className="w-4 h-4" />
+ )}
<span>{sessionDetails?.device_type || "Unknown"}</span>
</div>
</div>
diff --git a/client/src/lib/utils.ts b/client/src/lib/utils.ts
index b0b8564eb..a00dea859 100644
--- a/client/src/lib/utils.ts
+++ b/client/src/lib/utils.ts
@@ -50,3 +50,24 @@ export function formatDuration(seconds: number): string {
export function truncateString(str: string, n = 50) {
return str.length > n ? str.substring(0, n) + "..." : str;
}
+
+const regionNamesInEnglish = new Intl.DisplayNames(["en"], { type: "region" });
+const languageNamesInEnglish = new Intl.DisplayNames(["en"], {
+ type: "language",
+});
+
+export const getLanguageName = (languageCode: string) => {
+ try {
+ // Handle codes like "en-US" that have both language and region
+ if (languageCode.includes("-")) {
+ const [language, region] = languageCode.split("-");
+ const languageName = languageNamesInEnglish.of(language);
+ const regionName = regionNamesInEnglish.of(region);
+ return `${languageName} (${regionName})`;
+ }
+ // Just a language code
+ return languageNamesInEnglish.of(languageCode);
+ } catch (error) {
+ return languageCode;
+ }
+};
diff --git a/server/src/api/analytics/getUserInfo.ts b/server/src/api/analytics/getUserInfo.ts
index 413928d4e..d2447ea87 100644
--- a/server/src/api/analytics/getUserInfo.ts
+++ b/server/src/api/analytics/getUserInfo.ts
@@ -7,6 +7,7 @@ interface UserPageviewData {
duration: number;
country: string;
region: string;
+ city: string;
language: string;
device_type: string;
browser: string;
@@ -41,6 +42,7 @@ export async function getUserInfo(
user_id,
argMax(country, timestamp) AS country,
argMax(region, timestamp) AS region,
+ argMax(city, timestamp) AS city,
argMax(language, timestamp) AS language,
argMax(device_type, timestamp) AS device_type,
argMax(browser, timestamp) AS browser,
@@ -73,6 +75,7 @@ export async function getUserInfo(
ROUND(avg(session_duration)) AS duration,
any(country) as country,
any(region) AS region,
+ any(city) AS city,
any(language) AS language,
any(device_type) AS device_type,
any(browser) AS browser,