Skip to content

Open source, privacy-first web analytics. Lightweight, cookie-free Google Analytics alternative. Self-hosted or cloud.

Use new Detection module in installation screen (#5604)

Uku Taht00f24bfcd653
diff --git a/config/test.exs b/config/test.exs
index 131714300d..8df2c0f11a 100644
--- a/config/test.exs
+++ b/config/test.exs
@@ -49,4 +49,9 @@ config :plausible, Plausible.HelpScout,
plug: {Req.Test, Plausible.HelpScout}
]
+config :plausible, Plausible.InstallationSupport.Detection,
+ req_opts: [
+ plug: {Req.Test, :global}
+ ]
+
config :plausible, Plausible.Session.Salts, interval: :timer.hours(1)
diff --git a/lib/plausible_web/live/installationv2.ex b/lib/plausible_web/live/installationv2.ex
index 4acc4b9cf4..47f01c1362 100644
--- a/lib/plausible_web/live/installationv2.ex
+++ b/lib/plausible_web/live/installationv2.ex
@@ -3,8 +3,10 @@ defmodule PlausibleWeb.Live.InstallationV2 do
User assistance module around Plausible installation instructions/onboarding
"""
alias PlausibleWeb.Flows
- alias Plausible.InstallationSupport.{State, Checks, LegacyVerification}
+ alias Phoenix.LiveView.AsyncResult
+ alias Plausible.InstallationSupport.Detection
use PlausibleWeb, :live_view
+ require Logger
def mount(
%{"domain" => domain} = params,
@@ -20,146 +22,151 @@ defmodule PlausibleWeb.Live.InstallationV2 do
:viewer
])
- tracker_script_configuration =
- PlausibleWeb.Tracker.get_or_create_tracker_script_configuration!(site, %{
- outbound_links: true,
- form_submissions: true,
- file_downloads: true,
- installation_type: :manual
- })
-
flow = params["flow"] || Flows.provisioning()
- detect_installation_type? =
- connected?(socket) and flow == Flows.provisioning() and !params["type"]
-
- if detect_installation_type? do
- LegacyVerification.Checks.run("https://#{site.domain}", site.domain,
- checks: [
- Checks.FetchBody,
- Checks.ScanBody
- ],
- report_to: self(),
- async?: true,
- slowdown: 0
- )
- end
+ socket =
+ if connected?(socket) do
+ assign_async(
+ socket,
+ [
+ :recommended_installation_type,
+ :installation_type,
+ :tracker_script_configuration_form,
+ :v1_detected
+ ],
+ fn -> initialize_installation_data(flow, site, params) end
+ )
+ else
+ assign_loading_states(socket)
+ end
{:ok,
assign(socket,
site: site,
- tracker_script_configuration_form:
- to_form(
- Plausible.Site.TrackerScriptConfiguration.installation_changeset(
- tracker_script_configuration,
- %{}
- )
- ),
- flow: params["flow"] || "provisioning",
- detect_installation_type?: detect_installation_type?,
- installation_type: get_installation_type(params, tracker_script_configuration),
- recommended_installation_type: nil
+ flow: flow
)}
end
- def handle_info({:all_checks_done, %State{} = state}, socket) do
- installation_type =
- case state.diagnostics do
- %{wordpress_likely?: true} -> "wordpress"
- %{gtm_likely?: true} -> "gtm"
- _ -> "manual"
+ def handle_params(params, _url, socket) do
+ socket =
+ if socket.assigns.recommended_installation_type.result do
+ assign(socket,
+ installation_type: %AsyncResult{
+ result:
+ get_installation_type(
+ params,
+ socket.assigns.tracker_script_configuration_form.result.data
+ )
+ }
+ )
+ else
+ socket
end
- {:noreply,
- assign(socket,
- installation_type: installation_type,
- recommended_installation_type: installation_type
- )}
- end
-
- def handle_info({:check_start, _}, socket) do
{:noreply, socket}
end
- def handle_params(params, _url, socket) do
- {:noreply,
- assign(socket,
- installation_type:
- get_installation_type(params, socket.assigns.tracker_script_configuration_form.data)
- )}
- end
-
def render(assigns) do
~H"""
<div>
<PlausibleWeb.Components.FlowProgress.render flow={@flow} current_step="Install Plausible" />
<.focus_box>
- <div class="flex flex-row gap-2 bg-gray-100 rounded-md p-1">
- <.tab patch={"?type=manual&flow=#{@flow}"} selected={@installation_type == "manual"}>
- <.script_icon /> Script
- </.tab>
- <.tab patch={"?type=wordpress&flow=#{@flow}"} selected={@installation_type == "wordpress"}>
- <.wordpress_icon /> WordPress
- </.tab>
- <.tab patch={"?type=gtm&flow=#{@flow}"} selected={@installation_type == "gtm"}>
- <.tag_manager_icon /> Tag Manager
- </.tab>
- <.tab patch={"?type=npm&flow=#{@flow}"} selected={@installation_type == "npm"}>
- <.npm_icon /> NPM
- </.tab>
- </div>
-
- <div
- :if={@detect_installation_type? and is_nil(@recommended_installation_type)}
- class="flex items-center justify-center py-8"
- >
- <.spinner class="w-6 h-6" />
- </div>
-
- <.form
- :if={
- not @detect_installation_type? or
- (@detect_installation_type? and not is_nil(@recommended_installation_type))
- }
- for={@tracker_script_configuration_form}
- phx-submit="submit"
- class="mt-4"
- >
- <.input
- type="hidden"
- field={@tracker_script_configuration_form[:installation_type]}
- value={@installation_type}
- />
- <.manual_instructions
- :if={@installation_type == "manual"}
- tracker_script_configuration_form={@tracker_script_configuration_form}
- />
-
- <.wordpress_instructions
- :if={@installation_type == "wordpress"}
- flow={@flow}
- recommended_installation_type={@recommended_installation_type}
- />
- <.gtm_instructions
- :if={@installation_type == "gtm"}
- recommended_installation_type={@recommended_installation_type}
- />
- <.npm_instructions :if={@installation_type == "npm"} />
-
- <.button type="submit" class="w-full mt-8">
- <%= if @flow == PlausibleWeb.Flows.review() do %>
- Verify your installation
- <% else %>
- Start collecting data
- <% end %>
- </.button>
- </.form>
+ <.async_result :let={recommended_installation_type} assign={@recommended_installation_type}>
+ <:loading>
+ <div class="flex items-center justify-center py-8">
+ <.spinner class="w-6 h-6" />
+ </div>
+ </:loading>
+
+ <div class="flex flex-row gap-2 bg-gray-100 rounded-md p-1">
+ <.tab
+ patch={"?type=manual&flow=#{@flow}"}
+ selected={@installation_type.result == "manual"}
+ >
+ <.script_icon /> Script
+ </.tab>
+ <.tab
+ patch={"?type=wordpress&flow=#{@flow}"}
+ selected={@installation_type.result == "wordpress"}
+ >
+ <.wordpress_icon /> WordPress
+ </.tab>
+ <.tab patch={"?type=gtm&flow=#{@flow}"} selected={@installation_type.result == "gtm"}>
+ <.tag_manager_icon /> Tag Manager
+ </.tab>
+ <.tab patch={"?type=npm&flow=#{@flow}"} selected={@installation_type.result == "npm"}>
+ <.npm_icon /> NPM
+ </.tab>
+ </div>
+
+ <div :if={
+ @v1_detected.result == true and @recommended_installation_type.result == "manual" and
+ @installation_type.result == "manual"
+ }>
+ <.notice class="mt-4" theme={:yellow}>
+ Your website is running an outdated version of the tracking script. Please
+ <.styled_link new_tab href="https://plausible.io/docs">update</.styled_link>
+ your tracking script before continuing
+ </.notice>
+ </div>
+
+ <.form for={@tracker_script_configuration_form.result} phx-submit="submit" class="mt-4">
+ <.input
+ type="hidden"
+ field={@tracker_script_configuration_form.result[:installation_type]}
+ value={@installation_type.result}
+ />
+ <.manual_instructions
+ :if={@installation_type.result == "manual"}
+ tracker_script_configuration_form={@tracker_script_configuration_form.result}
+ />
+
+ <.wordpress_instructions
+ :if={@installation_type.result == "wordpress"}
+ flow={@flow}
+ recommended_installation_type={recommended_installation_type}
+ />
+ <.gtm_instructions
+ :if={@installation_type.result == "gtm"}
+ recommended_installation_type={recommended_installation_type}
+ />
+ <.npm_instructions :if={@installation_type.result == "npm"} />
+
+ <.button type="submit" class="w-full mt-8">
+ {verify_cta(@installation_type.result)}
+ </.button>
+ </.form>
+ </.async_result>
</.focus_box>
</div>
"""
end
+ defp verify_cta("manual"), do: "Verify Script installation"
+ defp verify_cta("wordpress"), do: "Verify WordPress installation"
+ defp verify_cta("gtm"), do: "Verify Tag Manager installation"
+ defp verify_cta("npm"), do: "Verify NPM installation"
+
+ defp get_recommended_installation_type(flow, url) do
+ case Detection.perform(url, detect_v1?: flow == Flows.review()) do
+ {:ok, result} ->
+ Logger.debug("Detection result: #{inspect(result)}")
+
+ type =
+ case result do
+ %{gtm_likely: true} -> "gtm"
+ %{wordpress_likely: true} -> "wordpress"
+ _ -> "manual"
+ end
+
+ {type, result.v1_detected}
+
+ _ ->
+ {"manual", false}
+ end
+ end
+
attr :tracker_script_configuration_form, :map, required: true
defp manual_instructions(assigns) do
@@ -500,6 +507,55 @@ defmodule PlausibleWeb.Live.InstallationV2 do
"""
end
+ defp initialize_installation_data(flow, site, params) do
+ {recommended_installation_type, v1_detected} =
+ get_recommended_installation_type(flow, "https://#{site.domain}")
+
+ tracker_script_configuration =
+ PlausibleWeb.Tracker.get_or_create_tracker_script_configuration!(site, %{
+ outbound_links: true,
+ form_submissions: true,
+ file_downloads: true,
+ installation_type: recommended_installation_type
+ })
+
+ selected_installation_type =
+ cond do
+ params["type"] in ["manual", "wordpress", "gtm", "npm"] ->
+ params["type"]
+
+ flow == Flows.review() and
+ not is_nil(tracker_script_configuration.installation_type) ->
+ Atom.to_string(tracker_script_configuration.installation_type)
+
+ true ->
+ recommended_installation_type
+ end
+
+ {:ok,
+ %{
+ recommended_installation_type: recommended_installation_type,
+ v1_detected: v1_detected,
+ installation_type: selected_installation_type,
+ tracker_script_configuration_form:
+ to_form(
+ Plausible.Site.TrackerScriptConfiguration.installation_changeset(
+ tracker_script_configuration,
+ %{}
+ )
+ )
+ }}
+ end
+
+ defp assign_loading_states(socket) do
+ assign(socket,
+ recommended_installation_type: AsyncResult.loading(),
+ v1_detected: AsyncResult.loading(),
+ installation_type: AsyncResult.loading(),
+ tracker_script_configuration_form: AsyncResult.loading()
+ )
+ end
+
defp get_installation_type(params, tracker_script_configuration) do
if params["type"] do
params["type"]
diff --git a/test/plausible_web/live/installationv2_test.exs b/test/plausible_web/live/installationv2_test.exs
index 0e17325afc..ed29274e86 100644
--- a/test/plausible_web/live/installationv2_test.exs
+++ b/test/plausible_web/live/installationv2_test.exs
@@ -3,6 +3,7 @@ defmodule PlausibleWeb.Live.InstallationV2Test do
import Phoenix.LiveViewTest
import Plausible.Test.Support.HTML
+ import Plausible.Teams.Test
alias Plausible.Site.TrackerScriptConfiguration
@@ -14,58 +15,16 @@ defmodule PlausibleWeb.Live.InstallationV2Test do
end
describe "GET /:domain/installationv2" do
- test "static installation screen renders", %{conn: conn, site: site} do
+ test "static installation screen renders with spinner", %{conn: conn, site: site} do
resp = get(conn, "/#{site.domain}/installationv2") |> html_response(200)
- assert resp =~ "Script"
- assert resp =~ "WordPress"
- assert resp =~ "Tag Manager"
- assert resp =~ "NPM"
- end
-
- test "static installation screen renders for flow=review", %{conn: conn, site: site} do
- resp =
- conn
- |> get("/#{site.domain}/installationv2?flow=review")
- |> html_response(200)
-
- assert resp =~ "Verify your installation"
- end
-
- test "renders pre-determined installation type: manual", %{conn: conn, site: site} do
- resp =
- conn |> get("/#{site.domain}/installationv2?type=manual") |> html_response(200)
-
- assert resp =~ "Script installation"
- end
-
- test "renders pre-determined installation type: WordPress", %{conn: conn, site: site} do
- resp =
- conn
- |> get("/#{site.domain}/installationv2?type=wordpress")
- |> html_response(200)
-
- assert resp =~ "WordPress installation"
- end
-
- test "renders pre-determined installation type: GTM", %{conn: conn, site: site} do
- resp =
- conn |> get("/#{site.domain}/installationv2?type=gtm") |> html_response(200)
-
- assert resp =~ "Tag Manager installation"
- end
-
- test "renders pre-determined installation type: NPM", %{conn: conn, site: site} do
- resp =
- conn |> get("/#{site.domain}/installationv2?type=npm") |> html_response(200)
-
- assert resp =~ "NPM installation"
+ assert resp =~ "animate-spin"
end
end
describe "LiveView" do
test "detects installation type when mounted", %{conn: conn, site: site} do
- stub_fetch_body(200, "wp-content")
+ stub_detection_wordpress()
{lv, _} = get_lv(conn, site)
@@ -73,7 +32,7 @@ defmodule PlausibleWeb.Live.InstallationV2Test do
html = render(lv)
{
- text(html) =~ "WordPress installation",
+ text(html) =~ "Verify WordPress installation",
html
}
end)
@@ -85,7 +44,7 @@ defmodule PlausibleWeb.Live.InstallationV2Test do
conn: conn,
site: site
} do
- stub_fetch_body(200, "wp-content")
+ stub_detection_wordpress()
{lv, _} = get_lv(conn, site, "?type=gtm")
@@ -93,7 +52,7 @@ defmodule PlausibleWeb.Live.InstallationV2Test do
html = render(lv)
{
- text(html) =~ "Tag Manager installation",
+ text(html) =~ "Verify Tag Manager installation",
html
}
end)
@@ -102,11 +61,12 @@ defmodule PlausibleWeb.Live.InstallationV2Test do
end
test "allows switching between installation tabs", %{conn: conn, site: site} do
+ stub_detection_manual()
{lv, _html} = get_lv(conn, site, "?type=manual")
assert eventually(fn ->
html = render(lv)
- {html =~ "Script installation", html}
+ {html =~ "Verify Script installation", html}
end)
lv
@@ -114,29 +74,30 @@ defmodule PlausibleWeb.Live.InstallationV2Test do
|> render_click()
html = render(lv)
- assert html =~ "WordPress installation"
+ assert html =~ "Verify WordPress installation"
lv
|> element("a[href*=\"type=gtm\"]")
|> render_click()
html = render(lv)
- assert html =~ "Tag Manager installation"
+ assert html =~ "Verify Tag Manager installation"
lv
|> element("a[href*=\"type=npm\"]")
|> render_click()
html = render(lv)
- assert html =~ "NPM installation"
+ assert html =~ "Verify NPM installation"
end
test "manual installation shows optional measurements", %{conn: conn, site: site} do
+ stub_detection_manual()
{lv, _html} = get_lv(conn, site, "?type=manual&flow=review")
assert eventually(fn ->
html = render(lv)
- {html =~ "Script installation", html}
+ {html =~ "Verify Script installation", html}
end)
html = render(lv)
@@ -147,11 +108,12 @@ defmodule PlausibleWeb.Live.InstallationV2Test do
end
test "manual installation shows advanced options in disclosure", %{conn: conn, site: site} do
+ stub_detection_manual()
{lv, _html} = get_lv(conn, site, "?type=manual&flow=review")
assert eventually(fn ->
html = render(lv)
- {html =~ "Script installation", html}
+ {html =~ "Verify Script installation", html}
end)
html = render(lv)
@@ -168,11 +130,12 @@ defmodule PlausibleWeb.Live.InstallationV2Test do
conn: conn,
site: site
} do
+ stub_detection_manual()
{lv, _html} = get_lv(conn, site, "?type=manual&flow=review")
assert eventually(fn ->
html = render(lv)
- {html =~ "Script installation", html}
+ {html =~ "Verify Script installation", html}
end)
config = TrackerScriptConfiguration |> Plausible.Repo.get_by!(site_id: site.id)
@@ -198,11 +161,12 @@ defmodule PlausibleWeb.Live.InstallationV2Test do
end
test "submitting form redirects to verification", %{conn: conn, site: site} do
+ stub_detection_manual()
{lv, _html} = get_lv(conn, site, "?type=manual")
assert eventually(fn ->
html = render(lv)
- {html =~ "Start collecting data", html}
+ {html =~ "Verify Script installation", html}
end)
lv
@@ -226,8 +190,14 @@ defmodule PlausibleWeb.Live.InstallationV2Test do
conn: conn,
site: site
} do
+ stub_detection_manual()
{lv, _html} = get_lv(conn, site, "?type=manual&flow=review")
+ assert eventually(fn ->
+ html = render(lv)
+ {html =~ "Verify Script installation", html}
+ end)
+
lv
|> element("form[phx-submit='submit']")
|> render_submit(%{
@@ -243,7 +213,7 @@ defmodule PlausibleWeb.Live.InstallationV2Test do
end
test "detected WordPress installation shows special message", %{conn: conn, site: site} do
- stub_fetch_body(200, "wp-content")
+ stub_detection_wordpress()
{lv, _} = get_lv(conn, site)
@@ -258,13 +228,13 @@ defmodule PlausibleWeb.Live.InstallationV2Test do
end
test "detected GTM installation shows special message", %{conn: conn, site: site} do
- stub_fetch_body(200, "googletagmanager.com/gtm.js")
+ stub_detection_gtm()
{lv, _} = get_lv(conn, site)
assert eventually(fn ->
html = render(lv)
- {html =~ "Tag Manager installation", html}
+ {html =~ "Verify Tag Manager installation", html}
end)
assert eventually(fn ->
@@ -276,17 +246,222 @@ defmodule PlausibleWeb.Live.InstallationV2Test do
}
end)
end
+
+ test "shows v1 detection warning for manual installation", %{conn: conn, site: site} do
+ stub_detection_manual_with_v1()
+
+ {lv, _} = get_lv(conn, site, "?type=manual")
+
+ assert eventually(fn ->
+ html = render(lv)
+
+ {
+ text(html) =~
+ "Your website is running an outdated version of the tracking script",
+ html
+ }
+ end)
+ end
+
+ test "does not show v1 detection warning for non-manual installation", %{
+ conn: conn,
+ site: site
+ } do
+ stub_detection_wordpress_with_v1()
+
+ {lv, _} = get_lv(conn, site, "?type=wordpress")
+
+ assert eventually(fn ->
+ html = render(lv)
+ {html =~ "Verify WordPress installation", html}
+ end)
+
+ html = render(lv)
+ refute text(html) =~ "Your website is running an outdated version of the tracking script"
+ end
+
+ test "falls back to manual installation when detection fails", %{conn: conn, site: site} do
+ stub_detection_error()
+
+ ExUnit.CaptureLog.capture_log(fn ->
+ {lv, _} = get_lv(conn, site)
+
+ assert eventually(fn ->
+ html = render(lv)
+ # Should default to manual installation when detection returns {:error, _}
+ {html =~ "Verify Script installation", html}
+ end)
+ end)
+ end
end
- defp stub_fetch_body(f) when is_function(f, 1) do
- Req.Test.stub(Plausible.InstallationSupport.Checks.FetchBody, f)
+ describe "Authorization" do
+ test "requires site access permissions", %{conn: conn} do
+ other_user = insert(:user)
+ other_site = new_site(owner: other_user)
+
+ assert_raise Ecto.NoResultsError, fn ->
+ get_lv(conn, other_site)
+ end
+ end
+
+ test "allows viewer access to installation page", %{conn: conn, user: user} do
+ site = new_site()
+ add_guest(site, user: user, role: :viewer)
+ stub_detection_manual()
+
+ {lv, _} = get_lv(conn, site)
+
+ assert eventually(fn ->
+ html = render(lv)
+ {html =~ "Verify Script installation", html}
+ end)
+ end
+
+ test "allows editor access to installation page", %{conn: conn, user: user} do
+ site = new_site()
+ add_guest(site, user: user, role: :editor)
+ stub_detection_manual()
+
+ {lv, _} = get_lv(conn, site)
+
+ assert eventually(fn ->
+ html = render(lv)
+ {html =~ "Verify Script installation", html}
+ end)
+ end
+ end
+
+ describe "URL Parameter Handling" do
+ test "falls back to manual installation when invalid installation type parameter supplied", %{
+ conn: conn,
+ site: site
+ } do
+ stub_detection_manual()
+
+ {lv, _} = get_lv(conn, site, "?type=invalid")
+
+ assert eventually(fn ->
+ html = render(lv)
+ {html =~ "Verify Script installation", html}
+ end)
+ end
+
+ test "falls back to provisioning flow when invalid flow parameter supplied", %{
+ conn: conn,
+ site: site
+ } do
+ stub_detection_manual()
+
+ {lv, _} = get_lv(conn, site, "?flow=invalid")
+
+ assert eventually(fn ->
+ html = render(lv)
+ {html =~ "Verify Script installation", html}
+ end)
+ end
+ end
+
+ describe "Detection Result Combinations" do
+ test "When GTM + Wordpress detected, GTM takes precedence", %{conn: conn, site: site} do
+ stub_detection_result(%{
+ "v1Detected" => false,
+ "gtmLikely" => true,
+ "wordpressLikely" => true,
+ "wordpressPlugin" => false
+ })
+
+ {lv, _} = get_lv(conn, site)
+
+ assert eventually(fn ->
+ html = render(lv)
+ {html =~ "Verify Tag Manager installation", html}
+ end)
+ end
+ end
+
+ describe "Legacy Installations" do
+ test "uses detected type in review flow when installation_type is nil", %{
+ conn: conn,
+ site: site
+ } do
+ _config =
+ PlausibleWeb.Tracker.get_or_create_tracker_script_configuration!(site, %{
+ installation_type: nil,
+ outbound_links: true,
+ file_downloads: false,
+ form_submissions: true
+ })
+
+ stub_detection_wordpress()
+
+ {lv, _} = get_lv(conn, site, "?flow=review")
+
+ assert eventually(fn ->
+ html = render(lv)
+ {html =~ "Verify WordPress installation", html}
+ end)
+ end
+ end
+
+ defp stub_detection_manual do
+ stub_detection_result(%{
+ "v1Detected" => false,
+ "gtmLikely" => false,
+ "wordpressLikely" => false,
+ "wordpressPlugin" => false
+ })
+ end
+
+ defp stub_detection_wordpress do
+ stub_detection_result(%{
+ "v1Detected" => false,
+ "gtmLikely" => false,
+ "wordpressLikely" => true,
+ "wordpressPlugin" => false
+ })
+ end
+
+ defp stub_detection_gtm do
+ stub_detection_result(%{
+ "v1Detected" => false,
+ "gtmLikely" => true,
+ "wordpressLikely" => false,
+ "wordpressPlugin" => false
+ })
+ end
+
+ defp stub_detection_manual_with_v1 do
+ stub_detection_result(%{
+ "v1Detected" => true,
+ "gtmLikely" => false,
+ "wordpressLikely" => false,
+ "wordpressPlugin" => false
+ })
+ end
+
+ defp stub_detection_wordpress_with_v1 do
+ stub_detection_result(%{
+ "v1Detected" => true,
+ "gtmLikely" => false,
+ "wordpressLikely" => true,
+ "wordpressPlugin" => false
+ })
+ end
+
+ defp stub_detection_result(js_data) do
+ Req.Test.stub(:global, fn conn ->
+ conn
+ |> put_resp_content_type("application/json")
+ |> send_resp(200, Jason.encode!(%{"data" => Map.put(js_data, "completed", true)}))
+ end)
end
- defp stub_fetch_body(status, body) do
- stub_fetch_body(fn conn ->
+ defp stub_detection_error do
+ Req.Test.stub(:global, fn conn ->
conn
- |> put_resp_content_type("text/html")
- |> send_resp(status, body)
+ |> put_resp_content_type("application/json")
+ |> send_resp(200, Jason.encode!(%{"data" => %{"error" => "Simulated browser error"}}))
end)
end