Skip to content

Commit

Permalink
Return empty list when breaking down by event:page without events
Browse files Browse the repository at this point in the history
This commit fixes a bug with pagination where breaking down by event:page
would always return results despite pagination.

Closes #2255
  • Loading branch information
vinibrsl committed Dec 19, 2022
1 parent 478e0c6 commit 24301cb
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 12 deletions.
28 changes: 16 additions & 12 deletions lib/plausible/stats/breakdown.ex
Original file line number Diff line number Diff line change
Expand Up @@ -118,21 +118,25 @@ defmodule Plausible.Stats.Breakdown do
Query.put_filter(query, "visit:entry_page", {:member, Enum.map(pages, & &1[:page])})
end

{limit, _page} = pagination
if Enum.any?(event_metrics) && Enum.empty?(event_result) do
[]
else
{limit, _page} = pagination

session_result =
breakdown_sessions(site, new_query, "visit:entry_page", session_metrics, {limit, 1})
|> transform_keys(%{entry_page: :page})
session_result =
breakdown_sessions(site, new_query, "visit:entry_page", session_metrics, {limit, 1})
|> transform_keys(%{entry_page: :page})

metrics = metrics ++ [:page]
metrics = metrics ++ [:page]

zip_results(
event_result,
session_result,
:page,
metrics
)
|> Enum.map(&Map.take(&1, metrics))
zip_results(
event_result,
session_result,
:page,
metrics
)
|> Enum.map(&Map.take(&1, metrics))
end
end

def breakdown(site, query, property, metrics, pagination) when property in @event_props do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -553,6 +553,34 @@ defmodule PlausibleWeb.Api.ExternalStatsController.BreakdownTest do
}
end

test "breakdown by event:page when there are no events in the second page", %{
conn: conn,
site: site
} do
populate_stats([
build(:pageview, pathname: "/", domain: site.domain, timestamp: ~N[2021-01-01 00:00:00]),
build(:pageview, pathname: "/", domain: site.domain, timestamp: ~N[2021-01-01 00:25:00]),
build(:pageview,
pathname: "/plausible.io",
domain: site.domain,
timestamp: ~N[2021-01-01 00:00:00]
)
])

conn =
get(conn, "/api/v1/stats/breakdown", %{
"site_id" => site.domain,
"period" => "day",
"date" => "2021-01-01",
"property" => "event:page",
"metrics" => "visitors,bounce_rate",
"page" => 2,
"limit" => 2
})

assert json_response(conn, 200) == %{"results" => []}
end

describe "custom events" do
test "can breakdown by event:name", %{conn: conn, site: site} do
populate_stats([
Expand Down

0 comments on commit 24301cb

Please sign in to comment.