Ignore unknown country code

This commit is contained in:
Uku Taht 2022-01-18 10:23:26 -06:00
parent 81eb2b73df
commit cd40579740
3 changed files with 25 additions and 2 deletions

View File

@ -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"}}
}
}
]

View File

@ -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

View File

@ -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",