diff --git a/lib/plausible/stats/funnel.ex b/lib/plausible/stats/funnel.ex index 34a510830..d9c497cee 100644 --- a/lib/plausible/stats/funnel.ex +++ b/lib/plausible/stats/funnel.ex @@ -10,6 +10,7 @@ defmodule Plausible.Stats.Funnel do alias Plausible.Funnels import Ecto.Query + import Plausible.Stats.Fragments alias Plausible.ClickhouseRepo alias Plausible.Stats.Base @@ -60,7 +61,7 @@ defmodule Plausible.Stats.Funnel do defp query_funnel(query, funnel_definition) do q_events = from(e in query, - select: %{user_id: e.user_id}, + select: %{user_id: e.user_id, _sample_factor: fragment("any(_sample_factor)")}, where: e.site_id == ^funnel_definition.site_id, group_by: e.user_id, order_by: [desc: fragment("step")] @@ -69,7 +70,7 @@ defmodule Plausible.Stats.Funnel do query = from(f in subquery(q_events), - select: {f.step, count(1)}, + select: {f.step, total()}, group_by: f.step ) diff --git a/test/plausible/funnels_test.exs b/test/plausible/funnels_test.exs index dbb0e0c76..ece96bf6c 100644 --- a/test/plausible/funnels_test.exs +++ b/test/plausible/funnels_test.exs @@ -321,5 +321,35 @@ defmodule Plausible.FunnelsTest do ] }} = funnel_data end + + @tag :slow + test "sampling", %{site: site, steps: [g1, g2, g3 | _]} do + {:ok, funnel} = + Funnels.create( + site, + "From blog to signup and purchase", + [g1, g2, g3] + ) + + stats = + 1..50_000 + |> Enum.flat_map(fn _ -> + user_id = SipHash.hash!("0123456789ABCDEF", :crypto.strong_rand_bytes(64)) + + [ + build(:pageview, pathname: "/go/to/blog/foo", user_id: user_id), + build(:event, name: "Signup", user_id: user_id), + build(:pageview, pathname: "/checkout", user_id: user_id) + ] + end) + + populate_stats(site, stats) + + query = + Plausible.Stats.Query.from(site, %{"period" => "all", "sample_threshold" => "10000"}) + + {:ok, funnel_data} = Stats.funnel(site, query, funnel.id) + assert_in_delta funnel_data[:all_visitors], 50_000, 1000 + end end end