mirror of
https://github.com/plausible/analytics.git
synced 2024-09-11 18:07:33 +03:00
Inflate sampled results in Stats.Funnel.funnel/3 (#3270)
* Inflate sampled results in Stats.Funnel.funnel/3 * Add test for sampling * Update test/plausible/funnels_test.exs Co-authored-by: hq1 <hq@mtod.org> * Ignore unused binding * Use bang version of hash function --------- Co-authored-by: hq1 <hq@mtod.org>
This commit is contained in:
parent
df19fad046
commit
ab4eb75109
@ -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
|
||||
)
|
||||
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user