|
|
|
|
@ -1,8 +1,8 @@
|
|
|
|
|
defmodule Journey do
|
|
|
|
|
defmodule Plausible.Test.Support.Journey do
|
|
|
|
|
defmacro __using__(_) do
|
|
|
|
|
quote do
|
|
|
|
|
require Journey
|
|
|
|
|
import Journey
|
|
|
|
|
require Plausible.Test.Support.Journey
|
|
|
|
|
import Plausible.Test.Support.Journey
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
@ -12,7 +12,12 @@ defmodule Journey do
|
|
|
|
|
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 = %{
|
|
|
|
|
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 ->
|
|
|
|
|
@ -20,7 +25,9 @@ defmodule Journey do
|
|
|
|
|
payload |> new_conn(state) |> ingest(state, Keyword.get(opts, :idle, 1))
|
|
|
|
|
end)
|
|
|
|
|
|
|
|
|
|
flush_buffers()
|
|
|
|
|
if !state[:manual] do
|
|
|
|
|
flush_buffers()
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
defp build_url(domain, url, _opts) do
|
|
|
|
|
@ -30,22 +37,34 @@ defmodule Journey do
|
|
|
|
|
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)
|
|
|
|
|
|> maybe_add_header("x-forwarded-for", invoke_if_function(state.ip))
|
|
|
|
|
|> maybe_add_header("user-agent", invoke_if_function(state.user_agent))
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
defp eval_now(now) when is_function(now, 0), do: now.()
|
|
|
|
|
defp eval_now(%NaiveDateTime{} = now), do: now
|
|
|
|
|
defp maybe_add_header(conn, _header, nil) do
|
|
|
|
|
conn
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
defp flush_buffers do
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
def 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)
|
|
|
|
|
now = invoke_if_function(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
|
|
|
|
|
@ -68,9 +87,12 @@ defmodule Journey do
|
|
|
|
|
|
|
|
|
|
defmacro journey(site, state \\ [], do: block) do
|
|
|
|
|
mod = :"#{__CALLER__.module}.Journey#{:erlang.phash2(binding())}"
|
|
|
|
|
__journey__(mod, site, state, block)
|
|
|
|
|
__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
|
|
|
|
|
@ -80,12 +102,22 @@ defmodule Journey do
|
|
|
|
|
|
|
|
|
|
unquote(block)
|
|
|
|
|
|
|
|
|
|
def run() do
|
|
|
|
|
Journey.run(@site, @initial_state, Enum.reverse(@journey))
|
|
|
|
|
def run(overrides \\ %{}) do
|
|
|
|
|
Plausible.Test.Support.Journey.run(
|
|
|
|
|
@site,
|
|
|
|
|
Map.merge(@initial_state, overrides),
|
|
|
|
|
Enum.reverse(@journey)
|
|
|
|
|
)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def flush() do
|
|
|
|
|
Plausible.Test.Support.Journey.flush_buffers()
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
unquote(mod).run()
|
|
|
|
|
if is_nil(unquote(state)[:manual]) do
|
|
|
|
|
unquote(mod).run()
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|