Open source, privacy-first web analytics. Lightweight, cookie-free Google Analytics alternative. Self-hosted or cloud.
TypeScript analyticsanalytics-dashboardclickhouseelixirgoogle-analyticsmarketingopen-source-analyticsphoenixplausible-analyticspostgresqlprivacyprivacy-friendlyself-hostedsimple-analyticsstatisticstailwindcssweb-analyticswebsite-analyticswebsite-statswebsite-tracking
Change metric labels for realtime with goal filter (#2560)
RobertJoonas13a05b4e1e5b
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 3a4b960063..b317325f9a 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -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 viewdiff --git a/lib/plausible_web/controllers/api/stats_controller.ex b/lib/plausible_web/controllers/api/stats_controller.exindex e9c22491fc..20e774d545 100644--- a/lib/plausible_web/controllers/api/stats_controller.ex+++ b/lib/plausible_web/controllers/api/stats_controller.ex@@ -230,6 +230,35 @@ defmodule PlausibleWeb.Api.StatsController doendend+ 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) doquery_30m = %Query{query | period: "30m"}diff --git a/test/plausible_web/controllers/api/stats_controller/top_stats_test.exs b/test/plausible_web/controllers/api/stats_controller/top_stats_test.exsindex 6a9a79a189..8da56f09bd 100644--- a/test/plausible_web/controllers/api/stats_controller/top_stats_test.exs+++ b/test/plausible_web/controllers/api/stats_controller/top_stats_test.exs@@ -139,6 +139,43 @@ defmodule PlausibleWeb.Api.StatsController.TopStatsTest dores = 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"]+ endenddescribe "GET /api/stats/top-stats - filters" do