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
Fix geolocation subdivision pattern matching (#2063)
Vinicius Brasilb415ebe77674
diff --git a/.gitignore b/.gitignoreindex 96ffc04198..621148da4f 100644--- a/.gitignore+++ b/.gitignore@@ -60,3 +60,6 @@ plausible-report.xml/priv/plts/*.plt.hash.env++# Geolocation databases+/priv/geodb/*.mmdbdiff --git a/CHANGELOG.md b/CHANGELOG.mdindex f9bb85835a..edd2c29321 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -37,6 +37,7 @@ All notable changes to this project will be documented in this file.- Prevent dashboard graph from being selected when long pressing on the graph in a mobile browser- The exported `pages.csv` file now includes pageviews again [plausible/analytics#1878](https://github.com/plausible/analytics/issues/1878)- Fix a bug where city, region and country filters were filtering stats but not the location list+- Fix a bug where regions were not being saved### Changed- Cache the tracking script for 24 hoursdiff --git a/config/test.exs b/config/test.exsindex 10d7164df3..a57ff19a38 100644--- a/config/test.exs+++ b/config/test.exs@@ -20,6 +20,29 @@ config :plausible,config :bamboo, :refute_timeout, 10+geolix_sample_lookup = %{+ city: %{geoname_id: 2_988_507, names: %{en: "Paris"}},+ continent: %{code: "EU", geoname_id: 6_255_148, names: %{en: "Europe"}},+ country: %{+ geoname_id: 3_017_382,+ is_in_european_union: true,+ iso_code: "FR",+ names: %{en: "France"}+ },+ ip_address: {2, 2, 2, 2},+ location: %{+ latitude: 48.8566,+ longitude: 2.35222,+ time_zone: "Europe/Paris",+ weather_code: "FRXX0076"+ },+ postal: %{code: "75000"},+ subdivisions: [+ %{geoname_id: 3_012_874, iso_code: "IDF", names: %{en: "Île-de-France"}},+ %{geoname_id: 2_968_815, iso_code: "75", names: %{en: "Paris"}}+ ]+}+config :geolix,databases: [%{@@ -27,6 +50,7 @@ config :geolix,adapter: Geolix.Adapter.Fake,data: %{{1, 1, 1, 1} => %{country: %{iso_code: "US"}},+ {2, 2, 2, 2} => geolix_sample_lookup,{1, 1, 1, 1, 1, 1, 1, 1} => %{country: %{iso_code: "US"}},{0, 0, 0, 0} => %{country: %{iso_code: "ZZ"}}}diff --git a/lib/mix/tasks/download_country_database.ex b/lib/mix/tasks/download_country_database.exindex d6e63e34fd..5a2027af7c 100644--- a/lib/mix/tasks/download_country_database.ex+++ b/lib/mix/tasks/download_country_database.ex@@ -1,4 +1,9 @@defmodule Mix.Tasks.DownloadCountryDatabase do+ @moduledoc """+ This task downloads the Country Lite database from DB-IP for self-hosted or development purposes.+ Plausible Cloud runs a paid version of DB-IP with more detailed geolocation data.+ """+use Mix.Taskuse Plausible.Reporequire Loggerdiff --git a/lib/plausible_web/controllers/api/external_controller.ex b/lib/plausible_web/controllers/api/external_controller.exindex 0c0234759b..871db3cb29 100644--- a/lib/plausible_web/controllers/api/external_controller.ex+++ b/lib/plausible_web/controllers/api/external_controller.ex@@ -481,13 +481,13 @@ defmodule PlausibleWeb.Api.ExternalController docountry_code =get_in(result, [:country, :iso_code])- |> ignore_unknown_country+ |> ignore_unknown_country()city_geoname_id = get_in(result, [:city, :geoname_id])subdivision1_code =case result do- %{geolocation: %{subdivisions: [%{iso_code: iso_code} | _rest]}} ->+ %{subdivisions: [%{iso_code: iso_code} | _rest]} ->country_code <> "-" <> iso_code_ ->@@ -496,7 +496,7 @@ defmodule PlausibleWeb.Api.ExternalController dosubdivision2_code =case result do- %{geolocation: %{subdivisions: [_first, %{iso_code: iso_code} | _rest]}} ->+ %{subdivisions: [_first, %{iso_code: iso_code} | _rest]} ->country_code <> "-" <> iso_code_ ->diff --git a/test/plausible_web/controllers/api/external_controller_test.exs b/test/plausible_web/controllers/api/external_controller_test.exsindex d13b09f904..87e497a362 100644--- a/test/plausible_web/controllers/api/external_controller_test.exs+++ b/test/plausible_web/controllers/api/external_controller_test.exs@@ -636,7 +636,7 @@ defmodule PlausibleWeb.Api.ExternalControllerTest doend# Fake data is set up in config/test.exs- test "looks up the country from the ip address", %{conn: conn} do+ test "looks up location data from the ip address", %{conn: conn} doparams = %{name: "pageview",domain: "external-controller-test-20.com",@@ -644,12 +644,15 @@ defmodule PlausibleWeb.Api.ExternalControllerTest do}conn- |> put_req_header("x-forwarded-for", "1.1.1.1")+ |> put_req_header("x-forwarded-for", "2.2.2.2")|> post("/api/event", params)pageview = get_event("external-controller-test-20.com")- assert pageview.country_code == "US"+ assert pageview.country_code == "FR"+ assert pageview.subdivision1_code == "FR-IDF"+ assert pageview.subdivision2_code == "FR-75"+ assert pageview.city_geoname_id == 2_988_507endtest "ignores unknown country code ZZ", %{conn: conn} do