Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FF] Average Scroll Depth Metric: dashboard CSV export #4898

Merged
merged 5 commits into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/plausible/stats/timeseries.ex
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ defmodule Plausible.Stats.Timeseries do
:views_per_visit -> Map.merge(row, %{views_per_visit: 0.0})
:conversion_rate -> Map.merge(row, %{conversion_rate: 0.0})
:group_conversion_rate -> Map.merge(row, %{group_conversion_rate: 0.0})
:scroll_depth -> Map.merge(row, %{scroll_depth: 0})
:scroll_depth -> Map.merge(row, %{scroll_depth: nil})
:bounce_rate -> Map.merge(row, %{bounce_rate: 0.0})
:visit_duration -> Map.merge(row, %{visit_duration: nil})
:average_revenue -> Map.merge(row, %{average_revenue: nil})
Expand Down
13 changes: 10 additions & 3 deletions lib/plausible_web/controllers/api/stats_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -831,9 +831,11 @@ defmodule PlausibleWeb.Api.StatsController do
params = Map.put(params, "property", "event:page")
query = Query.from(site, params, debug_metadata(conn))

include_scroll_depth? = !query.include_imported && scroll_depth_enabled?(site, current_user)

extra_metrics =
cond do
params["detailed"] && !query.include_imported && scroll_depth_enabled?(site, current_user) ->
params["detailed"] && include_scroll_depth? ->
[:pageviews, :bounce_rate, :time_on_page, :scroll_depth]

params["detailed"] ->
Expand All @@ -856,7 +858,12 @@ defmodule PlausibleWeb.Api.StatsController do
|> transform_keys(%{visitors: :conversions})
|> to_csv([:name, :conversions, :conversion_rate])
else
pages |> to_csv([:name, :visitors, :pageviews, :bounce_rate, :time_on_page])
cols = [:name, :visitors, :pageviews, :bounce_rate, :time_on_page]

# credo:disable-for-next-line Credo.Check.Refactor.Nesting
cols = if include_scroll_depth?, do: cols ++ [:scroll_depth], else: cols

