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
Add API to retrieve site by domain (#1942)
Kurt McAlpineb92889aaf0e8
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 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.exindex 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 doendend+ 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}) dosite = 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.exindex 6482de7f92..8328d16f0c 100644--- a/lib/plausible_web/router.ex+++ b/lib/plausible_web/router.ex@@ -101,6 +101,7 @@ defmodule PlausibleWeb.Router dopipe_through [:public_api, PlausibleWeb.AuthorizeSitesApiPlug]post "/", ExternalSitesController, :create_site+ get "/:site_id", ExternalSitesController, :get_sitedelete "/:site_id", ExternalSitesController, :delete_siteput "/shared-links", ExternalSitesController, :find_or_create_shared_linkput "/goals", ExternalSitesController, :find_or_create_goaldiff --git a/test/plausible_web/controllers/api/external_sites_controller_test.exs b/test/plausible_web/controllers/api/external_sites_controller_test.exsindex 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}endend++ 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+ endend