Skip to content

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

Enhance user session analytics with additional location and device details

Bill Yang8d9e3c39bbbf
diff --git a/client/src/api/analytics/userSessions.ts b/client/src/api/analytics/userSessions.ts
index e4cebf0c3..eedcf508a 100644
--- a/client/src/api/analytics/userSessions.ts
+++ b/client/src/api/analytics/userSessions.ts
@@ -8,43 +8,6 @@ import {
import { APIResponse } from "../types";
import { authedFetch, getQueryParams } from "../utils";
-export type UserSessionsResponse = {
- session_id: string;
- browser: string;
- operating_system: string;
- device_type: string;
- country: string;
- firstTimestamp: string;
- lastTimestamp: string;
- duration: number; // Duration in seconds
- pageviews: {
- pathname: string;
- querystring: string;
- title: string;
- timestamp: string;
- referrer: string;
- }[];
-}[];
-
-export function useGetUserSessions(userId: string) {
- const { time, site, filters } = useStore();
- const timeParams = getQueryParams(time);
-
- return useQuery({
- queryKey: ["user-sessions", userId, time, site, filters],
- queryFn: () => {
- return authedFetch<UserSessionsResponse>(
- `/user/${userId}/sessions/${site}`,
- {
- ...timeParams,
- filters,
- }
- );
- },
- staleTime: Infinity,
- });
-}
-
export type GetSessionsResponse = {
session_id: string;
user_id: string;
@@ -54,7 +17,11 @@ export type GetSessionsResponse = {
language: string;
device_type: string;
browser: string;
+ browser_version: string;
operating_system: string;
+ operating_system_version: string;
+ screen_width: number;
+ screen_height: number;
referrer: string;
channel: string;
utm_source: string;
@@ -128,6 +95,7 @@ export interface SessionDetails {
user_id: string;
country: string;
region: string;
+ city: string;
language: string;
device_type: string;
browser: string;
diff --git a/client/src/components/Sessions/SessionCard.tsx b/client/src/components/Sessions/SessionCard.tsx
index 70a690dd1..ab4ac5601 100644
--- a/client/src/components/Sessions/SessionCard.tsx
+++ b/client/src/components/Sessions/SessionCard.tsx
@@ -24,6 +24,7 @@ import { formatDuration } from "../../lib/dateTimeUtils";
import { Badge } from "../ui/badge";
import { SessionDetails } from "./SessionDetails";
import { userLocale, hour12 } from "../../lib/dateTimeUtils";
+import { useGetRegionName } from "../../lib/geo";
interface SessionCardProps {
session: GetSessionsResponse[number];
@@ -52,6 +53,8 @@ function truncatePath(path: string, maxLength: number = 32) {
}
export function SessionCard({ session, onClick, userId }: SessionCardProps) {
+ const { getRegionName } = useGetRegionName();
+
const [expanded, setExpanded] = useState(false);
// Calculate session duration in minutes
@@ -71,6 +74,20 @@ export function SessionCard({ session, onClick, userId }: SessionCardProps) {
}
};
+ const getFullLocation = (session: GetSessionsResponse[number]) => {
+ let location = "";
+ if (session.city) {
+ location += `${session.city}, `;
+ }
+ if (getRegionName(session.region)) {
+ location += `${getRegionName(session.region)}, `;
+ }
+ if (session.country) {
+ location += getCountryName(session.country);
+ }
+ return location;
+ };
+
return (
<div className="mb-3 rounded-lg bg-neutral-900 border border-neutral-800 overflow-hidden">
<div className="p-3 cursor-pointer" onClick={handleCardClick}>
@@ -92,7 +109,7 @@ export function SessionCard({ session, onClick, userId }: SessionCardProps) {
</div>
</TooltipTrigger>
<TooltipContent>
- <p>{getCountryName(session.country)}</p>
+ <p>{getFullLocation(session)}</p>
</TooltipContent>
</Tooltip>
)}
@@ -105,7 +122,10 @@ export function SessionCard({ session, onClick, userId }: SessionCardProps) {
</div>
</TooltipTrigger>
<TooltipContent>
- <p>{session.browser || "Unknown browser"}</p>
+ <p>
+ {session.browser || "Unknown browser"}
+ {session.browser_version && ` ${session.browser_version}`}
+ </p>
</TooltipContent>
</Tooltip>
@@ -117,7 +137,11 @@ export function SessionCard({ session, onClick, userId }: SessionCardProps) {
</div>
</TooltipTrigger>
<TooltipContent>
- <p>{session.operating_system || "Unknown OS"}</p>
+ <p>
+ {session.operating_system || "Unknown OS"}
+ {session.operating_system_version &&
+ ` ${session.operating_system_version}`}
+ </p>
</TooltipContent>
</Tooltip>
@@ -129,7 +153,12 @@ export function SessionCard({ session, onClick, userId }: SessionCardProps) {
</div>
</TooltipTrigger>
<TooltipContent>
- <p>{session.device_type || "Unknown device"}</p>
+ <p>
+ {session.device_type || "Unknown device"}
+ {session.screen_width &&
+ session.screen_height &&
+ ` ${session.screen_width}x${session.screen_height}`}
+ </p>
</TooltipContent>
</Tooltip>
diff --git a/client/src/components/Sessions/SessionDetails.tsx b/client/src/components/Sessions/SessionDetails.tsx
index 016e6a393..6749c3d23 100644
--- a/client/src/components/Sessions/SessionDetails.tsx
+++ b/client/src/components/Sessions/SessionDetails.tsx
@@ -30,6 +30,7 @@ import { cn, getCountryName, getLanguageName } from "../../lib/utils";
import { formatDuration } from "../../lib/dateTimeUtils";
import { Button } from "../ui/button";
import { hour12 } from "../../lib/dateTimeUtils";
+import { useGetRegionName } from "../../lib/geo";
// Component to display a single pageview or event
function PageviewItem({
@@ -255,6 +256,8 @@ export function SessionDetails({ session, userId }: SessionDetailsProps) {
return allEvents.filter((p: SessionEvent) => p.type !== "pageview").length;
}, [allEvents]);
+ const { getRegionName } = useGetRegionName();
+
return (
<div className="px-4 bg-neutral-900 border-t border-neutral-800">
{isLoading ? (
@@ -425,7 +428,7 @@ export function SessionDetails({ session, userId }: SessionDetailsProps) {
{sessionDetails?.country && (
<div className="flex items-center gap-2 text-sm">
<span className="font-medium text-gray-300 min-w-[80px]">
- Location:
+ Country:
</span>
<div className="flex items-center gap-1 text-gray-400">
<CountryFlag country={sessionDetails.country} />
@@ -436,6 +439,27 @@ export function SessionDetails({ session, userId }: SessionDetailsProps) {
</div>
</div>
)}
+ {sessionDetails?.region &&
+ getRegionName(sessionDetails.region) && (
+ <div className="flex items-center gap-2 text-sm">
+ <span className="font-medium text-gray-300 min-w-[80px]">
+ Region:
+ </span>
+ <span className="text-gray-400">
+ {getRegionName(sessionDetails.region)}
+ </span>
+ </div>
+ )}
+ {sessionDetails?.city && (
+ <div className="flex items-center gap-2 text-sm">
+ <span className="font-medium text-gray-300 min-w-[80px]">
+ City:
+ </span>
+ <span className="text-gray-400">
+ {sessionDetails.city}
+ </span>
+ </div>
+ )}
</div>
</div>
</div>
diff --git a/client/src/lib/geo.ts b/client/src/lib/geo.ts
index 20fb6aa8a..dd79fa291 100644
--- a/client/src/lib/geo.ts
+++ b/client/src/lib/geo.ts
@@ -56,9 +56,11 @@ export const useGetRegionName = () => {
return {
getRegionName: (region: string) => {
- return subdivisions?.features.find(
- (feature) => feature.properties.iso_3166_2 === region
- )?.properties.name;
+ return (
+ subdivisions?.features.find(
+ (feature) => feature.properties.iso_3166_2 === region
+ )?.properties.name ?? ""
+ );
},
};
};
diff --git a/server/src/api/analytics/getSession.ts b/server/src/api/analytics/getSession.ts
index caabbc01d..1a33040c6 100644
--- a/server/src/api/analytics/getSession.ts
+++ b/server/src/api/analytics/getSession.ts
@@ -8,6 +8,7 @@ export interface SessionDetails {
user_id: string;
country: string;
region: string;
+ city: string;
language: string;
device_type: string;
browser: string;
@@ -91,6 +92,7 @@ SELECT
any(user_id) as user_id,
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,
diff --git a/server/src/api/analytics/getSessions.ts b/server/src/api/analytics/getSessions.ts
index a1102de5a..378bc1947 100644
--- a/server/src/api/analytics/getSessions.ts
+++ b/server/src/api/analytics/getSessions.ts
@@ -17,7 +17,11 @@ export type GetSessionsResponse = {
language: string;
device_type: string;
browser: string;
+ browser_version: string;
operating_system: string;
+ operating_system_version: string;
+ screen_width: number;
+ screen_height: number;
referrer: string;
channel: string;
utm_source: string;
@@ -69,7 +73,9 @@ export async function getSessions(
argMax(language, timestamp) AS language,
argMax(device_type, timestamp) AS device_type,
argMax(browser, timestamp) AS browser,
+ argMax(browser_version, timestamp) AS browser_version,
argMax(operating_system, timestamp) AS operating_system,
+ argMax(operating_system_version, timestamp) AS operating_system_version,
argMax(screen_width, timestamp) AS screen_width,
argMax(screen_height, timestamp) AS screen_height,
argMin(referrer, timestamp) AS referrer,