From 9676d3ebd1cc9b3eb79a72d577636f83088d3fd3 Mon Sep 17 00:00:00 2001 From: Adam Rutkowski Date: Mon, 11 Mar 2024 16:19:59 +0100 Subject: [PATCH] PoC - device preparation --- test/plausible/funnels_test.exs | 4 ++-- test/support/journey.ex | 30 ++++++++++++++++++++++++++---- 2 files changed, 28 insertions(+), 6 deletions(-) diff --git a/test/plausible/funnels_test.exs b/test/plausible/funnels_test.exs index 5932241df..57d058ebc 100644 --- a/test/plausible/funnels_test.exs +++ b/test/plausible/funnels_test.exs @@ -175,13 +175,13 @@ defmodule Plausible.FunnelsTest do pageview "/irrelevant/page/not/in/funnel" end - journey site do + journey site, browser: Safari, debug: true do pageview "/go/to/blog/foo" custom_event "Signup" pageview "/checkout" end - journey site do + journey site, debug: true do pageview "/go/to/blog/bar" custom_event "Signup" end diff --git a/test/support/journey.ex b/test/support/journey.ex index c835fb89c..6e886a1dd 100644 --- a/test/support/journey.ex +++ b/test/support/journey.ex @@ -81,7 +81,29 @@ defmodule Plausible.Test.Support.Journey do (state.conn || %Plug.Conn{}) |> put_req_header("content-type", "application/json") |> maybe_add_header("x-forwarded-for", invoke_if_function(state.ip)) - |> maybe_add_header("user-agent", invoke_if_function(state.user_agent)) + |> 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 @@ -132,11 +154,11 @@ defmodule Plausible.Test.Support.Journey do Map.put(state, :now, new_now) end - defp debug(conn, state, idle, now, new_now) do + 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: #{inspect(state.user_agent)}") - IO.puts(" x-forwarded-for: #{state.ip}" <> IO.ANSI.green()) + 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