Skip to content

Open source, privacy-first web analytics. Lightweight, cookie-free Google Analytics alternative. Self-hosted or cloud.

Improve stats api (#1759)

RobertJoonase5cf800d4a71
diff --git a/lib/plausible/stats/base.ex b/lib/plausible/stats/base.ex
index 5452ada748..16b2f55e98 100644
--- a/lib/plausible/stats/base.ex
+++ b/lib/plausible/stats/base.ex
@@ -267,6 +267,15 @@ defmodule Plausible.Stats.Base do
|> select_session_metrics(rest)
end
+ def select_session_metrics(q, [:events | rest]) do
+ from(s in q,
+ select_merge: %{
+ events: fragment("toUInt64(round(sum(? * ?) * any(_sample_factor)))", s.sign, s.events)
+ }
+ )
+ |> select_session_metrics(rest)
+ end
+
def select_session_metrics(q, [:visitors | rest]) do
from(s in q,
select_merge: %{
diff --git a/lib/plausible/stats/breakdown.ex b/lib/plausible/stats/breakdown.ex
index ccef6a49a7..a8de13d893 100644
--- a/lib/plausible/stats/breakdown.ex
+++ b/lib/plausible/stats/breakdown.ex
@@ -148,15 +148,7 @@ defmodule Plausible.Stats.Breakdown do
] do
query = Query.treat_page_filter_as_entry_page(query)
- # "visits" is fetched when querying bounce rate and visit duration, as it
- # is needed to calculate these from imported data. Let's remove it from the
- # result if it wasn't requested.
- if (:bounce_rate in metrics or :visit_duration in metrics) and :visits not in metrics do
- breakdown_sessions(site, query, property, metrics, pagination)
- |> Enum.map(&Map.delete(&1, :visits))
- else
- breakdown_sessions(site, query, property, metrics, pagination)
- end
+ breakdown_sessions(site, query, property, metrics, pagination)
end
def breakdown(site, query, property, metrics, pagination) do
@@ -206,6 +198,7 @@ defmodule Plausible.Stats.Breakdown do
|> apply_pagination(pagination)
|> ClickhouseRepo.all()
|> transform_keys(%{operating_system: :os})
+ |> maybe_remove_visits_metric(metrics)
end
defp breakdown_events(_, _, _, [], _), do: []
@@ -534,6 +527,18 @@ defmodule Plausible.Stats.Breakdown do
end)
end
+ defp maybe_remove_visits_metric(results, metrics) do
+ # "visits" is fetched when querying bounce rate and visit duration, as it
+ # is needed to calculate these from imported data. Let's remove it from the
+ # result if it wasn't requested.
+ if (:bounce_rate in metrics or :visit_duration in metrics) and :visits not in metrics do
+ results
+ |> Enum.map(&Map.delete(&1, :visits))
+ else
+ results
+ end
+ end
+
defp apply_pagination(q, {limit, page}) do
offset = (page - 1) * limit
diff --git a/test/plausible_web/controllers/api/external_stats_controller/breakdown_test.exs b/test/plausible_web/controllers/api/external_stats_controller/breakdown_test.exs
index f1082f0f84..83b722d88a 100644
--- a/test/plausible_web/controllers/api/external_stats_controller/breakdown_test.exs
+++ b/test/plausible_web/controllers/api/external_stats_controller/breakdown_test.exs
@@ -1591,6 +1591,12 @@ defmodule PlausibleWeb.Api.ExternalStatsController.BreakdownTest do
referrer_source: "Google",
timestamp: ~N[2021-01-01 00:00:00]
),
+ build(:event,
+ name: "signup",
+ user_id: 1,
+ referrer_source: "Google",
+ timestamp: ~N[2021-01-01 00:05:00]
+ ),
build(:pageview,
user_id: 1,
referrer_source: "Google",
@@ -1612,7 +1618,7 @@ defmodule PlausibleWeb.Api.ExternalStatsController.BreakdownTest do
"period" => "day",
"date" => "2021-01-01",
"property" => "visit:source",
- "metrics" => "visitors,visits,pageviews,bounce_rate,visit_duration"
+ "metrics" => "visitors,visits,pageviews,events,bounce_rate,visit_duration"
})
assert json_response(conn, 200) == %{
@@ -1623,7 +1629,8 @@ defmodule PlausibleWeb.Api.ExternalStatsController.BreakdownTest do
"visits" => 2,
"bounce_rate" => 50,
"visit_duration" => 300,
- "pageviews" => 3
+ "pageviews" => 3,
+ "events" => 4
},
%{
"source" => "Twitter",
@@ -1631,7 +1638,51 @@ defmodule PlausibleWeb.Api.ExternalStatsController.BreakdownTest do
"visits" => 1,
"bounce_rate" => 100,
"visit_duration" => 0,
- "pageviews" => 1
+ "pageviews" => 1,
+ "events" => 1
+ }
+ ]
+ }
+ end
+
+ test "metrics=bounce_rate does not add visits to the response", %{conn: conn, site: site} do
+ populate_stats(site, [
+ build(:pageview,
+ user_id: 1,
+ pathname: "/entry-page-1",
+ timestamp: ~N[2021-01-01 00:00:00]
+ ),
+ build(:pageview,
+ user_id: 1,
+ pathname: "/some-page",
+ timestamp: ~N[2021-01-01 00:10:00]
+ ),
+ build(:pageview,
+ user_id: 2,
+ pathname: "/entry-page-2",
+ referrer_source: "Google",
+ timestamp: ~N[2021-01-01 00:05:00]
+ )
+ ])
+
+ conn =
+ get(conn, "/api/v1/stats/breakdown", %{
+ "site_id" => site.domain,
+ "period" => "day",
+ "date" => "2021-01-01",
+ "property" => "visit:entry_page",
+ "metrics" => "bounce_rate"
+ })
+
+ assert json_response(conn, 200) == %{
+ "results" => [
+ %{
+ "entry_page" => "/entry-page-1",
+ "bounce_rate" => 0
+ },
+ %{
+ "entry_page" => "/entry-page-2",
+ "bounce_rate" => 100
}
]
}