Channels in dashboard CSV export (#4787)

* Add channels.csv to dashboard CSV export

* Feature flag channels in CSV

* Disable credo
This commit is contained in:
Uku Taht 2024-11-11 11:31:57 +02:00 committed by GitHub
parent 3af8f24d34
commit 98bc3e7554
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 52 additions and 7 deletions

View File

@ -495,12 +495,22 @@ defmodule PlausibleWeb.Api.StatsController do
Stats.breakdown(site, query, metrics, pagination)
|> transform_keys(%{channel: :name})
if params["csv"] do
if Filters.filtering_on_dimension?(query, "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, %{
results: res,
meta: Stats.Breakdown.formatted_date_ranges(query),
skip_imported_reason: query.skip_imported_reason
})
end
end
on_ee do
def funnel(conn, %{"id" => funnel_id} = params) do

View File

@ -144,6 +144,15 @@ defmodule PlausibleWeb.StatsController do
~c"custom_props.csv" => fn -> Api.StatsController.all_custom_prop_values(conn, params) end
}
# credo:disable-for-lines:7
csvs =
if FunWithFlags.enabled?(:channels, for: site) ||
FunWithFlags.enabled?(:channels, for: conn.assigns[:current_user]) do
Map.put(csvs, ~c"channels.csv", fn -> Api.StatsController.channels(conn, params) end)
else
csvs
end
csv_values =
Map.values(csvs)
|> Plausible.ClickhouseRepo.parallel_tasks()

View File

@ -0,0 +1,2 @@
name,conversions,conversion_rate
Direct,1,50.0
1 name conversions conversion_rate
2 Direct 1 50.0

View File

@ -0,0 +1,2 @@
name,visitors,bounce_rate,visit_duration
Organic Search,1,0,60
1 name visitors bounce_rate visit_duration
2 Organic Search 1 0 60

View File

@ -0,0 +1,4 @@
name,visitors,bounce_rate,visit_duration
Direct,2,50,30
Organic Search,1,0,60
Paid Search,1,100,0
1 name visitors bounce_rate visit_duration
2 Direct 2 50 30
3 Organic Search 1 0 60
4 Paid Search 1 100 0

View File

@ -0,0 +1,4 @@
name,visitors,bounce_rate,visit_duration
Direct,2,50,30
Paid Search,2,100,0
Organic Search,1,0,60
1 name visitors bounce_rate visit_duration
2 Direct 2 50 30
3 Paid Search 2 100 0
4 Organic Search 1 0 60

View File

@ -208,6 +208,7 @@ defmodule PlausibleWeb.StatsControllerTest do
assert ~c"pages.csv" in zip
assert ~c"regions.csv" in zip
assert ~c"sources.csv" in zip
assert ~c"channels.csv" in zip
assert ~c"utm_campaigns.csv" in zip
assert ~c"utm_contents.csv" in zip
assert ~c"utm_mediums.csv" in zip
@ -423,6 +424,7 @@ defmodule PlausibleWeb.StatsControllerTest do
expected_filenames = [
"visitors.csv",
"sources.csv",
"channels.csv",
"utm_mediums.csv",
"utm_sources.csv",
"utm_campaigns.csv",
@ -471,6 +473,13 @@ defmodule PlausibleWeb.StatsControllerTest do
[""]
]
# Dummy - imported data is not actually included in exported CSVs yet
{~c"channels.csv", data} ->
assert parse_csv(data) == [
["name", "visitors", "bounce_rate", "visit_duration"],
[""]
]
{~c"utm_mediums.csv", data} ->
assert parse_csv(data) == [
["name", "visitors", "bounce_rate", "visit_duration"],
@ -685,7 +694,8 @@ defmodule PlausibleWeb.StatsControllerTest do
country_code: "EE",
subdivision1_code: "EE-37",
city_geoname_id: 588_409,
referrer_source: "Google"
referrer_source: "Google",
channel: "Organic Search"
),
build(:pageview,
user_id: 123,
@ -695,7 +705,8 @@ defmodule PlausibleWeb.StatsControllerTest do
country_code: "EE",
subdivision1_code: "EE-37",
city_geoname_id: 588_409,
referrer_source: "Google"
referrer_source: "Google",
channel: "Organic Search"
),
build(:pageview,
pathname: "/",
@ -706,6 +717,7 @@ defmodule PlausibleWeb.StatsControllerTest do
utm_source: "google",
utm_content: "content",
utm_term: "term",
channel: "Paid Search",
browser: "Firefox",
browser_version: "120",
operating_system: "Mac",
@ -726,6 +738,7 @@ defmodule PlausibleWeb.StatsControllerTest do
utm_campaign: "ads",
country_code: "EE",
referrer_source: "Google",
channel: "Paid Search",
browser: "FirefoxNoVersion",
operating_system: "MacNoVersion"
),

View File

@ -6,6 +6,7 @@ end
Mox.defmock(Plausible.HTTPClient.Mock, for: Plausible.HTTPClient.Interface)
Application.ensure_all_started(:double)
FunWithFlags.enable(:channels)
# Temporary flag to test `read_team_schemas` flag on all tests.
if System.get_env("TEST_READ_TEAM_SCHEMAS") == "1" do
FunWithFlags.enable(:read_team_schemas)