From 61c3959626e64c86ce92b666a528d078e0846528 Mon Sep 17 00:00:00 2001 From: Vignesh Joglekar Date: Wed, 3 Mar 2021 02:37:18 -0600 Subject: [PATCH] Fixes goal filters for glob goals (#781) * Fixes goal filters for glob goals * Format * Consolidates goal application --- lib/plausible/stats/clickhouse.ex | 60 +++++++++++++++---------------- 1 file changed, 28 insertions(+), 32 deletions(-) diff --git a/lib/plausible/stats/clickhouse.ex b/lib/plausible/stats/clickhouse.ex index e1f71e586..27f0cba00 100644 --- a/lib/plausible/stats/clickhouse.ex +++ b/lib/plausible/stats/clickhouse.ex @@ -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 @@ -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 @@ -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/(?