diff --git a/lib/plausible/goal/schema.ex b/lib/plausible/goal/schema.ex index 02a58ad1d..8f7c41ca0 100644 --- a/lib/plausible/goal/schema.ex +++ b/lib/plausible/goal/schema.ex @@ -1,21 +1,3 @@ -defimpl Jason.Encoder, for: Plausible.Goal do - def encode(value, opts) do - goal_type = - cond do - value.event_name -> :event - value.page_path -> :page - end - - domain = value.site.domain - - value - |> Map.put(:goal_type, goal_type) - |> Map.take([:id, :goal_type, :event_name, :page_path]) - |> Map.put(:domain, domain) - |> Jason.Encode.map(opts) - end -end - defmodule Plausible.Goal do use Ecto.Schema import Ecto.Changeset @@ -23,9 +5,10 @@ defmodule Plausible.Goal do schema "goals" do field :event_name, :string field :page_path, :string - field :currency, Ecto.Enum, values: Money.Currency.known_current_currencies() + many_to_many :funnels, Plausible.Funnel, join_through: Plausible.Funnel.Step + belongs_to :site, Plausible.Site timestamps() @@ -50,7 +33,7 @@ defmodule Plausible.Goal do def changeset(goal, attrs \\ %{}) do goal - |> cast(attrs, [:site_id, :event_name, :page_path, :currency]) + |> cast(attrs, [:id, :site_id, :event_name, :page_path, :currency]) |> validate_required([:site_id]) |> cast_assoc(:site) |> validate_event_name_and_page_path() @@ -88,3 +71,32 @@ defmodule Plausible.Goal do end end end + +defimpl Jason.Encoder, for: Plausible.Goal do + def encode(value, opts) do + goal_type = + cond do + value.event_name -> :event + value.page_path -> :page + end + + domain = value.site.domain + + value + |> Map.put(:goal_type, goal_type) + |> Map.take([:id, :goal_type, :event_name, :page_path]) + |> Map.put(:domain, domain) + |> Jason.Encode.map(opts) + end +end + +# TODO: consider Phoenix.HTML.Safe +defimpl String.Chars, for: Plausible.Goal do + def to_string(%{page_path: page_path}) when is_binary(page_path) do + "Visit " <> page_path + end + + def to_string(%{event_name: name}) when is_binary(name) do + name + end +end diff --git a/lib/plausible/goals.ex b/lib/plausible/goals.ex index 44a43e21e..bf6376237 100644 --- a/lib/plausible/goals.ex +++ b/lib/plausible/goals.ex @@ -1,6 +1,9 @@ defmodule Plausible.Goals do use Plausible.Repo alias Plausible.Goal + alias Ecto.Multi + + use Plausible.Funnel def create(site, params) do params = Map.merge(params, %{"site_id" => site.id}) @@ -47,26 +50,101 @@ defmodule Plausible.Goals do def find_or_create(_, %{"goal_type" => "page"}), do: {:missing, "page_path"} - def for_site(site) do + def for_site(site, opts \\ []) do query = from g in Goal, inner_join: assoc(g, :site), where: g.site_id == ^site.id, + order_by: [desc: g.id], preload: [:site] + query = + if opts[:preload_funnels?] do + from g in query, + left_join: assoc(g, :funnels), + group_by: g.id, + preload: [:funnels] + else + query + end + query |> Repo.all() |> Enum.map(&maybe_trim/1) end + def by_id!(site, id) do + Repo.one( + from g in Goal, + where: g.id == ^id, + where: g.site_id == ^site.id + ) + end + + @doc """ + If a goal belongs to funnel(s), we need to inspect their number of steps. + + If it exceeds the minimum allowed (defined via `Plausible.Funnel.min_steps/0`), + the funnel will be reduced (i.e. a step associated with the goal to be deleted + is removed), so that the minimum number of steps is preserved. This is done + implicitly, by postgres, as per on_delete: :delete_all. + + Otherwise, for associated funnel(s) consisting of minimum number steps only, + funnel record(s) are removed completely along with the targeted goal. + """ def delete(id, site) do - case Repo.delete_all( - from g in Goal, - where: g.id == ^id, - where: g.site_id == ^site.id - ) do - {1, _} -> :ok - {0, _} -> {:error, :not_found} + result = + Multi.new() + |> Multi.one( + :goal, + from(g in Goal, + where: g.id == ^id, + where: g.site_id == ^site.id, + preload: [funnels: :steps] + ) + ) + |> Multi.run(:funnel_ids_to_wipe, fn + _, %{goal: nil} -> + {:error, :not_found} + + _, %{goal: %{funnels: []}} -> + {:ok, []} + + _, %{goal: %{funnels: funnels}} -> + funnels_to_wipe = + funnels + |> Enum.filter(&(Enum.count(&1.steps) == Funnel.min_steps())) + |> Enum.map(& &1.id) + + {:ok, funnels_to_wipe} + end) + |> Multi.merge(fn + %{funnel_ids_to_wipe: []} -> + Ecto.Multi.new() + + %{funnel_ids_to_wipe: [_ | _] = funnel_ids} -> + Ecto.Multi.new() + |> Multi.delete_all( + :delete_funnels, + from(f in Funnel, + where: f.id in ^funnel_ids + ) + ) + end) + |> Multi.delete_all( + :delete_goals, + fn _ -> + from(g in Goal, + where: g.id == ^id, + where: g.site_id == ^site.id + ) + end + ) + |> Repo.transaction() + + case result do + {:ok, _} -> :ok + {:error, _step, reason, _context} -> {:error, reason} end end diff --git a/test/plausible/goals_test.exs b/test/plausible/goals_test.exs index 4e3f20dcd..d16992cd3 100644 --- a/test/plausible/goals_test.exs +++ b/test/plausible/goals_test.exs @@ -25,7 +25,7 @@ defmodule Plausible.GoalsTest do goals = Goals.for_site(site) - assert [%{event_name: "Signup"}, %{page_path: "/Signup"}] = goals + assert [%{page_path: "/Signup"}, %{event_name: "Signup"}] = goals end test "goals are present after domain change" do @@ -47,4 +47,86 @@ defmodule Plausible.GoalsTest do assert [] = Goals.for_site(site) end + + test "goals can be deleted" do + site = insert(:site) + goal = insert(:goal, %{site: site, event_name: " Signup "}) + :ok = Goals.delete(goal.id, site) + assert [] = Goals.for_site(site) + end + + test "goals can be fetched with funnel count preloaded" do + site = insert(:site) + + goals = + Enum.map(1..4, fn i -> + {:ok, g} = Goals.create(site, %{"page_path" => "/#{i}"}) + g + end) + + {:ok, %{id: funnel_id1}} = + Plausible.Funnels.create( + site, + "Funnel1", + [ + %{"goal_id" => Enum.at(goals, 1).id}, + %{"goal_id" => Enum.at(goals, 2).id}, + %{"goal_id" => Enum.at(goals, 3).id} + ] + ) + + {:ok, %{id: funnel_id2}} = + Plausible.Funnels.create( + site, + "Funnel2", + [ + %{"goal_id" => Enum.at(goals, 1).id}, + %{"goal_id" => Enum.at(goals, 3).id} + ] + ) + + assert [goal, _, _, _] = Goals.for_site(site, preload_funnels?: false) + assert %Ecto.Association.NotLoaded{} = goal.funnels + + assert [goal, _, _, _] = Goals.for_site(site, preload_funnels?: true) + assert [%{id: ^funnel_id1}, %{id: ^funnel_id2}] = goal.funnels + end + + test "deleting goals with funnels triggers funnel reduction" do + site = insert(:site) + {:ok, g1} = Goals.create(site, %{"page_path" => "/1"}) + {:ok, g2} = Goals.create(site, %{"page_path" => "/2"}) + {:ok, g3} = Goals.create(site, %{"page_path" => "/3"}) + + {:ok, f1} = + Plausible.Funnels.create( + site, + "Funnel 3 steps", + [ + %{"goal_id" => g1.id}, + %{"goal_id" => g2.id}, + %{"goal_id" => g3.id} + ] + ) + + {:ok, f2} = + Plausible.Funnels.create( + site, + "Funnel 2 steps", + [ + %{"goal_id" => g1.id}, + %{"goal_id" => g2.id} + ] + ) + + :ok = Goals.delete(g1.id, site) + + assert {:ok, f1} = Plausible.Funnels.get(site.id, f1.id) + assert Enum.count(f1.steps) == 2 + + assert {:error, "Funnel not found"} = Plausible.Funnels.get(site.id, f2.id) + assert Repo.all(from fs in Plausible.Funnel.Step, where: fs.funnel_id == ^f2.id) == [] + + assert [^g3, ^g2] = Goals.for_site(site) + end end