Proof of concept

journey
Adam Rutkowski 3 years ago
parent d5048fd6b4
commit dba8a02eee

@ -3,6 +3,7 @@ defmodule Plausible.FunnelsTest do
@moduletag :full_build_only
use Plausible
use Journey
on_full_build do
alias Plausible.Goals
@ -171,14 +172,29 @@ defmodule Plausible.FunnelsTest do
]
)
populate_stats(site, [
build(:pageview, pathname: "/irrelevant/page/not/in/funnel", user_id: 999),
build(:pageview, pathname: "/go/to/blog/foo", user_id: 123),
build(:event, name: "Signup", user_id: 123),
build(:pageview, pathname: "/checkout", user_id: 123),
build(:pageview, pathname: "/go/to/blog/bar", user_id: 666),
build(:event, name: "Signup", user_id: 666)
])
journey site do
pageview("/irrelevant/page/not/in/funnel")
end
journey site do
pageview("/go/to/blog/foo")
custom_event("Signup")
pageview("/checkout")
end
journey site do
pageview("/go/to/blog/bar")
custom_event("Signup")
end
# populate_stats(site, [
# build(:pageview, pathname: "/irrelevant/page/not/in/funnel", user_id: 999),
# build(:pageview, pathname: "/go/to/blog/foo", user_id: 123),
# build(:event, name: "Signup", user_id: 123),
# build(:pageview, pathname: "/checkout", user_id: 123),
# build(:pageview, pathname: "/go/to/blog/bar", user_id: 666),
# build(:event, name: "Signup", user_id: 666)
# ])
query = Plausible.Stats.Query.from(site, %{"period" => "all"})

@ -0,0 +1,105 @@
defmodule Journey do
defmacro __using__(_) do
quote do
require Journey
import Journey
end
end
import Phoenix.ConnTest
import Plug.Conn
def run(site, state, journey) do
Enum.reduce(journey, state, fn
{:pageview, [url, opts]}, state ->
payload = %{name: "pageview", domain: site.domain, url: build_url(site.domain, url, opts)}
payload |> new_conn(state) |> ingest(state, Keyword.get(opts, :idle, 1))
{:custom_event, [name, opts]}, state ->
payload = %{name: name, domain: site.domain, url: build_url(site.domain, "/", opts)}
payload |> new_conn(state) |> ingest(state, Keyword.get(opts, :idle, 1))
end)
flush_buffers()
end
defp build_url(domain, url, _opts) do
"https://" <> Path.join(domain, url)
end
defp new_conn(payload, state) do
(state.conn || build_conn(:post, "/api/events", payload))
|> put_req_header("content-type", "application/json")
|> put_req_header("x-forwarded-for", state.ip)
|> put_req_header("user-agent", state.user_agent |> IO.inspect(label: :ua))
end
defp eval_now(now) when is_function(now, 0), do: now.()
defp eval_now(%NaiveDateTime{} = now), do: now
defp flush_buffers do
Plausible.Session.WriteBuffer.flush()
Plausible.Event.WriteBuffer.flush()
end
defp ingest(conn, state, idle_offset) do
now = eval_now(state.now)
{:ok, request} = Plausible.Ingestion.Request.build(conn, now)
Plausible.Ingestion.Event.build_and_buffer(request)
new_now = NaiveDateTime.add(now, idle_offset, :second)
Map.put(state, :now, new_now)
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)
end
end
defmacro journey(site, state \\ [], do: block) do
mod = :"#{__CALLER__.module}.Journey#{:erlang.phash2(binding())}"
__journey__(mod, site, state, block)
end
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
Journey.run(@site, @initial_state, Enum.reverse(@journey))
end
end
unquote(mod).run()
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…
Cancel
Save