Skip to content

Commit 9978623

Browse files
committed
Move code under Plausible.Stats.Goals
1 parent 8f28b28 commit 9978623

File tree

4 files changed

+45
-50
lines changed

4 files changed

+45
-50
lines changed

lib/plausible/goals/goals.ex

Lines changed: 0 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -288,48 +288,6 @@ defmodule Plausible.Goals do
288288
:ok
289289
end
290290

291-
# :TODO: Move to Plausible.Stats.Goals
292-
293-
@match_all_ch_regex ".*"
294-
295-
@type goal_decomposition() :: %{
296-
indices: [non_neg_integer()],
297-
types: [String.t()],
298-
event_names_imports: [String.t()],
299-
event_names_by_type: [String.t()],
300-
page_regexes: [String.t()],
301-
scroll_thresholds: [non_neg_integer()]
302-
}
303-
304-
@spec decompose([Plausible.Goal.t()]) :: goal_decomposition()
305-
def decompose(goals) do
306-
%{
307-
indices: Enum.with_index(goals, 1) |> Enum.map(fn {_goal, idx} -> idx end),
308-
types: Enum.map(goals, &to_string(Plausible.Goal.type(&1))),
309-
# :TRICKY: This will contain "" for non-event goals
310-
event_names_imports: Enum.map(goals, &to_string(&1.event_name)),
311-
event_names_by_type:
312-
Enum.map(goals, fn goal ->
313-
case Plausible.Goal.type(goal) do
314-
:event -> goal.event_name
315-
:page -> "pageview"
316-
:scroll -> "pageleave"
317-
end
318-
end),
319-
# :TRICKY: event goals are considered to match everything for the sake of efficient queries in query_builder.ex
320-
# See also Plausible.Stats.SQL.Expression#event_goal_join
321-
page_regexes:
322-
Enum.map(goals, fn goal ->
323-
case Plausible.Goal.type(goal) do
324-
:event -> @match_all_ch_regex
325-
:page -> Filters.Utils.page_regex(goal.page_path)
326-
:scroll -> Filters.Utils.page_regex(goal.page_path)
327-
end
328-
end),
329-
scroll_thresholds: Enum.map(goals, & &1.scroll_threshold)
330-
}
331-
end
332-
333291
defp insert_goal(site, params, upsert?) do
334292
params = Map.delete(params, "site_id")
335293

lib/plausible/stats/goals.ex

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ defmodule Plausible.Stats.Goals do
66
import Ecto.Query
77
import Plausible.Stats.Filters.Utils, only: [page_regex: 1]
88

9-
alias Plausible.Stats.Filters
10-
119
@doc """
1210
Preloads goals data if needed for query-building and related work.
1311
"""
@@ -61,6 +59,49 @@ defmodule Plausible.Stats.Goals do
6159
end)
6260
end
6361

62+
@type goal_join_data() :: %{
63+
indices: [non_neg_integer()],
64+
types: [String.t()],
65+
event_names_imports: [String.t()],
66+
event_names_by_type: [String.t()],
67+
page_regexes: [String.t()],
68+
scroll_thresholds: [non_neg_integer()]
69+
}
70+
71+
@doc """
72+
Returns data needed to perform a GROUP BY on goals in an ecto query.
73+
"""
74+
@spec goal_join_data(Plausible.Stats.Query.t()) :: goal_join_data()
75+
def goal_join_data(query) do
76+
goals = query.preloaded_goals.matching_toplevel_filters
77+
78+
%{
79+
indices: Enum.with_index(goals, 1) |> Enum.map(fn {_goal, idx} -> idx end),
80+
types: Enum.map(goals, &to_string(Plausible.Goal.type(&1))),
81+
# :TRICKY: This will contain "" for non-event goals
82+
event_names_imports: Enum.map(goals, &to_string(&1.event_name)),
83+
event_names_by_type:
84+
Enum.map(goals, fn goal ->
85+
case Plausible.Goal.type(goal) do
86+
:event -> goal.event_name
87+
:page -> "pageview"
88+
:scroll -> "pageleave"
89+
end
90+
end),
91+
# :TRICKY: event goals are considered to match everything for the sake of efficient queries in query_builder.ex
92+
# See also Plausible.Stats.SQL.Expression#event_goal_join
93+
page_regexes:
94+
Enum.map(goals, fn goal ->
95+
case Plausible.Goal.type(goal) do
96+
:event -> ".?"
97+
:page -> Filters.Utils.page_regex(goal.page_path)
98+
:scroll -> Filters.Utils.page_regex(goal.page_path)
99+
end
100+
end),
101+
scroll_thresholds: Enum.map(goals, & &1.scroll_threshold)
102+
}
103+
end
104+
64105
defp filter_preloaded(goals, filter, clause) do
65106
Enum.filter(goals, fn goal -> matches?(goal, filter, clause) end)
66107
end

lib/plausible/stats/imported/imported.ex

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -238,9 +238,7 @@ defmodule Plausible.Stats.Imported do
238238
end
239239

240240
def merge_imported(q, site, %Query{dimensions: ["event:goal"]} = query, metrics) do
241-
goal_join_data =
242-
query.preloaded_goals.matching_toplevel_filters
243-
|> Plausible.Goals.decompose()
241+
goal_join_data = Plausible.Stats.Goals.goal_join_data(query)
244242

245243
Imported.Base.decide_tables(query)
246244
|> Enum.map(fn

lib/plausible/stats/sql/query_builder.ex

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,9 +139,7 @@ defmodule Plausible.Stats.SQL.QueryBuilder do
139139
end
140140

141141
defp dimension_group_by(q, :events, query, "event:goal" = dimension) do
142-
goal_join_data =
143-
query.preloaded_goals.matching_toplevel_filters
144-
|> Plausible.Goals.decompose()
142+
goal_join_data = Plausible.Stats.Goals.goal_join_data(query)
145143

146144
from(e in q,
147145
join: goal in Expression.event_goal_join(goal_join_data),

0 commit comments

Comments
 (0)