Skip to content

Commit c6d7cc2

Browse files
committed
Add inline docs clarifying funnel computation logic
1 parent c6bcc76 commit c6d7cc2

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

extra/lib/plausible/stats/funnel.ex

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,13 @@ defmodule Plausible.Stats.Funnel do
9292
select_closed_funnel(db_query, funnel_definition)
9393
end
9494

95+
# The closed funnel matches when a user completes 1 or more continuous steps,
96+
# starting from the first step.
97+
#
98+
# The select statement returns each completed step (1-indexed). Additionally
99+
# it returns `@enter_offset + firstStep` special step, where `firstStep`
100+
# is practically either 1 (user has entered the funnel) or 0 (user has
101+
# not entered the funnel).
95102
defp select_closed_funnel(db_query, funnel_definition) do
96103
window_funnel_steps =
97104
Enum.reduce(funnel_definition.steps, nil, fn step, acc ->
@@ -133,6 +140,22 @@ defmodule Plausible.Stats.Funnel do
133140
)
134141
end
135142

143+
# The open funnel matches when a user completes 1 or more continuous steps,
144+
# starting from any step in the funnel.
145+
#
146+
# First, an array or funnel subsequences (arrays) is built. Subseqeuences
147+
# are then checked starting from the 1st step, then from second, up until
148+
# a sequence with last step of the funnel only.
149+
#
150+
# There's an optimization where we exit early if we match a funnel subsequence
151+
# finishing at the last step of the funnel, as there's a guarantee that
152+
# the further, shorter ones won't return a longer matched sequence.
153+
#
154+
# Next, the longest sequence out of the checked sequences is chosen. An
155+
# additional step is appended computed as `@enter_offset + firstStep`
156+
# where `firstStep` is the step index at which the user has entered the funnel.
157+
# When `firstStep` is equal to 0, it means that the user has not enetered
158+
# the funnel.
136159
defp select_open_funnel(db_query, funnel_definition) do
137160
steps_count = length(funnel_definition.steps)
138161

0 commit comments

Comments
 (0)