Skip to content

Open source, privacy-first web analytics. Lightweight, cookie-free Google Analytics alternative. Self-hosted or cloud.

Fix bad suggestions due to inconsistent ordering in window queries (#6258)

Adrian Gruntkowskifc3dc7774ebc
diff --git a/lib/plausible/stats/exploration.ex b/lib/plausible/stats/exploration.ex
index ca762e80f8..e21b6e53f1 100644
--- a/lib/plausible/stats/exploration.ex
+++ b/lib/plausible/stats/exploration.ex
@@ -201,8 +201,8 @@ defmodule Plausible.Stats.Exploration do
defp steps_query(query, steps, direction) when is_integer(steps) do
event_ordering =
case direction do
- :backward -> [desc: :timestamp]
- _ -> [asc: :timestamp]
+ :backward -> [desc: :timestamp, desc: :name, desc: :pathname]
+ _ -> [asc: :timestamp, asc: :name, asc: :pathname]
end
q_pairs =
@@ -210,7 +210,7 @@ defmodule Plausible.Stats.Exploration do
windows: [
session_window: [
partition_by: e.user_id,
- order_by: [asc: e.timestamp]
+ order_by: ^event_ordering
]
],
select: %{
@@ -224,7 +224,7 @@ defmodule Plausible.Stats.Exploration do
timestamp: e.timestamp
},
where: e.name != "engagement",
- order_by: [asc: e.timestamp]
+ order_by: ^event_ordering
)
q_steps =
diff --git a/test/plausible/stats/exploration_test.exs b/test/plausible/stats/exploration_test.exs
index 64fe505f60..3a09ce8577 100644
--- a/test/plausible/stats/exploration_test.exs
+++ b/test/plausible/stats/exploration_test.exs
@@ -329,6 +329,61 @@ defmodule Plausible.Stats.ExplorationTest do
assert next_step2.visitors == 1
end
+ test "does not suggest the same path/pathname as in previous step (regression test)" do
+ site = new_site()
+
+ now = DateTime.utc_now()
+
+ ago = fn ms -> DateTime.shift(now, minute: -1 * ms) end
+
+ # The issue manifested in some very specific combinations of events with occurrences
+ # of different path/pathname combinations, some of them with identical timestamp.
+ #
+ # The cause was inconsistent ordering between `q_pairs` and `q_steps` in `steps_query`.
+ #
+ populate_stats(site, [
+ build(:pageview, user_id: 123, pathname: "/:dashboard", timestamp: ago.(100)),
+ build(:pageview, user_id: 123, pathname: "/sites", timestamp: ago.(100)),
+ build(:pageview, user_id: 123, pathname: "/sites", timestamp: ago.(99)),
+ build(:pageview, user_id: 123, pathname: "/:dashboard", timestamp: ago.(98)),
+ build(:pageview, user_id: 123, pathname: "/sites", timestamp: ago.(97)),
+ build(:pageview, user_id: 123, pathname: "/:dashboard", timestamp: ago.(96)),
+ build(:pageview, user_id: 123, pathname: "/sites", timestamp: ago.(95)),
+ build(:pageview, user_id: 123, pathname: "/:dashboard", timestamp: ago.(94)),
+ build(:pageview, user_id: 123, pathname: "/sites", timestamp: ago.(93)),
+ build(:pageview, user_id: 123, pathname: "/sites", timestamp: ago.(92)),
+ build(:pageview, user_id: 123, pathname: "/sites", timestamp: ago.(91)),
+ build(:pageview, user_id: 123, pathname: "/:dashboard", timestamp: ago.(90)),
+ build(:pageview, user_id: 123, pathname: "/sites", timestamp: ago.(89)),
+ build(:pageview, user_id: 123, pathname: "/sites", timestamp: ago.(88)),
+ build(:pageview, user_id: 123, pathname: "/:dashboard", timestamp: ago.(87)),
+ build(:pageview, user_id: 123, pathname: "/sites", timestamp: ago.(87)),
+ build(:pageview, user_id: 123, pathname: "/:dashboard/", timestamp: ago.(86)),
+ build(:pageview, user_id: 123, pathname: "/sites", timestamp: ago.(85)),
+ build(:pageview, user_id: 123, pathname: "/:dashboard", timestamp: ago.(84)),
+ build(:pageview, user_id: 123, pathname: "/sites", timestamp: ago.(83)),
+ build(:pageview, user_id: 123, pathname: "/:dashboard", timestamp: ago.(82)),
+ build(:pageview, user_id: 123, pathname: "/sites", timestamp: ago.(81)),
+ build(:pageview, user_id: 123, pathname: "/:dashboard", timestamp: ago.(80)),
+ build(:pageview, user_id: 123, pathname: "/sites", timestamp: ago.(79)),
+ build(:pageview, user_id: 123, pathname: "/:dashboard", timestamp: ago.(78)),
+ build(:pageview, user_id: 123, pathname: "/sites", timestamp: ago.(77)),
+ build(:pageview, user_id: 123, pathname: "/:dashboard", timestamp: ago.(76))
+ ])
+
+ journey = [
+ %Exploration.Journey.Step{name: "pageview", pathname: "/sites"}
+ ]
+
+ query = QueryBuilder.build!(site, input_date_range: :all)
+
+ assert {:ok, [%{step: %{pathname: "/:dashboard"}}, %{step: %{pathname: "/:dashboard/"}}]} =
+ Exploration.next_steps(query, journey, "", :forward)
+
+ assert {:ok, [%{step: %{pathname: "/:dashboard"}}, %{step: %{pathname: "/:dashboard/"}}]} =
+ Exploration.next_steps(query, journey, "", :backward)
+ end
+
test "treats identical sequence of events as a single step" do
site = new_site()