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
Change domain flow improvements (#5850)
RobertJoonasd1b6817cfe37
diff --git a/extra/lib/plausible/installation_support/check.ex b/extra/lib/plausible/installation_support/check.exindex 225e0273e5..d08638f065 100644--- a/extra/lib/plausible/installation_support/check.ex+++ b/extra/lib/plausible/installation_support/check.ex@@ -13,7 +13,7 @@ defmodule Plausible.InstallationSupport.Check do"""@type state() :: Plausible.InstallationSupport.State.t()@callback report_progress_as() :: String.t()- @callback perform(state()) :: state()+ @callback perform(state(), Keyword.t()) :: state()defmacro __using__(_) doquote do@@ -30,7 +30,7 @@ defmodule Plausible.InstallationSupport.Check dotask =Task.async(fn ->try do- perform(state)+ perform(state, opts)catch_, e ->Logger.error(diff --git a/extra/lib/plausible/installation_support/checks/detection.ex b/extra/lib/plausible/installation_support/checks/detection.exindex b4b7f925b3..be805fb07b 100644--- a/extra/lib/plausible/installation_support/checks/detection.ex+++ b/extra/lib/plausible/installation_support/checks/detection.ex@@ -53,10 +53,6 @@ defmodule Plausible.InstallationSupport.Checks.Detection do}"""- # 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@@ -67,7 +63,15 @@ defmodule Plausible.InstallationSupport.Checks.Detection dodef report_progress_as, do: "We're checking your site to recommend the best installation method"@impl true- def perform(%State{url: url, assigns: %{detect_v1?: detect_v1?}} = state) do+ def perform(%State{url: url, assigns: %{detect_v1?: detect_v1?}} = state, opts) do+ check_timeout = Keyword.fetch!(opts, :timeout)++ # We rely on Req's `:receive_timeout` to avoid waiting too long for a response, but+ # we also pass the same timeout (+ 1s) via a query param to the Browserless /function API+ # to not waste resources and leave a redundant process running there.+ req_timeout = check_timeout - 1000+ browserless_api_timeout = check_timeout+opts =[headers: %{content_type: "application/json"},@@ -82,14 +86,14 @@ defmodule Plausible.InstallationSupport.Checks.Detection dodebug: Application.get_env(:plausible, :environment) == "dev"}}),- params: %{timeout: @req_timeout + 1000},+ params: %{timeout: browserless_api_timeout},retry: fn _request, response_or_error ->case response_or_error do%{status: status} -> Map.get(BrowserlessConfig.retry_policy(), status, false)_ -> falseendend,- receive_timeout: @req_timeout,+ receive_timeout: req_timeout,retry_log_level: :warning,max_retries: @max_retries]diff --git a/extra/lib/plausible/installation_support/checks/url.ex b/extra/lib/plausible/installation_support/checks/url.exindex 0d0de94df5..95dbd56fea 100644--- a/extra/lib/plausible/installation_support/checks/url.ex+++ b/extra/lib/plausible/installation_support/checks/url.ex@@ -12,9 +12,8 @@ defmodule Plausible.InstallationSupport.Checks.Url dodef report_progress_as, do: "We're trying to reach your website"@impl true- @spec perform(Plausible.InstallationSupport.State.t()) ::- Plausible.InstallationSupport.State.t()- def perform(%State{url: url} = state) when is_binary(url) do+ @spec perform(State.t(), Keyword.t()) :: State.t()+ def perform(%State{url: url} = state, _opts) when is_binary(url) dowith {: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})@@ -32,7 +31,7 @@ defmodule Plausible.InstallationSupport.Checks.Url doendend- def perform(%State{data_domain: domain} = state) when is_binary(domain) do+ def perform(%State{data_domain: domain} = state, _opts) when is_binary(domain) docase find_working_url(domain) do{:ok, working_url} ->%State{state | url: working_url}diff --git a/extra/lib/plausible/installation_support/checks/verify_installation.ex b/extra/lib/plausible/installation_support/checks/verify_installation.exindex c0324d7462..fafdecb705 100644--- a/extra/lib/plausible/installation_support/checks/verify_installation.ex+++ b/extra/lib/plausible/installation_support/checks/verify_installation.ex@@ -89,7 +89,7 @@ defmodule Plausible.InstallationSupport.Checks.VerifyInstallation dodef report_progress_as, do: "We're verifying that your visitors are being counted correctly"@impl true- def perform(%State{url: url} = state) do+ def perform(%State{url: url} = state, _opts) doopts = [headers: %{content_type: "application/json"},body:diff --git a/extra/lib/plausible/installation_support/checks/verify_installation_cache_bust.ex b/extra/lib/plausible/installation_support/checks/verify_installation_cache_bust.exindex 2826213470..f8a84a492e 100644--- a/extra/lib/plausible/installation_support/checks/verify_installation_cache_bust.ex+++ b/extra/lib/plausible/installation_support/checks/verify_installation_cache_bust.ex@@ -19,7 +19,7 @@ defmodule Plausible.InstallationSupport.Checks.VerifyInstallationCacheBust dodef report_progress_as, do: "We're verifying that your visitors are being counted correctly"@impl true- def perform(%State{url: url} = state) do+ def perform(%State{url: url} = state, _opts) docase InstallationSupport.Verification.Checks.interpret_diagnostics(state, telemetry?: false) do%InstallationSupport.Result{ok?: true} ->state@@ -35,7 +35,7 @@ defmodule Plausible.InstallationSupport.Checks.VerifyInstallationCacheBust dostate|> struct!(diagnostics: reset_diagnostics)|> struct!(url: InstallationSupport.URL.bust_url(url))- |> InstallationSupport.Checks.VerifyInstallation.perform()+ |> InstallationSupport.Checks.VerifyInstallation.perform([])|> put_diagnostics(diagnostics_are_from_cache_bust: true)endenddiff --git a/lib/plausible_web/live/change_domain.ex b/lib/plausible_web/live/change_domain.exindex 821800f436..a840f623dc 100644--- a/lib/plausible_web/live/change_domain.ex+++ b/lib/plausible_web/live/change_domain.ex@@ -14,9 +14,12 @@ defmodule PlausibleWeb.Live.ChangeDomain doend@change_domain_docs_link "https://plausible.io/docs/change-domain-name"+ @change_domain_checklist_docs_link "https://plausible.io/docs/change-domain-name#domain-change-checklist"def change_domain_docs_link(), do: @change_domain_docs_link+ def change_domain_checklist_docs_link(), do: @change_domain_checklist_docs_link+def mount(%{"domain" => domain},_session,@@ -68,6 +71,8 @@ defmodule PlausibleWeb.Live.ChangeDomain doenddefp render_form_step(assigns) do+ assigns = assign(assigns, docs_link: @change_domain_docs_link)+~H"""<.focus_box><:title>Change your website domain</:title>@@ -78,7 +83,13 @@ defmodule PlausibleWeb.Live.ChangeDomain do<:footer><.focus_list><:item>- Changed your mind? Go back to+ See our+ <.styled_link new_tab={true} href={@docs_link}>+ domain change documentation+ </.styled_link>+ </:item>+ <:item>+ Return to<.styled_link href={Routes.site_path(@socket, :settings_general, @site.domain)}>Site Settings</.styled_link>@@ -92,6 +103,8 @@ defmodule PlausibleWeb.Live.ChangeDomain doenddefp render_success_step(assigns) do+ assigns = assign(assigns, docs_link: @change_domain_docs_link)+~H"""<.focus_box><:title>Domain Changed Successfully</:title>@@ -102,9 +115,20 @@ defmodule PlausibleWeb.Live.ChangeDomain do</:subtitle><:footer>- <.styled_link href={Routes.site_path(@socket, :settings_general, @site.domain)}>- ← Back to Site Settings- </.styled_link>+ <.focus_list>+ <:item>+ See our+ <.styled_link new_tab={true} href={@docs_link}>+ domain change documentation+ </.styled_link>+ </:item>+ <:item>+ Return to+ <.styled_link href={Routes.site_path(@socket, :settings_general, @site.domain)}>+ Site Settings+ </.styled_link>+ </:item>+ </.focus_list></:footer><%= on_ee do %><.async_result :let={detection_result} assign={@detection_result}>@@ -118,10 +142,26 @@ defmodule PlausibleWeb.Live.ChangeDomain do</:loading><:failed>- <.generic_notice />+ <div class="flex items-center">+ <Heroicons.exclamation_triangle class="w-4 h-4 mr-2 text-yellow-500" />+ <span class="text-sm font-bold">+ We could not reach your new domain+ </span>+ </div>++ <p class="mt-4 text-sm">+ Additional action may be required. If you're using our legacy snippet (i.e. your+ Plausible snippet includes the data-domain attribute) or the NPM package, you must+ also update the site domain of your Plausible installation within 72 hours to match+ the updated domain in order to guarantee continuous tracking.+ </p></:failed>- <.success_notice :if={detection_result} detection_result={detection_result} />+ <.render_detection_result+ :if={detection_result}+ detection_result={detection_result}+ site={@site}+ /></.async_result><% else %><.ce_generic_notice />@@ -131,52 +171,113 @@ defmodule PlausibleWeb.Live.ChangeDomain doendon_ee do- defp success_notice(assigns) do+ defp render_detection_result(assigns) docase assigns.detection_result do- %{v1_detected: true, wordpress_plugin: true} -> wordpress_plugin_notice(assigns)- %{v1_detected: true, wordpress_plugin: false} -> generic_notice(assigns)- %{v1_detected: false, npm: true} -> generic_notice(assigns)- _ -> ~H""+ %{v1_detected: true, wordpress_plugin: true} ->+ ~H"""+ <.additional_action_required />+ <.v1_wordpress_plugin_notice />+ """++ %{v1_detected: true} ->+ ~H"""+ <.additional_action_required />+ <.v1_generic_notice site={@site} />+ """++ %{v1_detected: false, npm: true} ->+ ~H"""+ <.additional_action_required />+ <.npm_notice />+ """++ _ ->+ ~H"""+ <.success_notice />+ <.tracking_works_notice />+ """endend- defp wordpress_plugin_notice(assigns) do- assigns = assign(assigns, docs_link: @change_domain_docs_link)+ defp success_notice(assigns) do+ ~H"""+ <div class="flex items-center">+ <Heroicons.check class="w-4 h-4 mr-2 text-green-500" />+ <span class="text-sm font-bold">+ Your new domain should be tracking nicely+ </span>+ </div>+ """+ end+ defp additional_action_required(assigns) do~H"""- <.notice class="mt-4" title="Additional Steps Required">- To guarantee continuous tracking, you <i>must</i>- also update the site <code>domain</code>- in your Plausible Wordpress Plugin settings within 72 hours- to match the updated domain. See- <.styled_link new_tab href={@docs_link}>- documentation+ <div class="flex items-center">+ <Heroicons.exclamation_triangle class="w-4 h-4 mr-2 text-yellow-500" />+ <span class="text-sm font-bold">+ Additional action required+ </span>+ </div>+ """+ end++ defp v1_wordpress_plugin_notice(assigns) do+ # A v2 tracker based WordPress plugin is not yet ready so the WP plugin users+ # need to change their site domain instead of upgrading to script v2.+ ~H"""+ <p class="mt-4 text-sm">+ We've detected you're using our WordPress plugin. To guarantee continuous tracking,+ you must also update the site domain in your Plausible Wordpress Plugin settings+ within 72 hours to match the updated domain.+ </p>+ """+ end++ defp v1_generic_notice(assigns) do+ ~H"""+ <p class="mt-4 text-sm">+ We've detected you're using our legacy script. This means that you'll also need+ to update the site domain of your Plausible installation within 72 hours to guarantee+ continuous tracking. The easiest way to fix that is to simply follow your+ <.styled_link+ new_tab+ href={Routes.site_path(PlausibleWeb.Endpoint, :installation, @site.domain)}+ >+ installation instructions</.styled_link>- for details.- </.notice>+ and upgrade to our new, more powerful tracking script.+ </p>"""end- defp generic_notice(assigns) do- assigns = assign(assigns, docs_link: @change_domain_docs_link)+ defp npm_notice(assigns) do+ ~H"""+ <p class="mt-4 text-sm">+ We've detected you're using our @plausible-analytics/tracker module. This means that you'll also need+ to update the site domain of your Plausible installation within 72 hours to+ guarantee continuous tracking.+ </p>+ """+ end++ defp tracking_works_notice(assigns) do+ assigns = assign(assigns, :docs_link, @change_domain_checklist_docs_link)~H"""- <.notice class="mt-4" title="Additional Steps Required">- To guarantee continuous tracking, you <i>must</i>- also update the site <code>domain</code>- of your Plausible Installation within 72 hours- to match the updated domain. See+ <p class="mt-4 text-sm">+ Take a quick look at our<.styled_link new_tab href={@docs_link}>- documentation+ domain change checklist</.styled_link>- for details.- </.notice>+ to make sure no further action is needed.+ </p>"""enddefp run_detection(domain) dowith {:ok, detection_result} <-Detection.Checks.run_with_rate_limit(nil, domain,+ detection_check_timeout: 11_000,detect_v1?: true,report_to: nil,async?: false,@@ -195,22 +296,19 @@ defmodule PlausibleWeb.Live.ChangeDomain doendelsedefp ce_generic_notice(assigns) do- assigns = assign(assigns, docs_link: @change_domain_docs_link)-~H"""- <.notice data-testid="ce-generic-notice" class="mt-4" title="Additional steps may be required">- If you're using our legacy script (i.e. your Plausible snippet includes the- <code>data-domain</code>- attribute), OR if you've installed Plausible using- our NPM package, you <i>must</i>- also update the site <code>domain</code>- of- your Plausible Installation within 72 hours to match the updated domain. See- <.styled_link new_tab href={@docs_link}>- documentation- </.styled_link>- for details.- </.notice>+ <div class="flex items-center">+ <Heroicons.exclamation_triangle class="w-4 h-4 mr-2 text-yellow-500" />+ <span class="text-sm font-bold">+ Additional action may be required+ </span>+ </div>+ <p class="mt-4 text-sm">+ If you're using our legacy snippet (i.e. your Plausible snippet includes the+ data-domain attribute) or the NPM package, you must also update the site domain+ of your Plausible installation within 72 hours to match the updated domain in+ order to guarantee continuous tracking.+ </p>"""endenddiff --git a/test/plausible/installation_support/check_test.exs b/test/plausible/installation_support/check_test.exsindex 3de4a7364b..f89b35d9e3 100644--- a/test/plausible/installation_support/check_test.exs+++ b/test/plausible/installation_support/check_test.exs@@ -19,7 +19,7 @@ defmodule Plausible.InstallationSupport.CheckTest dodef report_progress_as, do: "Faulty check"@impl true- def perform(_), do: raise("boom")+ def perform(_, _), do: raise("boom")endstate = %State{@@ -53,7 +53,7 @@ defmodule Plausible.InstallationSupport.CheckTest dodef report_progress_as, do: "Faulty check"@impl true- def perform(_), do: :erlang.throw(:boom)+ def perform(_, _), do: :erlang.throw(:boom)endstate = %State{@@ -83,7 +83,7 @@ defmodule Plausible.InstallationSupport.CheckTest dodef report_progress_as, do: "Faulty check"@impl true- def perform(_), do: :timer.sleep(500)+ def perform(_, _), do: :timer.sleep(500)endstate = %State{diff --git a/test/plausible/installation_support/checks/url_test.exs b/test/plausible/installation_support/checks/url_test.exsindex 864114beeb..28ac54b998 100644--- a/test/plausible/installation_support/checks/url_test.exs+++ b/test/plausible/installation_support/checks/url_test.exs@@ -30,11 +30,14 @@ defmodule Plausible.InstallationSupport.Checks.UrlTest doend)state =- @check.perform(%State{- data_domain: unquote(site_domain),- url: nil,- diagnostics: %Verification.Diagnostics{}- })+ @check.perform(+ %State{+ data_domain: unquote(site_domain),+ url: nil,+ diagnostics: %Verification.Diagnostics{}+ },+ []+ )assert state.url == "https://#{unquote(site_domain)}"refute state.diagnostics.service_error@@ -54,11 +57,14 @@ defmodule Plausible.InstallationSupport.Checks.UrlTest doend)state =- @check.perform(%State{- data_domain: site_domain,- url: nil,- diagnostics: %Verification.Diagnostics{}- })+ @check.perform(+ %State{+ data_domain: site_domain,+ url: nil,+ diagnostics: %Verification.Diagnostics{}+ },+ []+ )assert state.url == "https://www.example.com/any/deeper/path"refute state.diagnostics.service_error@@ -76,11 +82,14 @@ defmodule Plausible.InstallationSupport.Checks.UrlTest dodomain = "any.example.com"state =- @check.perform(%State{- data_domain: domain,- url: nil,- diagnostics: %Verification.Diagnostics{}- })+ @check.perform(+ %State{+ data_domain: domain,+ url: nil,+ diagnostics: %Verification.Diagnostics{}+ },+ []+ )assert state.url == nilassert state.diagnostics.service_error == %{code: :domain_not_found}@@ -99,11 +108,14 @@ defmodule Plausible.InstallationSupport.Checks.UrlTest doend)state =- @check.perform(%State{- data_domain: site_domain,- url: url,- diagnostics: %Verification.Diagnostics{}- })+ @check.perform(+ %State{+ data_domain: site_domain,+ url: url,+ diagnostics: %Verification.Diagnostics{}+ },+ []+ )assert state.url == "https://blog.example.com/recipes"refute state.diagnostics.service_error@@ -112,11 +124,14 @@ defmodule Plausible.InstallationSupport.Checks.UrlTest dotest "rejects file:// scheme, does not check domain" dostate =- @check.perform(%State{- data_domain: "example-com-rollup",- url: "file://example.com/archives/news?p=any#fragment",- diagnostics: %Verification.Diagnostics{}- })+ @check.perform(+ %State{+ data_domain: "example-com-rollup",+ url: "file://example.com/archives/news?p=any#fragment",+ diagnostics: %Verification.Diagnostics{}+ },+ []+ )assert state.url == "file://example.com/archives/news?p=any#fragment"assert state.diagnostics.service_error == %{code: :invalid_url}@@ -133,11 +148,14 @@ defmodule Plausible.InstallationSupport.Checks.UrlTest doend)state =- @check.perform(%State{- data_domain: site_domain,- url: url,- diagnostics: %Verification.Diagnostics{}- })+ @check.perform(+ %State{+ data_domain: site_domain,+ url: url,+ diagnostics: %Verification.Diagnostics{}+ },+ []+ )assert state.url == urlassert state.diagnostics.service_error == %{code: :domain_not_found}diff --git a/test/plausible_web/live/change_domain_test.exs b/test/plausible_web/live/change_domain_test.exsindex aa4dd3895d..864555415d 100644--- a/test/plausible_web/live/change_domain_test.exs+++ b/test/plausible_web/live/change_domain_test.exs@@ -173,7 +173,7 @@ defmodule PlausibleWeb.Live.ChangeDomainTest doassert_patch(lv, "/#{URI.encode_www_form(new_domain)}/change-domain/success")html = render_async(lv, 500)- assert html =~ "<i>must</i>"+ assert html =~ "using our WordPress plugin"assert html =~ "also update the site"assert html =~ "Plausible Wordpress Plugin settings"assert html =~ "within 72 hours"@@ -206,11 +206,11 @@ defmodule PlausibleWeb.Live.ChangeDomainTest doassert_patch(lv, "/#{URI.encode_www_form(new_domain)}/change-domain/success")html = render_async(lv, 500)- assert html =~ "<i>must</i>"- assert html =~ "also update the site"- assert html =~ "Plausible Installation"+ assert html =~ "using our legacy script"+ assert html =~ "update the site domain"+ assert html =~ "Plausible installation"assert html =~ "within 72 hours"- refute html =~ "Wordpress Plugin"+ refute html =~ "Wordpress"assert element_exists?(html,@@ -219,7 +219,10 @@ defmodule PlausibleWeb.Live.ChangeDomainTest doend@tag :ee_only- test "success page shows no notice when no v1 tracking detected", %{conn: conn, site: site} do+ test "success page shows 'tracking works' notice when no v1 tracking detected", %{+ conn: conn,+ site: site+ } dostub_detection_result(%{"v1Detected" => false,"gtmLikely" => false,@@ -237,18 +240,22 @@ defmodule PlausibleWeb.Live.ChangeDomainTest doassert_patch(lv, "/#{URI.encode_www_form(new_domain)}/change-domain/success")html = render_async(lv, 500)- refute html =~ "Additional Steps Required"- refute html =~ "<i>must</i>"- refute html =~ "also update the site"+ assert html =~ "Your new domain should be tracking nicely"+ assert html =~ "domain change checklist"- refute element_exists?(+ assert element_exists?(+ html,+ "a[href='#{PlausibleWeb.Live.ChangeDomain.change_domain_checklist_docs_link()}']"+ )++ assert element_exists?(html,"a[href='#{PlausibleWeb.Live.ChangeDomain.change_domain_docs_link()}']")end@tag :ee_only- test "success page shows generic npm notice when detected", %{conn: conn, site: site} do+ test "success page shows npm notice when detected", %{conn: conn, site: site} dostub_detection_result(%{"v1Detected" => false,"gtmLikely" => false,@@ -267,9 +274,8 @@ defmodule PlausibleWeb.Live.ChangeDomainTest doassert_patch(lv, "/#{URI.encode_www_form(new_domain)}/change-domain/success")html = render_async(lv, 500)- assert html =~ "<i>must</i>"- assert html =~ "also update the site"- assert html =~ "Plausible Installation"+ assert html =~ "using our @plausible-analytics/tracker module"+ assert html =~ "Plausible installation"assert html =~ "within 72 hours"assert element_exists?(@@ -311,10 +317,13 @@ defmodule PlausibleWeb.Live.ChangeDomainTest doassert_patch(lv, "/#{URI.encode_www_form(new_domain)}/change-domain/success")html = render_async(lv, 500)- assert html =~ "Additional Steps Required"- assert html =~ "<i>must</i>"- assert html =~ "also update the site"- assert html =~ "Plausible Installation"+ assert html =~ "We could not reach your new domain"+ assert html =~ "Additional action may be required"++ assert element_exists?(+ html,+ "a[href='#{PlausibleWeb.Live.ChangeDomain.change_domain_docs_link()}']"+ )end@tag ee_only: true, capture_log: true@@ -331,10 +340,13 @@ defmodule PlausibleWeb.Live.ChangeDomainTest doassert_patch(lv, "/#{URI.encode_www_form(new_domain)}/change-domain/success")html = render_async(lv, 500)- assert html =~ "Additional Steps Required"- assert html =~ "<i>must</i>"- assert html =~ "also update the site"- assert html =~ "Plausible Installation"+ assert html =~ "We could not reach your new domain"+ assert html =~ "Additional action may be required"++ assert element_exists?(+ html,+ "a[href='#{PlausibleWeb.Live.ChangeDomain.change_domain_docs_link()}']"+ )end@tag :ce_build_only@@ -352,9 +364,11 @@ defmodule PlausibleWeb.Live.ChangeDomainTest doassert_patch(lv, "/#{URI.encode_www_form(new_domain)}/change-domain/success")html = render_async(lv, 500)- notice = text_of_element(html, "div[data-testid='ce-generic-notice']")- assert notice =~ "Additional steps may be required"+ assert html =~ "Additional action may be required"+ assert html =~ "using our legacy snippet"+ assert html =~ "must also update the site domain"+ assert html =~ "72 hours"assert element_exists?(html,