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
Reduce funnels on special goals delete (#6607)
Adam66d2daad6b6d
diff --git a/lib/plausible/goals/goals.ex b/lib/plausible/goals/goals.exindex e79e278927..337e2918bf 100644--- a/lib/plausible/goals/goals.ex+++ b/lib/plausible/goals/goals.ex@@ -228,13 +228,16 @@ defmodule Plausible.Goals doenddef delete(id, site_id) do- goal_query =+ delete_matching(from(g in Goal,where: g.id == ^id,where: g.site_id == ^site_id)+ )+ end- goal_query = on_ee(do: preload(goal_query, funnels: :steps), else: goal_query)+ defp delete_matching(base_query) do+ goal_query = on_ee(do: preload(base_query, funnels: :steps), else: base_query)result =Multi.new()@@ -252,7 +255,7 @@ defmodule Plausible.Goals do_, %{goal: %{funnels: funnels}} ->funnels_to_wipe =funnels- |> Enum.filter(&(Enum.count(&1.steps) == Funnel.Const.min_steps()))+ |> Enum.filter(&(Enum.count(&1.steps) <= Funnel.Const.min_steps()))|> Enum.map(& &1.id){:ok, funnels_to_wipe}@@ -270,14 +273,7 @@ defmodule Plausible.Goals do))end)- |> Multi.delete_all(- :delete_goals,- fn _ ->- from g in Goal,- where: g.id == ^id,- where: g.site_id == ^site_id- end- )+ |> Multi.delete_all(:delete_goals, fn _ -> base_query end)|> Repo.transaction()case result do@@ -323,45 +319,32 @@ defmodule Plausible.Goals do@spec delete_outbound_links(Plausible.Site.t()) :: :okdef delete_outbound_links(%Plausible.Site{} = site) do- q =- from g in Goal,- where: g.site_id == ^site.id,- where: g.event_name == "Outbound Link: Click"-- Repo.delete_all(q)- :ok+ delete_by_event_name(site, "Outbound Link: Click")end@spec delete_file_downloads(Plausible.Site.t()) :: :okdef delete_file_downloads(%Plausible.Site{} = site) do- q =- from g in Goal,- where: g.site_id == ^site.id,- where: g.event_name == "File Download"-- Repo.delete_all(q)- :ok+ delete_by_event_name(site, "File Download")end@spec delete_404(Plausible.Site.t()) :: :okdef delete_404(%Plausible.Site{} = site) do- q =- from g in Goal,- where: g.site_id == ^site.id,- where: g.event_name == "404"-- Repo.delete_all(q)- :ok+ delete_by_event_name(site, "404")end@spec delete_form_submissions(Plausible.Site.t()) :: :okdef delete_form_submissions(%Plausible.Site{} = site) do- q =- from g in Goal,+ delete_by_event_name(site, "Form: Submission")+ end++ defp delete_by_event_name(site, event_name) do+ delete_matching(+ from(g in Goal,where: g.site_id == ^site.id,- where: g.event_name == "Form: Submission"+ where: g.event_name == ^event_name+ )+ )- Repo.delete_all(q):okenddiff --git a/test/plausible/goals_test.exs b/test/plausible/goals_test.exsindex c5217e6e46..b8011e76dc 100644--- a/test/plausible/goals_test.exs+++ b/test/plausible/goals_test.exs@@ -748,6 +748,29 @@ defmodule Plausible.GoalsTest doassert_matches [%{id: ^g3.id}, %{id: ^g2.id}] = Goals.for_site(site)end++ test "deleting special goal (e.g. via tracker script sync) also triggers funnel reduction" do+ site = new_site()+ {:ok, g1} = Goals.create(site, %{"page_path" => "/1"})+ :ok = Goals.create_404(site)+ [g404] = Repo.all(from g in Plausible.Goal, where: g.event_name == "404")++ {:ok, f1} =+ Plausible.Funnels.create(+ site,+ "Funnel with special goal",+ [+ %{"goal_id" => g1.id},+ %{"goal_id" => g404.id}+ ]+ )++ assert Plausible.Funnels.get(site.id, f1.id)++ :ok = Goals.delete_404(site)++ refute Plausible.Funnels.get(site.id, f1.id)+ endendtest "must be either page_path or event_name" do