Skip to content

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

Page Scroll Goals: migration step 2 (#5034)

RobertJoonas2579bbea7b16
diff --git a/lib/plausible/goal/schema.ex b/lib/plausible/goal/schema.ex
index 8f2e29febe..84d76cb991 100644
--- a/lib/plausible/goal/schema.ex
+++ b/lib/plausible/goal/schema.ex
@@ -8,6 +8,7 @@ defmodule Plausible.Goal do
schema "goals" do
field :event_name, :string
field :page_path, :string
+ field :scroll_threshold, :integer, default: -1
field :display_name, :string
on_ee do
@@ -23,7 +24,7 @@ defmodule Plausible.Goal do
timestamps()
end
- @fields [:id, :site_id, :event_name, :page_path, :display_name] ++
+ @fields [:id, :site_id, :event_name, :page_path, :scroll_threshold, :display_name] ++
on_ee(do: [:currency], else: [])
@max_event_name_length 120
@@ -40,6 +41,9 @@ defmodule Plausible.Goal do
|> maybe_put_display_name()
|> unique_constraint(:event_name, name: :goals_event_name_unique)
|> unique_constraint(:page_path, name: :goals_page_path_unique)
+ |> unique_constraint([:page_path, :scroll_threshold],
+ name: :goals_page_path_and_scroll_threshold_unique
+ )
|> unique_constraint(:display_name, name: :goals_site_id_display_name_index)
|> validate_length(:event_name, max: @max_event_name_length)
|> check_constraint(:event_name,
diff --git a/test/plausible/goals_test.exs b/test/plausible/goals_test.exs
index ede9287ba3..d57cedfcea 100644
--- a/test/plausible/goals_test.exs
+++ b/test/plausible/goals_test.exs
@@ -34,8 +34,11 @@ defmodule Plausible.GoalsTest do
test "create/2 fails to create the same pageview goal twice" do
site = new_site()
- {:ok, _} = Goals.create(site, %{"page_path" => "foo bar"})
- assert {:error, changeset} = Goals.create(site, %{"page_path" => "foo bar"})
+ {:ok, _} = Goals.create(site, %{"page_path" => "foo bar", "display_name" => "one"})
+
+ assert {:error, changeset} =
+ Goals.create(site, %{"page_path" => "foo bar", "display_name" => "two"})
+
assert {"has already been taken", _} = changeset.errors[:page_path]
end