🐸 Rybbit - open-source and privacy-friendly alternative to Google Analytics that is 10x more intuitive.
Enhance session location data and tooltip display with country information
Bill Yang94ee96615196
diff --git a/client/src/api/analytics/useGetSessionLocations.ts b/client/src/api/analytics/useGetSessionLocations.tsindex 60d9879e1..b4324b0d1 100644--- a/client/src/api/analytics/useGetSessionLocations.ts+++ b/client/src/api/analytics/useGetSessionLocations.ts@@ -7,6 +7,7 @@ export type LiveSessionLocation = {lon: number;count: number;city: string;+ country: string;};export function useGetSessionLocations() {diff --git a/client/src/app/[site]/globe/hooks/useCoordinatesLayer.ts b/client/src/app/[site]/globe/hooks/useCoordinatesLayer.tsindex e956aca62..f3b684537 100644--- a/client/src/app/[site]/globe/hooks/useCoordinatesLayer.ts+++ b/client/src/app/[site]/globe/hooks/useCoordinatesLayer.ts@@ -4,6 +4,7 @@ import mapboxgl from "mapbox-gl";import { useEffect, useRef } from "react";import { useGetSessionLocations } from "../../../../api/analytics/useGetSessionLocations";import { addFilter, removeFilter, useStore } from "../../../../lib/store";+import { renderCountryFlag } from "../utils/renderCountryFlag";const getSizeMultiplier = (total: number) => {if (total <= 50) return 3; // Large dots@@ -83,6 +84,7 @@ export function useCoordinatesLayer({properties: {count: location.count,city: location.city,+ country: location.country,isFiltered,lat: roundedLat,lon: roundedLon,@@ -156,10 +158,14 @@ export function useCoordinatesLayer({const city = feature.properties?.city || "Unknown";const count = feature.properties?.count || 0;+ const countryCode = feature.properties?.country?.length === 2 ? feature.properties?.country : "";+ const flagSVG = renderCountryFlag(countryCode);+const coordinates = e.lngLat;const html = `<div class="bg-neutral-850 border border-neutral-750 rounded-lg p-2">- <div class="flex items-center gap-1 mb-1">+ <div class="flex items-center gap-2 mb-1">+ ${flagSVG}<span class="text-sm font-medium text-white">${city}</span></div><div class="text-sm">diff --git a/client/src/app/[site]/globe/hooks/useCountriesLayer.ts b/client/src/app/[site]/globe/hooks/useCountriesLayer.tsindex 01009c597..4ae4e8039 100644--- a/client/src/app/[site]/globe/hooks/useCountriesLayer.ts+++ b/client/src/app/[site]/globe/hooks/useCountriesLayer.ts@@ -6,6 +6,7 @@ import { useSingleCol } from "../../../../api/analytics/useSingleCol";import { useCountries } from "../../../../lib/geo";import { processCountryData } from "../utils/processData";import { createColorScale } from "../utils/colorScale";+import { renderCountryFlag } from "../utils/renderCountryFlag";interface UseCountriesLayerProps {map: React.RefObject<mapboxgl.Map | null>;@@ -94,11 +95,14 @@ export function useCountriesLayer({ map, mapLoaded, mapView }: UseCountriesLayerconst foundData = currentData?.find((d: any) => d.value === code);const percentage = foundData?.percentage || 0;+ const flagSVG = renderCountryFlag(code);+// Use Mapbox native popupconst coordinates = e.lngLat;const html = `- <div class="bg-neutral-850 border border-neutral-700 rounded-lg p-3">- <div class="flex items-center gap-1 mb-1">+ <div class="bg-neutral-850 border border-neutral-700 rounded-lg p-2">+ <div class="flex items-center gap-2 mb-1">+ ${flagSVG}<span class="text-sm font-medium text-white">${name}</span></div><div class="text-sm">diff --git a/client/src/app/[site]/globe/hooks/useSubdivisionsLayer.ts b/client/src/app/[site]/globe/hooks/useSubdivisionsLayer.tsindex 2c7ef990b..f09dbb5f0 100644--- a/client/src/app/[site]/globe/hooks/useSubdivisionsLayer.ts+++ b/client/src/app/[site]/globe/hooks/useSubdivisionsLayer.ts@@ -6,6 +6,7 @@ import { useSingleCol } from "../../../../api/analytics/useSingleCol";import { useSubdivisions } from "../../../../lib/geo";import { processSubdivisionData } from "../utils/processData";import { createColorScale } from "../utils/colorScale";+import { renderCountryFlag } from "../utils/renderCountryFlag";interface UseSubdivisionsLayerProps {map: React.RefObject<mapboxgl.Map | null>;@@ -94,10 +95,15 @@ export function useSubdivisionsLayer({ map, mapLoaded, mapView }: UseSubdivisionconst foundData = currentData?.find((d: any) => d.value === code);const percentage = foundData?.percentage || 0;+ // Extract country code from iso_3166_2 (e.g., "US-CA" -> "US")+ const countryCode = code?.split("-")[0] || "";+ const flagSVG = renderCountryFlag(countryCode);+const coordinates = e.lngLat;const html = `- <div class="bg-neutral-850 border border-neutral-700 rounded-lg p-3">- <div class="flex items-center gap-1 mb-1">+ <div class="bg-neutral-850 border border-neutral-700 rounded-lg p-2">+ <div class="flex items-center gap-2 mb-1">+ ${flagSVG}<span class="text-sm font-medium text-white">${name}</span></div><div class="text-sm">diff --git a/client/src/app/[site]/globe/utils/renderCountryFlag.ts b/client/src/app/[site]/globe/utils/renderCountryFlag.tsnew file mode 100644index 000000000..ebef48869--- /dev/null+++ b/client/src/app/[site]/globe/utils/renderCountryFlag.ts@@ -0,0 +1,13 @@+import { createElement } from "react";+import * as CountryFlags from "country-flag-icons/react/3x2";+// @ts-ignore - React 19 has built-in types+import { renderToStaticMarkup } from "react-dom/server";++// Render country flag to static SVG+export function renderCountryFlag(countryCode: string): string {+ if (!countryCode || countryCode.length !== 2) return "";+ const FlagComponent = CountryFlags[countryCode.toUpperCase() as keyof typeof CountryFlags];+ if (!FlagComponent) return "";+ const flagElement = createElement(FlagComponent, { className: "w-5 h-3 inline-block" });+ return renderToStaticMarkup(flagElement);+}diff --git a/server/src/api/analytics/getSessionLocations.ts b/server/src/api/analytics/getSessionLocations.tsindex 6a1b5bfd6..3d35ecd4c 100644--- a/server/src/api/analytics/getSessionLocations.ts+++ b/server/src/api/analytics/getSessionLocations.ts@@ -24,7 +24,8 @@ WITH stuff AS (session_id,any(lat) AS lat,any(lon) AS lon,- any(city) AS city+ any(city) AS city,+ any(country) AS countryFROMeventsWHERE@@ -38,13 +39,15 @@ SELECTlat,lon,city,+ country,count() as countfromstuffGROUP BYlat,lon,- city`,+ city,+ country`,query_params: {site,},