Skip to content

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

feat(countries): enhance country and city handling in Countries component

Bill Yang7e0720c01871
diff --git a/client/src/app/[site]/main/components/sections/Countries.tsx b/client/src/app/[site]/main/components/sections/Countries.tsx
index 66cd4c5cd..c98833349 100644
--- a/client/src/app/[site]/main/components/sections/Countries.tsx
+++ b/client/src/app/[site]/main/components/sections/Countries.tsx
@@ -17,6 +17,15 @@ import { Button } from "../../../../../components/ui/button";
type Tab = "countries" | "regions" | "languages" | "cities" | "map";
+function getCountryCity(value: string) {
+ if (value.split("-").length === 2) {
+ const [country, city] = value.split("-");
+ return { country, region: "", city };
+ }
+ const [country, region, city] = value.split("-");
+ return { country, region, city };
+}
+
// Helper to extract country code from language code
const getCountryFromLanguage = (languageCode: string): string | null => {
if (languageCode.includes("-")) {
@@ -98,14 +107,14 @@ export function Countries() {
(feature) => feature.properties.iso_3166_2 === e.value
)?.properties;
+ const countryCode = e.value.split("-")[0];
+
return (
<div className="flex gap-2 items-center">
- <CountryFlag
- country={region?.iso_3166_2.slice(0, 2) ?? ""}
- />
- {region?.iso_3166_2.slice(0, 2)}
+ <CountryFlag country={countryCode} />
+ {countryCode}
<ChevronRight className="w-4 h-4 mx-[-4px]" />
- {region?.name}
+ {region?.name ?? e.value.slice(3)}
</div>
);
}}
@@ -120,10 +129,29 @@ export function Countries() {
getValue={(e) => e.value}
getKey={(e) => e.value}
getLabel={(e) => {
- if (!e.value) {
+ if (!e.value || e.value === "-") {
return "Unknown";
}
- return <div className="flex gap-2 items-center">{e.value}</div>;
+
+ const { country, region, city } = getCountryCity(e.value) ?? {};
+
+ const region_ = subdivisions?.features.find(
+ (feature) =>
+ feature.properties.iso_3166_2 === `${country}-${region}`
+ )?.properties;
+
+ return (
+ <div className="flex gap-2 items-center">
+ <CountryFlag country={country} />
+ {country}
+ {region_?.name && (
+ <ChevronRight className="w-4 h-4 mx-[-4px]" />
+ )}
+ {region_?.name}
+ {city && <ChevronRight className="w-4 h-4 mx-[-4px]" />}
+ {city}
+ </div>
+ );
}}
expanded={expanded}
close={close}
diff --git a/server/src/api/analytics/utils.ts b/server/src/api/analytics/utils.ts
index cddea3c54..7c4ee5dc7 100644
--- a/server/src/api/analytics/utils.ts
+++ b/server/src/api/analytics/utils.ts
@@ -127,6 +127,9 @@ export const getSqlParam = (parameter: FilterParameter) => {
if (parameter === "dimensions") {
return "concat(toString(screen_width), 'x', toString(screen_height))";
}
+ if (parameter === "city") {
+ return "concat(toString(region), '-', toString(city))";
+ }
return filterParamSchema.parse(parameter);
};