Improve stats api (#1759)

* can now query events metric when breaking down on a visit prop

* updated changelog

* fixed formatting

* fixed a bug - querying for bounce rate does not return visits anymore

* removed the changelog update
pull/1791/head
RobertJoonas 5 years ago committed by GitHub
parent 492f47ba1e
commit e5cf800d4a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -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: %{

@ -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

@ -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
}
]
}

Loading…
Cancel
Save