Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reject unknown imported cities from queries #2675

Merged
merged 3 commits into from
Feb 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ All notable changes to this project will be documented in this file.
- Add support for more Bamboo adapters, i.e. `Bamboo.MailgunAdapter`, `Bamboo.MandrillAdapter`, `Bamboo.SendGridAdapter` plausible/analytics#2649

### Fixed
- City report showing N/A instead of city names with imported data plausible/analytics#2675
- Empty values for Screen Size, OS and Browser are uniformly replaced with "(not set)"
- Fix [more pageviews with session prop filter than with no filters](https://github.com/plausible/analytics/issues/1666)
- Cascade delete sent_renewal_notifications table when user is deleted plausible/analytics#2549
Expand Down
9 changes: 6 additions & 3 deletions lib/plausible/stats/imported.ex
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ defmodule Plausible.Stats.Imported do
# GA only has 'source'
def merge_imported(q, _, _, "utm_source", _), do: q

# credo:disable-for-next-line Credo.Check.Refactor.CyclomaticComplexity
def merge_imported(q, site, query, property, metrics)
when property in [
"visit:source",
Expand Down Expand Up @@ -195,7 +196,9 @@ defmodule Plausible.Stats.Imported do
imported_q |> where([i], i.region != "") |> select_merge([i], %{region: i.region})

:city ->
imported_q |> where([i], i.city != 0) |> select_merge([i], %{city: i.city})
imported_q
|> where([i], i.city != 0 and not is_nil(i.city))
|> select_merge([i], %{city: i.city})

:device ->
imported_q |> select_merge([i], %{device: i.device})
Expand Down Expand Up @@ -287,8 +290,8 @@ defmodule Plausible.Stats.Imported do

:city ->
q
|> select_merge([i, s], %{
city: fragment("coalesce(?, ?)", s.city, i.city)
|> select_merge([s, i], %{
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bindings were in incorrect order :thisisfine:

city: fragment("coalesce(nullif(?, 0), ?)", i.city, s.city)
})

:device ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,20 @@ defmodule PlausibleWeb.Api.StatsController.CitiesTest do
%{"code" => 591_632, "country_flag" => "🇪🇪", "name" => "Kärdla", "visitors" => 2}
]
end

test "does not return missing cities from imported data", %{conn: conn, site: site} do
populate_stats(site, [
build(:imported_locations, country: "EE", region: "EE-37", city: 588_409),
build(:imported_locations, country: nil, region: nil, city: 0),
build(:imported_locations, country: nil, region: nil, city: nil)
])

conn = get(conn, "/api/stats/#{site.domain}/cities?period=day&with_imported=true")

assert json_response(conn, 200) == [
%{"code" => 588_409, "country_flag" => "🇪🇪", "name" => "Tallinn", "visitors" => 4},
%{"code" => 591_632, "country_flag" => "🇪🇪", "name" => "Kärdla", "visitors" => 2}
]
end
end
end