Change metric labels for realtime with goal filter (#2560)

* change metric labels for realtime with goal filter

* changelog

* fix formatting
This commit is contained in:
RobertJoonas 2023-01-09 09:31:55 +02:00 committed by GitHub
parent 1772ddff17
commit 13a05b4e1e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 67 additions and 0 deletions

View File

@ -4,6 +4,7 @@ All notable changes to this project will be documented in this file.
## Unreleased
### Fixed
- Show appropriate top-stat metric labels on the realtime dashboard when filtering by a goal
- Fix breakdown API pagination when using event metrics plausible/analytics#2562
- Automatically update all visible dashboard reports in the realtime view

View File

@ -230,6 +230,35 @@ defmodule PlausibleWeb.Api.StatsController do
end
end
defp fetch_top_stats(
site,
%Query{period: "realtime", filters: %{"event:goal" => _goal}} = query
) do
query_30m = %Query{query | period: "30m"}
%{
visitors: %{value: unique_conversions},
events: %{value: total_conversions}
} = Stats.aggregate(site, query_30m, [:visitors, :events])
stats = [
%{
name: "Current visitors",
value: Stats.current_visitors(site)
},
%{
name: "Unique conversions (last 30 min)",
value: unique_conversions
},
%{
name: "Total conversions (last 30 min)",
value: total_conversions
}
]
{stats, 100}
end
defp fetch_top_stats(site, %Query{period: "realtime"} = query) do
query_30m = %Query{query | period: "30m"}

View File

@ -139,6 +139,43 @@ defmodule PlausibleWeb.Api.StatsController.TopStatsTest do
res = json_response(conn, 200)
assert %{"name" => "Pageviews (last 30 min)", "value" => 3} in res["top_stats"]
end
test "shows current visitors (last 5 min) with goal filter", %{conn: conn, site: site} do
populate_stats(site, [
build(:pageview, timestamp: relative_time(minutes: -10)),
build(:pageview, timestamp: relative_time(minutes: -3)),
build(:event, name: "Signup", timestamp: relative_time(minutes: -2)),
build(:event, name: "Signup", timestamp: relative_time(minutes: -1))
])
filters = Jason.encode!(%{goal: "Signup"})
conn = get(conn, "/api/stats/#{site.domain}/top-stats?period=realtime&filters=#{filters}")
res = json_response(conn, 200)
assert %{"name" => "Current visitors", "value" => 3} in res["top_stats"]
end
test "shows unique/total conversions (last 30 min) with goal filter", %{
conn: conn,
site: site
} do
populate_stats(site, [
build(:event, name: "Signup", timestamp: relative_time(minutes: -45)),
build(:event, name: "Signup", timestamp: relative_time(minutes: -25)),
build(:event, name: "Signup", user_id: @user_id, timestamp: relative_time(minutes: -22)),
build(:event, name: "Signup", user_id: @user_id, timestamp: relative_time(minutes: -21)),
build(:event, name: "Signup", user_id: @user_id, timestamp: relative_time(minutes: -20))
])
filters = Jason.encode!(%{goal: "Signup"})
conn = get(conn, "/api/stats/#{site.domain}/top-stats?period=realtime&filters=#{filters}")
res = json_response(conn, 200)
assert %{"name" => "Unique conversions (last 30 min)", "value" => 2} in res["top_stats"]
assert %{"name" => "Total conversions (last 30 min)", "value" => 4} in res["top_stats"]
end
end
describe "GET /api/stats/top-stats - filters" do