diff --git a/lib/plausible/stats/exploration.ex b/lib/plausible/stats/exploration.ex index 84ceaf1ae333..3c5f702c86d7 100644 --- a/lib/plausible/stats/exploration.ex +++ b/lib/plausible/stats/exploration.ex @@ -36,17 +36,7 @@ defmodule Plausible.Stats.Exploration do @spec next_steps(Query.t(), journey(), String.t()) :: {:ok, [next_step()]} - def next_steps(query, journey, search_term \\ "") - - def next_steps(query, [], search_term) do - query - |> Base.base_event_query() - |> next_steps_first_query(search_term) - |> ClickhouseRepo.all() - |> then(&{:ok, &1}) - end - - def next_steps(query, journey, search_term) do + def next_steps(query, journey, search_term \\ "") do query |> Base.base_event_query() |> next_steps_query(journey, search_term) @@ -67,29 +57,6 @@ defmodule Plausible.Stats.Exploration do |> then(&{:ok, &1}) end - defp next_steps_first_query(query, search_term) do - q_steps = steps_query(query, 1) - - from(s in subquery(q_steps), - where: selected_as(:next_name) != "", - select: %{ - step: %Journey.Step{ - name: selected_as(s.name1, :next_name), - pathname: selected_as(s.pathname1, :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: [ - desc: selected_as(:count), - asc: selected_as(:next_pathname), - asc: selected_as(:next_name) - ], - limit: 10 - ) - |> maybe_search(search_term) - end - defp next_steps_query(query, steps, search_term) do next_step_idx = length(steps) + 1 q_steps = steps_query(query, next_step_idx) @@ -99,10 +66,7 @@ defmodule Plausible.Stats.Exploration do q_next = from(s in subquery(q_steps), - # avoid cycling back to the beginning of the exploration - where: - selected_as(:next_name) != "" and - (selected_as(:next_name) != s.name1 or selected_as(:next_pathname) != s.pathname1), + where: selected_as(:next_name) != "", select: %{ step: %Journey.Step{ name: selected_as(field(s, ^next_name), :next_name), diff --git a/lib/plausible_web/controllers/api/stats_controller.ex b/lib/plausible_web/controllers/api/stats_controller.ex index 4711d1182e8e..2ab0333ef338 100644 --- a/lib/plausible_web/controllers/api/stats_controller.ex +++ b/lib/plausible_web/controllers/api/stats_controller.ex @@ -372,7 +372,6 @@ defmodule PlausibleWeb.Api.StatsController do input |> Jason.decode!() |> Enum.map(&parse_journey_step/1) - |> Enum.reject(&is_nil/1) |> then(&{:ok, &1}) end diff --git a/test/plausible/stats/exploration_test.exs b/test/plausible/stats/exploration_test.exs index 89765172362b..9f385bbc6198 100644 --- a/test/plausible/stats/exploration_test.exs +++ b/test/plausible/stats/exploration_test.exs @@ -140,12 +140,14 @@ defmodule Plausible.Stats.ExplorationTest do %Exploration.Journey.Step{name: "pageview", pathname: "/login"} ] - assert {:ok, [next_step1, next_step2]} = Exploration.next_steps(query, journey) + assert {:ok, [next_step1, next_step2, next_step3]} = Exploration.next_steps(query, journey) assert next_step1.step.pathname == "/docs" assert next_step1.visitors == 1 - assert next_step2.step.pathname == "/logout" + assert next_step2.step.pathname == "/home" assert next_step2.visitors == 1 + assert next_step3.step.pathname == "/logout" + assert next_step3.visitors == 1 end test "suggests the first step in the journey", %{site: site} do diff --git a/test/plausible_web/controllers/api/stats_controller/exploration_test.exs b/test/plausible_web/controllers/api/stats_controller/exploration_test.exs index 0b7b71edffe2..e2deb58d053e 100644 --- a/test/plausible_web/controllers/api/stats_controller/exploration_test.exs +++ b/test/plausible_web/controllers/api/stats_controller/exploration_test.exs @@ -74,11 +74,13 @@ defmodule PlausibleWeb.Api.StatsController.ExplorationTest do |> get("/api/stats/#{site.domain}/exploration/next/?journey=#{journey}&period=24h") |> json_response(200) - assert [next_step1, next_step2] = resp + assert [next_step1, next_step2, next_step3] = resp assert next_step1["step"]["pathname"] == "/docs" assert next_step1["visitors"] == 1 - assert next_step2["step"]["pathname"] == "/logout" + assert next_step2["step"]["pathname"] == "/home" assert next_step2["visitors"] == 1 + assert next_step3["step"]["pathname"] == "/logout" + assert next_step3["visitors"] == 1 end test "it filters", %{conn: conn, site: site} do