Optionally preload funnels for goals and stub funnel-goal deletion

pull/2859/head
Adam Rutkowski 4 years ago
parent f5b577574e
commit 325b3d197f

@ -21,12 +21,13 @@ defmodule Plausible.Goal do
import Ecto.Changeset
schema "goals" do
field :event_name, :string
field :page_path, :string
field(:event_name, :string)
field(:page_path, :string)
field(:currency, Ecto.Enum, values: Money.Currency.known_current_currencies())
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
belongs_to(:site, Plausible.Site)
timestamps()
end

@ -49,7 +49,7 @@ 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),
@ -58,6 +58,17 @@ defmodule Plausible.Goals do
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)
@ -77,18 +88,22 @@ defmodule Plausible.Goals do
end
def belongs_to_funnels?(goal_id) do
Repo.exists?(from fs in Plausible.Funnel.Step, where: fs.goal_id == ^goal_id)
Repo.exists?(from(fs in Plausible.Funnel.Step, where: fs.goal_id == ^goal_id))
end
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}
if not belongs_to_funnels?(id) 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}
end
else
raise "Implement me"
end
end

@ -3,18 +3,21 @@ defmodule PlausibleWeb.SiteController do
use Plausible.Repo
alias Plausible.{Sites, Goals}
plug PlausibleWeb.RequireAccountPlug
plug(PlausibleWeb.RequireAccountPlug)
plug PlausibleWeb.AuthorizeSiteAccess,
[:owner, :admin, :super_admin] when action not in [:index, :new, :create_site]
plug(
PlausibleWeb.AuthorizeSiteAccess,
[:owner, :admin, :super_admin] when action not in [:index, :new, :create_site]
)
def index(conn, params) do
user = conn.assigns[:current_user]
invitations =
Repo.all(
from i in Plausible.Auth.Invitation,
from(i in Plausible.Auth.Invitation,
where: i.email == ^user.email
)
)
|> Repo.preload(:site)
@ -109,10 +112,11 @@ defmodule PlausibleWeb.SiteController do
is_first_site =
!Repo.exists?(
from sm in Plausible.Site.Membership,
from(sm in Plausible.Site.Membership,
where:
sm.user_id == ^user.id and
sm.site_id != ^site.id
)
)
conn
@ -212,7 +216,7 @@ defmodule PlausibleWeb.SiteController do
def settings_visibility(conn, _params) do
site = conn.assigns[:site] |> Repo.preload(:custom_domain)
shared_links = Repo.all(from l in Plausible.Site.SharedLink, where: l.site_id == ^site.id)
shared_links = Repo.all(from(l in Plausible.Site.SharedLink, where: l.site_id == ^site.id))
conn
|> assign(:skip_plausible_tracking, true)
@ -225,7 +229,7 @@ defmodule PlausibleWeb.SiteController do
def settings_goals(conn, _params) do
site = conn.assigns[:site] |> Repo.preload(:custom_domain)
goals = Goals.for_site(site)
goals = Goals.for_site(site, preload_funnels?: true)
conn
|> assign(:skip_plausible_tracking, true)
@ -420,7 +424,7 @@ defmodule PlausibleWeb.SiteController do
def disable_weekly_report(conn, _params) do
site = conn.assigns[:site]
Repo.delete_all(from wr in Plausible.Site.WeeklyReport, where: wr.site_id == ^site.id)
Repo.delete_all(from(wr in Plausible.Site.WeeklyReport, where: wr.site_id == ^site.id))
conn
|> put_flash(:success, "You will not receive weekly email reports going forward")
@ -470,7 +474,7 @@ defmodule PlausibleWeb.SiteController do
def disable_monthly_report(conn, _params) do
site = conn.assigns[:site]
Repo.delete_all(from mr in Plausible.Site.MonthlyReport, where: mr.site_id == ^site.id)
Repo.delete_all(from(mr in Plausible.Site.MonthlyReport, where: mr.site_id == ^site.id))
conn
|> put_flash(:success, "You will not receive monthly email reports going forward")
@ -530,7 +534,7 @@ defmodule PlausibleWeb.SiteController do
def disable_spike_notification(conn, _params) do
site = conn.assigns[:site]
Repo.delete_all(from mr in Plausible.Site.SpikeNotification, where: mr.site_id == ^site.id)
Repo.delete_all(from(mr in Plausible.Site.SpikeNotification, where: mr.site_id == ^site.id))
conn
|> put_flash(:success, "Spike notification disabled")
@ -646,9 +650,10 @@ defmodule PlausibleWeb.SiteController do
site_id = site.id
case Repo.delete_all(
from l in Plausible.Site.SharedLink,
from(l in Plausible.Site.SharedLink,
where: l.slug == ^slug,
where: l.site_id == ^site_id
)
) do
{1, _} ->
conn
@ -667,9 +672,10 @@ defmodule PlausibleWeb.SiteController do
site_id = site.id
case Repo.delete_all(
from d in Plausible.Site.CustomDomain,
from(d in Plausible.Site.CustomDomain,
where: d.site_id == ^site_id,
where: d.id == ^domain_id
)
) do
{1, _} ->
conn

