Open source, privacy-first web analytics. Lightweight, cookie-free Google Analytics alternative. Self-hosted or cloud.
TypeScript analyticsanalytics-dashboardclickhouseelixirgoogle-analyticsmarketingopen-source-analyticsphoenixplausible-analyticspostgresqlprivacyprivacy-friendlyself-hostedsimple-analyticsstatisticstailwindcssweb-analyticswebsite-analyticswebsite-statswebsite-tracking
Verification Fixes (#5822)
RobertJoonasad2c8e8e3907
diff --git a/extra/lib/plausible/installation_support/browserless_config.ex b/extra/lib/plausible/installation_support/browserless_config.exindex 25d457cf12..43532a1bf9 100644--- a/extra/lib/plausible/installation_support/browserless_config.ex+++ b/extra/lib/plausible/installation_support/browserless_config.ex@@ -4,41 +4,14 @@ defmodule Plausible.InstallationSupport.BrowserlessConfig do"""use Plausible- @retry_policy_by_status %{+ @retry_policy %{# rate limit429 => {:delay, 1000},- # timeout- 408 => {:delay, 500},# even 400 are verified manually to sometimes succeed on retry400 => {:delay, 500}}- @doc """- Examples:-- iex> retry_browserless_request([429, 400]).(nil, %{status: 429})- {:delay, 1000}-- iex> retry_browserless_request([429, 400]).(nil, %{status: 400})- {:delay, 500}-- iex> retry_browserless_request([429, 400]).(nil, %{status: 408})- nil-- iex> retry_browserless_request([429, 400]).(nil, :some_error)- false- """-- def retry_browserless_request(statuses_to_retry) do- policies = Map.take(@retry_policy_by_status, statuses_to_retry)-- fn _request, response_or_error ->- case response_or_error do- %{status: status} -> Map.get(policies, status)- _ -> false- end- end- end+ def retry_policy(), do: @retry_policyon_ee dodef browserless_function_api_endpoint() dodiff --git a/extra/lib/plausible/installation_support/check.ex b/extra/lib/plausible/installation_support/check.exindex 6be4ee7faf..c8367bf6e1 100644--- a/extra/lib/plausible/installation_support/check.ex+++ b/extra/lib/plausible/installation_support/check.ex@@ -40,7 +40,7 @@ defmodule Plausible.InstallationSupport.Check do"Error running check #{inspect(__MODULE__)} on #{state.url}: #{inspect(e)}")- put_diagnostics(state, service_error: e)+ put_diagnostics(state, service_error: %{code: :internal_check_error, extra: e})endend)@@ -49,7 +49,14 @@ defmodule Plausible.InstallationSupport.Check docatch:exit, {:timeout, _} ->Task.shutdown(task, :brutal_kill)- put_diagnostics(state, service_error: :check_timeout)+ check_name = __MODULE__ |> Atom.to_string() |> String.split(".") |> List.last()++ put_diagnostics(state,+ service_error: %{+ code: :internal_check_timeout,+ extra: "#{check_name} timed out after #{timeout_ms()}ms"+ }+ )endendenddiff --git a/extra/lib/plausible/installation_support/checks/detection.ex b/extra/lib/plausible/installation_support/checks/detection.exindex f071affced..d5a3248aee 100644--- a/extra/lib/plausible/installation_support/checks/detection.ex+++ b/extra/lib/plausible/installation_support/checks/detection.ex@@ -53,8 +53,9 @@ defmodule Plausible.InstallationSupport.Checks.Detection do}"""- # We define a timeout for the browserless endpoint call to avoid waiting too long for a response- @endpoint_timeout_ms 5_000+ # We rely on Req's `:receive_timeout` to avoid waiting too long for a response. We also pass the same timeout (+ 1s) via a query+ # param to the Browserless /function API to not waste resources.+ @req_timeout 5_000# This timeout determines how long we wait for window.plausible to be initialized on the page, used for detecting whether v1 installed@plausible_window_check_timeout_ms 1_500@@ -84,8 +85,14 @@ defmodule Plausible.InstallationSupport.Checks.Detection dodebug: Application.get_env(:plausible, :environment) == "dev"}}),- params: %{timeout: @endpoint_timeout_ms},- retry: BrowserlessConfig.retry_browserless_request([429, 400]),+ params: %{timeout: @req_timeout + 1000},+ retry: fn _request, response_or_error ->+ case response_or_error do+ %{status: status} -> Map.get(BrowserlessConfig.retry_policy(), status, false)+ _ -> false+ end+ end,+ receive_timeout: @req_timeout,retry_log_level: :warning,max_retries: @max_retries]@@ -95,10 +102,13 @@ defmodule Plausible.InstallationSupport.Checks.Detection do{:ok, %{body: body, status: status}} ->handle_browserless_response(state, body, status)+ {:error, %Req.TransportError{reason: :timeout}} ->+ put_diagnostics(state, service_error: %{code: :browserless_timeout})+{:error, %{reason: reason}} ->Logger.warning(warning_message("Browserless request error: #{inspect(reason)}", state))- put_diagnostics(state, service_error: reason)+ put_diagnostics(state, service_error: %{code: :req_error, extra: reason})endend@@ -120,7 +130,9 @@ defmodule Plausible.InstallationSupport.Checks.Detection do))- put_diagnostics(state, service_error: data["error"]["message"])+ put_diagnostics(state,+ service_error: %{code: :browserless_client_error, extra: data["error"]["message"]}+ )endend@@ -130,7 +142,7 @@ defmodule Plausible.InstallationSupport.Checks.Detection dowarning_message("#{error}; body: #{inspect(body)}", state)|> Logger.warning()- put_diagnostics(state, service_error: error)+ put_diagnostics(state, service_error: %{code: :bad_browserless_response, extra: status})enddefp warning_message(message, state) dodiff --git a/extra/lib/plausible/installation_support/checks/installation_v2.ex b/extra/lib/plausible/installation_support/checks/installation_v2.exindex cae296f37f..f90d8eff50 100644--- a/extra/lib/plausible/installation_support/checks/installation_v2.ex+++ b/extra/lib/plausible/installation_support/checks/installation_v2.ex@@ -74,8 +74,9 @@ defmodule Plausible.InstallationSupport.Checks.InstallationV2 do# To support browserless API being unavailable or overloaded, we retry the endpoint call if it doesn't return a successful response@max_retries 1- # We define a timeout for the browserless endpoint call to avoid waiting too long for a response- @endpoint_timeout_ms 15_000+ # We rely on Req's `:receive_timeout` to avoid waiting too long for a response. We also pass the same timeout (+ 1s) via a query+ # param to the Browserless /function API to not waste resources.+ @req_timeout 15_000# This timeout determines how long we wait for window.plausible to be initialized on the page, including sending the test event@plausible_window_check_timeout_ms 4_000@@ -107,11 +108,17 @@ defmodule Plausible.InstallationSupport.Checks.InstallationV2 dodebug: Application.get_env(:plausible, :environment) == "dev"}}),- params: %{timeout: @endpoint_timeout_ms},- retry: BrowserlessConfig.retry_browserless_request([429, 408, 400]),+ params: %{timeout: @req_timeout + 1000},+ retry: fn _request, response_or_error ->+ case response_or_error do+ %{status: status} -> Map.get(BrowserlessConfig.retry_policy(), status, false)+ %Req.TransportError{reason: :timeout} -> {:delay, 500}+ _ -> false+ end+ end,retry_log_level: :warning,max_retries: @max_retries,- receive_timeout: @endpoint_timeout_ms + 2_000+ receive_timeout: @req_timeout]extra_opts = Application.get_env(:plausible, __MODULE__)[:req_opts] || []@@ -121,10 +128,13 @@ defmodule Plausible.InstallationSupport.Checks.InstallationV2 do{:ok, %{body: body, status: status}} ->handle_browserless_response(state, body, status)+ {:error, %Req.TransportError{reason: :timeout}} ->+ put_diagnostics(state, service_error: %{code: :browserless_timeout})+{:error, %{reason: reason}} ->Logger.warning(warning_message("Browserless request error: #{inspect(reason)}", state))- put_diagnostics(state, service_error: reason)+ put_diagnostics(state, service_error: %{code: :req_error, extra: reason})endend@@ -146,7 +156,9 @@ defmodule Plausible.InstallationSupport.Checks.InstallationV2 do))- put_diagnostics(state, service_error: data["error"]["message"])+ put_diagnostics(state,+ service_error: %{code: :browserless_client_error, extra: data["error"]["message"]}+ )endend@@ -156,7 +168,7 @@ defmodule Plausible.InstallationSupport.Checks.InstallationV2 dowarning_message("#{error}; body: #{inspect(body)}", state)|> Logger.warning()- put_diagnostics(state, service_error: error)+ put_diagnostics(state, service_error: %{code: :bad_browserless_response, extra: status})enddefp warning_message(message, state) dodiff --git a/extra/lib/plausible/installation_support/checks/url.ex b/extra/lib/plausible/installation_support/checks/url.exindex d37363d16e..0d0de94df5 100644--- a/extra/lib/plausible/installation_support/checks/url.ex+++ b/extra/lib/plausible/installation_support/checks/url.ex@@ -22,12 +22,12 @@ defmodule Plausible.InstallationSupport.Checks.Url doelse{:error, :no_a_record} ->put_diagnostics(%State{state | skip_further_checks?: true},- service_error: :domain_not_found+ service_error: %{code: :domain_not_found})_ ->put_diagnostics(%State{state | skip_further_checks?: true},- service_error: :invalid_url+ service_error: %{code: :invalid_url})endend@@ -39,7 +39,7 @@ defmodule Plausible.InstallationSupport.Checks.Url do{:error, :domain_not_found} ->put_diagnostics(%State{state | url: nil, skip_further_checks?: true},- service_error: :domain_not_found+ service_error: %{code: :domain_not_found})endenddiff --git a/extra/lib/plausible/installation_support/verification/checks.ex b/extra/lib/plausible/installation_support/verification/checks.exindex d35fbeed47..4718d2bab1 100644--- a/extra/lib/plausible/installation_support/verification/checks.ex+++ b/extra/lib/plausible/installation_support/verification/checks.ex@@ -62,8 +62,13 @@ defmodule Plausible.InstallationSupport.Verification.Checks do{false, _} ->:skip- {_, %{unhandled: true}} ->- Sentry.capture_message("Unhandled case for site verification (v2)",+ {_, %{unhandled: true, browserless_issue: browserless_issue}} ->+ sentry_msg =+ if browserless_issue,+ do: "Browserless failure in verification (v2)",+ else: "Unhandled case for site verification (v2)"++ Sentry.capture_message(sentry_msg,extra: %{message: inspect(diagnostics),url: url,diff --git a/extra/lib/plausible/installation_support/verification/diagnostics.ex b/extra/lib/plausible/installation_support/verification/diagnostics.exindex 23e249c11d..5ea40d0346 100644--- a/extra/lib/plausible/installation_support/verification/diagnostics.ex+++ b/extra/lib/plausible/installation_support/verification/diagnostics.ex@@ -82,7 +82,7 @@ defmodule Plausible.InstallationSupport.Verification.Diagnostics do)when response_status in [200, 202] anddomain == expected_domain,- do: error(@error_succeeds_only_after_cache_bust)+ do: handled_error(@error_succeeds_only_after_cache_bust)def interpret(%__MODULE__{@@ -116,8 +116,10 @@ defmodule Plausible.InstallationSupport.Verification.Diagnostics do_url)when response_status in [200, 202] and- domain != expected_domain,- do: error_unexpected_domain(selected_installation_type)+ domain != expected_domain do+ error_unexpected_domain(selected_installation_type)+ |> handled_error()+ end@error_proxy_network_error Error.new!(%{message:@@ -148,9 +150,9 @@ defmodule Plausible.InstallationSupport.Verification.Diagnostics doproxying? = not String.starts_with?(request_url, PlausibleWeb.Endpoint.url())if proxying? do- error(@error_proxy_network_error)+ handled_error(@error_proxy_network_error)else- error(@error_plausible_network_error)+ handled_error(@error_plausible_network_error)endend@@ -166,8 +168,10 @@ defmodule Plausible.InstallationSupport.Verification.Diagnostics do_url)when plausible_is_on_window != true and- plausible_is_initialized != true,- do: error_plausible_not_found("manual")+ plausible_is_initialized != true do+ error_plausible_not_found("manual")+ |> handled_error()+ end@error_csp_disallowed Error.new!(%{message:@@ -185,7 +189,7 @@ defmodule Plausible.InstallationSupport.Verification.Diagnostics do_expected_domain,_url),- do: error(@error_csp_disallowed)+ do: handled_error(@error_csp_disallowed)@error_domain_not_found Error.new!(%{message: "We couldn't find your website at <%= @attempted_url %>",@@ -194,12 +198,12 @@ defmodule Plausible.InstallationSupport.Verification.Diagnostics dourl: @verify_manually_url})- def interpret(%__MODULE__{service_error: service_error}, expected_domain, url)- when service_error in [:domain_not_found, :invalid_url] do+ def interpret(%__MODULE__{service_error: %{code: code}}, expected_domain, url)+ when code in [:domain_not_found, :invalid_url] doattempted_url = if url, do: url, else: "https://#{expected_domain}"@error_domain_not_found- |> error(attempted_url: attempted_url)+ |> handled_error(attempted_url: attempted_url)|> struct!(data: %{offer_custom_url_input: true})end@@ -211,15 +215,32 @@ defmodule Plausible.InstallationSupport.Verification.Diagnostics dourl: @verify_manually_url})- def interpret(%__MODULE__{service_error: "net::" <> _}, _expected_domain, url)+ def interpret(+ %__MODULE__{service_error: %{code: :browserless_client_error, extra: "net::" <> _}},+ _expected_domain,+ url+ )when is_binary(url) doattempted_url = shorten_url(url)@error_browserless_network- |> error(attempted_url: attempted_url)+ |> handled_error(attempted_url: attempted_url)|> struct!(data: %{offer_custom_url_input: true})end+ @error_browserless_temporary Error.new!(%{+ message:+ "Our verification tool encountered a temporary service error",+ recommendation:+ "Please try again in a few minutes or verify your installation manually",+ url: @verify_manually_url+ })++ def interpret(%__MODULE__{service_error: %{code: code}}, _expected_domain, _url)+ when code in [:bad_browserless_response, :browserless_timeout] do+ unhandled_error(@error_browserless_temporary, browserless_issue: true)+ end+@error_unexpected_page_response Error.new!(%{message:"We couldn't verify your website at <%= @attempted_url %>",@@ -244,7 +265,7 @@ defmodule Plausible.InstallationSupport.Verification.Diagnostics doattempted_url = shorten_url(url)@error_unexpected_page_response- |> error(attempted_url: attempted_url, page_response_status: page_response_status)+ |> handled_error(attempted_url: attempted_url, page_response_status: page_response_status)|> struct!(data: %{offer_custom_url_input: true})end@@ -256,12 +277,14 @@ defmodule Plausible.InstallationSupport.Verification.Diagnostics do},_expected_domain,_url- ),- do: error_plausible_not_found(selected_installation_type)+ ) do+ error_plausible_not_found(selected_installation_type)+ |> handled_error()+ enddef interpret(%__MODULE__{} = diagnostics, _expected_domain, _url) doerror_plausible_not_found(diagnostics.selected_installation_type)- |> Map.put(:data, %{unhandled: true})+ |> unhandled_error()end@message_plausible_not_found "We couldn't detect Plausible on your site"@@ -291,10 +314,10 @@ defmodule Plausible.InstallationSupport.Verification.Diagnostics do})defp error_plausible_not_found(selected_installation_type) docase selected_installation_type do- "npm" -> error(@error_plausible_not_found_for_npm)- "gtm" -> error(@error_plausible_not_found_for_gtm)- "wordpress" -> error(@error_plausible_not_found_for_wordpress)- _ -> error(@error_plausible_not_found_for_manual)+ "npm" -> @error_plausible_not_found_for_npm+ "gtm" -> @error_plausible_not_found_for_gtm+ "wordpress" -> @error_plausible_not_found_for_wordpress+ _ -> @error_plausible_not_found_for_manualendend@@ -328,10 +351,10 @@ defmodule Plausible.InstallationSupport.Verification.Diagnostics do})defp error_unexpected_domain(selected_installation_type) docase selected_installation_type do- "npm" -> error(@error_unexpected_domain_for_npm)- "gtm" -> error(@error_unexpected_domain_for_gtm)- "wordpress" -> error(@error_unexpected_domain_for_wordpress)- _ -> error(@error_unexpected_domain_for_manual)+ "npm" -> @error_unexpected_domain_for_npm+ "gtm" -> @error_unexpected_domain_for_gtm+ "wordpress" -> @error_unexpected_domain_for_wordpress+ _ -> @error_unexpected_domain_for_manualendend@@ -343,7 +366,7 @@ defmodule Plausible.InstallationSupport.Verification.Diagnostics do%Result{ok?: true}end- defp error(%Error{} = error, assigns \\ []) do+ defp handled_error(%Error{} = error, assigns \\ []) domessage = EEx.eval_string(error.message, assigns: assigns)recommendation = EEx.eval_string(error.recommendation, assigns: assigns)@@ -353,4 +376,15 @@ defmodule Plausible.InstallationSupport.Verification.Diagnostics dorecommendations: [%{text: recommendation, url: error.url}]}end++ defp unhandled_error(%Error{} = error, opts \\ []) do+ browserless_issue = Keyword.get(opts, :browserless_issue, false)++ %Result{+ ok?: false,+ data: %{unhandled: true, browserless_issue: browserless_issue},+ errors: [error.message],+ recommendations: [%{text: error.recommendation, url: error.url}]+ }+ endenddiff --git a/test/plausible/installation_support/check_test.exs b/test/plausible/installation_support/check_test.exsindex 71ab107811..2142c8cbae 100644--- a/test/plausible/installation_support/check_test.exs+++ b/test/plausible/installation_support/check_test.exs@@ -36,7 +36,12 @@ defmodule Plausible.InstallationSupport.CheckTest doassert log =~~s|Error running check Plausible.InstallationSupport.CheckTest.FaultyCheckRaise on https://example.com: %RuntimeError{message: "boom"}|- assert_matches %Verification.Diagnostics{service_error: %RuntimeError{message: "boom"}} =+ assert_matches %Verification.Diagnostics{+ service_error: %{+ code: :internal_check_error,+ extra: %RuntimeError{message: "boom"}+ }+ } =result.diagnosticsend@@ -65,7 +70,9 @@ defmodule Plausible.InstallationSupport.CheckTest doassert log =~~s|Error running check Plausible.InstallationSupport.CheckTest.FaultyCheckThrow on https://example.com: :boom|- assert_matches %Verification.Diagnostics{service_error: :boom} = result.diagnostics+ assert_matches %Verification.Diagnostics{+ service_error: %{code: :internal_check_error, extra: :boom}+ } = result.diagnosticsendtest "a check that times out" do@@ -91,7 +98,12 @@ defmodule Plausible.InstallationSupport.CheckTest doresult =FaultyCheckTimeout.perform_safe(state)- assert_matches %Verification.Diagnostics{service_error: :check_timeout} = result.diagnostics+ assert_matches %Verification.Diagnostics{+ service_error: %{+ code: :internal_check_timeout,+ extra: "FaultyCheckTimeout timed out after 100ms"+ }+ } = result.diagnosticsendendenddiff --git a/test/plausible/installation_support/checks/url_test.exs b/test/plausible/installation_support/checks/url_test.exsindex d736adc0bc..864114beeb 100644--- a/test/plausible/installation_support/checks/url_test.exs+++ b/test/plausible/installation_support/checks/url_test.exs@@ -83,7 +83,7 @@ defmodule Plausible.InstallationSupport.Checks.UrlTest do})assert state.url == nil- assert state.diagnostics.service_error == :domain_not_found+ assert state.diagnostics.service_error == %{code: :domain_not_found}assert state.skip_further_checks?endend@@ -119,7 +119,7 @@ defmodule Plausible.InstallationSupport.Checks.UrlTest do})assert state.url == "file://example.com/archives/news?p=any#fragment"- assert state.diagnostics.service_error == :invalid_url+ assert state.diagnostics.service_error == %{code: :invalid_url}assert state.skip_further_checks?end@@ -140,7 +140,7 @@ defmodule Plausible.InstallationSupport.Checks.UrlTest do})assert state.url == url- assert state.diagnostics.service_error == :domain_not_found+ assert state.diagnostics.service_error == %{code: :domain_not_found}assert state.skip_further_checks?endenddiff --git a/test/plausible/installation_support/verification/checks_test.exs b/test/plausible/installation_support/verification/checks_test.exsindex 4baa8ea60a..f0ad063c92 100644--- a/test/plausible/installation_support/verification/checks_test.exs+++ b/test/plausible/installation_support/verification/checks_test.exs@@ -10,159 +10,88 @@ defmodule Plausible.InstallationSupport.Verification.ChecksTest doalias Plausible.InstallationSupport.Resultuse Plausible.Test.Support.DNSimport Plug.Conn+ import ExUnit.CaptureLog@moduletag :capture_log- describe "running verification" do- for status <- [200, 202] do- test "returns success if test event response status is #{status} and domain is as expected" do- expected_domain = "example.com"- url_to_verify = "https://#{expected_domain}"-- stub_lookup_a_records(expected_domain)-- verification_counter =- stub_verification_result(%{- "completed" => true,- "trackerIsInHtml" => true,- "plausibleIsOnWindow" => true,- "plausibleIsInitialized" => true,- "testEvent" => %{- "normalizedBody" => %{- "domain" => "example.com"- },- "responseStatus" => unquote(status)- }- })+ @expected_domain "example.com"+ @url_to_verify "https://#{@expected_domain}"- assert %Result{- ok?: true- } ==- Checks.run(url_to_verify, expected_domain, "manual",- report_to: nil,- async?: false,- slowdown: 0- )- |> Checks.interpret_diagnostics()+ describe "URL check" do+ test "returns error when DNS check fails with domain not found error, offers custom URL input" do+ stub_lookup_a_records(@expected_domain, [])- assert 1 == :atomics.get(verification_counter, 1)- end+ assert_matches %Result{+ ok?: false,+ data: %{offer_custom_url_input: true},+ errors: [+ ^any(:string, ~r/We couldn't find your website at #{@url_to_verify}$/)+ ],+ recommendations: [+ %{+ text:+ "Please check that the domain you entered is correct and reachable publicly. If it's intentionally private, you'll need to verify that Plausible works manually",+ url:+ "https://plausible.io/docs/troubleshoot-integration#how-to-manually-check-your-integration"+ }+ ]+ } =+ Checks.run(@url_to_verify, @expected_domain, "manual",+ report_to: nil,+ async?: false,+ slowdown: 0+ )+ |> Checks.interpret_diagnostics()end- test "returns error when it 'succeeds', but only after cache bust" do- expected_domain = "example.com"- url_to_verify = "https://#{expected_domain}"-- stub_lookup_a_records(expected_domain)-- counter = :atomics.new(1, [])-- get_context_url_from_body = fn conn ->- {:ok, body, _conn} = Plug.Conn.read_body(conn)- JSON.decode!(body)["context"]["url"]- end-- stub_verification_result(fn conn ->- js_data =- if :atomics.add_get(counter, 1, 1) == 1 do- assert get_context_url_from_body.(conn) == url_to_verify-- %{- "completed" => true,- "trackerIsInHtml" => false,- "plausibleIsOnWindow" => false,- "plausibleIsInitialized" => false- }- else- assert [_, "plausible_verification" <> _] =- String.split(get_context_url_from_body.(conn), "?")-- %{- "completed" => true,- "trackerIsInHtml" => true,- "plausibleIsOnWindow" => true,- "plausibleIsInitialized" => true,- "testEvent" => %{- "normalizedBody" => %{- "domain" => "example.com"- },- "responseStatus" => 200- }- }- end-- conn- |> put_resp_content_type("application/json")- |> send_resp(200, Jason.encode!(%{"data" => js_data}))- end)+ test "returns error when DNS check fails with invalid URL error, offers custom URL input" do+ url_to_verify = "file://#{@expected_domain}"+ stub_lookup_a_records(@expected_domain, [])assert_matches %Result{ok?: false,- errors: [^any(:string, ~r/.*cache.*/)],+ data: %{offer_custom_url_input: true},+ errors: [+ ^any(:string, ~r/We couldn't find your website at #{url_to_verify}$/)+ ],recommendations: [%{- text: ^any(:string, ~r/.*cache.*/),+ text:+ "Please check that the domain you entered is correct and reachable publicly. If it's intentionally private, you'll need to verify that Plausible works manually",url:- "https://plausible.io/docs/troubleshoot-integration#have-you-cleared-the-cache-of-your-site"+ "https://plausible.io/docs/troubleshoot-integration#how-to-manually-check-your-integration"}]} =- Checks.run(url_to_verify, expected_domain, "manual",+ Checks.run(url_to_verify, @expected_domain, "manual",report_to: nil,async?: false,slowdown: 0)|> Checks.interpret_diagnostics()-- assert 2 == :atomics.get(counter, 1)end+ end- test "cache bust diagnostics fully replace the initial installation check diagnostics" do- expected_domain = "example.com"- url_to_verify = "https://#{expected_domain}"-- stub_lookup_a_records(expected_domain)-- counter = :atomics.new(1, [])-- stub_verification_result(fn conn ->- if :atomics.add_get(counter, 1, 1) == 1 do- js_data =- %{- "completed" => true,- "trackerIsInHtml" => false,- "plausibleIsOnWindow" => false,- "plausibleIsInitialized" => false+ describe "InstallationV2 check" do+ for status <- [200, 202] do+ test "returns success if test event response status is #{status} and domain is as expected" do+ verification_stub =+ json_response_verification_stub(%{+ "completed" => true,+ "trackerIsInHtml" => true,+ "plausibleIsOnWindow" => true,+ "plausibleIsInitialized" => true,+ "testEvent" => %{+ "normalizedBody" => %{+ "domain" => @expected_domain+ },+ "responseStatus" => unquote(status)}+ })- conn- |> put_resp_content_type("application/json")- |> send_resp(200, Jason.encode!(%{"data" => js_data}))- else- conn- |> put_resp_content_type("text/html")- |> send_resp(408, "Request has timed out")- end- end)-- state =- Checks.run(url_to_verify, expected_domain, "manual",- report_to: nil,- async?: false,- slowdown: 0- )-- assert_matches %Diagnostics{- tracker_is_in_html: nil,- plausible_is_on_window: nil,- plausible_is_initialized: nil,- service_error: ^any(:string, ~r/.*408.*/)- } = state.diagnostics-- # Browserless gets called 3 times:- # 1) initial/regular installation check- # 2) cache bust installation check- # 3) retry due to #2 responding with 408- assert 3 == :atomics.get(counter, 1)+ assert %Result{ok?: true} ==+ run_checks(verification_stub, expected_req_count: 1)+ |> Checks.interpret_diagnostics()+ endendfor {installation_type, expected_recommendation} <- [@@ -176,12 +105,8 @@ defmodule Plausible.InstallationSupport.Verification.ChecksTest do"Please check that the snippet on your site matches the installation instructions exactly, or verify your installation manually"}] dotest "returns error when test event domain doesn't match the expected domain, with recommendation for installation type: #{installation_type}" do- expected_domain = "example.com"- url_to_verify = "https://#{expected_domain}"- stub_lookup_a_records(expected_domain)-- verification_counter =- stub_verification_result(%{+ verification_stub =+ json_response_verification_stub(%{"completed" => true,"trackerIsInHtml" => true,"plausibleIsOnWindow" => true,@@ -205,24 +130,17 @@ defmodule Plausible.InstallationSupport.Verification.ChecksTest do}]} =- Checks.run(url_to_verify, expected_domain, unquote(installation_type),- report_to: nil,- async?: false,- slowdown: 0+ run_checks(verification_stub,+ installation_type: unquote(installation_type),+ expected_req_count: 2)|> Checks.interpret_diagnostics()-- assert 2 == :atomics.get(verification_counter, 1)endendtest "returns error when proxy network error occurs" do- expected_domain = "example.com"- url_to_verify = "https://#{expected_domain}"- stub_lookup_a_records(expected_domain)-- verification_counter =- stub_verification_result(%{+ verification_stub =+ json_response_verification_stub(%{"completed" => true,"trackerIsInHtml" => true,"plausibleIsOnWindow" => true,@@ -246,23 +164,13 @@ defmodule Plausible.InstallationSupport.Verification.ChecksTest do}]} =- Checks.run(url_to_verify, expected_domain, "manual",- report_to: nil,- async?: false,- slowdown: 0- )+ run_checks(verification_stub, expected_req_count: 2)|> Checks.interpret_diagnostics()-- assert 2 == :atomics.get(verification_counter, 1)endtest "returns error when Plausible network error occurs" do- expected_domain = "example.com"- url_to_verify = "https://#{expected_domain}"- stub_lookup_a_records(expected_domain)-- verification_counter =- stub_verification_result(%{+ verification_stub =+ json_response_verification_stub(%{"completed" => true,"trackerIsInHtml" => true,"plausibleIsOnWindow" => true,@@ -287,23 +195,13 @@ defmodule Plausible.InstallationSupport.Verification.ChecksTest do}]} =- Checks.run(url_to_verify, expected_domain, "manual",- report_to: nil,- async?: false,- slowdown: 0- )+ run_checks(verification_stub, expected_req_count: 2)|> Checks.interpret_diagnostics()-- assert 2 == :atomics.get(verification_counter, 1)endtest "returns error when the snippet is not found for manual installation method, it has priority over CSP-related error" do- expected_domain = "example.com"- url_to_verify = "https://#{expected_domain}"- stub_lookup_a_records(expected_domain)-- verification_counter =- stub_verification_result(%{+ verification_stub =+ json_response_verification_stub(%{"completed" => true,"trackerIsInHtml" => false,"plausibleIsOnWindow" => nil,@@ -323,23 +221,13 @@ defmodule Plausible.InstallationSupport.Verification.ChecksTest do}]} =- Checks.run(url_to_verify, expected_domain, "manual",- report_to: nil,- async?: false,- slowdown: 0- )+ run_checks(verification_stub, expected_req_count: 2)|> Checks.interpret_diagnostics()-- assert 2 == :atomics.get(verification_counter, 1)endtest "returns error when Plausible domain is disallowed by CSP" do- expected_domain = "example.com"- url_to_verify = "https://#{expected_domain}"- stub_lookup_a_records(expected_domain)-- verification_counter =- stub_verification_result(%{+ verification_stub =+ json_response_verification_stub(%{"completed" => true,"trackerIsInHtml" => true,"plausibleIsOnWindow" => nil,@@ -361,81 +249,15 @@ defmodule Plausible.InstallationSupport.Verification.ChecksTest do}]} =- Checks.run(url_to_verify, expected_domain, "manual",- report_to: nil,- async?: false,- slowdown: 0- )- |> Checks.interpret_diagnostics()-- assert 2 == :atomics.get(verification_counter, 1)- end-- test "returns error when DNS check fails with domain not found error, offers custom URL input" do- expected_domain = "example.com"- url_to_verify = "https://#{expected_domain}"- stub_lookup_a_records(expected_domain, [])-- assert_matches %Result{- ok?: false,- data: %{offer_custom_url_input: true},- errors: [- ^any(:string, ~r/We couldn't find your website at #{url_to_verify}$/)- ],- recommendations: [- %{- text:- "Please check that the domain you entered is correct and reachable publicly. If it's intentionally private, you'll need to verify that Plausible works manually",- url:- "https://plausible.io/docs/troubleshoot-integration#how-to-manually-check-your-integration"- }- ]- } =- Checks.run(url_to_verify, expected_domain, "manual",- report_to: nil,- async?: false,- slowdown: 0- )- |> Checks.interpret_diagnostics()- end-- test "returns error when DNS check fails with invalid URL error, offers custom URL input" do- expected_domain = "example.com"- url_to_verify = "file://#{expected_domain}"- stub_lookup_a_records(expected_domain, [])-- assert_matches %Result{- ok?: false,- data: %{offer_custom_url_input: true},- errors: [- ^any(:string, ~r/We couldn't find your website at #{url_to_verify}$/)- ],- recommendations: [- %{- text:- "Please check that the domain you entered is correct and reachable publicly. If it's intentionally private, you'll need to verify that Plausible works manually",- url:- "https://plausible.io/docs/troubleshoot-integration#how-to-manually-check-your-integration"- }- ]- } =- Checks.run(url_to_verify, expected_domain, "manual",- report_to: nil,- async?: false,- slowdown: 0- )+ run_checks(verification_stub, expected_req_count: 2)|> Checks.interpret_diagnostics()endtest "returns error when there's a network error during verification, offers custom URL input" do- expected_domain = "example.com"- url_to_verify = "https://example.com?plausible_verification=123123123"- stub_lookup_a_records(expected_domain)-- verification_counter =- stub_verification_result(%{+ verification_stub =+ json_response_verification_stub(%{"completed" => false,- "error" => %{"message" => "net::ERR_CONNECTION_CLOSED at #{url_to_verify}"}+ "error" => %{"message" => "net::ERR_CONNECTION_CLOSED at #{@url_to_verify}"}})assert_matches %Result{@@ -453,23 +275,13 @@ defmodule Plausible.InstallationSupport.Verification.ChecksTest do}]} =- Checks.run(url_to_verify, expected_domain, "manual",- report_to: nil,- async?: false,- slowdown: 0- )+ run_checks(verification_stub, expected_req_count: 2)|> Checks.interpret_diagnostics()-- assert 2 == :atomics.get(verification_counter, 1)endtest "returns error when Plausible not installed and website response status is not 200, offers custom URL input" do- expected_domain = "example.com"- url_to_verify = "https://#{expected_domain}?plausible_verification=123123123"- stub_lookup_a_records(expected_domain)-- verification_counter =- stub_verification_result(%{+ verification_stub =+ json_response_verification_stub(%{"completed" => true,"responseStatus" => 403,"trackerIsInHtml" => nil,@@ -493,14 +305,8 @@ defmodule Plausible.InstallationSupport.Verification.ChecksTest do}]} =- Checks.run(url_to_verify, expected_domain, "manual",- report_to: nil,- async?: false,- slowdown: 0- )+ run_checks(verification_stub, expected_req_count: 2)|> Checks.interpret_diagnostics()-- assert 2 == :atomics.get(verification_counter, 1)endfor {installation_type, expected_recommendation} <- [@@ -514,12 +320,8 @@ defmodule Plausible.InstallationSupport.Verification.ChecksTest do"Please make sure you've copied the snippet to the head of your site, or verify your installation manually"}] dotest "returns error \"We couldn't detect Plausible on your site\" when plausible_is_on_window is false (with best guess recommendation for installation type: #{installation_type})" do- expected_domain = "example.com"- url_to_verify = "https://#{expected_domain}"- stub_lookup_a_records(expected_domain)-- verification_counter =- stub_verification_result(%{+ verification_stub =+ json_response_verification_stub(%{"completed" => true,"responseStatus" => 200,"trackerIsInHtml" => false,@@ -539,23 +341,16 @@ defmodule Plausible.InstallationSupport.Verification.ChecksTest do}]} =- Checks.run(url_to_verify, expected_domain, unquote(installation_type),- report_to: nil,- async?: false,- slowdown: 0+ run_checks(verification_stub,+ installation_type: unquote(installation_type),+ expected_req_count: 2)|> Checks.interpret_diagnostics()-- assert 2 == :atomics.get(verification_counter, 1)endtest "falls back to error \"We couldn't detect Plausible on your site\" when no other case matches (with best guess recommendation for installation type: #{installation_type}), sends diagnostics to Sentry" do- expected_domain = "example.com"- url_to_verify = "https://#{expected_domain}"- stub_lookup_a_records(expected_domain)-- verification_counter =- stub_verification_result(%{+ verification_stub =+ json_response_verification_stub(%{"completed" => true,"responseStatus" => nil,"trackerIsInHtml" => nil,@@ -575,30 +370,299 @@ defmodule Plausible.InstallationSupport.Verification.ChecksTest do}]} =- Checks.run(url_to_verify, expected_domain, unquote(installation_type),- report_to: nil,- async?: false,- slowdown: 0+ run_checks(verification_stub,+ installation_type: unquote(installation_type),+ expected_req_count: 1)|> Checks.interpret_diagnostics()+ end+ end+ end++ describe "InstallationV2 & InstallationV2CacheBust" do+ test "returns error when it 'succeeds', but only after cache bust" do+ counter = :atomics.new(1, [])++ get_context_url_from_body = fn conn ->+ {:ok, body, _conn} = Plug.Conn.read_body(conn)+ JSON.decode!(body)["context"]["url"]+ end++ verification_stub = fn conn ->+ js_data =+ if :atomics.add_get(counter, 1, 1) == 1 do+ assert get_context_url_from_body.(conn) == @url_to_verify++ %{+ "completed" => true,+ "trackerIsInHtml" => false,+ "plausibleIsOnWindow" => false,+ "plausibleIsInitialized" => false+ }+ else+ assert [_, "plausible_verification" <> _] =+ String.split(get_context_url_from_body.(conn), "?")++ %{+ "completed" => true,+ "trackerIsInHtml" => true,+ "plausibleIsOnWindow" => true,+ "plausibleIsInitialized" => true,+ "testEvent" => %{+ "normalizedBody" => %{+ "domain" => "example.com"+ },+ "responseStatus" => 200+ }+ }+ end++ conn+ |> put_resp_content_type("application/json")+ |> send_resp(200, Jason.encode!(%{"data" => js_data}))+ end++ assert_matches %Result{+ ok?: false,+ errors: [^any(:string, ~r/.*cache.*/)],+ recommendations: [+ %{+ text: ^any(:string, ~r/.*cache.*/),+ url:+ "https://plausible.io/docs/troubleshoot-integration#have-you-cleared-the-cache-of-your-site"+ }+ ]+ } = run_checks(verification_stub) |> Checks.interpret_diagnostics()++ assert 2 == :atomics.get(counter, 1)+ end++ test "cache bust diagnostics fully replace the initial installation check diagnostics" do+ counter = :atomics.new(1, [])++ verification_stub = fn conn ->+ if :atomics.add_get(counter, 1, 1) == 1 do+ js_data =+ %{+ "completed" => true,+ "trackerIsInHtml" => false,+ "plausibleIsOnWindow" => false,+ "plausibleIsInitialized" => false+ }++ conn+ |> put_resp_content_type("application/json")+ |> send_resp(200, Jason.encode!(%{"data" => js_data}))+ else+ Req.Test.transport_error(conn, :timeout)+ end+ end++ state = run_checks(verification_stub)++ assert_matches %Diagnostics{+ tracker_is_in_html: nil,+ plausible_is_on_window: nil,+ plausible_is_initialized: nil,+ service_error: %{code: :browserless_timeout}+ } = state.diagnostics++ # Browserless gets called 3 times:+ # 1) initial/regular installation check+ # 2) cache bust installation check+ # 3) retry due to #2 timing out+ assert 3 == :atomics.get(counter, 1)+ end- assert 1 == :atomics.get(verification_counter, 1)+ test "timeouts are retried, cache bust skipped, interpreted as temporary errors" do+ verification_stub = fn conn ->+ Req.Test.transport_error(conn, :timeout)+ end++ state = run_checks(verification_stub, expected_req_count: 2)++ assert_matches %Diagnostics{service_error: %{code: :browserless_timeout}} =+ state.diagnostics++ assert_matches %Result{+ ok?: false,+ errors: [^any(:string, ~r/.*temporary service error.*/)],+ recommendations: [+ %{+ text: ^any(:string, ~r/.*in a few minutes.*/),+ url:+ "https://plausible.io/docs/troubleshoot-integration#how-to-manually-check-your-integration"+ }+ ]+ } = Checks.interpret_diagnostics(state)+ end++ for status <- [400, 429] do+ test "#{status} responses are retried, cache bust skipped, interpreted as temporary errors" do+ verification_stub = fn conn ->+ conn+ |> put_resp_content_type("text/html")+ |> send_resp(unquote(status), "some error message")+ end++ state = run_checks(verification_stub, expected_req_count: 2)++ assert_matches %Diagnostics{+ service_error: %{+ code: :bad_browserless_response,+ extra: ^unquote(status)+ }+ } = state.diagnostics++ assert_matches %Result{+ ok?: false,+ errors: [^any(:string, ~r/.*temporary service error.*/)],+ recommendations: [+ %{+ text: ^any(:string, ~r/.*in a few minutes.*/),+ url:+ "https://plausible.io/docs/troubleshoot-integration#how-to-manually-check-your-integration"+ }+ ]+ } = Checks.interpret_diagnostics(state)endendend- defp stub_verification_result(js_data) when is_map(js_data) do- counter = :atomics.new(1, [])+ describe "observability" do+ setup %{test: test, test_pid: test_pid} do+ :telemetry.attach_many(+ "#{test}-telemetry-handler",+ [+ Checks.telemetry_event_handled(),+ Checks.telemetry_event_unhandled()+ ],+ fn event, %{}, _, _ ->+ send(test_pid, {:telemetry_event, event})+ end,+ %{}+ )+ end++ test "known installation issue detected is considered handled" do+ wrong_domain_verification_stub =+ json_response_verification_stub(%{+ "completed" => true,+ "trackerIsInHtml" => true,+ "plausibleIsOnWindow" => true,+ "plausibleIsInitialized" => true,+ "testEvent" => %{+ "normalizedBody" => %{+ "domain" => "wrong-domain.com"+ },+ "responseStatus" => 200+ }+ })++ state = run_checks(wrong_domain_verification_stub)++ log = capture_log(fn -> Checks.interpret_diagnostics(state) end)++ assert log == ""++ assert_receive {:telemetry_event, telemetry_event}+ assert telemetry_event == Checks.telemetry_event_handled()+ end++ test "unhandled verification case" do+ verification_stub =+ json_response_verification_stub(%{+ "completed" => true,+ "trackerIsInHtml" => true,+ "plausibleIsOnWindow" => true,+ "plausibleIsInitialized" => true,+ "testEvent" => %{}+ })++ state = run_checks(verification_stub)++ log = capture_log(fn -> Checks.interpret_diagnostics(state) end)++ assert log =~ "[VERIFICATION v2] Unhandled case (data_domain='#{@expected_domain}')"+ assert log =~ "test_event: %{}"++ assert_receive {:telemetry_event, telemetry_event}+ assert telemetry_event == Checks.telemetry_event_unhandled()+ end- stub_verification_result(fn conn ->- :atomics.add_get(counter, 1, 1)+ test "browserless request timing out is considered unhandled" do+ verification_stub = fn conn -> Req.Test.transport_error(conn, :timeout) end+ state = run_checks(verification_stub)+ log = capture_log(fn -> Checks.interpret_diagnostics(state) end)++ assert log =~ "[VERIFICATION v2] Unhandled case (data_domain='#{@expected_domain}')"+ assert log =~ "service_error: %{code: :browserless_timeout}"++ assert_receive {:telemetry_event, telemetry_event}+ assert telemetry_event == Checks.telemetry_event_unhandled()+ end++ test "flaky Browserless response is considered unhandled" do+ verification_stub = fn conn ->+ conn+ |> put_resp_content_type("text/html")+ |> send_resp(400, "some error message")+ end++ state = run_checks(verification_stub)++ log = capture_log(fn -> Checks.interpret_diagnostics(state) end)++ assert log =~ "[VERIFICATION v2] Unhandled case (data_domain='#{@expected_domain}')"+ assert log =~ "service_error: %{code: :bad_browserless_response, extra: 400}"++ assert_receive {:telemetry_event, telemetry_event}+ assert telemetry_event == Checks.telemetry_event_unhandled()+ end+ end++ defp json_response_verification_stub(js_data) do+ fn conn ->conn|> put_resp_content_type("application/json")|> send_resp(200, Jason.encode!(%{"data" => js_data}))- end)+ end+ end++ defp run_checks(verification_stub, opts \\ []) do+ installation_type = Keyword.get(opts, :installation_type, "manual")+ expected_req_count = Keyword.get(opts, :expected_req_count)++ stub_lookup_a_records(@expected_domain)++ if is_integer(expected_req_count) do+ counter = :atomics.new(1, [])- counter+ stub_verification_result(fn conn ->+ :atomics.add_get(counter, 1, 1)+ verification_stub.(conn)+ end)++ state =+ Checks.run(@url_to_verify, @expected_domain, installation_type,+ report_to: nil,+ async?: false,+ slowdown: 0+ )++ assert expected_req_count == :atomics.get(counter, 1)++ state+ else+ stub_verification_result(verification_stub)++ Checks.run(@url_to_verify, @expected_domain, installation_type,+ report_to: nil,+ async?: false,+ slowdown: 0+ )+ endenddefp stub_verification_result(f) dodiff --git a/test/plausible_web/live/components/verification_test.exs b/test/plausible_web/live/components/verification_test.exsindex 857e3c278d..d335c04ba7 100644--- a/test/plausible_web/live/components/verification_test.exs+++ b/test/plausible_web/live/components/verification_test.exs@@ -121,7 +121,7 @@ defmodule PlausibleWeb.Live.Components.VerificationTest dodiagnostics: %Verification.Diagnostics{plausible_is_on_window: false,plausible_is_initialized: false,- service_error: :domain_not_found+ service_error: %{code: :domain_not_found}}})