diff --git a/lib/beacon/plug.ex b/lib/beacon/plug.ex index f0c59c5a..ea57cd55 100644 --- a/lib/beacon/plug.ex +++ b/lib/beacon/plug.ex @@ -17,9 +17,32 @@ defmodule Beacon.Plug do """ @behaviour Plug + @private_routes [ + "__beacon_check__", + "__beacon_assets__", + "__beacon_media__" + ] + @impl Plug def init(_opts), do: [] @impl Plug - def call(conn, _opts), do: Plug.Conn.put_session(conn, :beacon_variant_roll, Enum.random(1..100)) + def call(conn, _opts) do + if Enum.any?(@private_routes, &(&1 in conn.path_info)) do + conn + else + put_roll(conn) + end + end + + defp put_roll(conn) do + path_list = conn.path_params["path"] + + with %{private: %{phoenix_live_view: {_, _, %{extra: %{session: %{"beacon_site" => site}}}}}} <- conn, + {_, _} <- Beacon.RouterServer.lookup_path(site, path_list, 1) do + Plug.Conn.put_session(conn, "beacon_variant_roll", Enum.random(1..100)) + else + _ -> conn + end + end end