Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 2 additions & 38 deletions lib/plausible/stats/exploration.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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),
Expand Down
1 change: 0 additions & 1 deletion lib/plausible_web/controllers/api/stats_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
6 changes: 4 additions & 2 deletions test/plausible/stats/exploration_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading