Skip to content

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

Plugins API: handle Revenue Goals creation wrt Business Tier (#3440)

hq1957138a8ec81
diff --git a/lib/plausible/plugins/api/goals.ex b/lib/plausible/plugins/api/goals.ex
index 93372c2c37..3a6b128f06 100644
--- a/lib/plausible/plugins/api/goals.ex
+++ b/lib/plausible/plugins/api/goals.ex
@@ -18,7 +18,10 @@ defmodule Plausible.Plugins.API.Goals do
@spec create(
Plausible.Site.t(),
create_request() | list(create_request())
- ) :: {:ok, list(Plausible.Goal.t())} | {:error, Ecto.Changeset.t()}
+ ) ::
+ {:ok, list(Plausible.Goal.t())}
+ | {:error, Ecto.Changeset.t()}
+ | {:error, :upgrade_required}
def create(site, goal_or_goals) do
Repo.transaction(fn -> find_or_create(site, goal_or_goals) end)
end
diff --git a/lib/plausible_web/plugins/api/controllers/goals.ex b/lib/plausible_web/plugins/api/controllers/goals.ex
index 7e9b2738c2..598b63b3b4 100644
--- a/lib/plausible_web/plugins/api/controllers/goals.ex
+++ b/lib/plausible_web/plugins/api/controllers/goals.ex
@@ -36,6 +36,7 @@ defmodule PlausibleWeb.Plugins.API.Controllers.Goals do
oneOf: [Schemas.Goal.ListResponse, Schemas.Goal]
}},
unauthorized: {"Unauthorized", "application/json", Schemas.Unauthorized},
+ payment_required: {"Payment required", "application/json", Schemas.PaymentRequired},
unprocessable_entity:
{"Unprocessable entity", "application/json", Schemas.UnprocessableEntity}
}
@@ -56,6 +57,9 @@ defmodule PlausibleWeb.Plugins.API.Controllers.Goals do
|> put_resp_header("location", goals_url(base_uri(), :get, goal.id))
|> render("goal.json", goal: goal, authorized_site: site)
+ {:error, :upgrade_required} ->
+ payment_required(conn)
+
{:error, changeset} ->
Errors.error(conn, 422, changeset)
end
@@ -77,6 +81,9 @@ defmodule PlausibleWeb.Plugins.API.Controllers.Goals do
|> put_status(:created)
|> render("index.json", goals: goals, authorized_site: site)
+ {:error, :upgrade_required} ->
+ payment_required(conn)
+
{:error, changeset} ->
Errors.error(conn, 422, changeset)
end
@@ -157,4 +164,12 @@ defmodule PlausibleWeb.Plugins.API.Controllers.Goals do
send_resp(conn, :no_content, "")
end
end
+
+ defp payment_required(conn) do
+ Errors.error(
+ conn,
+ 402,
+ "#{Plausible.Billing.Feature.RevenueGoals.display_name()} is part of the Plausible Business plan. To get access to this feature, please upgrade your account."
+ )
+ end
end
diff --git a/lib/plausible_web/plugins/api/schemas/payment_required.ex b/lib/plausible_web/plugins/api/schemas/payment_required.ex
new file mode 100644
index 0000000000..ab22e69527
--- /dev/null
+++ b/lib/plausible_web/plugins/api/schemas/payment_required.ex
@@ -0,0 +1,23 @@
+defmodule PlausibleWeb.Plugins.API.Schemas.PaymentRequired do
+ @moduledoc """
+ OpenAPI schema for a generic 402 response
+ """
+
+ use PlausibleWeb, :open_api_schema
+
+ OpenApiSpex.schema(%{
+ description: """
+ The response that is returned when the user makes a request that cannot be
+ processed due to their subscription limitations.
+ """,
+ type: :object,
+ title: "PaymentRequiredError",
+ required: [:errors],
+ properties: %{
+ errors: %OpenApiSpex.Schema{
+ items: Schemas.Error,
+ type: :array
+ }
+ }
+ })
+end
diff --git a/test/plausible_web/plugins/api/controllers/goals_test.exs b/test/plausible_web/plugins/api/controllers/goals_test.exs
index a63edd8f3c..fa1a3b6611 100644
--- a/test/plausible_web/plugins/api/controllers/goals_test.exs
+++ b/test/plausible_web/plugins/api/controllers/goals_test.exs
@@ -44,6 +44,70 @@ defmodule PlausibleWeb.Plugins.API.Controllers.GoalsTest do
end
end
+ describe "business tier" do
+ test "fails on revenue goal creation attempt with insufficient plan", %{
+ site: site,
+ token: token,
+ conn: conn
+ } do
+ site = Plausible.Repo.preload(site, :owner)
+ FunWithFlags.enable(:business_tier, for_actor: site.owner)
+ insert(:growth_subscription, user: site.owner)
+
+ url = Routes.goals_url(base_uri(), :create)
+
+ payload = %{
+ goal_type: "Goal.Revenue",
+ goal: %{event_name: "Purchase", currency: "EUR"}
+ }
+
+ assert_request_schema(payload, "Goal.CreateRequest.Revenue", spec())
+
+ conn
+ |> authenticate(site.domain, token)
+ |> put_req_header("content-type", "application/json")
+ |> put(url, payload)
+ |> json_response(402)
+ |> assert_schema("PaymentRequiredError", spec())
+ end
+
+ test "fails on bulk revenue goal creation attempt with insufficient plan", %{
+ site: site,
+ token: token,
+ conn: conn
+ } do
+ site = Plausible.Repo.preload(site, :owner)
+ FunWithFlags.enable(:business_tier, for_actor: site.owner)
+ insert(:growth_subscription, user: site.owner)
+
+ url = Routes.goals_url(base_uri(), :create)
+
+ payload = %{
+ goals: [
+ %{
+ goal_type: "Goal.CustomEvent",
+ goal: %{event_name: "Signup"}
+ },
+ %{
+ goal_type: "Goal.Revenue",
+ goal: %{event_name: "Purchase", currency: "EUR"}
+ },
+ %{
+ goal_type: "Goal.Pageview",
+ goal: %{path: "/checkout"}
+ }
+ ]
+ }
+
+ conn
+ |> authenticate(site.domain, token)
+ |> put_req_header("content-type", "application/json")
+ |> put(url, payload)
+ |> json_response(402)
+ |> assert_schema("PaymentRequiredError", spec())
+ end
+ end
+
describe "put /goals - create a single goal" do
test "validates input according to the schema", %{conn: conn, token: token, site: site} do
url = Routes.goals_url(base_uri(), :create)