Skip to content
Draft
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
35 changes: 32 additions & 3 deletions extra/lib/plausible/stats/exploration.ex
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,7 @@ defmodule Plausible.Stats.Exploration do

from(s in q, where: ^step_condition)
end)
|> maybe_exclude_step_matches(List.last(steps))

# Fan out each q_combined row into up to two output rows (exact + wildcard)
# using ARRAY JOIN over a small boolean array.
Expand Down Expand Up @@ -399,6 +400,30 @@ defmodule Plausible.Stats.Exploration do
)
end

defp maybe_exclude_step_matches(query, %{includes_subpaths: true} = step) do
pattern = wildcard_pattern(step.pathname)

from m in query,
where:
selected_as(:name) != ^step.name or
not fragment("match(?, ?)", selected_as(:pathname), ^pattern)
end

defp maybe_exclude_step_matches(query, %{is_goal: true, name: "pageview"} = step) do
if String.contains?(step.pathname, "*") do
pattern = Filters.Utils.page_regex(step.pathname)

from m in query,
where:
selected_as(:name) != ^step.name or
not fragment("match(?, ?)", selected_as(:pathname), ^pattern)
else
query
end
end

defp maybe_exclude_step_matches(query, _), do: query

defp exclude_goal_matches(query, goals) do
to_exclude =
goals
Expand Down Expand Up @@ -647,9 +672,7 @@ defmodule Plausible.Stats.Exploration do
defp step_condition(step, count) when count <= @max_steps do
cond do
step.includes_subpaths ->
escaped = Regex.escape(step.pathname)

pattern = "^#{escaped}(/.+)?$"
pattern = wildcard_pattern(step.pathname)

dynamic(
[s],
Expand All @@ -675,6 +698,12 @@ defmodule Plausible.Stats.Exploration do
end
end

defp wildcard_pattern(pathname) when is_binary(pathname) do
escaped = Regex.escape(pathname)

"^#{escaped}(/.+)?$"
end

defp maybe_search(query, search_term) do
case String.trim(search_term) do
term when byte_size(term) > 2 ->
Expand Down
Loading