@ -13,24 +13,23 @@
<div class="border-b border-gray-300 dark:border-gray-500 py-3 flex justify-between">
<span class="text-sm font-medium text-gray-900 dark:text-gray-100"><%= goal_name(goal) %></span>
<% prompt = if Plausible.Goals.belongs_to_funnels?(goal) do %>
The goal <%= goal_name(goal) %> is part of some funnel(s).
If you are going to delete it, the associated funnels will be either reduced or deleted completely.
Are you sure you want to remove goal <%= goal_name(goal) %>?
<%= if not Enum.empty?(goal.funnels) do %>
<%= button(to: "/#{URI.encode_www_form(@site.domain)}/goals/#{goal.id}", method: :delete, class: "text-sm text-red-600",
data: [confirm: "The goal #{goal_name(goal)} is part of some funnel(s). If you are going to delete it, the associated funnels will be either reduced or deleted completely. Are you sure you want to remove goal #{goal_name(goal)}?"]) do %>
<svg class="feather feather-sm" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><polyline points="3 6 5 6 21 6"></polyline><path d="M19 6v14a2 2 0 0 1-2 2H7a2 2 0 0 1-2-2V6m3 0V4a2 2 0 0 1 2-2h4a2 2 0 0 1 2 2v2"></path><line x1="10" y1="11" x2="10" y2="17"></line><line x1="14" y1="11" x2="14" y2="17"></line></svg>
<% end %>
<% else %>
Are you sure you want to remove goal <%= goal_name(goal) %>? This will just affect the UI, all of your analytics data will stay intact.
<% end %>
<%= button(to: "/#{URI.encode_www_form(@site.domain)}/goals/#{goal.id}", method: :delete, class: "text-sm text-red-600", data: [confirm: prompt]) do %>
<svg class="feather feather-sm" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><polyline points="3 6 5 6 21 6"></polyline><path d="M19 6v14a2 2 0 0 1-2 2H7a2 2 0 0 1-2-2V6m3 0V4a2 2 0 0 1 2-2h4a2 2 0 0 1 2 2v2"></path><line x1="10" y1="11" x2="10" y2="17"></line><line x1="14" y1="11" x2="14" y2="17"></line></svg>
<% end %>
<%= button(to: "/#{URI.encode_www_form(@site.domain)}/goals/#{goal.id}", method: :delete, class: "text-sm text-red-600",
data: [confirm: "Are you sure you want to remove goal #{goal_name(goal)}? This will just affect the UI, all of your analytics data will stay intact."]) do %>
<svg class="feather feather-sm" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><polyline points="3 6 5 6 21 6"></polyline><path d="M19 6v14a2 2 0 0 1-2 2H7a2 2 0 0 1-2-2V6m3 0V4a2 2 0 0 1 2-2h4a2 2 0 0 1 2 2v2"></path><line x1="10" y1="11" x2="10" y2="17"></line><line x1="14" y1="11" x2="14" y2="17"></line></svg>
<% end %>
</div>
<% end %>
<% end %>
</div>
<% end %>
<% else %>
<div class="mt-4 dark:text-gray-100">No goals configured for this site yet</div>
<% end %>
<% end %>
<%= link("+ Add goal", to: "/#{URI.encode_www_form(@site.domain)}/goals/new", class: "button mt-6") %>
<%= if Enum.count(@goals) >= 2 do %>

@ -47,4 +47,68 @@ 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, _} =
Plausible.Funnels.create(
site,
"Funnel",
[
%{"goal_id" => g1.id},
%{"goal_id" => g2.id},
%{"goal_id" => g3.id}
]
)
:ok = Goals.delete(g1.id, site)
end
end

Loading…
Cancel
Save