From 13a05b4e1e5b66c70eb1d027d64f3b25f51253ab Mon Sep 17 00:00:00 2001 From: RobertJoonas <56999674+RobertJoonas@users.noreply.github.com> Date: Mon, 9 Jan 2023 09:31:55 +0200 Subject: [PATCH] Change metric labels for realtime with goal filter (#2560) * change metric labels for realtime with goal filter * changelog * fix formatting --- CHANGELOG.md | 1 + .../controllers/api/stats_controller.ex | 29 +++++++++++++++ .../api/stats_controller/top_stats_test.exs | 37 +++++++++++++++++++ 3 files changed, 67 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3a4b96006..b317325f9 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 view diff --git a/lib/plausible_web/controllers/api/stats_controller.ex b/lib/plausible_web/controllers/api/stats_controller.ex index e9c22491f..20e774d54 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 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"} 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.exs index 6a9a79a18..8da56f09b 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 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