Skip to content

Open source, privacy-first web analytics. Lightweight, cookie-free Google Analytics alternative. Self-hosted or cloud.

Include "Direct / None" in source filter suggestions (#5636)

Adam Rutkowskib786fba14734
diff --git a/lib/plausible/stats/filter_suggestions.ex b/lib/plausible/stats/filter_suggestions.ex
index 91e9149780..dd1da7355b 100644
--- a/lib/plausible/stats/filter_suggestions.ex
+++ b/lib/plausible/stats/filter_suggestions.ex
@@ -233,6 +233,7 @@ defmodule Plausible.Stats.FilterSuggestions do
)
|> limit(25)
|> ClickhouseRepo.all()
+ |> add_direct_suggestion(filter_name, filter_search)
|> Enum.filter(fn suggestion -> suggestion != "" end)
|> wrap_suggestions()
end
@@ -283,6 +284,19 @@ defmodule Plausible.Stats.FilterSuggestions do
defp apply_additional_filters(q, _, _), do: q
defp wrap_suggestions(list) do
- Enum.map(list, fn val -> %{value: val, label: val} end)
+ list |> Enum.uniq() |> Enum.map(fn val -> %{value: val, label: val} end)
+ end
+
+ @no_ref "Direct / None"
+ defp add_direct_suggestion(suggestions, :referrer_source, filter_search) do
+ if String.contains?(String.downcase(@no_ref), String.downcase(filter_search)) do
+ [@no_ref | suggestions]
+ else
+ suggestions
+ end
+ end
+
+ defp add_direct_suggestion(suggestions, _filter_name, _filter_search) do
+ suggestions
end
end
diff --git a/test/plausible_web/controllers/api/stats_controller/suggestions_test.exs b/test/plausible_web/controllers/api/stats_controller/suggestions_test.exs
index 45d8f0d742..252ae879b9 100644
--- a/test/plausible_web/controllers/api/stats_controller/suggestions_test.exs
+++ b/test/plausible_web/controllers/api/stats_controller/suggestions_test.exs
@@ -86,6 +86,7 @@ defmodule PlausibleWeb.Api.StatsController.SuggestionsTest do
get(conn, "/api/stats/#{site.domain}/suggestions/source?period=month&date=2019-01-01")
assert json_response(conn, 200) == [
+ %{"label" => "Direct / None", "value" => "Direct / None"},
%{"label" => "Bing", "value" => "Bing"},
%{"label" => "10words", "value" => "10words"}
]
@@ -1158,10 +1159,18 @@ defmodule PlausibleWeb.Api.StatsController.SuggestionsTest do
"/api/stats/#{site.domain}/suggestions/source?period=month&date=2019-01-01&q=#{unquote(q)}&with_imported=true"
)
- assert json_response(conn, 200) == [
- %{"value" => "Google", "label" => "Google"},
- %{"value" => "Bing", "label" => "Bing"}
- ]
+ if unquote(label) == "with filter" do
+ assert json_response(conn, 200) == [
+ %{"value" => "Google", "label" => "Google"},
+ %{"value" => "Bing", "label" => "Bing"}
+ ]
+ else
+ assert json_response(conn, 200) == [
+ %{"value" => "Direct / None", "label" => "Direct / None"},
+ %{"value" => "Google", "label" => "Google"},
+ %{"value" => "Bing", "label" => "Bing"}
+ ]
+ end
end
test "merges channel suggestions from native and imported data #{label}", %{