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 v2: Better Browserless error handling + verify custom URL option (#5673)
RobertJoonasc3af79a115e8
diff --git a/lib/plausible/installation_support/verification/diagnostics.ex b/lib/plausible/installation_support/verification/diagnostics.exindex e55f392af6..9b82b6066c 100644--- a/lib/plausible/installation_support/verification/diagnostics.ex+++ b/lib/plausible/installation_support/verification/diagnostics.ex@@ -199,29 +199,38 @@ defmodule Plausible.InstallationSupport.Verification.Diagnostics dodo: error(@error_gtm_selected_maybe_cookie_banner)@error_domain_not_found Error.new!(%{- message: "We couldn't verify your website",+ message: "We couldn't find your website at <%= @attempted_url %>",recommendation:- "Please check that the domain you entered is correct and that the website is reachable publicly. If it's intentionally private, you'll need to verify that Plausible works manually",+ "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"})- def interpret(%__MODULE__{service_error: service_error}, _expected_domain, _url)- when service_error in [:domain_not_found, :invalid_url],- do: error(@error_domain_not_found)- def interpret(%__MODULE__{} = diagnostics, _expected_domain, url),- do: unknown_error(diagnostics, url)+ def interpret(%__MODULE__{service_error: service_error}, expected_domain, url)+ when service_error in [:domain_not_found, :invalid_url] do+ attempted_url = if url, do: url, else: "https://#{expected_domain}"- defp success() do- %Result{ok?: true}+ @error_domain_not_found+ |> error(attempted_url: attempted_url)+ |> struct!(data: %{offer_custom_url_input: true})end- defp error(%Error{} = error) do- %Result{- ok?: false,- errors: [error.message],- recommendations: [%{text: error.recommendation, url: error.url}]- }+ @error_browserless_network Error.new!(%{+ message:+ "We couldn't verify your website at <%= @attempted_url %>",+ recommendation:+ "Our verification tool encountered a network error while trying to verify your website. Please verify your integration manually",+ url:+ "https://plausible.io/docs/troubleshoot-integration#how-to-manually-check-your-integration"+ })++ def interpret(%__MODULE__{service_error: "net::" <> _}, _expected_domain, url)+ when is_binary(url) do+ attempted_url = String.split(url, "?") |> List.first()++ @error_browserless_network+ |> error(attempted_url: attempted_url)+ |> struct!(data: %{offer_custom_url_input: true})end@unknown_error Error.new!(%{@@ -231,7 +240,7 @@ defmodule Plausible.InstallationSupport.Verification.Diagnostics dourl:"https://plausible.io/docs/troubleshoot-integration#how-to-manually-check-your-integration"})- defp unknown_error(diagnostics, url) do+ def interpret(%__MODULE__{} = diagnostics, _expected_domain, url) doSentry.capture_message("Unhandled case for site verification (v2)",extra: %{message: inspect(diagnostics),@@ -242,4 +251,19 @@ defmodule Plausible.InstallationSupport.Verification.Diagnostics doerror(@unknown_error)end++ defp success() do+ %Result{ok?: true}+ end++ defp error(%Error{} = error, assigns \\ []) do+ message = EEx.eval_string(error.message, assigns: assigns)+ recommendation = EEx.eval_string(error.recommendation, assigns: assigns)++ %Result{+ ok?: false,+ errors: [message],+ recommendations: [%{text: recommendation, url: error.url}]+ }+ endenddiff --git a/lib/plausible_web/live/components/verification.ex b/lib/plausible_web/live/components/verification.exindex 807bd541fd..e5e1d79a33 100644--- a/lib/plausible_web/live/components/verification.ex+++ b/lib/plausible_web/live/components/verification.ex@@ -29,134 +29,175 @@ defmodule PlausibleWeb.Live.Components.Verification dodef render(assigns) do~H"""- <div id="progress-indicator">- <.focus_box>- <div- :if={not @finished?}- class="mx-auto flex h-12 w-12 items-center justify-center rounded-full bg-green-100 dark:bg-gray-700"- >- <div class="block pulsating-circle"></div>- </div>-- <div- :if={@finished? and @success?}- class="mx-auto flex h-12 w-12 items-center justify-center rounded-full bg-green-100 dark:bg-green-500"- id="check-circle"- >- <Heroicons.check_badge class="h-6 w-6 text-green-600 bg-green-100 dark:bg-green-500 dark:text-green-200" />- </div>+ <div id="verification-ui">+ <.render_progress :if={not @finished?} message={@message} />+ <.render_success+ :if={@finished? and @success?}+ awaiting_first_pageview?={@awaiting_first_pageview?}+ domain={@domain}+ />+ <.render_failed+ :if={@finished? and not @success?}+ interpretation={@interpretation}+ attempts={@attempts}+ domain={@domain}+ flow={@flow}+ installation_type={@installation_type}+ />+ <.render_super_admin_diagnostics+ :if={not is_nil(@verification_state) && @super_admin? && @finished?}+ verification_state={@verification_state}+ />+ </div>+ """+ end- <div- :if={@finished? and not @success?}- class="mx-auto flex h-12 w-12 items-center justify-center rounded-full bg-red-100 dark:bg-red-200"- id="error-circle"- >- <Heroicons.exclamation_triangle class="h-6 w-6 text-red-600 bg-red-100 dark:bg-red-200 dark:text-red-800" />- </div>+ defp render_progress(assigns) do+ ~H"""+ <.focus_box>+ <div class="mx-auto flex h-12 w-12 items-center justify-center rounded-full bg-green-100 dark:bg-gray-700">+ <div class="block pulsating-circle"></div>+ </div>+ <div class="mt-8">+ <.title>Verifying your installation</.title>+ <p class="text-sm mt-4 animate-pulse" id="progress">{@message}</p>+ </div>+ </.focus_box>+ """+ end- <div class="mt-8">- <.title>- <span :if={@finished? and @success?}>Success!</span>- <span :if={not @finished?}>Verifying your installation</span>+ defp render_success(assigns) do+ ~H"""+ <.focus_box>+ <div+ class="mx-auto flex h-12 w-12 items-center justify-center rounded-full bg-green-100 dark:bg-green-500"+ id="check-circle"+ >+ <Heroicons.check_badge class="h-6 w-6 text-green-600 bg-green-100 dark:bg-green-500 dark:text-green-200" />+ </div>- <span :if={@finished? and not @success? and @interpretation}>- {List.first(@interpretation.errors)}- </span>- </.title>- <p :if={@finished? and @success?} id="progress" class="text-sm mt-4">- Your installation is working and visitors are being counted accurately- </p>- <p- :if={@finished? and @success? and @awaiting_first_pageview?}- id="awaiting"- class="text-sm mt-4 animate-pulse"- >- Awaiting your first pageview- </p>- <p :if={not @finished?} class="text-sm mt-4 animate-pulse" id="progress">{@message}</p>+ <div class="mt-8">+ <.title>Success!</.title>+ <p class="text-sm mt-4">+ Your installation is working and visitors are being counted accurately+ </p>+ <p :if={@awaiting_first_pageview?} id="awaiting" class="text-sm mt-4 animate-pulse">+ Awaiting your first pageview+ </p>+ </div>+ <.button_link+ mt?={false}+ href={"/#{URI.encode_www_form(@domain)}?skip_to_dashboard=true"}+ class="w-full font-bold mb-4"+ >+ Go to the dashboard+ </.button_link>+ </.focus_box>+ """+ end- <p- :if={@finished? and not @success? and @interpretation}- class="mt-4 text-sm text-ellipsis overflow-hidden"- id="recommendation"- >- <span>{List.first(@interpretation.recommendations).text}. </span>- <.styled_link href={List.first(@interpretation.recommendations).url} new_tab={true}>- Learn more- </.styled_link>- </p>- </div>+ defp render_failed(assigns) do+ ~H"""+ <.focus_box>+ <div+ class="mx-auto flex h-12 w-12 items-center justify-center rounded-full bg-red-100 dark:bg-red-200"+ id="error-circle"+ >+ <Heroicons.exclamation_triangle class="h-6 w-6 text-red-600 bg-red-100 dark:bg-red-200 dark:text-red-800" />+ </div>- <div :if={@finished?} class="mt-6">- <.button_link :if={not @success?} mt?={false} href="#" phx-click="retry" class="w-full">- Verify installation again- </.button_link>- <.button_link- :if={@success?}- mt?={false}- href={"/#{URI.encode_www_form(@domain)}?skip_to_dashboard=true"}- class="w-full font-bold mb-4"- >- Go to the dashboard- </.button_link>- </div>+ <div :if={@interpretation} class="mt-8">+ <.title>{List.first(@interpretation.errors)}</.title>+ <p id="recommendation" class="mt-4 text-sm text-ellipsis overflow-hidden">+ <span>{List.first(@interpretation.recommendations).text}. </span>+ <.styled_link href={List.first(@interpretation.recommendations).url} new_tab={true}>+ Learn more+ </.styled_link>+ </p>+ </div>- <:footer :if={@finished? and not @success?}>- <.focus_list>- <:item :if={ee?() and @attempts >= 3}>- <b>Need further help with your installation?</b>- <.styled_link href="https://plausible.io/contact">- Contact us- </.styled_link>- </:item>- <:item>- Need to see installation instructions again?+ <div class="mt-6">+ <.button_link mt?={false} href="#" phx-click="retry" class="w-full">+ Verify installation again+ </.button_link>+ </div>+ <:footer>+ <.focus_list>+ <:item :if={+ @interpretation && is_map(@interpretation.data) &&+ @interpretation.data[:offer_custom_url_input]+ }>+ <span id="verify-custom-url-link">+ Is your website located at a different URL?<.styled_link href={- Routes.site_path(PlausibleWeb.Endpoint, :installation, @domain,+ Routes.site_path(PlausibleWeb.Endpoint, :verification, @domain,flow: @flow,- installation_type: @installation_type+ installation_type: @installation_type,+ custom_url: true)}>Click here</.styled_link>- </:item>- <:item>- Run verification later and go to site settings?- <.styled_link href={"/#{URI.encode_www_form(@domain)}/settings/general"}>- Click here- </.styled_link>+ </span>+ </:item>+ <:item :if={ee?() and @attempts >= 3}>+ <b>Need further help with your installation?</b>+ <.styled_link href="https://plausible.io/contact">+ Contact us+ </.styled_link>+ </:item>+ <:item>+ Need to see installation instructions again?+ <.styled_link href={+ Routes.site_path(PlausibleWeb.Endpoint, :installation, @domain,+ flow: @flow,+ installation_type: @installation_type+ )+ }>+ Click here+ </.styled_link>+ </:item>+ <:item>+ Run verification later and go to site settings?+ <.styled_link href={"/#{URI.encode_www_form(@domain)}/settings/general"}>+ Click here+ </.styled_link>+ </:item>+ </.focus_list>+ </:footer>+ </.focus_box>+ """+ end++ defp render_super_admin_diagnostics(assigns) do+ ~H"""+ <.focus_box>+ <div+ class="flex flex-col dark:text-gray-200"+ x-data="{ showDiagnostics: false }"+ id="super-admin-report"+ >+ <p class="text-sm">+ <a+ href="#"+ @click.prevent="showDiagnostics = !showDiagnostics"+ class="bg-yellow-100 dark:text-gray-800"+ >+ As a super-admin, you're eligible to see diagnostics details. Click to expand.+ </a>+ </p>+ <div x-show="showDiagnostics" x-cloak>+ <.focus_list>+ <:item :for={{diag, value} <- Map.from_struct(@verification_state.diagnostics)}>+ <span class="text-sm">+ {Phoenix.Naming.humanize(diag)}:+ <span class="font-mono">{to_string_value(value)}</span>+ </span></:item></.focus_list>- </:footer>- </.focus_box>- <.focus_box :if={not is_nil(@verification_state) && @super_admin? && @finished?}>- <div- class="flex flex-col dark:text-gray-200"- x-data="{ showDiagnostics: false }"- id="super-admin-report"- >- <p class="text-sm">- <a- href="#"- @click.prevent="showDiagnostics = !showDiagnostics"- class="bg-yellow-100 dark:text-gray-800"- >- As a super-admin, you're eligible to see diagnostics details. Click to expand.- </a>- </p>- <div x-show="showDiagnostics" x-cloak>- <.focus_list>- <:item :for={{diag, value} <- Map.from_struct(@verification_state.diagnostics)}>- <span class="text-sm">- {Phoenix.Naming.humanize(diag)}:- <span class="font-mono">{to_string_value(value)}</span>- </span>- </:item>- </.focus_list>- </div></div>- </.focus_box>- </div>+ </div>+ </.focus_box>"""enddiff --git a/lib/plausible_web/live/verification.ex b/lib/plausible_web/live/verification.exindex 1fcc0f2552..94cf097d85 100644--- a/lib/plausible_web/live/verification.ex+++ b/lib/plausible_web/live/verification.ex@@ -7,6 +7,8 @@ defmodule PlausibleWeb.Live.Verification douse Plausibleuse PlausibleWeb, :live_view+ import PlausibleWeb.Components.Generic+alias Plausible.InstallationSupport.{State, LegacyVerification, Verification}@component PlausibleWeb.Live.Components.Verification@@ -30,6 +32,7 @@ defmodule PlausibleWeb.Live.Verification dosuper_admin? = Plausible.Auth.is_super_admin?(socket.assigns.current_user)has_pageviews? = has_pageviews?(site)+ custom_url_input? = params["custom_url"] == "true"socket =assign(socket,@@ -46,11 +49,12 @@ defmodule PlausibleWeb.Live.Verification doflow: params["flow"] || "",checks_pid: nil,attempts: 0,- polling_pageviews?: false+ polling_pageviews?: false,+ custom_url_input?: custom_url_input?)on_ee do- if connected?(socket) do+ if connected?(socket) and not custom_url_input? dolaunch_delayed(socket)endend@@ -75,8 +79,9 @@ defmodule PlausibleWeb.Live.Verification dodef render(assigns) do~H"""<PlausibleWeb.Components.FlowProgress.render flow={@flow} current_step="Verify installation" />-+ <.custom_url_form :if={@custom_url_input?} domain={@domain} /><.live_component+ :if={not @custom_url_input?}module={@component}installation_type={@installation_type}domain={@domain}@@ -112,12 +117,22 @@ defmodule PlausibleWeb.Live.Verification dodef handle_event("launch-verification", _, socket) dolaunch_delayed(socket)- {:noreply, reset_component(socket, nil)}+ {:noreply, reset_component(socket)}end- def handle_event("retry", params, socket) do+ def handle_event("retry", _, socket) dolaunch_delayed(socket)- {:noreply, reset_component(socket, params["url_to_verify"])}+ {:noreply, reset_component(socket)}+ end++ def handle_event("verify-custom-url", %{"custom_url" => custom_url}, socket) do+ socket =+ socket+ |> assign(url_to_verify: custom_url)+ |> assign(custom_url_input?: false)++ launch_delayed(socket)+ {:noreply, reset_component(socket)}enddef handle_info({:start, report_to}, socket) do@@ -221,7 +236,7 @@ defmodule PlausibleWeb.Live.Verification doredirect(socket, to: stats_url)end- defp reset_component(socket, url_to_verify) do+ defp reset_component(socket) doupdate_component(socket,message: "We're visiting your site to ensure that everything is working",finished?: false,@@ -229,11 +244,7 @@ defmodule PlausibleWeb.Live.Verification dodiagnostics: nil)- if is_binary(url_to_verify) do- assign(socket, url_to_verify: url_to_verify)- else- socket- end+ socketenddefp update_component(_socket, updates) do@@ -250,4 +261,49 @@ defmodule PlausibleWeb.Live.Verification dodefp has_pageviews?(site) doPlausible.Stats.Clickhouse.has_pageviews?(site)end++ on_ee do+ defp custom_url_form(assigns) do+ ~H"""+ <.focus_box>+ <div class="mx-auto flex h-12 w-12 items-center justify-center rounded-full bg-blue-100 dark:bg-blue-900">+ <Heroicons.globe_alt class="h-6 w-6 text-blue-600 dark:text-blue-200" />+ </div>+ <div class="mt-8">+ <h3 class="text-lg leading-6 font-medium text-gray-900 dark:text-white">+ Enter Your Custom URL+ </h3>+ <p class="text-sm mt-4 text-gray-600 dark:text-gray-400">+ Please enter the URL where your website with the Plausible script is located.+ </p>+ <form phx-submit="verify-custom-url" class="mt-6">+ <div class="mb-4">+ <label+ for="custom_url"+ class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2"+ >+ Website URL+ </label>+ <input+ type="url"+ name="custom_url"+ id="custom_url"+ required+ class="w-full px-3 py-2 border border-gray-300 dark:border-gray-600 rounded-md shadow-sm focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 dark:bg-gray-800 dark:text-white"+ placeholder={"https://#{@domain}"}+ value={"https://#{@domain}"}+ />+ </div>+ <button+ type="submit"+ class="w-full flex justify-center py-2 px-4 border border-transparent rounded-md shadow-sm text-sm font-medium text-white bg-indigo-600 hover:bg-indigo-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500 dark:bg-indigo-500 dark:hover:bg-indigo-600"+ >+ Verify Installation+ </button>+ </form>+ </div>+ </.focus_box>+ """+ end+ endenddiff --git a/test/plausible/installation_support/verification/diagnostics_test.exs b/test/plausible/installation_support/verification/diagnostics_test.exsindex fd83643778..65c0b5df5f 100644--- a/test/plausible/installation_support/verification/diagnostics_test.exs+++ b/test/plausible/installation_support/verification/diagnostics_test.exs@@ -185,6 +185,63 @@ defmodule Plausible.InstallationSupport.Verification.DiagnosticsTest do} = Diagnostics.interpret(diagnostics, expected_domain, url_to_verify)end+ for error_code <- [:domain_not_found, :invalid_url] do+ test "error when DNS check fails (#{error_code})" do+ expected_domain = "example.com"+ url_to_verify = "https://#{expected_domain}"++ diagnostics = %Diagnostics{+ plausible_is_on_window: nil,+ plausible_is_initialized: nil,+ service_error: unquote(error_code)+ }++ assert_matches %Result{+ ok?: false,+ data: %{offer_custom_url_input: true},+ errors: [+ ^any(:string, ~r/.*couldn't find your website at #{url_to_verify}.*/)+ ],+ recommendations: [+ %{+ text: ^any(:string, ~r/.*verify.*manually.*/),+ url:+ "https://plausible.io/docs/troubleshoot-integration#how-to-manually-check-your-integration"+ }+ ]+ } = Diagnostics.interpret(diagnostics, expected_domain, url_to_verify)+ end+ end++ test "error when Browserless encounters a network error during verification" do+ expected_domain = "example.com"+ url_to_verify = "https://#{expected_domain}?plausible_verification=123123123"++ diagnostics = %Diagnostics{+ plausible_is_on_window: nil,+ plausible_is_initialized: nil,+ service_error: "net::ERR_CONNECTION_CLOSED at https://example.com"+ }++ 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/.*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}"diff --git a/test/plausible_web/live/components/verification_test.exs b/test/plausible_web/live/components/verification_test.exsindex 3c3eba4e49..5b615a2b89 100644--- a/test/plausible_web/live/components/verification_test.exs+++ b/test/plausible_web/live/components/verification_test.exs@@ -3,14 +3,14 @@ defmodule PlausibleWeb.Live.Components.VerificationTest doimport Phoenix.LiveViewTest, only: [render_component: 2]import Plausible.Test.Support.HTML- alias Plausible.InstallationSupport.{State, LegacyVerification}+ alias Plausible.InstallationSupport.{State, LegacyVerification, Verification}@component PlausibleWeb.Live.Components.Verification- @progress ~s|#progress-indicator p#progress|+ @progress ~s|#verification-ui p#progress|- @pulsating_circle ~s|div#progress-indicator div.pulsating-circle|- @check_circle ~s|div#progress-indicator #check-circle|- @error_circle ~s|div#progress-indicator #error-circle|+ @pulsating_circle ~s|div#verification-ui div.pulsating-circle|+ @check_circle ~s|div#verification-ui #check-circle|+ @error_circle ~s|div#verification-ui #error-circle|@recommendations ~s|#recommendation|@super_admin_report ~s|#super-admin-report|@@ -112,6 +112,35 @@ defmodule PlausibleWeb.Live.Components.VerificationTest doassert element_exists?(html, ~s|a[href="https://plausible.io/contact"]|)end+ test "renders link to verify installation at a different URL" do+ interpretation =+ Verification.Checks.interpret_diagnostics(%State{+ url: "example.com",+ diagnostics: %Verification.Diagnostics{+ plausible_is_on_window: false,+ plausible_is_initialized: false,+ service_error: :domain_not_found+ }+ })++ assert interpretation.data.offer_custom_url_input == true++ expected_link_href =+ PlausibleWeb.Router.Helpers.site_path(PlausibleWeb.Endpoint, :verification, "example.com")++ html =+ render_component(@component,+ domain: "example.com",+ finished?: true,+ success?: false,+ interpretation: interpretation+ )++ assert text_of_element(html, "#verify-custom-url-link") =~ "different URL?"+ assert text_of_attr(html, "#verify-custom-url-link a", "href") =~ expected_link_href+ assert text_of_attr(html, "#verify-custom-url-link a", "href") =~ "custom_url=true"+ end+test "offers escape paths: settings and installation instructions on failure" dohtml =render_component(@component,diff --git a/test/plausible_web/live/verification_test.exs b/test/plausible_web/live/verification_test.exsindex b9964a149a..65d2fccb00 100644--- a/test/plausible_web/live/verification_test.exs+++ b/test/plausible_web/live/verification_test.exs@@ -11,9 +11,9 @@ defmodule PlausibleWeb.Live.VerificationTest do# @verify_button ~s|button#launch-verification-button[phx-click="launch-verification"]|@retry_button ~s|a[phx-click="retry"]|# @go_to_dashboard_button ~s|a[href$="?skip_to_dashboard=true"]|- @progress ~s|#progress-indicator p#progress|- @awaiting ~s|#progress-indicator p#awaiting|- @heading ~s|#progress-indicator h2|+ @progress ~s|#verification-ui p#progress|+ @awaiting ~s|#verification-ui p#awaiting|+ @heading ~s|#verification-ui h2|describe "GET /:domain" do@tag :ee_only@@ -58,6 +58,28 @@ defmodule PlausibleWeb.Live.VerificationTest doassert html =~ "Awaiting your first pageview …"end+ @tag :ee_only+ test "from custom URL input form to verification", %{conn: conn, site: site} do+ # Get liveview with ?custom_url=true query param+ {:ok, lv, html} =+ conn |> no_slowdown() |> live("/#{site.domain}/verification?custom_url=true")++ verifying_installation_text = "Verifying your installation"++ # Assert form is rendered instead of kicking off verification automatically+ assert html =~ "Enter Your Custom URL"+ assert html =~ ~s[value="https://#{site.domain}"]+ assert html =~ ~s[placeholder="https://#{site.domain}"]+ refute html =~ verifying_installation_text++ # Submit custom URL form+ html = lv |> element("form") |> render_submit(%{"custom_url" => "https://abc.de"})++ # Should now show verification progress and hide custom URL form+ assert html =~ verifying_installation_text+ refute html =~ "Enter Your Custom URL"+ end+@tag :ee_onlytest "eventually verifies installation", %{conn: conn, site: site} dostub_fetch_body(200, source(site.domain))