Skip to content

Commit 3c63b79

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

File tree

4 files changed

+45
-48
lines changed

4 files changed

+45
-48
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 & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,49 @@ defmodule Plausible.Stats.Goals do
6161
end)
6262
end
6363

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