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

Add referrers.csv to CSV export (#2624) #3002

Merged
merged 8 commits into from
Aug 7, 2023
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
29 changes: 29 additions & 0 deletions lib/plausible_web/controllers/api/stats_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
3 changes: 2 additions & 1 deletion lib/plausible_web/controllers/stats_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
name,conversions,conversion_rate
Direct / None,1,25.0
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
name,visitors,bounce_rate,visit_duration
Direct / None,1,0,60
2 changes: 2 additions & 0 deletions test/plausible_web/controllers/CSVs/30d/referrers.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
name,visitors,bounce_rate,visit_duration
Direct / None,4,75,15
2 changes: 2 additions & 0 deletions test/plausible_web/controllers/CSVs/6m/referrers.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
name,visitors,bounce_rate,visit_duration
Direct / None,5,80,12