Skip to content

Commit

Permalink
Ignore invalid imported region codes in suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
zoldar committed Jun 3, 2024
1 parent 482cfc9 commit 69dfcaa
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 4 deletions.
11 changes: 7 additions & 4 deletions lib/plausible/stats/filter_suggestions.ex
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,14 @@ defmodule Plausible.Stats.FilterSuggestions do
|> Enum.map(fn c ->
subdiv = Location.get_subdivision(c)

%{
value: c,
label: subdiv.name
}
if subdiv do
%{
value: c,
label: subdiv.name
}
end
end)
|> Enum.filter(& &1)
end

def filter_suggestions(site, query, "region", filter_search) do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -749,6 +749,39 @@ defmodule PlausibleWeb.Api.StatsController.SuggestionsTest do
end
end

test "ignores invalid region codes in imported data (GA4)", %{
conn: conn,
site: site,
site_import: site_import
} do
# NOTE: Currently, the regions imported from GA4 do not conform to region code standard
# we are using. Instead, literal region names are persisted. Those names often do not
# match the names from our region databases either. Such entries are excluded from search
# suggestions entirely to avoid confusion, when, for instance, a region with matching name
# is picked but either native or imported data is missing in the dashboard with a given
# filter applied when it's expected.
populate_stats(site, site_import.id, [
build(:imported_locations, country: "EE", region: "EE-37", pageviews: 2),
build(:imported_locations, country: "EE", region: "Hiiumaa", pageviews: 1)
])

conn =
get(
conn,
"/api/stats/#{site.domain}/suggestions/region?q=&with_imported=true"
)

assert json_response(conn, 200) == [%{"value" => "EE-37", "label" => "Harjumaa"}]

conn2 =
get(
conn,
"/api/stats/#{site.domain}/suggestions/region?q=H&with_imported=true"
)

assert json_response(conn2, 200) == [%{"value" => "EE-37", "label" => "Harjumaa"}]
end

test "ignores imported data in region suggestions when a different property is filtered by",
%{
conn: conn,
Expand Down

0 comments on commit 69dfcaa

Please sign in to comment.