Compare commits
22 Commits
master
...
journey-re
| Author | SHA1 | Date |
|---|---|---|
|
|
9676d3ebd1 | 3 years ago |
|
|
7e8fa24f8f | 3 years ago |
|
|
641e3f636c | 3 years ago |
|
|
5b228d65bc | 3 years ago |
|
|
4e1ffba5f5 | 3 years ago |
|
|
b8f2c14332 | 3 years ago |
|
|
1f1a6d96b7 | 3 years ago |
|
|
0aada2632e | 3 years ago |
|
|
79d427a4a6 | 3 years ago |
|
|
39aef3447b | 3 years ago |
|
|
dd19966600 | 3 years ago |
|
|
04d01cc860 | 3 years ago |
|
|
b5d6c917a9 | 3 years ago |
|
|
d60a18dc12 | 3 years ago |
|
|
00b9213d0c | 3 years ago |
|
|
61cdfd2529 | 3 years ago |
|
|
2fcf5a0bb4 | 3 years ago |
|
|
70063bc79e | 3 years ago |
|
|
1bf2be88d3 | 3 years ago |
|
|
be6aa3ef4c | 3 years ago |
|
|
1033334279 | 3 years ago |
|
|
0f95999eb1 | 3 years ago |
@ -0,0 +1,63 @@
|
||||
defmodule Plausible.FunnelsSamplingTest do
|
||||
use Plausible.DataCase, async: false
|
||||
@moduletag :full_build_only
|
||||
|
||||
use Plausible
|
||||
use Plausible.Test.Support.Journey
|
||||
|
||||
# This test does a massive ingest, so we make sure the caches are utilized
|
||||
# (both Sites.Cache and UA parsing). We also ingest on all cores, where only
|
||||
# initially SipHash is a slow-down but warms up later on.
|
||||
# On 24 cores it takes around 5.5s.
|
||||
|
||||
on_full_build do
|
||||
alias Plausible.Goals
|
||||
alias Plausible.Funnels
|
||||
alias Plausible.Stats
|
||||
|
||||
setup do
|
||||
site = insert(:site)
|
||||
|
||||
{:ok, g1} = Goals.create(site, %{"page_path" => "/go/to/blog/**"})
|
||||
{:ok, g2} = Goals.create(site, %{"event_name" => "Signup"})
|
||||
{:ok, g3} = Goals.create(site, %{"page_path" => "/checkout"})
|
||||
|
||||
{:ok,
|
||||
%{
|
||||
site: site,
|
||||
goals: [g1, g2, g3],
|
||||
steps: [g1, g2, g3] |> Enum.map(&%{"goal_id" => &1.id})
|
||||
}}
|
||||
end
|
||||
|
||||
setup_patch_env(Plausible.Cache, enabled: true)
|
||||
|
||||
test "sampling", %{steps: [g1, g2, g3]} do
|
||||
site = insert(:site, domain: "1.1.1.1")
|
||||
|
||||
{:ok, funnel} =
|
||||
Funnels.create(
|
||||
site,
|
||||
"From blog to signup",
|
||||
[g1, g2, g3]
|
||||
)
|
||||
|
||||
Plausible.Site.Cache.refresh_all()
|
||||
journey = MassJourney
|
||||
|
||||
journey site, manual: journey, ip: &random_ipv6/0, user_agent: "JourneyBrowser" do
|
||||
pageview "/go/to/blog/foo"
|
||||
custom_event "Signup"
|
||||
pageview "/checkout"
|
||||
end
|
||||
|
||||
journey.run_many(50_000)
|
||||
|
||||
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, 5000
|
||||
end
|
||||
end
|
||||
end
|
||||
@ -0,0 +1,247 @@
|
||||
defmodule Plausible.Test.Support.Journey do
|
||||
@moduledoc "TODO"
|
||||
|
||||
defmacro __using__(_) do
|
||||
quote do
|
||||
require Plausible.Test.Support.Journey
|
||||
import Plausible.Test.Support.Journey
|
||||
|
||||
Module.register_attribute(__MODULE__, :journey_index, accumulate: false)
|
||||
Module.put_attribute(__MODULE__, :journey_index, 0)
|
||||
end
|
||||
end
|
||||
|
||||
import Plug.Adapters.Test.Conn, only: [conn: 4]
|
||||
import Plug.Conn
|
||||
|
||||
def run(site, state, journey) do
|
||||
conn = new_conn(state)
|
||||
# credo:disable-for-this-file Credo.Check.Warning.UnusedEnumOperation
|
||||
Enum.reduce(journey, state, fn
|
||||
{:pageview, [url, opts]}, state ->
|
||||
payload =
|
||||
%{
|
||||
name: "pageview",
|
||||
domain: site.domain,
|
||||
url: build_url(site, url, opts)
|
||||
}
|
||||
|> add_common_body_params(opts)
|
||||
|
||||
conn
|
||||
|> Plug.Adapters.Test.Conn.conn(:post, "/api/events", payload)
|
||||
|> ingest(state, invoke_if_function(Keyword.get(opts, :idle, 1), state.now))
|
||||
|
||||
{:custom_event, [name, opts]}, state ->
|
||||
payload =
|
||||
%{name: name, domain: site.domain, url: build_url(site, "/", opts)}
|
||||
|> add_common_body_params(opts)
|
||||
|
||||
conn
|
||||
|> conn(:post, "/api/events", payload)
|
||||
|> ingest(state, invoke_if_function(Keyword.get(opts, :idle, 1), state.now))
|
||||
end)
|
||||
|
||||
if !state[:manual] do
|
||||
flush_buffers()
|
||||
end
|
||||
end
|
||||
|
||||
defp add_common_body_params(payload, params) do
|
||||
params =
|
||||
params
|
||||
|> Enum.into(%{})
|
||||
|> Map.take(~w[revenue referrer url props]a)
|
||||
|
||||
if Enum.empty?(params) do
|
||||
payload
|
||||
else
|
||||
Map.merge(payload, params)
|
||||
end
|
||||
end
|
||||
|
||||
def build_url(site, path, params) do
|
||||
if params[:url] do
|
||||
params[:url]
|
||||
else
|
||||
site_domain = site.domain |> URI.encode_www_form()
|
||||
|
||||
query_string =
|
||||
params
|
||||
|> Enum.into(%{})
|
||||
|> Map.take(~w[utm_source utm_medium utm_campaign utm_term utm_content]a)
|
||||
|> URI.encode_query()
|
||||
|
||||
uri = URI.new!("https://" <> Path.join(site_domain, path))
|
||||
uri = %{uri | query: if(query_string != "", do: query_string)}
|
||||
to_string(uri)
|
||||
end
|
||||
end
|
||||
|
||||
defp new_conn(state) do
|
||||
(state.conn || %Plug.Conn{})
|
||||
|> put_req_header("content-type", "application/json")
|
||||
|> maybe_add_header("x-forwarded-for", invoke_if_function(state.ip))
|
||||
|> prepare_device(state)
|
||||
end
|
||||
|
||||
defp prepare_device(conn, state) do
|
||||
case state[:browser] do
|
||||
browser when is_atom(browser) and not is_nil(browser) ->
|
||||
put_req_header(conn, "user-agent", sample_user_agent(browser))
|
||||
|
||||
nil ->
|
||||
maybe_add_header(conn, "user-agent", invoke_if_function(state.user_agent))
|
||||
end
|
||||
end
|
||||
|
||||
defp sample_user_agent(Safari) do
|
||||
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.3.1 Safari/605.1.1"
|
||||
end
|
||||
|
||||
defp sample_user_agent(Edge) do
|
||||
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36 Edg/122.0.0."
|
||||
end
|
||||
|
||||
defp sample_user_agent(x) do
|
||||
raise "User agent #{x} unsupported, add me :)"
|
||||
end
|
||||
|
||||
defp maybe_add_header(conn, _header, nil) do
|
||||
conn
|
||||
end
|
||||
|
||||
defp maybe_add_header(conn, header, value) do
|
||||
put_req_header(conn, header, value)
|
||||
end
|
||||
|
||||
defp invoke_if_function(f) when is_function(f, 0), do: f.()
|
||||
defp invoke_if_function(value), do: value
|
||||
|
||||
defp invoke_if_function(f, arg) when is_function(f, 1), do: f.(arg)
|
||||
defp invoke_if_function(value, _), do: value
|
||||
|
||||
def flush_buffers do
|
||||
Plausible.Session.WriteBuffer.flush()
|
||||
Plausible.Event.WriteBuffer.flush()
|
||||
end
|
||||
|
||||
defp ingest(conn, state, idle) do
|
||||
now =
|
||||
state.now
|
||||
|> invoke_if_function()
|
||||
|> Plausible.Timezones.to_utc_datetime()
|
||||
|
||||
idle_offset =
|
||||
case idle do
|
||||
n when is_integer(n) ->
|
||||
n
|
||||
|
||||
%NaiveDateTime{} = ndt ->
|
||||
NaiveDateTime.diff(ndt, now, :second)
|
||||
end
|
||||
|
||||
{:ok, request} =
|
||||
Plausible.Ingestion.Request.build(conn, now)
|
||||
|
||||
Plausible.Ingestion.Event.build_and_buffer(request)
|
||||
|
||||
new_now = NaiveDateTime.add(now, idle_offset, :second)
|
||||
|
||||
if state.debug do
|
||||
debug(conn, state, idle, now, new_now)
|
||||
end
|
||||
|
||||
Map.put(state, :now, new_now)
|
||||
end
|
||||
|
||||
defp debug(conn, _state, idle, now, new_now) do
|
||||
IO.puts("\n[#{now}] Request:" <> IO.ANSI.yellow())
|
||||
IO.puts(" #{conn.method} #{conn.request_path}?#{conn.query_string}" <> IO.ANSI.cyan())
|
||||
IO.puts(" user-agent: #{get_req_header(conn, "user-agent")}")
|
||||
IO.puts(" x-forwarded-for: #{get_req_header(conn, "x-forwarded-for")}" <> IO.ANSI.green())
|
||||
IO.puts(" #{Jason.encode!(conn.body_params)}" <> IO.ANSI.reset())
|
||||
IO.puts("[#{new_now}] Idle complete (#{inspect(idle)})")
|
||||
end
|
||||
|
||||
defmacro default(state) do
|
||||
h1 = :erlang.phash2(__CALLER__.module, 256)
|
||||
h2 = :erlang.phash2(__CALLER__.line, 256)
|
||||
|
||||
default_ip = "#{h1}.#{h2}.#{h1}.#{h2}"
|
||||
default_user_agent = "JourneyBrowser #{__CALLER__.module}/#{__CALLER__.line}"
|
||||
|
||||
quote do
|
||||
unquote(state)
|
||||
|> Map.update(:now, &NaiveDateTime.utc_now/0, & &1)
|
||||
|> Map.update(:conn, nil, & &1)
|
||||
|> Map.update(:ip, unquote(default_ip), & &1)
|
||||
|> Map.update(:user_agent, unquote(default_user_agent), & &1)
|
||||
|> Map.update(:debug, false, & &1)
|
||||
end
|
||||
end
|
||||
|
||||
defmacro journey(site, state \\ [], do: block) do
|
||||
Code.compiler_options(ignore_module_conflict: true)
|
||||
|
||||
idx = Module.get_attribute(__CALLER__.module, :journey_index)
|
||||
idx = idx + 1
|
||||
|
||||
Module.put_attribute(__CALLER__.module, :journey_index, idx)
|
||||
mod = :"#{__CALLER__.module}.Journey#{idx}"
|
||||
|
||||
__journey__(aliased(state[:manual]) || mod, site, state, block)
|
||||
end
|
||||
|
||||
defp aliased({:__aliases__, _, mod}), do: Module.concat(mod)
|
||||
defp aliased(other), do: other
|
||||
|
||||
defp __journey__(mod, site, state, block) do
|
||||
quote do
|
||||
defmodule unquote(mod) do
|
||||
Module.register_attribute(__MODULE__, :journey, accumulate: true)
|
||||
@site unquote(site)
|
||||
@initial_state default(Enum.into(unquote(state), %{}))
|
||||
|
||||
unquote(block)
|
||||
|
||||
def run() do
|
||||
Plausible.Test.Support.Journey.run(
|
||||
@site,
|
||||
@initial_state,
|
||||
Enum.reverse(@journey)
|
||||
)
|
||||
end
|
||||
|
||||
def flush() do
|
||||
Plausible.Test.Support.Journey.flush_buffers()
|
||||
end
|
||||
|
||||
def run_many(n) do
|
||||
1..n
|
||||
|> Task.async_stream(fn _ -> run() end)
|
||||
|> Stream.run()
|
||||
|
||||
flush()
|
||||
end
|
||||
end
|
||||
|
||||
if is_nil(unquote(state)[:manual]) do
|
||||
unquote(mod).run()
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
defmacro pageview(url, opts \\ []) do
|
||||
quote do: store(:pageview, [unquote(url), unquote(opts)])
|
||||
end
|
||||
|
||||
defmacro custom_event(name, opts \\ []) do
|
||||
quote do: store(:custom_event, [unquote(name), unquote(opts)])
|
||||
end
|
||||
|
||||
defmacro store(op, args) do
|
||||
quote do
|
||||
Module.put_attribute(__MODULE__, :journey, {unquote(op), unquote(args)})
|
||||
end
|
||||
end
|
||||
end
|
||||
Loading…
Reference in New Issue