Open source, privacy-first web analytics. Lightweight, cookie-free Google Analytics alternative. Self-hosted or cloud.
TypeScript analyticsanalytics-dashboardclickhouseelixirgoogle-analyticsmarketingopen-source-analyticsphoenixplausible-analyticspostgresqlprivacyprivacy-friendlyself-hostedsimple-analyticsstatisticstailwindcssweb-analyticswebsite-analyticswebsite-statswebsite-tracking
Use new location library
Uku Taht442e401edee4
diff --git a/assets/js/dashboard/router.js b/assets/js/dashboard/router.jsindex 0e7ec77ef2..84df38d8ae 100644--- a/assets/js/dashboard/router.js+++ b/assets/js/dashboard/router.js@@ -55,7 +55,7 @@ export default function Router({site, loggedIn, currentUserRole}) {<ModalTable title="Top regions" site={site} endpoint={`/api/stats/${encodeURIComponent(site.domain)}/regions`} filter={{region: 'code', region_name: 'name'}} keyLabel="Region" renderIcon={renderRegionIcon} /></Route><Route path="/:domain/cities">- <ModalTable title="Top cities" site={site} endpoint={`/api/stats/${encodeURIComponent(site.domain)}/cities`} filter={{city: 'code', city_name: 'name'}} keyLabel="City" />+ <ModalTable title="Top cities" site={site} endpoint={`/api/stats/${encodeURIComponent(site.domain)}/cities`} filter={{city: 'code', city_name: 'name'}} keyLabel="City" renderIcon={renderCityIcon} /></Route><Route path={["/:domain/filter/:field"]}><FilterModal site={site} />@@ -66,6 +66,10 @@ export default function Router({site, loggedIn, currentUserRole}) {);}+function renderCityIcon(city) {+ return <span className="mr-1">{city.country_flag}</span>+}+function renderCountryIcon(country) {return <span className="mr-1">{country.flag}</span>}diff --git a/assets/js/dashboard/stats/locations/index.js b/assets/js/dashboard/stats/locations/index.jsindex ec6c228390..2327d95075 100644--- a/assets/js/dashboard/stats/locations/index.js+++ b/assets/js/dashboard/stats/locations/index.js@@ -58,6 +58,10 @@ function Cities({query, site}) {return api.get(apiPath(site, '/cities'), query, {limit: 9})}+ function renderIcon(city) {+ return site.cities && <span className="mr-1">{city.country_flag}</span>+ }+return (<ListReportfetchData={fetchData}@@ -65,6 +69,7 @@ function Cities({query, site}) {keyLabel="City"detailsLink={sitePath(site, '/cities')}query={query}+ renderIcon={renderIcon}color="bg-orange-50"/>)diff --git a/lib/plausible/application.ex b/lib/plausible/application.exindex 044c83de02..5e7f312853 100644--- a/lib/plausible/application.ex+++ b/lib/plausible/application.ex@@ -24,6 +24,7 @@ defmodule Plausible.Application dosetup_opentelemetry()setup_sentry()setup_cache_stats()+ Location.load_all()Application.put_env(:plausible, :server_start, Timex.now())Supervisor.start_link(children, opts)enddiff --git a/lib/plausible_web/controllers/api/stats_controller.ex b/lib/plausible_web/controllers/api/stats_controller.exindex ad42b284f4..a04596a355 100644--- a/lib/plausible_web/controllers/api/stats_controller.ex+++ b/lib/plausible_web/controllers/api/stats_controller.ex@@ -494,8 +494,8 @@ defmodule PlausibleWeb.Api.StatsController docountries =countries|> Enum.map(fn country ->- country_iso_info = ISOCodes.ISO3166_1.get(country["code"])- Map.put(country, "name", country_iso_info.name)+ country_info = Location.get_country(country["code"])+ Map.put(country, "name", country_info.name)end)if Map.has_key?(query.filters, "event:goal") do@@ -508,7 +508,7 @@ defmodule PlausibleWeb.Api.StatsController doelsecountries =Enum.map(countries, fn row ->- country = ISOCodes.ISO3166_1.get(row["code"])+ country = Location.get_country(row["code"])Map.merge(row, %{"name" => country.name,@@ -530,10 +530,8 @@ defmodule PlausibleWeb.Api.StatsController doStats.breakdown(site, query, "visit:region", ["visitors"], pagination)|> transform_keys(%{"region" => "code"})|> Enum.map(fn region ->- country_code = region["code"] |> String.split("-") |> List.first()-- region_entry = ISOCodes.ISO3166_2.get(region["code"])- country_entry = ISOCodes.ISO3166_1.get(country_code)+ region_entry = Location.get_subdivision(region["code"])+ country_entry = Location.get_country(region_entry.country_code)Map.merge(region, %{"name" => region_entry.name, "country_flag" => country_entry.flag})end)@@ -549,8 +547,18 @@ defmodule PlausibleWeb.Api.StatsController doStats.breakdown(site, query, "visit:city", ["visitors"], pagination)|> transform_keys(%{"city" => "code"})|> Enum.map(fn city ->- name = Stats.CountryName.from_geoname_id(city["code"], "N/A")- Map.put(city, "name", name)+ city_info = Location.get_city(city["code"])++ if city_info do+ country_info = Location.get_country(city_info.country_code)++ Map.merge(city, %{+ "name" => city_info.name,+ "country_flag" => country_info.flag+ })+ else+ Map.merge(city, %{"name" => "N/A"})+ endend)json(conn, cities)diff --git a/mix.exs b/mix.exsindex f8cb2e03a3..6e65f2c39a 100644--- a/mix.exs+++ b/mix.exs@@ -85,7 +85,7 @@ defmodule Plausible.MixProject do{:sshex, "2.2.1"},{:geolix, "~> 1.0"},{:clickhouse_ecto, git: "https://github.com/plausible/clickhouse_ecto.git"},- {:iso_codes, git: "https://github.com/plausible/iso_codes"},+ {:location, git: "https://github.com/plausible/location.git"},{:geolix_adapter_mmdb2, "~> 0.5.0"},{:cachex, "~> 3.4"},{:dialyxir, "~> 1.0", only: [:dev, :test], runtime: false},diff --git a/mix.lock b/mix.lockindex b76bf89c16..8719d22357 100644--- a/mix.lock+++ b/mix.lock@@ -51,10 +51,10 @@"httpoison": {:hex, :httpoison, "1.8.0", "6b85dea15820b7804ef607ff78406ab449dd78bed923a49c7160e1886e987a3d", [:mix], [{:hackney, "~> 1.17", [hex: :hackney, repo: "hexpm", optional: false]}], "hexpm", "28089eaa98cf90c66265b6b5ad87c59a3729bea2e74e9d08f9b51eb9729b3c3a"},"hut": {:hex, :hut, "1.3.0", "71f2f054e657c03f959cf1acc43f436ea87580696528ca2a55c8afb1b06c85e7", [:"erlang.mk", :rebar, :rebar3], [], "hexpm", "7e15d28555d8a1f2b5a3a931ec120af0753e4853a4c66053db354f35bf9ab563"},"idna": {:hex, :idna, "6.1.1", "8a63070e9f7d0c62eb9d9fcb360a7de382448200fbbd1b106cc96d3d8099df8d", [:rebar3], [{:unicode_util_compat, "~>0.7.0", [hex: :unicode_util_compat, repo: "hexpm", optional: false]}], "hexpm", "92376eb7894412ed19ac475e4a86f7b413c1b9fbb5bd16dccd57934157944cea"},- "iso_codes": {:git, "https://github.com/plausible/iso_codes", "413312b01cbdbd9a8283deb94f91bca12830eba4", []},"jason": {:hex, :jason, "1.2.2", "ba43e3f2709fd1aa1dce90aaabfd039d000469c05c56f0b8e31978e03fa39052", [:mix], [{:decimal, "~> 1.0 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "18a228f5f0058ee183f29f9eae0805c6e59d61c3b006760668d8d18ff0d12179"},"jumper": {:hex, :jumper, "1.0.1", "3c00542ef1a83532b72269fab9f0f0c82bf23a35e27d278bfd9ed0865cecabff", [:mix], [], "hexpm", "318c59078ac220e966d27af3646026db9b5a5e6703cb2aa3e26bcfaba65b7433"},"kaffy": {:hex, :kaffy, "0.9.0", "bef34c9729f6a3af4d0dea8eede8bcb9e11371a83ac9a8b393991bce81839517", [:mix], [{:ecto, "~> 3.0", [hex: :ecto, repo: "hexpm", optional: false]}, {:phoenix, "~> 1.4", [hex: :phoenix, repo: "hexpm", optional: false]}, {:phoenix_html, "~> 2.11", [hex: :phoenix_html, repo: "hexpm", optional: false]}], "hexpm", "d18ff57b8e68feb433aed11e71510cd357abc7034e75358af5deff7d0d4c6ed3"},+ "location": {:git, "https://github.com/plausible/location.git", "07a628bd6e698f674aa034409fb813505e61b207", []},"metrics": {:hex, :metrics, "1.0.1", "25f094dea2cda98213cecc3aeff09e940299d950904393b2a29d191c346a8486", [:rebar3], [], "hexpm", "69b09adddc4f74a40716ae54d140f93beb0fb8978d8636eaded0c31b6f099f16"},"mime": {:hex, :mime, "1.6.0", "dabde576a497cef4bbdd60aceee8160e02a6c89250d6c0b29e56c0dfb00db3d2", [:mix], [], "hexpm", "31a1a8613f8321143dde1dafc36006a17d28d02bdfecb9e95a880fa7aabd19a7"},"mimerl": {:hex, :mimerl, "1.2.0", "67e2d3f571088d5cfd3e550c383094b47159f3eee8ffa08e64106cdf5e981be3", [:rebar3], [], "hexpm", "f278585650aa581986264638ebf698f8bb19df297f66ad91b18910dfc6e19323"},