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 2-step flow (#5630)
Uku Tahta32e9ee53d99
diff --git a/lib/plausible_web/live/change_domain_v2.ex b/lib/plausible_web/live/change_domain_v2.exindex bbf0f0b9cd..389b9eda57 100644--- a/lib/plausible_web/live/change_domain_v2.ex+++ b/lib/plausible_web/live/change_domain_v2.ex@@ -4,8 +4,10 @@ defmodule PlausibleWeb.Live.ChangeDomainV2 do"""use PlausibleWeb, :live_view- alias Plausible.Sitealias PlausibleWeb.Router.Helpers, as: Routes+ alias PlausibleWeb.Live.ChangeDomainV2.Form+ alias Plausible.InstallationSupport.Detection+ alias Phoenix.LiveView.AsyncResultdef mount(%{"domain" => domain},@@ -19,17 +21,37 @@ defmodule PlausibleWeb.Live.ChangeDomainV2 do:super_admin])- changeset = Site.update_changeset(site)-{:ok,assign(socket,site: site,- changeset: changeset,- domain: domain+ detection_result: AsyncResult.loading())}end- def render(assigns) do+ def handle_params(_params, _url, socket) do+ socket =+ if socket.assigns.live_action == :success and connected?(socket) do+ site_domain = socket.assigns.site.domain++ assign_async(socket, :detection_result, fn ->+ run_detection("https://#{site_domain}")+ end)+ else+ socket+ end++ {:noreply, socket}+ end++ def render(%{live_action: :change_domain_v2} = assigns) do+ render_form_step(assigns)+ end++ def render(%{live_action: :success} = assigns) do+ render_success_step(assigns)+ end++ defp render_form_step(assigns) do~H"""<.focus_box><:title>Change your website domain</:title>@@ -48,43 +70,95 @@ defmodule PlausibleWeb.Live.ChangeDomainV2 do</.focus_list></:footer>- <.form :let={f} for={@changeset} phx-submit="submit">- <.input- help_text="Just the naked domain or subdomain without 'www', 'https' etc."- type="text"- placeholder="example.com"- field={f[:domain]}- label="Domain"- />-- <.button type="submit" class="mt-4 w-full">- Change Domain- </.button>-- <.notice class="mt-4" title="Additional Steps May Be Required">- If you are using the Wordpress plugin, NPM module, or Events API for tracking, you must also update the tracking- <code>domain</code>- to match the updated domain. See- <.styled_link new_tab href="https://plausible.io/docs/change-domain-name/">- documentation- </.styled_link>- for details.- </.notice>- </.form>+ <.live_component module={Form} id="change-domain-form" site={@site} /></.focus_box>"""end- def handle_event("submit", %{"site" => %{"domain" => new_domain}}, socket) do- case Site.Domain.change(socket.assigns.site, new_domain) do- {:ok, updated_site} ->- {:noreply,- socket- |> put_flash(:success, "Website domain changed successfully")- |> push_navigate(to: Routes.site_path(socket, :settings_general, updated_site.domain))}+ defp render_success_step(assigns) do+ ~H"""+ <.focus_box>+ <:title>Domain Changed Successfully</:title>+ <:subtitle>+ Your website domain has been successfully updated from+ <strong>{@site.domain_changed_from}</strong>+ to <strong><%= @site.domain %></strong>.+ </:subtitle>++ <:footer>+ <.focus_list>+ <:item>+ <.styled_link href={Routes.site_path(@socket, :settings_general, @site.domain)}>+ Go to Site Settings+ </.styled_link>+ </:item>+ </.focus_list>+ </:footer>++ <.async_result :let={detection_result} assign={@detection_result}>+ <:loading>+ <div class="flex items-center">+ <.spinner class="w-4 h-4 mr-2" />+ <span class="text-sm text-gray-600">Checking your new domain...</span>+ </div>+ </:loading>++ <:failed>+ <.generic_notice />+ </:failed>++ <.wordpress_plugin_notice :if={+ detection_result && detection_result.v1_detected && detection_result.wordpress_plugin+ } />+ <.generic_notice :if={+ detection_result && detection_result.v1_detected && !detection_result.wordpress_plugin+ } />+ </.async_result>+ </.focus_box>+ """+ end++ defp wordpress_plugin_notice(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="https://plausible.io/docs/change-domain-name/">+ documentation+ </.styled_link>+ for details.+ </.notice>+ """+ end++ defp generic_notice(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>+ of your Plausible Installation within 72 hours+ to match the updated domain. See+ <.styled_link new_tab href="https://plausible.io/docs/change-domain-name/">+ documentation+ </.styled_link>+ for details.+ </.notice>+ """+ end++ def handle_info({:domain_changed, updated_site}, socket) do+ {:noreply,+ socket+ |> assign(site: updated_site)+ |> push_patch(to: Routes.site_path(socket, :success, updated_site.domain))}+ end- {:error, changeset} ->- {:noreply, assign(socket, changeset: changeset)}+ defp run_detection(url) do+ case Detection.perform(url, detect_v1?: true) do+ {:ok, result} -> {:ok, %{detection_result: result}}+ e -> eendendenddiff --git a/lib/plausible_web/live/change_domain_v2/form.ex b/lib/plausible_web/live/change_domain_v2/form.exnew file mode 100644index 0000000000..73d83345b2--- /dev/null+++ b/lib/plausible_web/live/change_domain_v2/form.ex@@ -0,0 +1,45 @@+defmodule PlausibleWeb.Live.ChangeDomainV2.Form do+ @moduledoc """+ Live component for the change domain form+ """+ use PlausibleWeb, :live_component++ alias Plausible.Site++ def update(assigns, socket) do+ changeset = Site.update_changeset(assigns.site)++ {:ok, assign(socket, Map.put(assigns, :changeset, changeset))}+ end++ def render(assigns) do+ ~H"""+ <div>+ <.form :let={f} for={@changeset} phx-submit="submit" phx-target={@myself}>+ <.input+ help_text="Just the naked domain or subdomain without 'www', 'https' etc."+ type="text"+ placeholder="example.com"+ field={f[:domain]}+ label="Domain"+ />++ <.button type="submit" class="mt-4 w-full">+ Change Domain+ </.button>+ </.form>+ </div>+ """+ end++ def handle_event("submit", %{"site" => %{"domain" => new_domain}}, socket) do+ case Plausible.Site.Domain.change(socket.assigns.site, new_domain) do+ {:ok, updated_site} ->+ send(self(), {:domain_changed, updated_site})+ {:noreply, socket}++ {:error, changeset} ->+ {:noreply, assign(socket, changeset: changeset)}+ end+ end+enddiff --git a/lib/plausible_web/router.ex b/lib/plausible_web/router.exindex 58f8988fd1..813f5a1d05 100644--- a/lib/plausible_web/router.ex+++ b/lib/plausible_web/router.ex@@ -631,6 +631,7 @@ defmodule PlausibleWeb.Router dodogfood_page_path: "/:website/change-domain-v2"} dolive "/:domain/change-domain-v2", ChangeDomainV2, :change_domain_v2, as: :site+ live "/:domain/change-domain-v2/success", ChangeDomainV2, :success, as: :siteendenddiff --git a/test/plausible_web/live/change_domain_v2_test.exs b/test/plausible_web/live/change_domain_v2_test.exsindex a320bc0dbe..ba2c992471 100644--- a/test/plausible_web/live/change_domain_v2_test.exs+++ b/test/plausible_web/live/change_domain_v2_test.exs@@ -1,6 +1,8 @@defmodule PlausibleWeb.Live.ChangeDomainV2Test do- use PlausibleWeb.ConnCase, async: true+ use PlausibleWeb.ConnCase, async: falseimport Phoenix.LiveViewTest+ import Plausible.TestUtils+ import ExUnit.CaptureLogalias Plausible.Repo@@ -56,26 +58,51 @@ defmodule PlausibleWeb.Live.ChangeDomainV2Test doassert is_nil(site.domain_changed_from)end- test "successful form submission navigates to installation", %{conn: conn, site: site} do+ test "successful form submission updates database", %{conn: conn, site: site} do+ stub_detection_result(%{+ "v1Detected" => false,+ "gtmLikely" => false,+ "wordpressLikely" => false,+ "wordpressPlugin" => false+ })+original_domain = site.domainnew_domain = "new-example.com"{:ok, lv, _html} = live(conn, "/#{site.domain}/change-domain-v2")- result =- lv- |> element("form")- |> render_submit(%{site: %{domain: new_domain}})-- assert {:error, {:live_redirect, %{to: redirect_path}}} = result-- assert redirect_path ==- Routes.site_path(conn, :settings_general, new_domain)+ lv+ |> element("form")+ |> render_submit(%{site: %{domain: new_domain}})site = Repo.reload!(site)assert site.domain == new_domainassert site.domain_changed_from == original_domainend+ test "successful form submission navigates to success page", %{conn: conn, site: site} do+ stub_detection_result(%{+ "v1Detected" => false,+ "gtmLikely" => false,+ "wordpressLikely" => false,+ "wordpressPlugin" => false+ })++ original_domain = site.domain+ new_domain = "new-example.com"+ {:ok, lv, _html} = live(conn, "/#{site.domain}/change-domain-v2")++ lv+ |> element("form")+ |> render_submit(%{site: %{domain: new_domain}})++ assert_patch(lv, "/#{new_domain}/change-domain-v2/success")++ html = render_async(lv, 500)+ assert html =~ "Domain Changed Successfully"+ assert html =~ original_domain+ assert html =~ new_domain+ end+test "form validation shows error for empty domain", %{conn: conn, site: site} do{:ok, lv, _html} = live(conn, "/#{site.domain}/change-domain-v2")@@ -104,5 +131,117 @@ defmodule PlausibleWeb.Live.ChangeDomainV2Test doexpected_link = Routes.site_path(conn, :settings_general, site.domain)assert html =~ expected_linkend++ test "success page shows WordPress plugin notice when detected", %{conn: conn, site: site} do+ stub_detection_result(%{+ "v1Detected" => true,+ "gtmLikely" => false,+ "wordpressLikely" => true,+ "wordpressPlugin" => true+ })++ new_domain = "new-example.com"+ {:ok, lv, _html} = live(conn, "/#{site.domain}/change-domain-v2")++ lv+ |> element("form")+ |> render_submit(%{site: %{domain: new_domain}})++ assert_patch(lv, "/#{new_domain}/change-domain-v2/success")++ html = render_async(lv, 500)+ assert html =~ "<i>must</i>"+ assert html =~ "also update the site"+ assert html =~ "Plausible Wordpress Plugin settings"+ assert html =~ "within 72 hours"+ end++ test "success page shows generic v1 notice when detected but not WordPress", %{+ conn: conn,+ site: site+ } do+ stub_detection_result(%{+ "v1Detected" => true,+ "gtmLikely" => false,+ "wordpressLikely" => false,+ "wordpressPlugin" => false+ })++ new_domain = "new-example.com"+ {:ok, lv, _html} = live(conn, "/#{site.domain}/change-domain-v2")++ lv+ |> element("form")+ |> render_submit(%{site: %{domain: new_domain}})++ assert_patch(lv, "/#{new_domain}/change-domain-v2/success")++ html = render_async(lv, 500)+ assert html =~ "<i>must</i>"+ assert html =~ "also update the site"+ assert html =~ "Plausible Installation"+ assert html =~ "within 72 hours"+ refute html =~ "Wordpress Plugin"+ end++ test "success page shows no notice when no v1 tracking detected", %{conn: conn, site: site} do+ stub_detection_result(%{+ "v1Detected" => false,+ "gtmLikely" => false,+ "wordpressLikely" => false,+ "wordpressPlugin" => false+ })++ new_domain = "new-example.com"+ {:ok, lv, _html} = live(conn, "/#{site.domain}/change-domain-v2")++ lv+ |> element("form")+ |> render_submit(%{site: %{domain: new_domain}})++ assert_patch(lv, "/#{new_domain}/change-domain-v2/success")++ html = render_async(lv, 500)+ refute html =~ "Additional Steps Required"+ refute html =~ "<i>must</i>"+ refute html =~ "also update the site"+ end++ test "success page handles detection error gracefully", %{conn: conn, site: site} do+ stub_detection_error()++ capture_log(fn ->+ new_domain = "new-example.com"+ {:ok, lv, _html} = live(conn, "/#{site.domain}/change-domain-v2")++ lv+ |> element("form")+ |> render_submit(%{site: %{domain: new_domain}})++ assert_patch(lv, "/#{new_domain}/change-domain-v2/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"+ end)+ end+ end++ defp stub_detection_result(js_data) do+ Req.Test.stub(:global, fn conn ->+ conn+ |> put_resp_content_type("application/json")+ |> send_resp(200, Jason.encode!(%{"data" => Map.put(js_data, "completed", true)}))+ end)+ end++ defp stub_detection_error do+ Req.Test.stub(:global, fn conn ->+ conn+ |> put_resp_content_type("application/json")+ |> send_resp(200, Jason.encode!(%{"data" => %{"error" => "Simulated browser error"}}))+ end)endenddiff --git a/test/plausible_web/live/installationv2_test.exs b/test/plausible_web/live/installationv2_test.exsindex ff62b70a9e..a168feb0a5 100644--- a/test/plausible_web/live/installationv2_test.exs+++ b/test/plausible_web/live/installationv2_test.exs@@ -28,16 +28,8 @@ defmodule PlausibleWeb.Live.InstallationV2Test do{lv, _} = get_lv(conn, site)- assert eventually(fn ->- html = render(lv)-- {- text(html) =~ "Verify WordPress installation",- html- }- end)-- _ = render(lv)+ html = render_async(lv, 500)+ assert text(html) =~ "Verify WordPress installation"endtest "When ?type URL parameter is supplied, detected type is unused", %{@@ -48,26 +40,16 @@ defmodule PlausibleWeb.Live.InstallationV2Test do{lv, _} = get_lv(conn, site, "?type=gtm")- assert eventually(fn ->- html = render(lv)-- {- text(html) =~ "Verify Tag Manager installation",- html- }- end)-- _ = render(lv)+ html = render_async(lv, 500)+ assert text(html) =~ "Verify Tag Manager installation"endtest "allows switching between installation tabs", %{conn: conn, site: site} dostub_detection_manual(){lv, _html} = get_lv(conn, site, "?type=manual")- assert eventually(fn ->- html = render(lv)- {html =~ "Verify Script installation", html}- end)+ html = render_async(lv, 500)+ assert html =~ "Verify Script installation"lv|> element("a[href*=\"type=wordpress\"]")@@ -111,12 +93,8 @@ defmodule PlausibleWeb.Live.InstallationV2Test dostub_detection_manual(){lv, _html} = get_lv(conn, site, "?type=manual&flow=review")- assert eventually(fn ->- html = render(lv)- {html =~ "Verify Script installation", html}- end)-- html = render(lv)+ html = render_async(lv, 500)+ assert html =~ "Verify Script installation"assert html =~ "Optional measurements"assert html =~ "Outbound links"assert html =~ "File downloads"@@ -127,14 +105,9 @@ defmodule PlausibleWeb.Live.InstallationV2Test dostub_detection_manual(){lv, _html} = get_lv(conn, site, "?type=manual&flow=review")- assert eventually(fn ->- html = render(lv)- {html =~ "Verify Script installation", html}- end)-- html = render(lv)+ html = render_async(lv, 500)+ assert html =~ "Verify Script installation"assert html =~ "Advanced options"-assert html =~ "Manual tagging"assert html =~ "404 error pages"assert html =~ "Hashed page paths"@@ -149,10 +122,8 @@ defmodule PlausibleWeb.Live.InstallationV2Test dostub_detection_manual(){lv, _html} = get_lv(conn, site, "?type=manual&flow=review")- assert eventually(fn ->- html = render(lv)- {html =~ "Verify Script installation", html}- end)+ html = render_async(lv, 500)+ assert html =~ "Verify Script installation"config = TrackerScriptConfiguration |> Plausible.Repo.get_by!(site_id: site.id)assert config.outbound_links == true@@ -180,10 +151,8 @@ defmodule PlausibleWeb.Live.InstallationV2Test dostub_detection_manual(){lv, _html} = get_lv(conn, site, "?type=manual")- assert eventually(fn ->- html = render(lv)- {html =~ "Verify Script installation", html}- end)+ html = render_async(lv, 500)+ assert html =~ "Verify Script installation"lv|> element("form[phx-submit='submit']")@@ -206,10 +175,8 @@ defmodule PlausibleWeb.Live.InstallationV2Test dostub_detection_manual(){lv, _html} = get_lv(conn, site, "?type=manual")- assert eventually(fn ->- html = render(lv)- {html =~ "Verify Script installation", html}- end)+ html = render_async(lv, 500)+ assert html =~ "Verify Script installation"# Test with all options disabledlv@@ -235,10 +202,8 @@ defmodule PlausibleWeb.Live.InstallationV2Test dostub_detection_manual(){lv, _html} = get_lv(conn, site, "?type=manual&flow=review")- assert eventually(fn ->- html = render(lv)- {html =~ "Verify Script installation", html}- end)+ html = render_async(lv, 500)+ assert html =~ "Verify Script installation"lv|> element("form[phx-submit='submit']")@@ -259,14 +224,8 @@ defmodule PlausibleWeb.Live.InstallationV2Test do{lv, _} = get_lv(conn, site)- assert eventually(fn ->- html = render(lv)-- {- text(html) =~ "We've detected your website is using WordPress",- html- }- end)+ html = render_async(lv, 500)+ assert text(html) =~ "We've detected your website is using WordPress"endtest "detected GTM installation shows special message", %{conn: conn, site: site} do@@ -274,19 +233,9 @@ defmodule PlausibleWeb.Live.InstallationV2Test do{lv, _} = get_lv(conn, site)- assert eventually(fn ->- html = render(lv)- {html =~ "Verify Tag Manager installation", html}- end)-- assert eventually(fn ->- html = render(lv)-- {- text(html) =~ "We've detected your website is using Google Tag Manager",- html- }- end)+ html = render_async(lv, 500)+ assert html =~ "Verify Tag Manager installation"+ assert text(html) =~ "We've detected your website is using Google Tag Manager"endtest "shows v1 detection warning for manual installation", %{conn: conn, site: site} do@@ -294,15 +243,8 @@ defmodule PlausibleWeb.Live.InstallationV2Test do{lv, _} = get_lv(conn, site, "?type=manual")- assert eventually(fn ->- html = render(lv)-- {- text(html) =~- "Your website is running an outdated version of the tracking script",- html- }- end)+ html = render_async(lv, 500)+ assert text(html) =~ "Your website is running an outdated version of the tracking script"endtest "does not show v1 detection warning for non-manual installation", %{@@ -313,12 +255,8 @@ defmodule PlausibleWeb.Live.InstallationV2Test do{lv, _} = get_lv(conn, site, "?type=wordpress")- assert eventually(fn ->- html = render(lv)- {html =~ "Verify WordPress installation", html}- end)-- html = render(lv)+ html = render_async(lv, 500)+ assert html =~ "Verify WordPress installation"refute text(html) =~ "Your website is running an outdated version of the tracking script"end@@ -328,11 +266,9 @@ defmodule PlausibleWeb.Live.InstallationV2Test doExUnit.CaptureLog.capture_log(fn ->{lv, _} = get_lv(conn, site)- assert eventually(fn ->- html = render(lv)- # Should default to manual installation when detection returns {:error, _}- {html =~ "Verify Script installation", html}- end)+ html = render_async(lv, 500)+ # Should default to manual installation when detection returns {:error, _}+ assert html =~ "Verify Script installation"end)endend@@ -354,10 +290,8 @@ defmodule PlausibleWeb.Live.InstallationV2Test do{lv, _} = get_lv(conn, site)- assert eventually(fn ->- html = render(lv)- {html =~ "Verify Script installation", html}- end)+ html = render_async(lv, 500)+ assert html =~ "Verify Script installation"endtest "allows editor access to installation page", %{conn: conn, user: user} do@@ -367,10 +301,8 @@ defmodule PlausibleWeb.Live.InstallationV2Test do{lv, _} = get_lv(conn, site)- assert eventually(fn ->- html = render(lv)- {html =~ "Verify Script installation", html}- end)+ html = render_async(lv, 500)+ assert html =~ "Verify Script installation"endend@@ -383,10 +315,8 @@ defmodule PlausibleWeb.Live.InstallationV2Test do{lv, _} = get_lv(conn, site, "?type=invalid")- assert eventually(fn ->- html = render(lv)- {html =~ "Verify Script installation", html}- end)+ html = render_async(lv, 500)+ assert html =~ "Verify Script installation"endtest "falls back to provisioning flow when invalid flow parameter supplied", %{@@ -397,10 +327,8 @@ defmodule PlausibleWeb.Live.InstallationV2Test do{lv, _} = get_lv(conn, site, "?flow=invalid")- assert eventually(fn ->- html = render(lv)- {html =~ "Verify Script installation", html}- end)+ html = render_async(lv, 500)+ assert html =~ "Verify Script installation"endend@@ -415,10 +343,8 @@ defmodule PlausibleWeb.Live.InstallationV2Test do{lv, _} = get_lv(conn, site)- assert eventually(fn ->- html = render(lv)- {html =~ "Verify Tag Manager installation", html}- end)+ html = render_async(lv, 500)+ assert html =~ "Verify Tag Manager installation"endend@@ -439,10 +365,8 @@ defmodule PlausibleWeb.Live.InstallationV2Test do{lv, _} = get_lv(conn, site, "?flow=review")- assert eventually(fn ->- html = render(lv)- {html =~ "Verify WordPress installation", html}- end)+ html = render_async(lv, 500)+ assert html =~ "Verify WordPress installation"endend