From 76f65a5a5105b230be1dc3c5f78a78a6daf8d7a7 Mon Sep 17 00:00:00 2001 From: Adrian Gruntkowski Date: Tue, 28 Apr 2026 14:16:35 +0200 Subject: [PATCH 1/5] Stop ordering by name and pathname in steps_query window definitions --- extra/lib/plausible/stats/exploration.ex | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/extra/lib/plausible/stats/exploration.ex b/extra/lib/plausible/stats/exploration.ex index 1a9b551d4fcb..86dc98865f3c 100644 --- a/extra/lib/plausible/stats/exploration.ex +++ b/extra/lib/plausible/stats/exploration.ex @@ -408,14 +408,12 @@ defmodule Plausible.Stats.Exploration do end defp steps_query(query, steps, direction) when is_integer(steps) do - event_ordering = [asc: :timestamp, asc: :name, asc: :pathname] - q_pairs = from(e in query, windows: [ session_window: [ partition_by: e.user_id, - order_by: ^event_ordering + order_by: [asc: e.timestamp] ] ], select: %{ @@ -433,7 +431,7 @@ defmodule Plausible.Stats.Exploration do q_steps = from(e in subquery(q_pairs), - windows: [step_window: [partition_by: e.user_id, order_by: ^event_ordering]], + windows: [step_window: [partition_by: e.user_id]], select: %{ user_id: e.user_id, _sample_factor: e._sample_factor, From 19e5702a16ba8542e6e026b51490b0b8f8741cfb Mon Sep 17 00:00:00 2001 From: Adrian Gruntkowski Date: Tue, 28 Apr 2026 14:37:23 +0200 Subject: [PATCH 2/5] Add explicit ordering by timestamp --- extra/lib/plausible/stats/exploration.ex | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/extra/lib/plausible/stats/exploration.ex b/extra/lib/plausible/stats/exploration.ex index 86dc98865f3c..c10842293995 100644 --- a/extra/lib/plausible/stats/exploration.ex +++ b/extra/lib/plausible/stats/exploration.ex @@ -425,7 +425,8 @@ defmodule Plausible.Stats.Exploration do fragment("if(? = '/', ?, trimRight(?, '/'))", e.pathname, e.pathname, e.pathname), timestamp: e.timestamp }, - where: e.name != "engagement" + where: e.name != "engagement", + order_by: [asc: e.timestamp] ) |> select_previous(direction) From 398072f1bae4bf58727e434d175f86b78569782b Mon Sep 17 00:00:00 2001 From: Adrian Gruntkowski Date: Tue, 28 Apr 2026 15:41:00 +0200 Subject: [PATCH 3/5] Ensure consistent ordering between windows via row number enumeration --- extra/lib/plausible/stats/exploration.ex | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/extra/lib/plausible/stats/exploration.ex b/extra/lib/plausible/stats/exploration.ex index c10842293995..6bc3bf484a5a 100644 --- a/extra/lib/plausible/stats/exploration.ex +++ b/extra/lib/plausible/stats/exploration.ex @@ -420,6 +420,7 @@ defmodule Plausible.Stats.Exploration do site_id: e.site_id, user_id: e.user_id, _sample_factor: e._sample_factor, + row_number: row_number() |> over(:session_window), name: e.name, pathname: fragment("if(? = '/', ?, trimRight(?, '/'))", e.pathname, e.pathname, e.pathname), @@ -432,7 +433,9 @@ defmodule Plausible.Stats.Exploration do q_steps = from(e in subquery(q_pairs), - windows: [step_window: [partition_by: e.user_id]], + windows: [ + step_window: [partition_by: e.user_id, order_by: [asc: e.timestamp, asc: e.row_number]] + ], select: %{ user_id: e.user_id, _sample_factor: e._sample_factor, From b7a7f8f280b50a18394f2a9a6deff6f6ecb94c41 Mon Sep 17 00:00:00 2001 From: Adrian Gruntkowski Date: Tue, 28 Apr 2026 15:44:26 +0200 Subject: [PATCH 4/5] Remove explicit ordering by timestamp from `steps_query` --- extra/lib/plausible/stats/exploration.ex | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/extra/lib/plausible/stats/exploration.ex b/extra/lib/plausible/stats/exploration.ex index 6bc3bf484a5a..ad499d84a070 100644 --- a/extra/lib/plausible/stats/exploration.ex +++ b/extra/lib/plausible/stats/exploration.ex @@ -426,8 +426,7 @@ defmodule Plausible.Stats.Exploration do fragment("if(? = '/', ?, trimRight(?, '/'))", e.pathname, e.pathname, e.pathname), timestamp: e.timestamp }, - where: e.name != "engagement", - order_by: [asc: e.timestamp] + where: e.name != "engagement" ) |> select_previous(direction) From d39e13ce4286417d6d57c6e7a6faeb462dd46b9f Mon Sep 17 00:00:00 2001 From: Adrian Gruntkowski Date: Tue, 28 Apr 2026 16:08:12 +0200 Subject: [PATCH 5/5] Filter out entries with 0 visitors (likely artifact or window optimisation) --- extra/lib/plausible/stats/exploration.ex | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/extra/lib/plausible/stats/exploration.ex b/extra/lib/plausible/stats/exploration.ex index ad499d84a070..ae48b8196408 100644 --- a/extra/lib/plausible/stats/exploration.ex +++ b/extra/lib/plausible/stats/exploration.ex @@ -269,10 +269,15 @@ defmodule Plausible.Stats.Exploration do ), on: true, hints: "ARRAY", + where: selected_as(:visitors) > 0, select: %{ name: m.name, pathname: m.pathname, - visitors: fragment("if(?, ?, ?)", is_wildcard, m.wildcard_visitors, m.exact_visitors), + visitors: + selected_as( + fragment("if(?, ?, ?)", is_wildcard, m.wildcard_visitors, m.exact_visitors), + :visitors + ), includes_subpaths: fragment("CAST(?, 'Bool')", is_wildcard), subpaths_count: fragment("if(?, ?, 0)", is_wildcard, m.subpaths_count) }