Skip to content

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

Fix default tracking toggles failing to switch off when their goal was deleted (#6805)

Marko Saric9bc90b0063c8
diff --git a/lib/plausible/goals/goals.ex b/lib/plausible/goals/goals.ex
index 192e6a5fc6..84597feb92 100644
--- a/lib/plausible/goals/goals.ex
+++ b/lib/plausible/goals/goals.ex
@@ -348,12 +348,19 @@ defmodule Plausible.Goals do
end
defp delete_by_event_name(site, event_name) do
- delete_matching(
+ query =
from(g in Goal,
where: g.site_id == ^site.id,
where: g.event_name == ^event_name
)
- )
+
+ # The goal may already be gone, e.g. deleted by hand in the goal settings.
+ # `delete_matching/1` would then fail inside its own transaction, and when
+ # called from within another transaction (as when a tracker script setting
+ # is switched off) that failure rolls back the caller's transaction too.
+ if Repo.exists?(query) do
+ delete_matching(query)
+ end
:ok
end
diff --git a/test/plausible_web/tracker_test.exs b/test/plausible_web/tracker_test.exs
index 0a870cca68..7a04f4309c 100644
--- a/test/plausible_web/tracker_test.exs
+++ b/test/plausible_web/tracker_test.exs
@@ -77,6 +77,27 @@ defmodule PlausibleWeb.TrackerTest do
assert tracker_script_configuration.outbound_links == true
end
+ test "can switch off a default tracking option whose goal was already deleted" do
+ site = new_site()
+ tracker_script_configuration = create_config(site)
+
+ assert tracker_script_configuration.form_submissions
+ goal = Repo.get_by!(Plausible.Goal, site_id: site.id, event_name: "Form: Submission")
+ Repo.delete!(goal)
+
+ Tracker.update_script_configuration!(
+ site,
+ %{form_submissions: false, outbound_links: true},
+ :installation
+ )
+
+ tracker_script_configuration = Repo.reload!(tracker_script_configuration)
+
+ refute tracker_script_configuration.form_submissions
+ assert tracker_script_configuration.outbound_links
+ assert Repo.get_by(Plausible.Goal, site_id: site.id, event_name: "Outbound Link: Click")
+ end
+
on_ee do
test "CDN purge is scheduled when config is updated" do
site = new_site()