Skip to content

Open source, privacy-first web analytics. Lightweight, cookie-free Google Analytics alternative. Self-hosted or cloud.

Ignore unknown country code

Uku Tahtcd4057974078
diff --git a/config/test.exs b/config/test.exs
index f7a38be932..10d7164df3 100644
--- a/config/test.exs
+++ b/config/test.exs
@@ -27,7 +27,8 @@ config :geolix,
adapter: Geolix.Adapter.Fake,
data: %{
{1, 1, 1, 1} => %{country: %{iso_code: "US"}},
- {1, 1, 1, 1, 1, 1, 1, 1} => %{country: %{iso_code: "US"}}
+ {1, 1, 1, 1, 1, 1, 1, 1} => %{country: %{iso_code: "US"}},
+ {0, 0, 0, 0} => %{country: %{iso_code: "ZZ"}}
}
}
]
diff --git a/lib/plausible_web/controllers/api/external_controller.ex b/lib/plausible_web/controllers/api/external_controller.ex
index 6fe216206e..6c47da64e2 100644
--- a/lib/plausible_web/controllers/api/external_controller.ex
+++ b/lib/plausible_web/controllers/api/external_controller.ex
@@ -385,7 +385,10 @@ defmodule PlausibleWeb.Api.ExternalController do
PlausibleWeb.RemoteIp.get(conn)
|> Geolix.lookup()
- country_code = get_in(result, [:geolocation, :country, :iso_code])
+ country_code =
+ get_in(result, [:geolocation, :country, :iso_code])
+ |> ignore_unknown_country
+
city_geoname_id = get_in(result, [:geolocation, :city, :geoname_id])
subdivision1_code =
@@ -414,6 +417,9 @@ defmodule PlausibleWeb.Api.ExternalController do
}
end
+ defp ignore_unknown_country("ZZ"), do: nil
+ defp ignore_unknown_country(country), do: country
+
@decorate trace("ingest.parse_referrer")
defp parse_referrer(_, nil), do: nil
diff --git a/test/plausible_web/controllers/api/external_controller_test.exs b/test/plausible_web/controllers/api/external_controller_test.exs
index 3203a2b9fe..24032e5676 100644
--- a/test/plausible_web/controllers/api/external_controller_test.exs
+++ b/test/plausible_web/controllers/api/external_controller_test.exs
@@ -558,6 +558,22 @@ defmodule PlausibleWeb.Api.ExternalControllerTest do
assert pageview.country_code == "US"
end
+ test "ignores unknown country code ZZ", %{conn: conn} do
+ params = %{
+ name: "pageview",
+ domain: "external-controller-test-zz-country.com",
+ url: "http://gigride.live/"
+ }
+
+ conn
+ |> put_req_header("x-forwarded-for", "0.0.0.0")
+ |> post("/api/event", params)
+
+ pageview = get_event("external-controller-test-zz-country.com")
+
+ assert pageview.country_code == <<0, 0>>
+ end
+
test "scrubs port from x-forwarded-for", %{conn: conn} do
params = %{
name: "pageview",