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: new interpretation case for non-200 page responses (#5683)
RobertJoonas4548e3acc51e
diff --git a/lib/plausible/installation_support/checks/installation_v2.ex b/lib/plausible/installation_support/checks/installation_v2.exindex a3f88ff916..9c69148eaa 100644--- a/lib/plausible/installation_support/checks/installation_v2.ex+++ b/lib/plausible/installation_support/checks/installation_v2.ex@@ -23,6 +23,7 @@ defmodule Plausible.InstallationSupport.Checks.InstallationV2 dotry {await page.setUserAgent(userAgent)const response = await page.goto(url)+ const responseStatus = response.status()const responseHeaders = response.headers()async function verify() {@@ -40,7 +41,8 @@ defmodule Plausible.InstallationSupport.Checks.InstallationV2 doreturn {data: {...output.data,- attempts+ attempts,+ responseStatus},};} catch (error) {@@ -165,6 +167,7 @@ defmodule Plausible.InstallationSupport.Checks.InstallationV2 dotest_event: data["testEvent"],cookie_banner_likely: data["cookieBannerLikely"],attempts: data["attempts"],+ response_status: data["responseStatus"],service_error: nil]enddiff --git a/lib/plausible/installation_support/checks/url.ex b/lib/plausible/installation_support/checks/url.exindex f8c1644db3..d37363d16e 100644--- a/lib/plausible/installation_support/checks/url.ex+++ b/lib/plausible/installation_support/checks/url.ex@@ -15,7 +15,7 @@ defmodule Plausible.InstallationSupport.Checks.Url do@spec perform(Plausible.InstallationSupport.State.t()) ::Plausible.InstallationSupport.State.t()def perform(%State{url: url} = state) when is_binary(url) do- with {:ok, %URI{scheme: scheme} = uri} when scheme in ["https"] <- URI.new(url),+ with {:ok, %URI{scheme: scheme} = uri} when scheme in ["http", "https"] <- URI.new(url),:ok <- check_domain(uri.host) dostripped_url = URI.to_string(%URI{uri | query: nil, fragment: nil})%State{state | url: stripped_url}@@ -55,15 +55,15 @@ defmodule Plausible.InstallationSupport.Checks.Url do"www.#{domain_without_path}"]|> Enum.reduce_while({:error, :domain_not_found}, fn d, _acc ->- case check_domain(d) do+ case dns_lookup(d) do:ok -> {:halt, {:ok, "https://" <> unsplit_domain(d, rest)}}{:error, :no_a_record} -> {:cont, {:error, :domain_not_found}}endend)end- @spec check_domain(String.t()) :: :ok | {:error, :no_a_record}- defp check_domain(domain) do+ @spec dns_lookup(String.t()) :: :ok | {:error, :no_a_record}+ defp dns_lookup(domain) dolookup_timeout = 1_000resolve_timeout = 1_000@@ -84,6 +84,14 @@ defmodule Plausible.InstallationSupport.Checks.Url doendend+ defp check_domain(domain) do+ if Application.get_env(:plausible, :environment) == "dev" and domain == "localhost" do+ :ok+ else+ dns_lookup(domain)+ end+ end+@spec split_domain(String.t()) :: [String.t()]defp split_domain(domain) doString.split(domain, "/", parts: 2)diff --git a/lib/plausible/installation_support/verification/diagnostics.ex b/lib/plausible/installation_support/verification/diagnostics.exindex 9b82b6066c..2c6851494f 100644--- a/lib/plausible/installation_support/verification/diagnostics.ex+++ b/lib/plausible/installation_support/verification/diagnostics.ex@@ -14,6 +14,7 @@ defmodule Plausible.InstallationSupport.Verification.Diagnostics dodiagnostics_are_from_cache_bust: nil,test_event: nil,cookie_banner_likely: nil,+ response_status: nil,service_error: nil,attempts: nil@@ -226,13 +227,40 @@ defmodule Plausible.InstallationSupport.Verification.Diagnostics dodef interpret(%__MODULE__{service_error: "net::" <> _}, _expected_domain, url)when is_binary(url) do- attempted_url = String.split(url, "?") |> List.first()+ attempted_url = shorten_url(url)@error_browserless_network|> error(attempted_url: attempted_url)|> struct!(data: %{offer_custom_url_input: true})end+ @error_non_200_page_response Error.new!(%{+ message:+ "We couldn't verify your website at <%= @attempted_url %>",+ recommendation:+ "Our verification tool encountered a <%= @page_response_status %> error. Please check for anything that might be blocking it from reaching your site, like a firewall, authentication requirements, or CDN rules. If you'd prefer, you can skip this and verify your integration manually",+ url:+ "https://plausible.io/docs/troubleshoot-integration#how-to-manually-check-your-integration"+ })++ def interpret(+ %__MODULE__{+ plausible_is_on_window: plausible_is_on_window,+ plausible_is_initialized: plausible_is_initialized,+ response_status: page_response_status+ },+ _expected_domain,+ url+ )+ when is_binary(url) and page_response_status != 200 and plausible_is_on_window != true and+ plausible_is_initialized != true do+ attempted_url = shorten_url(url)++ @error_non_200_page_response+ |> error(attempted_url: attempted_url, page_response_status: page_response_status)+ |> struct!(data: %{offer_custom_url_input: true})+ end+@unknown_error Error.new!(%{message: "Your Plausible integration is not working",recommendation:@@ -240,6 +268,7 @@ defmodule Plausible.InstallationSupport.Verification.Diagnostics dourl:"https://plausible.io/docs/troubleshoot-integration#how-to-manually-check-your-integration"})+def interpret(%__MODULE__{} = diagnostics, _expected_domain, url) doSentry.capture_message("Unhandled case for site verification (v2)",extra: %{@@ -252,6 +281,10 @@ defmodule Plausible.InstallationSupport.Verification.Diagnostics doerror(@unknown_error)end+ defp shorten_url(url) do+ String.split(url, "?") |> List.first()+ end+defp success() do%Result{ok?: true}enddiff --git a/test/plausible/installation_support/checks/url_test.exs b/test/plausible/installation_support/checks/url_test.exsindex 7f0185a1ea..73c9b24b54 100644--- a/test/plausible/installation_support/checks/url_test.exs+++ b/test/plausible/installation_support/checks/url_test.exs@@ -103,19 +103,17 @@ defmodule Plausible.InstallationSupport.Checks.UrlTest dorefute state.skip_further_checks?end- for scheme <- ["http", "file"] do- test "rejects not-https scheme '#{scheme}', does not check domain" do- state =- @check.perform(%State{- data_domain: "example-com-rollup",- url: "#{unquote(scheme)}://example.com/archives/news?p=any#fragment",- diagnostics: %Verification.Diagnostics{}- })+ test "rejects file:// scheme, does not check domain" do+ state =+ @check.perform(%State{+ data_domain: "example-com-rollup",+ url: "file://example.com/archives/news?p=any#fragment",+ diagnostics: %Verification.Diagnostics{}+ })- assert state.url == "#{unquote(scheme)}://example.com/archives/news?p=any#fragment"- assert state.diagnostics.service_error == :invalid_url- assert state.skip_further_checks?- end+ assert state.url == "file://example.com/archives/news?p=any#fragment"+ assert state.diagnostics.service_error == :invalid_url+ assert state.skip_further_checks?endtest "rejects invalid urls" dodiff --git a/test/plausible/installation_support/verification/diagnostics_test.exs b/test/plausible/installation_support/verification/diagnostics_test.exsindex 65c0b5df5f..e275ace839 100644--- a/test/plausible/installation_support/verification/diagnostics_test.exs+++ b/test/plausible/installation_support/verification/diagnostics_test.exs@@ -242,6 +242,39 @@ defmodule Plausible.InstallationSupport.Verification.DiagnosticsTest do} = Diagnostics.interpret(diagnostics, expected_domain, url_to_verify)end+ test "error when plausible not installed and page.goto(url) response is not 200" do+ expected_domain = "example.com"+ url_to_verify = "https://#{expected_domain}?plausible_verification=123123123"++ diagnostics = %Diagnostics{+ plausible_is_on_window: false,+ plausible_is_initialized: nil,+ response_status: 403+ }++ assert_matches %Result{+ ok?: false,+ data: %{offer_custom_url_input: true},+ errors: [+ ^any(+ :string,+ ~r/.*couldn't verify your website at https:\/\/#{expected_domain}.*/+ )+ ],+ recommendations: [+ %{+ text:+ ^any(+ :string,+ ~r/403 error.*firewall.*authentication.*CDN.*verify your integration manually/+ ),+ url:+ "https://plausible.io/docs/troubleshoot-integration#how-to-manually-check-your-integration"+ }+ ]+ } = Diagnostics.interpret(diagnostics, expected_domain, url_to_verify)+ end+test "unknown error when no specific case matches" doexpected_domain = "example.com"url_to_verify = "https://#{expected_domain}"@@ -249,6 +282,7 @@ defmodule Plausible.InstallationSupport.Verification.DiagnosticsTest dodiagnostics = %Diagnostics{plausible_is_on_window: false,plausible_is_initialized: false,+ response_status: 200,service_error: nil}