pages |> to_csv(cols)
end
else
json(conn, %{
Expand Down Expand Up @@ -1614,7 +1621,7 @@ defmodule PlausibleWeb.Api.StatsController do

defp realtime_period_to_30m(params), do: params

defp scroll_depth_enabled?(site, user) do
def scroll_depth_enabled?(site, user) do
FunWithFlags.enabled?(:scroll_depth, for: user) ||
FunWithFlags.enabled?(:scroll_depth, for: site)
end
Expand Down
15 changes: 11 additions & 4 deletions lib/plausible_web/controllers/stats_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ defmodule PlausibleWeb.StatsController do
limited_params = Map.merge(params, %{"limit" => "100"})

csvs = %{
~c"visitors.csv" => fn -> main_graph_csv(site, query) end,
~c"visitors.csv" => fn -> main_graph_csv(site, query, conn.assigns[:current_user]) end,
~c"sources.csv" => fn -> Api.StatsController.sources(conn, params) end,
~c"utm_mediums.csv" => fn -> Api.StatsController.utm_mediums(conn, params) end,
~c"utm_sources.csv" => fn -> Api.StatsController.utm_sources(conn, params) end,
Expand Down Expand Up @@ -177,8 +177,8 @@ defmodule PlausibleWeb.StatsController do
end
end

defp main_graph_csv(site, query) do
{metrics, column_headers} = csv_graph_metrics(query)
defp main_graph_csv(site, query, current_user) do
{metrics, column_headers} = csv_graph_metrics(query, site, current_user)

map_bucket_to_row = fn bucket -> Enum.map([:date | metrics], &bucket[&1]) end
prepend_column_headers = fn data -> [column_headers | data] end
Expand All @@ -190,7 +190,12 @@ defmodule PlausibleWeb.StatsController do
|> NimbleCSV.RFC4180.dump_to_iodata()
end

defp csv_graph_metrics(query) do
defp csv_graph_metrics(query, site, current_user) do
include_scroll_depth? =
!query.include_imported &&
PlausibleWeb.Api.StatsController.scroll_depth_enabled?(site, current_user) &&
Filters.filtering_on_dimension?(query, "event:page")

{metrics, column_headers} =
if Filters.filtering_on_dimension?(query, "event:goal") do
{
Expand All @@ -207,6 +212,8 @@ defmodule PlausibleWeb.StatsController do
:visit_duration
]

metrics = if include_scroll_depth?, do: metrics ++ [:scroll_depth], else: metrics

{
metrics,
[:date | metrics]
Expand Down
4 changes: 2 additions & 2 deletions test/plausible_web/controllers/CSVs/30d-filter-path/pages.csv
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
name,visitors,pageviews,bounce_rate,time_on_page
/some-other-page,1,1,0,60.0
name,visitors,pageviews,bounce_rate,time_on_page,scroll_depth
/some-other-page,1,1,0,60.0,
64 changes: 32 additions & 32 deletions test/plausible_web/controllers/CSVs/30d-filter-path/visitors.csv
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
date,visitors,pageviews,visits,views_per_visit,bounce_rate,visit_duration
2021-09-20,0,0,0,0.0,0.0,
2021-09-21,0,0,0,0.0,0.0,
2021-09-22,0,0,0,0.0,0.0,
2021-09-23,0,0,0,0.0,0.0,
2021-09-24,0,0,0,0.0,0.0,
2021-09-25,0,0,0,0.0,0.0,
2021-09-26,0,0,0,0.0,0.0,
2021-09-27,0,0,0,0.0,0.0,
2021-09-28,0,0,0,0.0,0.0,
2021-09-29,0,0,0,0.0,0.0,
2021-09-30,0,0,0,0.0,0.0,
2021-10-01,0,0,0,0.0,0.0,
2021-10-02,0,0,0,0.0,0.0,
2021-10-03,0,0,0,0.0,0.0,
2021-10-04,0,0,0,0.0,0.0,
2021-10-05,0,0,0,0.0,0.0,
2021-10-06,0,0,0,0.0,0.0,
2021-10-07,0,0,0,0.0,0.0,
2021-10-08,0,0,0,0.0,0.0,
2021-10-09,0,0,0,0.0,0.0,
2021-10-10,0,0,0,0.0,0.0,
2021-10-11,0,0,0,0.0,0.0,
2021-10-12,0,0,0,0.0,0.0,
2021-10-13,0,0,0,0.0,0.0,
2021-10-14,0,0,0,0.0,0.0,
2021-10-15,0,0,0,0.0,0.0,
2021-10-16,0,0,0,0.0,0.0,
2021-10-17,0,0,0,0.0,0.0,
2021-10-18,0,0,0,0.0,0.0,
2021-10-19,0,0,0,0.0,0.0,
2021-10-20,1,1,1,2.0,0,60
date,visitors,pageviews,visits,views_per_visit,bounce_rate,visit_duration,scroll_depth
2021-09-20,0,0,0,0.0,0.0,,
2021-09-21,0,0,0,0.0,0.0,,
2021-09-22,0,0,0,0.0,0.0,,
2021-09-23,0,0,0,0.0,0.0,,
2021-09-24,0,0,0,0.0,0.0,,
2021-09-25,0,0,0,0.0,0.0,,
2021-09-26,0,0,0,0.0,0.0,,
2021-09-27,0,0,0,0.0,0.0,,
2021-09-28,0,0,0,0.0,0.0,,
2021-09-29,0,0,0,0.0,0.0,,
2021-09-30,0,0,0,0.0,0.0,,
2021-10-01,0,0,0,0.0,0.0,,
2021-10-02,0,0,0,0.0,0.0,,
2021-10-03,0,0,0,0.0,0.0,,
2021-10-04,0,0,0,0.0,0.0,,
2021-10-05,0,0,0,0.0,0.0,,
2021-10-06,0,0,0,0.0,0.0,,
2021-10-07,0,0,0,0.0,0.0,,
2021-10-08,0,0,0,0.0,0.0,,
2021-10-09,0,0,0,0.0,0.0,,
2021-10-10,0,0,0,0.0,0.0,,
2021-10-11,0,0,0,0.0,0.0,,
2021-10-12,0,0,0,0.0,0.0,,
2021-10-13,0,0,0,0.0,0.0,,
2021-10-14,0,0,0,0.0,0.0,,
2021-10-15,0,0,0,0.0,0.0,,
2021-10-16,0,0,0,0.0,0.0,,
2021-10-17,0,0,0,0.0,0.0,,
2021-10-18,0,0,0,0.0,0.0,,
2021-10-19,0,0,0,0.0,0.0,,
2021-10-20,1,1,1,2.0,0,60,
8 changes: 4 additions & 4 deletions test/plausible_web/controllers/CSVs/30d/pages.csv
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name,visitors,pageviews,bounce_rate,time_on_page
/,4,3,67,
/signup,1,1,0,60.0
/some-other-page,1,1,0,60.0
name,visitors,pageviews,bounce_rate,time_on_page,scroll_depth
/,4,3,67,,
/signup,1,1,0,60.0,
/some-other-page,1,1,0,60.0,
8 changes: 4 additions & 4 deletions test/plausible_web/controllers/CSVs/6m/pages.csv
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name,visitors,pageviews,bounce_rate,time_on_page
/,5,4,75,
/signup,1,1,0,60.0
/some-other-page,1,1,0,60.0
name,visitors,pageviews,bounce_rate,time_on_page,scroll_depth
/,5,4,75,,
/signup,1,1,0,60.0,
/some-other-page,1,1,0,60.0,
Loading
Loading