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

Fixes goal filters for glob goals #781

Merged
merged 3 commits into from
Mar 3, 2021
Merged
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
60 changes: 28 additions & 32 deletions lib/plausible/stats/clickhouse.ex
Original file line number Diff line number Diff line change
Expand Up @@ -1162,22 +1162,7 @@ defmodule Plausible.Stats.Clickhouse do
end

defp base_query_w_sessions(site, query) do
q = base_query_w_sessions_bare(site, query)

{goal_event, path} = event_name_for_goal(query)

q =
if goal_event do
from(e in q, where: e.name == ^goal_event)
else
from(e in q, where: e.name == "pageview")
end

if path do
from(e in q, where: e.pathname == ^path)
else
q
end
base_query_w_sessions_bare(site, query) |> include_goal_conversions(query)
end

defp base_session_query(site, query) do
Expand Down Expand Up @@ -1412,22 +1397,7 @@ defmodule Plausible.Stats.Clickhouse do
end

defp base_query(site, query) do
q = base_query_bare(site, query)

{goal_event, path} = event_name_for_goal(query)

q =
if path do
from(e in q, where: e.pathname == ^path)
else
q
end

if goal_event do
from(e in q, where: e.name == ^goal_event)
else
from(e in q, where: e.name == "pageview")
end
base_query_bare(site, query) |> include_goal_conversions(query)
end

defp utc_boundaries(%Query{period: "30m"}, _timezone) do
Expand Down Expand Up @@ -1472,4 +1442,30 @@ defmodule Plausible.Stats.Clickhouse do
{nil, nil}
end
end

defp include_goal_conversions(db_query, query) do
{goal_event, path} = event_name_for_goal(query)

q =
if goal_event do
from(e in db_query, where: e.name == ^goal_event)
else
from(e in db_query, where: e.name == "pageview")
end

if path do
if String.match?(path, ~r/\*/) do
path_regex =
"^#{path}\/?$"
|> String.replace(~r/\*\*/, ".*")
|> String.replace(~r/(?<!\.)\*/, "[^/]*")

from(e in q, where: fragment("match(?, ?)", e.pathname, ^path_regex))
else
from(e in q, where: e.pathname == ^path)
end
else
q
end
end
end