Skip to content

Commit

Permalink
Allow custom event timeseries in stats API
Browse files Browse the repository at this point in the history
  • Loading branch information
salomvary committed Nov 13, 2023
1 parent 26d9e16 commit 8199c4a
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
3 changes: 2 additions & 1 deletion lib/plausible/stats/timeseries.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 8199c4a

Please sign in to comment.