Skip breakdown of imported pages with pageviews=0 (#3655)

* Skip breakdown of imported pages with pageviews=0

* Fixup

* Prove good impoted records are merged
This commit is contained in:
hq1 2024-01-02 13:19:04 +01:00 committed by GitHub
parent 73923404b0
commit 8be9397199
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 48 additions and 0 deletions

View File

@ -368,6 +368,7 @@ defmodule Plausible.Stats.Imported do
defp select_imported_metrics(q, [:pageviews | rest]) do
q
|> where([i], i.pageviews > 0)
|> select_merge([i], %{pageviews: sum(i.pageviews)})
|> select_imported_metrics(rest)
end

View File

@ -523,6 +523,53 @@ defmodule PlausibleWeb.Api.ExternalStatsController.BreakdownTest do
}
end
test "pageviews breakdown by event:page - imported data having pageviews=0 and visitors=n should be bypassed",
%{conn: conn, site: site} do
site =
site
|> Plausible.Site.start_import(~D[2005-01-01], Timex.today(), "Google Analytics", "ok")
|> Plausible.Repo.update!()
populate_stats(site, [
build(:pageview, pathname: "/", timestamp: ~N[2021-01-01 00:00:00]),
build(:pageview, pathname: "/", timestamp: ~N[2021-01-01 00:25:00]),
build(:pageview,
pathname: "/plausible.io",
timestamp: ~N[2021-01-01 00:00:00]
),
build(:imported_pages,
page: "/skip-me",
date: ~D[2021-01-01],
visitors: 1,
pageviews: 0
),
build(:imported_pages,
page: "/include-me",
date: ~D[2021-01-01],
visitors: 1,
pageviews: 1
)
])
conn =
get(conn, "/api/v1/stats/breakdown", %{
"site_id" => site.domain,
"period" => "day",
"date" => "2021-01-01",
"property" => "event:page",
"with_imported" => "true",
"metrics" => "pageviews"
})
assert json_response(conn, 200) == %{
"results" => [
%{"page" => "/", "pageviews" => 2},
%{"page" => "/plausible.io", "pageviews" => 1},
%{"page" => "/include-me", "pageviews" => 1}
]
}
end
test "breakdown by event:page", %{conn: conn, site: site} do
populate_stats(site, [
build(:pageview, pathname: "/", timestamp: ~N[2021-01-01 00:00:00]),