Skip to content

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

Fix custom property value filter suggestions (#4720)

RobertJoonas0f93328b6f26
diff --git a/CHANGELOG.md b/CHANGELOG.md
index ab7442ad3e..95bced6531 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -8,6 +8,8 @@ All notable changes to this project will be documented in this file.
### Changed
### Fixed
+- Fix returning filter suggestions for multiple custom property values in the dashboard Filter modal
+
## v2.1.4 - 2024-10-08
### Added
diff --git a/assets/js/dashboard/stats/modals/filter-modal-props-row.js b/assets/js/dashboard/stats/modals/filter-modal-props-row.js
index 7de1553646..c13b3c924b 100644
--- a/assets/js/dashboard/stats/modals/filter-modal-props-row.js
+++ b/assets/js/dashboard/stats/modals/filter-modal-props-row.js
@@ -56,7 +56,10 @@ export default function FilterModalPropsRow({
return Promise.resolve([])
}
return fetchSuggestions(
- apiPath(site, `/suggestions/prop_value`),
+ apiPath(
+ site,
+ `/suggestions/custom-prop-values/${encodeURIComponent(propKey)}`
+ ),
query,
input,
[FILTER_OPERATIONS.isNot, filterKey, ['(none)']]
diff --git a/lib/plausible/stats.ex b/lib/plausible/stats.ex
index 124dfdcfee..8c3fac9a58 100644
--- a/lib/plausible/stats.ex
+++ b/lib/plausible/stats.ex
@@ -50,4 +50,9 @@ defmodule Plausible.Stats do
include_sentry_replay_info()
FilterSuggestions.filter_suggestions(site, query, filter_name, filter_search)
end
+
+ def custom_prop_value_filter_suggestions(site, query, prop_key, filter_search) do
+ include_sentry_replay_info()
+ FilterSuggestions.custom_prop_value_filter_suggestions(site, query, prop_key, filter_search)
+ end
end
diff --git a/lib/plausible/stats/filter_suggestions.ex b/lib/plausible/stats/filter_suggestions.ex
index dac70f87e9..c097e513a0 100644
--- a/lib/plausible/stats/filter_suggestions.ex
+++ b/lib/plausible/stats/filter_suggestions.ex
@@ -180,6 +180,10 @@ defmodule Plausible.Stats.FilterSuggestions do
|> wrap_suggestions()
end
+ # Deprecated (buggy) and will be replaced with a new endpoint (see
+ # custom_prop_value_filter_suggestion/4). Will only be kept around
+ # for some time until the dashboards get updated to query the new
+ # endpoint. Tests have already been adjusted to the new endpoint.
def filter_suggestions(site, query, "prop_value", filter_search) do
filter_query = if filter_search == nil, do: "%", else: "%#{filter_search}%"
@@ -270,6 +274,40 @@ defmodule Plausible.Stats.FilterSuggestions do
|> wrap_suggestions()
end
+ def custom_prop_value_filter_suggestions(site, query, prop_key, filter_search) do
+ filter_query = if filter_search == nil, do: "%", else: "%#{filter_search}%"
+
+ none_q =
+ from(
+ e in base_event_query(
+ site,
+ Query.remove_top_level_filters(query, ["event:props:#{prop_key}"])
+ ),
+ select: "(none)",
+ where: not has_key(e, :meta, ^prop_key),
+ limit: 1
+ )
+
+ search_q =
+ from(e in base_event_query(site, query),
+ select: get_by_key(e, :meta, ^prop_key),
+ where:
+ has_key(e, :meta, ^prop_key) and
+ fragment(
+ "? ilike ?",
+ get_by_key(e, :meta, ^prop_key),
+ ^filter_query
+ ),
+ group_by: get_by_key(e, :meta, ^prop_key),
+ order_by: [desc: fragment("count(*)")],
+ limit: 25
+ )
+
+ ClickhouseRepo.all(none_q)
+ |> Kernel.++(ClickhouseRepo.all(search_q))
+ |> wrap_suggestions()
+ end
+
defp apply_additional_filters(q, :hostname, site) do
case Plausible.Shields.allowed_hostname_patterns(site.domain) do
:all ->
diff --git a/lib/plausible_web/controllers/api/stats_controller.ex b/lib/plausible_web/controllers/api/stats_controller.ex
index f32694dd28..e21b98f1f4 100644
--- a/lib/plausible_web/controllers/api/stats_controller.ex
+++ b/lib/plausible_web/controllers/api/stats_controller.ex
@@ -1361,6 +1361,17 @@ defmodule PlausibleWeb.Api.StatsController do
)
end
+ def custom_prop_value_filter_suggestions(conn, %{"prop_key" => prop_key} = params) do
+ site = conn.assigns[:site]
+
+ query = Query.from(site, params, debug_metadata(conn))
+
+ json(
+ conn,
+ Stats.custom_prop_value_filter_suggestions(site, query, prop_key, params["q"])
+ )
+ end
+
defp transform_keys(result, keys_to_replace) when is_map(result) do
for {key, val} <- result, do: {Map.get(keys_to_replace, key, key), val}, into: %{}
end
diff --git a/lib/plausible_web/router.ex b/lib/plausible_web/router.ex
index 9ed13fbd21..6cedfd0933 100644
--- a/lib/plausible_web/router.ex
+++ b/lib/plausible_web/router.ex
@@ -197,6 +197,10 @@ defmodule PlausibleWeb.Router do
get "/:domain/conversions", StatsController, :conversions
get "/:domain/custom-prop-values/:prop_key", StatsController, :custom_prop_values
get "/:domain/suggestions/:filter_name", StatsController, :filter_suggestions
+
+ get "/:domain/suggestions/custom-prop-values/:prop_key",
+ StatsController,
+ :custom_prop_value_filter_suggestions
end
scope "/api/v1/stats", PlausibleWeb.Api, assigns: %{api_scope: "stats:read:*"} do
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 b19eb631b7..bccb9efcbb 100644
--- a/test/plausible_web/controllers/api/stats_controller/suggestions_test.exs
+++ b/test/plausible_web/controllers/api/stats_controller/suggestions_test.exs
@@ -424,7 +424,7 @@ defmodule PlausibleWeb.Api.StatsController.SuggestionsTest do
]
end
- test "returns suggestions found in time frame", %{
+ test "returns prop key suggestions found in time frame", %{
conn: conn,
site: site
} do
@@ -442,7 +442,7 @@ defmodule PlausibleWeb.Api.StatsController.SuggestionsTest do
])
conn =
- get(conn, "/api/stats/#{site.domain}/suggestions/prop_key?period=day&date=2022-01-01")
+ get(conn, "/api/stats/#{site.domain}/suggestions/prop_key?period=day&date=2022-01-01&q=")
assert json_response(conn, 200) == [
%{"label" => "author", "value" => "author"},
@@ -450,6 +450,34 @@ defmodule PlausibleWeb.Api.StatsController.SuggestionsTest do
]
end
+ test "returns prop key suggestions by a search string", %{
+ conn: conn,
+ site: site
+ } do
+ populate_stats(site, [
+ build(:pageview,
+ "meta.key": ["author", "logged_in"],
+ "meta.value": ["Uku Taht", "false"],
+ timestamp: ~N[2022-01-01 00:00:00]
+ ),
+ build(:pageview,
+ "meta.key": ["dark_mode"],
+ "meta.value": ["true"],
+ timestamp: ~N[2022-01-02 00:00:00]
+ )
+ ])
+
+ conn =
+ get(
+ conn,
+ "/api/stats/#{site.domain}/suggestions/prop_key?period=day&date=2022-01-01&q=aut"
+ )
+
+ assert json_response(conn, 200) == [
+ %{"label" => "author", "value" => "author"}
+ ]
+ end
+
test "returns only prop keys which exist under filter", %{
conn: conn,
site: site
@@ -573,7 +601,7 @@ defmodule PlausibleWeb.Api.StatsController.SuggestionsTest do
conn =
get(
conn,
- "/api/stats/#{site.domain}/suggestions/prop_value?period=day&date=2022-01-01&filters=#{filters}"
+ "/api/stats/#{site.domain}/suggestions/custom-prop-values/author?period=day&date=2022-01-01&filters=#{filters}&q="
)
assert json_response(conn, 200) == [
@@ -583,6 +611,43 @@ defmodule PlausibleWeb.Api.StatsController.SuggestionsTest do
]
end
+ test "returns (none) value as the first suggestion even when a search string is provided",
+ %{conn: conn, site: site} do
+ populate_stats(site, [
+ build(:pageview,
+ "meta.key": ["author"],
+ "meta.value": ["Uku Taht"],
+ timestamp: ~N[2022-01-01 00:00:00]
+ ),
+ build(:pageview,
+ "meta.key": ["author"],
+ "meta.value": ["Marko Saric"],
+ timestamp: ~N[2022-01-01 00:00:00]
+ ),
+ build(:pageview,
+ "meta.key": ["author"],
+ "meta.value": ["Marko Saric"],
+ timestamp: ~N[2022-01-01 00:00:00]
+ ),
+ build(:pageview,
+ timestamp: ~N[2022-01-01 00:00:00]
+ )
+ ])
+
+ filters = Jason.encode!(%{props: %{author: "!(none)"}})
+
+ conn =
+ get(
+ conn,
+ "/api/stats/#{site.domain}/suggestions/custom-prop-values/author?period=day&date=2022-01-01&filters=#{filters}&q=Mar"
+ )
+
+ assert json_response(conn, 200) == [
+ %{"label" => "(none)", "value" => "(none)"},
+ %{"label" => "Marko Saric", "value" => "Marko Saric"}
+ ]
+ end
+
test "does not show (none) value suggestion when all events have that prop_key", %{
conn: conn,
site: site
@@ -610,7 +675,7 @@ defmodule PlausibleWeb.Api.StatsController.SuggestionsTest do
conn =
get(
conn,
- "/api/stats/#{site.domain}/suggestions/prop_value?period=day&date=2022-01-01&filters=#{filters}"
+ "/api/stats/#{site.domain}/suggestions/custom-prop-values/author?period=day&date=2022-01-01&filters=#{filters}"
)
assert json_response(conn, 200) == [
@@ -619,6 +684,42 @@ defmodule PlausibleWeb.Api.StatsController.SuggestionsTest do
]
end
+ test "returns prop value suggestions with multiple custom property filters in query", %{
+ conn: conn,
+ site: site
+ } do
+ populate_stats(site, [
+ build(:pageview,
+ "meta.key": ["author", "browser_language"],
+ "meta.value": ["Uku Taht", "en-US"],
+ timestamp: ~N[2022-01-01 00:00:00]
+ ),
+ build(:pageview,
+ "meta.key": ["author", "browser_language"],
+ "meta.value": ["Uku Taht", "en-US"],
+ timestamp: ~N[2022-01-01 00:00:00]
+ ),
+ build(:pageview,
+ "meta.key": ["author", "browser_language"],
+ "meta.value": ["Marko Saric", "de-DE"],
+ timestamp: ~N[2022-01-01 00:00:00]
+ ),
+ build(:pageview, timestamp: ~N[2022-01-01 00:00:00])
+ ])
+
+ filters = Jason.encode!(%{props: %{browser_language: "!(none)", author: "Uku Taht"}})
+
+ conn =
+ get(
+ conn,
+ "/api/stats/#{site.domain}/suggestions/custom-prop-values/browser_language?period=day&date=2022-01-01&filters=#{filters}"
+ )
+
+ assert json_response(conn, 200) == [
+ %{"label" => "en-US", "value" => "en-US"}
+ ]
+ end
+
test "when date is borked, bad request is returned", %{
conn: conn,
site: site
@@ -644,7 +745,7 @@ defmodule PlausibleWeb.Api.StatsController.SuggestionsTest do
conn =
get(
conn,
- "/api/stats/#{site.domain}/suggestions/prop_value?period=all&date=CLEVER_SECURITY_RESEARCH&filters=#{filters}"
+ "/api/stats/#{site.domain}/suggestions/custom-prop-values/author?period=all&date=CLEVER_SECURITY_RESEARCH&filters=#{filters}"
)
assert json_response(conn, 400) == %{
@@ -1008,7 +1109,7 @@ defmodule PlausibleWeb.Api.StatsController.SuggestionsTest do
value_conn =
get(
conn,
- "/api/stats/#{site.domain}/suggestions/prop_value?period=day&date=2022-01-01&with_imported=true&filters=#{filters}"
+ "/api/stats/#{site.domain}/suggestions/custom-prop-values/url?period=day&date=2022-01-01&with_imported=true&filters=#{filters}"
)
assert json_response(value_conn, 200) == [