From 31f84aee5965c6dd171841d63a888111e610f999 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1rton=20Salomv=C3=A1ry?= Date: Wed, 8 Nov 2023 19:32:05 +0100 Subject: [PATCH 1/2] Allow custom event timeseries in stats API --- CHANGELOG.md | 1 + lib/plausible/stats/timeseries.ex | 3 +- .../timeseries_test.exs | 49 +++++++++++++++++++ 3 files changed, 52 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3c23f77f5060..f0c3640d52e1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -34,6 +34,7 @@ All notable changes to this project will be documented in this file. - Update bot detection (matomo 6.1.4, ua_inspector 3.4.0) - Improved the Goal Settings page (search, autcompletion etc.) - Log mailer errors plausible/analytics#3336 +- Allow custom event timeseries in stats API plausible/analytics#3505 ## v2.0.0 - 2023-07-12 diff --git a/lib/plausible/stats/timeseries.ex b/lib/plausible/stats/timeseries.ex index 95d1c35ccd0d..43f5a129a35b 100644 --- a/lib/plausible/stats/timeseries.ex +++ b/lib/plausible/stats/timeseries.ex @@ -15,7 +15,7 @@ defmodule Plausible.Stats.Timeseries do @typep value :: nil | integer() | float() @type results :: nonempty_list(%{required(:date) => Date.t(), required(metric()) => value()}) - @event_metrics [:visitors, :pageviews, :average_revenue, :total_revenue] + @event_metrics [:visitors, :pageviews, :events, :average_revenue, :total_revenue] @session_metrics [:visits, :bounce_rate, :visit_duration, :views_per_visit] def timeseries(site, query, metrics) do steps = buckets(query) @@ -222,6 +222,7 @@ defmodule Plausible.Stats.Timeseries do Enum.reduce(metrics, %{date: date}, fn metric, row -> case metric do :pageviews -> Map.merge(row, %{pageviews: 0}) + :events -> Map.merge(row, %{events: 0}) :visitors -> Map.merge(row, %{visitors: 0}) :visits -> Map.merge(row, %{visits: 0}) :views_per_visit -> Map.merge(row, %{views_per_visit: 0.0}) diff --git a/test/plausible_web/controllers/api/external_stats_controller/timeseries_test.exs b/test/plausible_web/controllers/api/external_stats_controller/timeseries_test.exs index 2581a7a18f42..eb75e0c2a57c 100644 --- a/test/plausible_web/controllers/api/external_stats_controller/timeseries_test.exs +++ b/test/plausible_web/controllers/api/external_stats_controller/timeseries_test.exs @@ -846,6 +846,55 @@ defmodule PlausibleWeb.Api.ExternalStatsController.TimeseriesTest do } end + test "shows events for last 7d", %{conn: conn, site: site} do + populate_stats(site, [ + build(:event, name: "Signup", timestamp: ~N[2021-01-01 00:00:00]), + build(:event, name: "Signup", timestamp: ~N[2021-01-01 00:00:00]), + build(:event, name: "Signup", timestamp: ~N[2021-01-07 23:59:00]) + ]) + + conn = + get(conn, "/api/v1/stats/timeseries", %{ + "site_id" => site.domain, + "period" => "7d", + "metrics" => "events", + "date" => "2021-01-07" + }) + + assert json_response(conn, 200) == %{ + "results" => [ + %{ + "date" => "2021-01-01", + "events" => 2 + }, + %{ + "date" => "2021-01-02", + "events" => 0 + }, + %{ + "date" => "2021-01-03", + "events" => 0 + }, + %{ + "date" => "2021-01-04", + "events" => 0 + }, + %{ + "date" => "2021-01-05", + "events" => 0 + }, + %{ + "date" => "2021-01-06", + "events" => 0 + }, + %{ + "date" => "2021-01-07", + "events" => 1 + } + ] + } + end + test "rounds views_per_visit to two decimal places", %{ conn: conn, site: site From 1bbfecc5e2a58306461e25db9cd0f0ad89a8572a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1rton=20Salomv=C3=A1ry?= Date: Tue, 14 Nov 2023 14:43:06 +0100 Subject: [PATCH 2/2] Fix linting error --- lib/plausible/stats/timeseries.ex | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/plausible/stats/timeseries.ex b/lib/plausible/stats/timeseries.ex index 43f5a129a35b..4ddca40562e2 100644 --- a/lib/plausible/stats/timeseries.ex +++ b/lib/plausible/stats/timeseries.ex @@ -218,6 +218,7 @@ defmodule Plausible.Stats.Timeseries do end end + # credo:disable-for-next-line Credo.Check.Refactor.CyclomaticComplexity defp empty_row(date, metrics) do Enum.reduce(metrics, %{date: date}, fn metric, row -> case metric do