Open source, privacy-first web analytics. Lightweight, cookie-free Google Analytics alternative. Self-hosted or cloud.
TypeScript analyticsanalytics-dashboardclickhouseelixirgoogle-analyticsmarketingopen-source-analyticsphoenixplausible-analyticspostgresqlprivacyprivacy-friendlyself-hostedsimple-analyticsstatisticstailwindcssweb-analyticswebsite-analyticswebsite-statswebsite-tracking
Modify backward funnel exploration behaviour (#6259)
Adrian Gruntkowski77e169807712
diff --git a/lib/plausible/stats/exploration.ex b/lib/plausible/stats/exploration.exindex e21b6e53f1..e4377cbbdb 100644--- a/lib/plausible/stats/exploration.ex+++ b/lib/plausible/stats/exploration.ex@@ -71,14 +71,11 @@ defmodule Plausible.Stats.Exploration dodef journey_funnel(_query, [], _direction), do: {:error, :empty_journey}def journey_funnel(query, journey, direction) when is_direction(direction) do- journey_for_query = journey_for_query(journey, direction)-query|> Base.base_event_query()- |> journey_funnel_query(journey_for_query)+ |> journey_funnel_query(journey, direction)|> ClickhouseRepo.all()- |> to_funnel(journey_for_query)- |> maybe_reverse_funnel(direction)+ |> to_funnel(journey)|> then(&{:ok, &1})end@@ -108,7 +105,8 @@ defmodule Plausible.Stats.Exploration do),name: selected_as(field(s, ^next_name), :next_name),pathname: selected_as(field(s, ^next_pathname), :next_pathname)- }+ },+ visitors: selected_as(scale_sample(fragment("uniq(?)", s.user_id)), :count)},group_by: [selected_as(:next_name), selected_as(:next_pathname)],order_by: [@@ -118,7 +116,6 @@ defmodule Plausible.Stats.Exploration do],limit: 10)- |> count_by_direction(query, steps, direction)|> maybe_search(search_term)steps@@ -133,40 +130,8 @@ defmodule Plausible.Stats.Exploration doend)end- defp count_by_direction(steps_query, _query, steps, direction)- when steps == [] or direction == :forward do- from(s in steps_query,- select_merge: %{- visitors: selected_as(scale_sample(fragment("uniq(?)", s.user_id)), :count)- }- )- end-- defp count_by_direction(steps_query, query, _steps, :backward) do- q_steps_first = steps_query(query, 1, :forward)-- q_counts =- from(fs in subquery(q_steps_first),- where: fs.name1 != "",- select: %{- name: fs.name1,- pathname: fs.pathname1,- visitors: scale_sample(fragment("uniq(?)", fs.user_id))- },- group_by: [fs.name1, fs.pathname1]- )-- from(s in steps_query,- inner_join: c in subquery(q_counts),- on: c.name == selected_as(:next_name) and c.pathname == selected_as(:next_pathname),- select_merge: %{- visitors: selected_as(scale_sample(fragment("any(?)", c.visitors)), :count)- }- )- end-- defp journey_funnel_query(query, steps) do- q_steps = steps_query(query, length(steps), :forward)+ defp journey_funnel_query(query, steps, direction) do+ q_steps = steps_query(query, length(steps), direction)[first_step | steps] = steps@@ -199,11 +164,7 @@ defmodule Plausible.Stats.Exploration doenddefp steps_query(query, steps, direction) when is_integer(steps) do- event_ordering =- case direction do- :backward -> [desc: :timestamp, desc: :name, desc: :pathname]- _ -> [asc: :timestamp, asc: :name, asc: :pathname]- end+ event_ordering = [asc: :timestamp, asc: :name, asc: :pathname]q_pairs =from(e in query,@@ -217,8 +178,6 @@ defmodule Plausible.Stats.Exploration dosite_id: e.site_id,user_id: e.user_id,_sample_factor: e._sample_factor,- prev_pathname: lag(e.pathname) |> over(:session_window),- prev_name: lag(e.name) |> over(:session_window),name: e.name,pathname: e.pathname,timestamp: e.timestamp@@ -226,6 +185,7 @@ defmodule Plausible.Stats.Exploration dowhere: e.name != "engagement",order_by: ^event_ordering)+ |> select_previous(direction)q_steps =from(e in subquery(q_pairs),@@ -242,18 +202,49 @@ defmodule Plausible.Stats.Exploration doif steps > 1 doEnum.reduce(1..(steps - 1), q_steps, fn idx, q ->- from(e in q,- select_merge: %{- ^:"name#{idx + 1}" => lead(e.name, ^idx) |> over(:step_window),- ^:"pathname#{idx + 1}" => lead(e.pathname, ^idx) |> over(:step_window)- }- )+ select_next(q, idx, direction)end)elseq_stepsendend+ defp select_previous(query, :forward) do+ from(e in query,+ select_merge: %{+ prev_pathname: lag(e.pathname) |> over(:session_window),+ prev_name: lag(e.name) |> over(:session_window)+ }+ )+ end++ defp select_previous(query, :backward) do+ from(e in query,+ select_merge: %{+ prev_pathname: lead(e.pathname) |> over(:session_window),+ prev_name: lead(e.name) |> over(:session_window)+ }+ )+ end++ defp select_next(query, idx, :forward) do+ from(e in query,+ select_merge: %{+ ^:"name#{idx + 1}" => lead(e.name, ^idx) |> over(:step_window),+ ^:"pathname#{idx + 1}" => lead(e.pathname, ^idx) |> over(:step_window)+ }+ )+ end++ defp select_next(query, idx, :backward) do+ from(e in query,+ select_merge: %{+ ^:"name#{idx + 1}" => lag(e.name, ^idx) |> over(:step_window),+ ^:"pathname#{idx + 1}" => lag(e.pathname, ^idx) |> over(:step_window)+ }+ )+ end+defp step_condition(step, count) dodynamic([s],@@ -310,10 +301,4 @@ defmodule Plausible.Stats.Exploration do|> Map.fetch!(:funnel)|> Enum.reverse()end-- defp journey_for_query(journey, :backward), do: Enum.reverse(journey)- defp journey_for_query(journey, :forward), do: journey-- defp maybe_reverse_funnel(funnel, :backward), do: Enum.reverse(funnel)- defp maybe_reverse_funnel(funnel, :forward), do: funnelenddiff --git a/test/plausible/stats/exploration_test.exs b/test/plausible/stats/exploration_test.exsindex 3a09ce8577..01b42f59fe 100644--- a/test/plausible/stats/exploration_test.exs+++ b/test/plausible/stats/exploration_test.exs@@ -207,23 +207,23 @@ defmodule Plausible.Stats.ExplorationTest doExploration.journey_funnel(query, journey, :backward)assert step1.step.pathname == "/logout"- assert step1.visitors == 1- assert step1.dropoff == 1- assert step1.dropoff_percentage == "50"- assert step1.conversion_rate == "50"- assert step1.conversion_rate_step == "50"+ assert step1.visitors == 2+ assert step1.dropoff == 0+ assert step1.dropoff_percentage == "0"+ assert step1.conversion_rate == "100"+ assert step1.conversion_rate_step == "0"assert step2.step.pathname == "/login"- assert step2.visitors == 2- assert step2.dropoff == 0- assert step2.dropoff_percentage == "0"- assert step2.conversion_rate == "100"- assert step2.conversion_rate_step == "100"+ assert step2.visitors == 1+ assert step2.dropoff == 1+ assert step2.dropoff_percentage == "50"+ assert step2.conversion_rate == "50"+ assert step2.conversion_rate_step == "50"assert step3.step.pathname == "/home"- assert step3.visitors == 2+ assert step3.visitors == 1assert step3.dropoff == 0assert step3.dropoff_percentage == "0"- assert step3.conversion_rate == "100"- assert step3.conversion_rate_step == "0"+ assert step3.conversion_rate == "50"+ assert step3.conversion_rate_step == "100"endend@@ -323,9 +323,9 @@ defmodule Plausible.Stats.ExplorationTest doassert {:ok, [next_step1, next_step2]} =Exploration.next_steps(query, journey, "", :backward)- assert next_step1.step.pathname == "/login"- assert next_step1.visitors == 2- assert next_step2.step.pathname == "/docs"+ assert next_step1.step.pathname == "/docs"+ assert next_step1.visitors == 1+ assert next_step2.step.pathname == "/login"assert next_step2.visitors == 1enddiff --git a/test/plausible_web/controllers/api/stats_controller/exploration_test.exs b/test/plausible_web/controllers/api/stats_controller/exploration_test.exsindex 4a667658b9..19b49f6dd3 100644--- a/test/plausible_web/controllers/api/stats_controller/exploration_test.exs+++ b/test/plausible_web/controllers/api/stats_controller/exploration_test.exs@@ -123,9 +123,9 @@ defmodule PlausibleWeb.Api.StatsController.ExplorationTest do|> json_response(200)assert [next_step1, next_step2] = resp- assert next_step1["step"]["pathname"] == "/login"- assert next_step1["visitors"] == 2- assert next_step2["step"]["pathname"] == "/docs"+ assert next_step1["step"]["pathname"] == "/docs"+ assert next_step1["visitors"] == 1+ assert next_step2["step"]["pathname"] == "/login"assert next_step2["visitors"] == 1endend@@ -192,23 +192,23 @@ defmodule PlausibleWeb.Api.StatsController.ExplorationTest doassert [step1, step2, step3] = respassert step1["step"]["pathname"] == "/logout"- assert step1["visitors"] == 1- assert step1["dropoff"] == 1- assert step1["dropoff_percentage"] == "50"- assert step1["conversion_rate"] == "50"- assert step1["conversion_rate_step"] == "50"+ assert step1["visitors"] == 2+ assert step1["dropoff"] == 0+ assert step1["dropoff_percentage"] == "0"+ assert step1["conversion_rate"] == "100"+ assert step1["conversion_rate_step"] == "0"assert step2["step"]["pathname"] == "/login"- assert step2["visitors"] == 2- assert step2["dropoff"] == 0- assert step2["dropoff_percentage"] == "0"- assert step2["conversion_rate"] == "100"- assert step2["conversion_rate_step"] == "100"+ assert step2["visitors"] == 1+ assert step2["dropoff"] == 1+ assert step2["dropoff_percentage"] == "50"+ assert step2["conversion_rate"] == "50"+ assert step2["conversion_rate_step"] == "50"assert step3["step"]["pathname"] == "/home"- assert step3["visitors"] == 2+ assert step3["visitors"] == 1assert step3["dropoff"] == 0assert step3["dropoff_percentage"] == "0"- assert step3["conversion_rate"] == "100"- assert step3["conversion_rate_step"] == "0"+ assert step3["conversion_rate"] == "50"+ assert step3["conversion_rate_step"] == "100"endendend