Skip to content

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

Add API to retrieve site by domain (#1942)

Kurt McAlpineb92889aaf0e8
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 500b4e459f..5d3e0e8029 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -25,6 +25,7 @@ All notable changes to this project will be documented in this file.
- New filter type: `contains`, available for `page`, `entry_page`, `exit_page`
- Add filter for custom property
- Add ability to import historical data from GA: plausible/analytics#1753
+- API route `GET /api/v1/sites/:site_id`
### Fixed
- UI fix where multi-line text in pills would not be underlined properly on small screens.
diff --git a/lib/plausible_web/controllers/api/external_sites_controller.ex b/lib/plausible_web/controllers/api/external_sites_controller.ex
index 299195ca86..37ade7c60d 100644
--- a/lib/plausible_web/controllers/api/external_sites_controller.ex
+++ b/lib/plausible_web/controllers/api/external_sites_controller.ex
@@ -28,6 +28,16 @@ defmodule PlausibleWeb.Api.ExternalSitesController do
end
end
+ def get_site(conn, %{"site_id" => site_id}) do
+ site = Sites.get_for_user(conn.assigns[:current_user].id, site_id, [:owner, :admin])
+
+ if site do
+ json(conn, site)
+ else
+ H.not_found(conn, "Site could not be found")
+ end
+ end
+
def delete_site(conn, %{"site_id" => site_id}) do
site = Sites.get_for_user(conn.assigns[:current_user].id, site_id, [:owner])
diff --git a/lib/plausible_web/router.ex b/lib/plausible_web/router.ex
index 6482de7f92..8328d16f0c 100644
--- a/lib/plausible_web/router.ex
+++ b/lib/plausible_web/router.ex
@@ -101,6 +101,7 @@ defmodule PlausibleWeb.Router do
pipe_through [:public_api, PlausibleWeb.AuthorizeSitesApiPlug]
post "/", ExternalSitesController, :create_site
+ get "/:site_id", ExternalSitesController, :get_site
delete "/:site_id", ExternalSitesController, :delete_site
put "/shared-links", ExternalSitesController, :find_or_create_shared_link
put "/goals", ExternalSitesController, :find_or_create_goal
diff --git a/test/plausible_web/controllers/api/external_sites_controller_test.exs b/test/plausible_web/controllers/api/external_sites_controller_test.exs
index 0ae9d36d10..a73cbacec6 100644
--- a/test/plausible_web/controllers/api/external_sites_controller_test.exs
+++ b/test/plausible_web/controllers/api/external_sites_controller_test.exs
@@ -384,4 +384,20 @@ defmodule PlausibleWeb.Api.ExternalSitesControllerTest do
}
end
end
+
+ describe "GET /api/v1/sites/:site_id" do
+ setup :create_new_site
+
+ test "get a site by it's domain", %{conn: conn, site: site} do
+ conn = get(conn, "/api/v1/sites/" <> site.domain)
+
+ assert json_response(conn, 200) == %{"domain" => site.domain, "timezone" => site.timezone}
+ end
+
+ test "is 404 when site cannot be found", %{conn: conn} do
+ conn = get(conn, "/api/v1/sites/foobar.baz")
+
+ assert json_response(conn, 404) == %{"error" => "Site could not be found"}
+ end
+ end
end