diff --git a/CHANGELOG.md b/CHANGELOG.md index 1564fed003a4..3355d253c238 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ All notable changes to this project will be documented in this file. ## Unreleased +- Add `referrers.csv` to CSV export ### Fixed - Only return `(none)` values in custom property breakdown for the first page (pagination) of results - Fixed weekly/monthly e-mail report [rendering issues](https://github.com/plausible/analytics/issues/284) diff --git a/lib/plausible_web/controllers/api/stats_controller.ex b/lib/plausible_web/controllers/api/stats_controller.ex index 54b23be6365f..61c11c364397 100644 --- a/lib/plausible_web/controllers/api/stats_controller.ex +++ b/lib/plausible_web/controllers/api/stats_controller.ex @@ -701,6 +701,35 @@ defmodule PlausibleWeb.Api.StatsController do end end + def referrers(conn, params) do + site = conn.assigns[:site] + + query = + Query.from(site, params) + |> Filters.add_prefix() + + pagination = parse_pagination(params) + + metrics = [:visitors, :bounce_rate, :visit_duration] + + res = + Stats.breakdown(site, query, "visit:referrer", metrics, pagination) + |> add_cr(site, query, pagination, :referrer, "visit:referrer") + |> transform_keys(%{referrer: :name}) + + if params["csv"] do + if Map.has_key?(query.filters, "event:goal") do + res + |> transform_keys(%{visitors: :conversions}) + |> to_csv([:name, :conversions, :conversion_rate]) + else + res |> to_csv([:name, :visitors, :bounce_rate, :visit_duration]) + end + else + json(conn, res) + end + end + def referrer_drilldown(conn, %{"referrer" => "Google"} = params) do site = conn.assigns[:site] |> Repo.preload(:google_auth) diff --git a/lib/plausible_web/controllers/stats_controller.ex b/lib/plausible_web/controllers/stats_controller.ex index 19087125e99b..2c0838a0a9ba 100644 --- a/lib/plausible_web/controllers/stats_controller.ex +++ b/lib/plausible_web/controllers/stats_controller.ex @@ -145,7 +145,8 @@ defmodule PlausibleWeb.StatsController do 'operating_systems.csv' => fn -> Api.StatsController.operating_systems(conn, params) end, 'devices.csv' => fn -> Api.StatsController.screen_sizes(conn, params) end, 'conversions.csv' => fn -> Api.StatsController.conversions(conn, params) end, - 'prop_breakdown.csv' => fn -> Api.StatsController.all_props_breakdown(conn, params) end + 'prop_breakdown.csv' => fn -> Api.StatsController.all_props_breakdown(conn, params) end, + 'referrers.csv' => fn -> Api.StatsController.referrers(conn, params) end } csvs = diff --git a/test/plausible_web/controllers/CSVs/30d-filter-goal/referrers.csv b/test/plausible_web/controllers/CSVs/30d-filter-goal/referrers.csv new file mode 100644 index 000000000000..fcc1dfa68087 --- /dev/null +++ b/test/plausible_web/controllers/CSVs/30d-filter-goal/referrers.csv @@ -0,0 +1,2 @@ +name,conversions,conversion_rate +Direct / None,1,25.0 diff --git a/test/plausible_web/controllers/CSVs/30d-filter-path/referrers.csv b/test/plausible_web/controllers/CSVs/30d-filter-path/referrers.csv new file mode 100644 index 000000000000..324c36e7f479 --- /dev/null +++ b/test/plausible_web/controllers/CSVs/30d-filter-path/referrers.csv @@ -0,0 +1,2 @@ +name,visitors,bounce_rate,visit_duration +Direct / None,1,0,60 diff --git a/test/plausible_web/controllers/CSVs/30d/referrers.csv b/test/plausible_web/controllers/CSVs/30d/referrers.csv new file mode 100644 index 000000000000..12abdf9cf1d8 --- /dev/null +++ b/test/plausible_web/controllers/CSVs/30d/referrers.csv @@ -0,0 +1,2 @@ +name,visitors,bounce_rate,visit_duration +Direct / None,4,75,15 diff --git a/test/plausible_web/controllers/CSVs/6m/referrers.csv b/test/plausible_web/controllers/CSVs/6m/referrers.csv new file mode 100644 index 000000000000..85b5ad5e7543 --- /dev/null +++ b/test/plausible_web/controllers/CSVs/6m/referrers.csv @@ -0,0 +1,2 @@ +name,visitors,bounce_rate,visit_duration +Direct / None,5,80,12