Skip to content

Commit

Permalink
Move code under Plausible.Stats.Goals
Browse files Browse the repository at this point in the history
  • Loading branch information
macobo committed Jan 30, 2025
1 parent 8f28b28 commit 9978623
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 50 deletions.
42 changes: 0 additions & 42 deletions lib/plausible/goals/goals.ex
Original file line number Diff line number Diff line change
Expand Up @@ -288,48 +288,6 @@ defmodule Plausible.Goals do
:ok
end

# :TODO: Move to Plausible.Stats.Goals

@match_all_ch_regex ".*"

@type goal_decomposition() :: %{
indices: [non_neg_integer()],
types: [String.t()],
event_names_imports: [String.t()],
event_names_by_type: [String.t()],
page_regexes: [String.t()],
scroll_thresholds: [non_neg_integer()]
}

@spec decompose([Plausible.Goal.t()]) :: goal_decomposition()
def decompose(goals) do
%{
indices: Enum.with_index(goals, 1) |> Enum.map(fn {_goal, idx} -> idx end),
types: Enum.map(goals, &to_string(Plausible.Goal.type(&1))),
# :TRICKY: This will contain "" for non-event goals
event_names_imports: Enum.map(goals, &to_string(&1.event_name)),
event_names_by_type:
Enum.map(goals, fn goal ->
case Plausible.Goal.type(goal) do
:event -> goal.event_name
:page -> "pageview"
:scroll -> "pageleave"
end
end),
# :TRICKY: event goals are considered to match everything for the sake of efficient queries in query_builder.ex
# See also Plausible.Stats.SQL.Expression#event_goal_join
page_regexes:
Enum.map(goals, fn goal ->
case Plausible.Goal.type(goal) do
:event -> @match_all_ch_regex
:page -> Filters.Utils.page_regex(goal.page_path)
:scroll -> Filters.Utils.page_regex(goal.page_path)
end
end),
scroll_thresholds: Enum.map(goals, & &1.scroll_threshold)
}
end

defp insert_goal(site, params, upsert?) do
params = Map.delete(params, "site_id")

Expand Down
45 changes: 43 additions & 2 deletions lib/plausible/stats/goals.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ defmodule Plausible.Stats.Goals do
import Ecto.Query
import Plausible.Stats.Filters.Utils, only: [page_regex: 1]

alias Plausible.Stats.Filters

@doc """
Preloads goals data if needed for query-building and related work.
"""
Expand Down Expand Up @@ -61,6 +59,49 @@ defmodule Plausible.Stats.Goals do
end)
end

@type goal_join_data() :: %{
indices: [non_neg_integer()],
types: [String.t()],
event_names_imports: [String.t()],
event_names_by_type: [String.t()],
page_regexes: [String.t()],
scroll_thresholds: [non_neg_integer()]
}

@doc """
Returns data needed to perform a GROUP BY on goals in an ecto query.
"""
@spec goal_join_data(Plausible.Stats.Query.t()) :: goal_join_data()
def goal_join_data(query) do
goals = query.preloaded_goals.matching_toplevel_filters

%{
indices: Enum.with_index(goals, 1) |> Enum.map(fn {_goal, idx} -> idx end),
types: Enum.map(goals, &to_string(Plausible.Goal.type(&1))),
# :TRICKY: This will contain "" for non-event goals
event_names_imports: Enum.map(goals, &to_string(&1.event_name)),
event_names_by_type:
Enum.map(goals, fn goal ->
case Plausible.Goal.type(goal) do
:event -> goal.event_name
:page -> "pageview"
:scroll -> "pageleave"
end
end),
# :TRICKY: event goals are considered to match everything for the sake of efficient queries in query_builder.ex
# See also Plausible.Stats.SQL.Expression#event_goal_join
page_regexes:
Enum.map(goals, fn goal ->
case Plausible.Goal.type(goal) do
:event -> ".?"
:page -> Filters.Utils.page_regex(goal.page_path)
:scroll -> Filters.Utils.page_regex(goal.page_path)
end
end),
scroll_thresholds: Enum.map(goals, & &1.scroll_threshold)
}
end

defp filter_preloaded(goals, filter, clause) do
Enum.filter(goals, fn goal -> matches?(goal, filter, clause) end)
end
Expand Down
4 changes: 1 addition & 3 deletions lib/plausible/stats/imported/imported.ex
Original file line number Diff line number Diff line change
Expand Up @@ -238,9 +238,7 @@ defmodule Plausible.Stats.Imported do
end

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

Imported.Base.decide_tables(query)
|> Enum.map(fn
Expand Down
4 changes: 1 addition & 3 deletions lib/plausible/stats/sql/query_builder.ex
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,7 @@ defmodule Plausible.Stats.SQL.QueryBuilder do
end

defp dimension_group_by(q, :events, query, "event:goal" = dimension) do
goal_join_data =
query.preloaded_goals.matching_toplevel_filters
|> Plausible.Goals.decompose()
goal_join_data = Plausible.Stats.Goals.goal_join_data(query)

from(e in q,
join: goal in Expression.event_goal_join(goal_join_data),
Expand Down

0 comments on commit 9978623

Please sign in to comment.