Skip to content

Open source, privacy-first web analytics. Lightweight, cookie-free Google Analytics alternative. Self-hosted or cloud.

Scriptv2 - change domain (#5565)

Uku Tahtb7f8ed460763
diff --git a/lib/plausible_web/controllers/site_controller.ex b/lib/plausible_web/controllers/site_controller.ex
index b5ba6735b4..9690a9df78 100644
--- a/lib/plausible_web/controllers/site_controller.ex
+++ b/lib/plausible_web/controllers/site_controller.ex
@@ -747,12 +747,18 @@ defmodule PlausibleWeb.SiteController do
end
def change_domain(conn, _params) do
- changeset = Plausible.Site.update_changeset(conn.assigns.site)
+ if FunWithFlags.enabled?(:scriptv2, for: conn.assigns.site) do
+ redirect(conn,
+ to: Routes.site_path(conn, :change_domain_v2, conn.assigns.site.domain)
+ )
+ else
+ changeset = Plausible.Site.update_changeset(conn.assigns.site)
- render(conn, "change_domain.html",
- skip_plausible_tracking: true,
- changeset: changeset
- )
+ render(conn, "change_domain.html",
+ skip_plausible_tracking: true,
+ changeset: changeset
+ )
+ end
end
def change_domain_submit(conn, %{"site" => %{"domain" => new_domain}}) do
diff --git a/lib/plausible_web/live/change_domain_v2.ex b/lib/plausible_web/live/change_domain_v2.ex
new file mode 100644
index 0000000000..bbf0f0b9cd
--- /dev/null
+++ b/lib/plausible_web/live/change_domain_v2.ex
@@ -0,0 +1,90 @@
+defmodule PlausibleWeb.Live.ChangeDomainV2 do
+ @moduledoc """
+ LiveView for the change domain v2 flow.
+ """
+ use PlausibleWeb, :live_view
+
+ alias Plausible.Site
+ alias PlausibleWeb.Router.Helpers, as: Routes
+
+ def mount(
+ %{"domain" => domain},
+ _session,
+ socket
+ ) do
+ site =
+ Plausible.Sites.get_for_user!(socket.assigns.current_user, domain, [
+ :owner,
+ :admin,
+ :super_admin
+ ])
+
+ changeset = Site.update_changeset(site)
+
+ {:ok,
+ assign(socket,
+ site: site,
+ changeset: changeset,
+ domain: domain
+ )}
+ end
+
+ def render(assigns) do
+ ~H"""
+ <.focus_box>
+ <:title>Change your website domain</:title>
+ <:subtitle>
+ If you have changed the domain name of your site and would like your new domain name to be displayed in your Plausible dashboard, you can do so here. You won't lose any of your historical stats in this process.
+ </:subtitle>
+
+ <:footer>
+ <.focus_list>
+ <:item>
+ Changed your mind? Go back to
+ <.styled_link href={Routes.site_path(@socket, :settings_general, @site.domain)}>
+ Site Settings
+ </.styled_link>
+ </:item>
+ </.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>
+ </.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))}
+
+ {:error, changeset} ->
+ {:noreply, assign(socket, changeset: changeset)}
+ end
+ end
+end
diff --git a/lib/plausible_web/router.ex b/lib/plausible_web/router.ex
index 0cd93b23d1..359db7dc95 100644
--- a/lib/plausible_web/router.ex
+++ b/lib/plausible_web/router.ex
@@ -624,6 +624,12 @@ defmodule PlausibleWeb.Router do
} do
live "/:domain/verification", Verification, :verification, as: :site
end
+
+ scope assigns: %{
+ dogfood_page_path: "/:website/change-domain-v2"
+ } do
+ live "/:domain/change-domain-v2", ChangeDomainV2, :change_domain_v2, as: :site
+ end
end
get "/:domain/settings", SiteController, :settings
diff --git a/test/plausible_web/controllers/site_controller_test.exs b/test/plausible_web/controllers/site_controller_test.exs
index 61b3fb8f2c..5c3de15602 100644
--- a/test/plausible_web/controllers/site_controller_test.exs
+++ b/test/plausible_web/controllers/site_controller_test.exs
@@ -1935,6 +1935,15 @@ defmodule PlausibleWeb.SiteControllerTest do
assert site.domain == new_domain
assert site.domain_changed_from == original_domain
end
+
+ test "change_domain redirects to v2 when scriptv2 flag is enabled", %{conn: conn, site: site} do
+ FunWithFlags.enable(:scriptv2, for_actor: site)
+
+ conn = get(conn, Routes.site_path(conn, :change_domain, site.domain))
+
+ assert redirected_to(conn) ==
+ Routes.site_path(conn, :change_domain_v2, site.domain)
+ end
end
describe "reset stats" do
diff --git a/test/plausible_web/live/change_domain_v2_test.exs b/test/plausible_web/live/change_domain_v2_test.exs
new file mode 100644
index 0000000000..a320bc0dbe
--- /dev/null
+++ b/test/plausible_web/live/change_domain_v2_test.exs
@@ -0,0 +1,108 @@
+defmodule PlausibleWeb.Live.ChangeDomainV2Test do
+ use PlausibleWeb.ConnCase, async: true
+ import Phoenix.LiveViewTest
+
+ alias Plausible.Repo
+
+ describe "ChangeDomainV2 LiveView" do
+ setup [:create_user, :log_in, :create_site]
+
+ test "mounts and renders form", %{conn: conn, site: site} do
+ {:ok, _lv, html} = live(conn, "/#{site.domain}/change-domain-v2")
+
+ assert html =~ "Change your website domain"
+ end
+
+ test "form submission when no change is made", %{conn: conn, site: site} do
+ {:ok, lv, _html} = live(conn, "/#{site.domain}/change-domain-v2")
+
+ html =
+ lv
+ |> element("form")
+ |> render_submit(%{site: %{domain: site.domain}})
+
+ assert html =~ "New domain must be different than the current one"
+ end
+
+ test "form submission to an existing domain", %{conn: conn, site: site} do
+ another_site = insert(:site)
+ {:ok, lv, _html} = live(conn, "/#{site.domain}/change-domain-v2")
+
+ html =
+ lv
+ |> element("form")
+ |> render_submit(%{site: %{domain: another_site.domain}})
+
+ assert html =~ "This domain cannot be registered"
+
+ site = Repo.reload!(site)
+ assert site.domain != another_site.domain
+ assert is_nil(site.domain_changed_from)
+ end
+
+ test "form submission to a domain in transition period", %{conn: conn, site: site} do
+ _another_site = insert(:site, domain_changed_from: "foo.example.com")
+ {:ok, lv, _html} = live(conn, "/#{site.domain}/change-domain-v2")
+
+ html =
+ lv
+ |> element("form")
+ |> render_submit(%{site: %{domain: "foo.example.com"}})
+
+ assert html =~ "This domain cannot be registered"
+
+ site = Repo.reload!(site)
+ assert site.domain != "foo.example.com"
+ assert is_nil(site.domain_changed_from)
+ end
+
+ test "successful form submission navigates to installation", %{conn: conn, site: site} do
+ original_domain = site.domain
+ new_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)
+
+ site = Repo.reload!(site)
+ assert site.domain == new_domain
+ assert site.domain_changed_from == original_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")
+
+ html =
+ lv
+ |> element("form")
+ |> render_submit(%{site: %{domain: ""}})
+
+ assert html =~ "can&#39;t be blank"
+ end
+
+ test "form validation shows error for invalid domain format", %{conn: conn, site: site} do
+ {:ok, lv, _html} = live(conn, "/#{site.domain}/change-domain-v2")
+
+ html =
+ lv
+ |> element("form")
+ |> render_submit(%{site: %{domain: "invalid domain with spaces"}})
+
+ assert html =~ "only letters, numbers, slashes and period allowed"
+ end
+
+ test "renders back to settings link with correct path", %{conn: conn, site: site} do
+ {:ok, _lv, html} = live(conn, "/#{site.domain}/change-domain-v2")
+
+ expected_link = Routes.site_path(conn, :settings_general, site.domain)
+ assert html =~ expected_link
+ end
+ end
+end