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

Fix breakdown API pagination when using event metrics #2562

Merged
merged 3 commits into from
Jan 5, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -4,6 +4,7 @@ All notable changes to this project will be documented in this file.
## Unreleased

### Fixed
- Fix breakdown API pagination when using event metrics plausible/analytics#2562
- Automatically update all visible dashboard reports in the realtime view

### Changed
Expand Down
2 changes: 1 addition & 1 deletion lib/plausible/stats/breakdown.ex
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ defmodule Plausible.Stats.Breakdown do

defp breakdown_events(site, query, property, metrics, pagination) do
from(e in base_event_query(site, query),
order_by: [desc: fragment("uniq(?)", e.user_id)],
order_by: [desc: fragment("uniq(?)", e.user_id), asc: fragment("min(?)", e.timestamp)],
vinibrsl marked this conversation as resolved.
Show resolved Hide resolved
select: %{}
)
|> do_group_by(property)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1588,6 +1588,47 @@ defmodule PlausibleWeb.Api.ExternalStatsController.BreakdownTest do
assert Enum.count(res["results"]) == 2
end

test "does not repeat results", %{conn: conn, site: site} do
vinibrsl marked this conversation as resolved.
Show resolved Hide resolved
populate_stats([
build(:pageview, %{domain: site.domain, "meta.key": ["item"], "meta.value": ["apple"]}),
build(:pageview, %{domain: site.domain, "meta.key": ["item"], "meta.value": ["kiwi"]}),
build(:pageview, %{domain: site.domain, "meta.key": ["item"], "meta.value": ["kiwi"]}),
build(:pageview, %{domain: site.domain, "meta.key": ["item"], "meta.value": ["pineapple"]}),
build(:pageview, %{domain: site.domain, "meta.key": ["item"], "meta.value": ["pineapple"]}),
build(:pageview, %{domain: site.domain, "meta.key": ["item"], "meta.value": ["pineapple"]}),
build(:pageview, %{domain: site.domain, "meta.key": ["item"], "meta.value": ["grapes"]}),
build(:pageview, %{domain: site.domain, "meta.key": ["item"], "meta.value": ["grapes"]}),
build(:pageview, %{domain: site.domain, "meta.key": ["item"], "meta.value": ["grapes"]}),
build(:pageview, %{domain: site.domain, "meta.key": ["item"], "meta.value": ["grapes"]})
])

params = %{
"site_id" => site.domain,
"metrics" => "visitors",
"property" => "event:props:item",
"limit" => 3,
"page" => nil
}

first_page =
conn
|> get("/api/v1/stats/breakdown", %{params | "page" => 1})
|> json_response(200)
|> Map.get("results")
|> Enum.map(& &1["item"])
|> MapSet.new()

second_page =
conn
|> get("/api/v1/stats/breakdown", %{params | "page" => 2})
|> json_response(200)
|> Map.get("results")
|> Enum.map(& &1["item"])
|> MapSet.new()

assert first_page |> MapSet.intersection(second_page) |> Enum.empty?()
end

@invalid_limit_message "Please provide limit as a number between 1 and 1000."

test "returns error when limit too large", %{conn: conn, site: site} do
Expand Down