Skip to content

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

Page Scroll Goals (#5029)

RobertJoonas4d0524503689
diff --git a/lib/plausible/goal/schema.ex b/lib/plausible/goal/schema.ex
index 84d76cb991..2900c598d4 100644
--- a/lib/plausible/goal/schema.ex
+++ b/lib/plausible/goal/schema.ex
@@ -38,14 +38,19 @@ defmodule Plausible.Goal do
|> cast_assoc(:site)
|> update_leading_slash()
|> validate_event_name_and_page_path()
+ |> validate_page_path_for_scroll_goal()
|> 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)
+ |> validate_number(:scroll_threshold,
+ greater_than_or_equal_to: -1,
+ less_than_or_equal_to: 100,
+ message: "Should be -1 (missing) or in range [0, 100]"
+ )
|> check_constraint(:event_name,
name: :check_event_name_or_page_path,
message: "cannot co-exist with page_path"
@@ -58,9 +63,14 @@ defmodule Plausible.Goal do
goal.display_name
end
- @spec type(t()) :: :event | :page
- def type(%{event_name: event_name}) when is_binary(event_name), do: :event
- def type(%{page_path: page_path}) when is_binary(page_path), do: :page
+ @spec type(t()) :: :event | :scroll | :page
+ def type(goal) do
+ cond do
+ is_binary(goal.event_name) -> :event
+ is_binary(goal.page_path) && goal.scroll_threshold > -1 -> :scroll
+ is_binary(goal.page_path) -> :page
+ end
+ end
defp update_leading_slash(changeset) do
case get_field(changeset, :page_path) do
@@ -90,6 +100,18 @@ defmodule Plausible.Goal do
end
end
+ defp validate_page_path_for_scroll_goal(changeset) do
+ scroll_threshold = get_field(changeset, :scroll_threshold)
+ page_path = get_field(changeset, :page_path)
+
+ if scroll_threshold > -1 and is_nil(page_path) do
+ changeset
+ |> add_error(:scroll_threshold, "page_path field missing for page scroll goal")
+ else
+ changeset
+ end
+ end
+
defp validate_page_path(changeset) do
value = get_field(changeset, :page_path)
diff --git a/lib/plausible/stats/filters/query_parser.ex b/lib/plausible/stats/filters/query_parser.ex
index 751761ac45..38fd78fbe5 100644
--- a/lib/plausible/stats/filters/query_parser.ex
+++ b/lib/plausible/stats/filters/query_parser.ex
@@ -445,7 +445,7 @@ defmodule Plausible.Stats.Filters.QueryParser do
def preload_goals_and_revenue(site, metrics, filters, dimensions) do
preloaded_goals =
- Plausible.Goals.Filters.preload_needed_goals(site, dimensions, filters)
+ Plausible.Stats.Goals.preload_needed_goals(site, dimensions, filters)
{revenue_warning, revenue_currencies} =
preload_revenue(site, preloaded_goals, metrics, dimensions)
diff --git a/lib/plausible/stats/filters/utils.ex b/lib/plausible/stats/filters/utils.ex
index 957ef2ab3b..48dd501a87 100644
--- a/lib/plausible/stats/filters/utils.ex
+++ b/lib/plausible/stats/filters/utils.ex
@@ -1,20 +1,6 @@
defmodule Plausible.Stats.Filters.Utils do
@moduledoc false
- def split_goals(goals) do
- Enum.split_with(goals, fn goal -> Plausible.Goal.type(goal) == :event end)
- end
-
- def split_goals_query_expressions(goals) do
- {event_goals, pageview_goals} = split_goals(goals)
- events = Enum.map(event_goals, fn goal -> goal.event_name end)
-
- page_regexes =
- Enum.map(pageview_goals, fn goal -> page_regex(goal.page_path) end)
-
- {events, page_regexes}
- end
-
def page_regex(expr) do
escaped =
expr
diff --git a/lib/plausible/goals/filters.ex b/lib/plausible/stats/goals.ex
similarity index 53%
rename from lib/plausible/goals/filters.ex
rename to lib/plausible/stats/goals.ex
index 0caf5e4e51..92a9d9cf47 100644
--- a/lib/plausible/goals/filters.ex
+++ b/lib/plausible/stats/goals.ex
@@ -1,5 +1,7 @@
-defmodule Plausible.Goals.Filters do
- @moduledoc false
+defmodule Plausible.Stats.Goals do
+ @moduledoc """
+ Stats code related to filtering and grouping by goals.
+ """
import Ecto.Query
import Plausible.Stats.Filters.Utils, only: [page_regex: 1]
@@ -59,6 +61,49 @@ defmodule Plausible.Goals.Filters do
end)
end
+ @type goal_join_data() :: %{
+ indices: [non_neg_integer()],
+ types: [String.t()],
+ event_names_imports: [String.t()],
+ event_names_by_type: [String.t()],
+ page_regexes: [String.t()],
+ scroll_thresholds: [non_neg_integer()]
+ }
+
+ @doc """
+ Returns data needed to perform a GROUP BY on goals in an ecto query.
+ """
+ @spec goal_join_data(Plausible.Stats.Query.t()) :: goal_join_data()
+ def goal_join_data(query) do
+ goals = query.preloaded_goals.matching_toplevel_filters
+
+ %{
+ indices: Enum.with_index(goals, 1) |> Enum.map(fn {_goal, idx} -> idx end),
+ types: Enum.map(goals, &to_string(Plausible.Goal.type(&1))),
+ # :TRICKY: This will contain "" for non-event goals
+ event_names_imports: Enum.map(goals, &to_string(&1.event_name)),
+ event_names_by_type:
+ Enum.map(goals, fn goal ->
+ case Plausible.Goal.type(goal) do
+ :event -> goal.event_name
+ :page -> "pageview"
+ :scroll -> "pageleave"
+ end
+ end),
+ # :TRICKY: event goals are considered to match everything for the sake of efficient queries in query_builder.ex
+ # See also Plausible.Stats.SQL.Expression#event_goal_join
+ page_regexes:
+ Enum.map(goals, fn goal ->
+ case Plausible.Goal.type(goal) do
+ :event -> ".?"
+ :page -> Filters.Utils.page_regex(goal.page_path)
+ :scroll -> Filters.Utils.page_regex(goal.page_path)
+ end
+ end),
+ scroll_thresholds: Enum.map(goals, & &1.scroll_threshold)
+ }
+ end
+
defp filter_preloaded(goals, filter, clause) do
Enum.filter(goals, fn goal -> matches?(goal, filter, clause) end)
end
@@ -106,33 +151,47 @@ defmodule Plausible.Goals.Filters do
defp build_condition(filtered_goals, imported?) do
Enum.reduce(filtered_goals, false, fn goal, dynamic_statement ->
- case goal do
- nil ->
- dynamic([e], ^dynamic_statement)
-
- %Plausible.Goal{event_name: event_name} when is_binary(event_name) ->
- dynamic([e], e.name == ^event_name or ^dynamic_statement)
-
- %Plausible.Goal{page_path: page_path} when is_binary(page_path) ->
- dynamic([e], ^page_filter_condition(page_path, imported?) or ^dynamic_statement)
+ if is_nil(goal) do
+ dynamic([e], ^dynamic_statement)
+ else
+ type = Plausible.Goal.type(goal)
+ dynamic([e], ^goal_condition(type, goal, imported?) or ^dynamic_statement)
end
end)
end
- defp page_filter_condition(page_path, imported?) do
- db_field = page_path_db_field(imported?)
+ defp goal_condition(:event, goal, _) do
+ dynamic([e], e.name == ^goal.event_name)
+ end
- page_condition =
- if String.contains?(page_path, "*") do
- dynamic([e], fragment("match(?, ?)", field(e, ^db_field), ^page_regex(page_path)))
- else
- dynamic([e], field(e, ^db_field) == ^page_path)
- end
+ defp goal_condition(:scroll, goal, false = _imported?) do
+ pathname_condition = page_path_condition(goal.page_path, _imported? = false)
+ name_condition = dynamic([e], e.name == "pageleave")
+
+ scroll_condition =
+ dynamic([e], e.scroll_depth <= 100 and e.scroll_depth >= ^goal.scroll_threshold)
+
+ dynamic([e], ^pathname_condition and ^name_condition and ^scroll_condition)
+ end
+
+ defp goal_condition(:page, goal, true = _imported?) do
+ page_path_condition(goal.page_path, _imported? = true)
+ end
+
+ defp goal_condition(:page, goal, false = _imported?) do
+ name_condition = dynamic([e], e.name == "pageview")
+ pathname_condition = page_path_condition(goal.page_path, _imported? = false)
+
+ dynamic([e], ^pathname_condition and ^name_condition)
+ end
+
+ defp page_path_condition(page_path, imported?) do
+ db_field = page_path_db_field(imported?)
- if imported? do
- dynamic([e], ^page_condition)
+ if String.contains?(page_path, "*") do
+ dynamic([e], fragment("match(?, ?)", field(e, ^db_field), ^page_regex(page_path)))
else
- dynamic([e], ^page_condition and e.name == "pageview")
+ dynamic([e], field(e, ^db_field) == ^page_path)
end
end
diff --git a/lib/plausible/stats/imported/base.ex b/lib/plausible/stats/imported/base.ex
index 4346f4d71f..e0720acb70 100644
--- a/lib/plausible/stats/imported/base.ex
+++ b/lib/plausible/stats/imported/base.ex
@@ -191,6 +191,7 @@ defmodule Plausible.Stats.Imported.Base do
|> Enum.map(fn
:event -> "imported_custom_events"
:page -> "imported_pages"
+ :scroll -> nil
end)
case Enum.uniq(table_candidates ++ filter_goal_table_candidates) do
diff --git a/lib/plausible/stats/imported/imported.ex b/lib/plausible/stats/imported/imported.ex
index 61d99373de..f8373493ac 100644
--- a/lib/plausible/stats/imported/imported.ex
+++ b/lib/plausible/stats/imported/imported.ex
@@ -5,7 +5,6 @@ defmodule Plausible.Stats.Imported do
import Ecto.Query
import Plausible.Stats.Imported.SQL.Expression
- alias Plausible.Stats.Filters
alias Plausible.Stats.Imported
alias Plausible.Stats.Query
alias Plausible.Stats.SQL.QueryBuilder
@@ -239,8 +238,7 @@ defmodule Plausible.Stats.Imported do
end
def merge_imported(q, site, %Query{dimensions: ["event:goal"]} = query, metrics) do
- {events, page_regexes} =
- Filters.Utils.split_goals_query_expressions(query.preloaded_goals.matching_toplevel_filters)
+ goal_join_data = Plausible.Stats.Goals.goal_join_data(query)
Imported.Base.decide_tables(query)
|> Enum.map(fn
@@ -248,7 +246,12 @@ defmodule Plausible.Stats.Imported do
Imported.Base.query_imported("imported_custom_events", site, query)
|> where([i], i.visitors > 0)
|> select_merge_as([i], %{
- dim0: fragment("-indexOf(?, ?)", type(^events, {:array, :string}), i.name)
+ dim0:
+ fragment(
+ "indexOf(?, ?)",
+ type(^goal_join_data.event_names_imports, {:array, :string}),
+ i.name
+ )
})
|> select_imported_metrics(metrics)
|> group_by([], selected_as(:dim0))
@@ -260,15 +263,24 @@ defmodule Plausible.Stats.Imported do
|> where(
[i],
fragment(
- "notEmpty(multiMatchAllIndices(?, ?) as indices)",
+ """
+ notEmpty(
+ arrayFilter(
+ goal_idx -> ?[goal_idx] = 'page' AND match(?, ?[goal_idx]),
+ ?
+ ) as indices
+ )
+ """,
+ type(^goal_join_data.types, {:array, :string}),
i.page,
- type(^page_regexes, {:array, :string})
+ type(^goal_join_data.page_regexes, {:array, :string}),
+ type(^goal_join_data.indices, {:array, :integer})
)
)
|> join(:inner, [_i], index in fragment("indices"), hints: "ARRAY", on: true)
|> group_by([_i, index], index)
|> select_merge_as([_i, index], %{
- dim0: type(fragment("?", index), :integer)
+ dim0: fragment("CAST(?, 'UInt64')", index)
})
|> select_imported_metrics(metrics)
end)
diff --git a/lib/plausible/stats/imported/sql/where_builder.ex b/lib/plausible/stats/imported/sql/where_builder.ex
index 93d8476210..a9b8437265 100644
--- a/lib/plausible/stats/imported/sql/where_builder.ex
+++ b/lib/plausible/stats/imported/sql/where_builder.ex
@@ -53,7 +53,7 @@ defmodule Plausible.Stats.Imported.SQL.WhereBuilder do
db_field = Plausible.Stats.Filters.without_prefix(dimension)
if db_field == :goal do
- Plausible.Goals.Filters.add_filter(query, filter, imported?: true)
+ Plausible.Stats.Goals.add_filter(query, filter, imported?: true)
else
mapped_db_field = Map.get(@db_field_mappings, db_field, db_field)
diff --git a/lib/plausible/stats/query_runner.ex b/lib/plausible/stats/query_runner.ex
index a4abbce93b..99f3c60c25 100644
--- a/lib/plausible/stats/query_runner.ex
+++ b/lib/plausible/stats/query_runner.ex
@@ -18,7 +18,6 @@ defmodule Plausible.Stats.QueryRunner do
QueryOptimizer,
QueryResult,
Legacy,
- Filters,
SQL,
Util,
Time
@@ -137,15 +136,11 @@ defmodule Plausible.Stats.QueryRunner do
end
defp dimension_label("event:goal", entry, query) do
- {events, paths} = Filters.Utils.split_goals(query.preloaded_goals.matching_toplevel_filters)
-
goal_index = Map.get(entry, Util.shortname(query, "event:goal"))
- # Closely coupled logic with SQL.Expression.event_goal_join/2
- cond do
- goal_index < 0 -> Enum.at(events, -goal_index - 1) |> Plausible.Goal.display_name()
- goal_index > 0 -> Enum.at(paths, goal_index - 1) |> Plausible.Goal.display_name()
- end
+ query.preloaded_goals.matching_toplevel_filters
+ |> Enum.at(goal_index - 1)
+ |> Plausible.Goal.display_name()
end
defp dimension_label("time:" <> _ = time_dimension, entry, query) do
diff --git a/lib/plausible/stats/sql/expression.ex b/lib/plausible/stats/sql/expression.ex
index 34311dcb76..f4f9a57be8 100644
--- a/lib/plausible/stats/sql/expression.ex
+++ b/lib/plausible/stats/sql/expression.ex
@@ -196,6 +196,7 @@ defmodule Plausible.Stats.SQL.Expression do
})
end
+ # TODO: make it possible to query events from pageleave events (total conversions for page scroll goals)
def event_metric(:events) do
wrap_alias([e], %{
events: fragment("toUInt64(round(countIf(? != 'pageleave') * any(_sample_factor)))", e.name)
@@ -327,19 +328,27 @@ defmodule Plausible.Stats.SQL.Expression do
def session_metric(:conversion_rate, _query), do: %{}
def session_metric(:group_conversion_rate, _query), do: %{}
- defmacro event_goal_join(events, page_regexes) do
+ defmacro event_goal_join(goal_join_data) do
quote do
fragment(
"""
- arrayPushFront(
- CAST(multiMatchAllIndices(?, ?) AS Array(Int64)),
- -indexOf(?, ?)
+ arrayIntersect(
+ multiMatchAllIndices(?, ?),
+ arrayMap(
+ (expected_name, threshold, index) -> if(expected_name = ? and ? between threshold and 100, index, -1),
+ ?,
+ ?,
+ ?
+ )
)
""",
e.pathname,
- type(^unquote(page_regexes), {:array, :string}),
- type(^unquote(events), {:array, :string}),
- e.name
+ type(^unquote(goal_join_data).page_regexes, {:array, :string}),
+ e.name,
+ e.scroll_depth,
+ type(^unquote(goal_join_data).event_names_by_type, {:array, :string}),
+ type(^unquote(goal_join_data).scroll_thresholds, {:array, :integer}),
+ type(^unquote(goal_join_data).indices, {:array, :integer})
)
end
end
diff --git a/lib/plausible/stats/sql/query_builder.ex b/lib/plausible/stats/sql/query_builder.ex
index f0b1e2b56f..942222f2e3 100644
--- a/lib/plausible/stats/sql/query_builder.ex
+++ b/lib/plausible/stats/sql/query_builder.ex
@@ -8,7 +8,7 @@ defmodule Plausible.Stats.SQL.QueryBuilder do
import Plausible.Stats.Imported
import Plausible.Stats.Util
- alias Plausible.Stats.{Filters, Query, QueryOptimizer, TableDecider, SQL}
+ alias Plausible.Stats.{Query, QueryOptimizer, TableDecider, SQL}
alias Plausible.Stats.SQL.Expression
require Plausible.Stats.SQL.Expression
@@ -138,19 +138,17 @@ defmodule Plausible.Stats.SQL.QueryBuilder do
Enum.reduce(query.dimensions, q, &dimension_group_by(&2, table, query, &1))
end
- defp dimension_group_by(q, _table, query, "event:goal" = dimension) do
- {events, page_regexes} =
- Filters.Utils.split_goals_query_expressions(query.preloaded_goals.matching_toplevel_filters)
+ defp dimension_group_by(q, :events, query, "event:goal" = dimension) do
+ goal_join_data = Plausible.Stats.Goals.goal_join_data(query)
from(e in q,
- join: goal in Expression.event_goal_join(events, page_regexes),
+ join: goal in Expression.event_goal_join(goal_join_data),
hints: "ARRAY",
on: true,
select_merge: %{
^shortname(query, dimension) => fragment("?", goal)
},
- group_by: goal,
- where: goal != 0 and (e.name == "pageview" or goal < 0)
+ group_by: goal
)
end
diff --git a/lib/plausible/stats/sql/where_builder.ex b/lib/plausible/stats/sql/where_builder.ex
index e19e691bd4..ad32ac961f 100644
--- a/lib/plausible/stats/sql/where_builder.ex
+++ b/lib/plausible/stats/sql/where_builder.ex
@@ -113,7 +113,7 @@ defmodule Plausible.Stats.SQL.WhereBuilder do
end
defp add_filter(:events, query, [_, "event:goal" | _rest] = filter) do
- Plausible.Goals.Filters.add_filter(query, filter)
+ Plausible.Stats.Goals.add_filter(query, filter)
end
defp add_filter(:events, _query, [_, "event:page" | _rest] = filter) do
diff --git a/lib/plausible_web/live/goal_settings/form.ex b/lib/plausible_web/live/goal_settings/form.ex
index 669ceee695..c66c63b56d 100644
--- a/lib/plausible_web/live/goal_settings/form.ex
+++ b/lib/plausible_web/live/goal_settings/form.ex
@@ -84,6 +84,7 @@ defmodule PlausibleWeb.Live.GoalSettings.Form do
f={f}
goal={@goal}
suffix={@context_unique_id}
+ current_user={@current_user}
site={@site}
/>
@@ -127,6 +128,7 @@ defmodule PlausibleWeb.Live.GoalSettings.Form do
x-show="!tabSelectionInProgress"
f={f}
suffix={suffix(@context_unique_id, @tab_sequence_id)}
+ current_user={@current_user}
site={@site}
x-init="tabSelectionInProgress = false"
/>
@@ -156,6 +158,7 @@ defmodule PlausibleWeb.Live.GoalSettings.Form do
end
attr(:f, Phoenix.HTML.Form)
+ attr(:current_user, Plausible.Auth.User)
attr(:site, Plausible.Site)
attr(:suffix, :string)
attr(:goal, Plausible.Goal, default: nil)
@@ -193,6 +196,17 @@ defmodule PlausibleWeb.Live.GoalSettings.Form do
x-data="{ firstFocus: true }"
x-on:focus="if (firstFocus) { $el.select(); firstFocus = false; }"
/>
+
+ <.input
+ :if={Plausible.Stats.ScrollDepth.feature_visible?(@site, @current_user)}
+ label="Scroll Depth Threshold (optional)"
+ field={@f[:scroll_threshold]}
+ type="number"
+ value={if @goal && @goal.scroll_threshold > -1, do: @goal.scroll_threshold, else: nil}
+ min="0"
+ max="100"
+ step="1"
+ />
</div>
"""
end
diff --git a/lib/plausible_web/live/goal_settings/list.ex b/lib/plausible_web/live/goal_settings/list.ex
index 82e0df7ad9..0d652623df 100644
--- a/lib/plausible_web/live/goal_settings/list.ex
+++ b/lib/plausible_web/live/goal_settings/list.ex
@@ -99,7 +99,14 @@ defmodule PlausibleWeb.Live.GoalSettings.List do
"""
end
- def pageview_description(goal) do
+ defp page_scroll_description(goal) do
+ case pageview_description(goal) do
+ "" -> "Scroll > #{goal.scroll_threshold}"
+ path -> "Scroll > #{goal.scroll_threshold} on #{path}"
+ end
+ end
+
+ defp pageview_description(goal) do
path = goal.page_path
case goal.display_name do
@@ -108,13 +115,23 @@ defmodule PlausibleWeb.Live.GoalSettings.List do
end
end
- def custom_event_description(goal) do
+ defp custom_event_description(goal) do
if goal.display_name == goal.event_name, do: "", else: "#{goal.event_name}"
end
- def goal_description(assigns) do
+ defp goal_description(assigns) do
~H"""
- <span :if={@goal.page_path} class="block truncate text-gray-400 dark:text-gray-600">
+ <span
+ :if={@goal.page_path && @goal.scroll_threshold > -1}
+ class="block truncate text-gray-400 dark:text-gray-600"
+ >
+ {page_scroll_description(@goal)}
+ </span>
+
+ <span
+ :if={@goal.page_path && @goal.scroll_threshold == -1}
+ class="block truncate text-gray-400 dark:text-gray-600"
+ >
{pageview_description(@goal)}
</span>
diff --git a/test/plausible/goals_test.exs b/test/plausible/goals_test.exs
index d57cedfcea..385cc9a46a 100644
--- a/test/plausible/goals_test.exs
+++ b/test/plausible/goals_test.exs
@@ -32,6 +32,66 @@ defmodule Plausible.GoalsTest do
assert {"should be at most %{count} character(s)", _} = changeset.errors[:event_name]
end
+ test "create/2 validates scroll_threshold in range [-1, 100]" do
+ site = new_site()
+
+ {:error, changeset} =
+ Goals.create(site, %{"page_path" => "/blog/post-1", "scroll_threshold" => -2})
+
+ assert {"Should be -1 (missing) or in range [0, 100]", _} =
+ changeset.errors[:scroll_threshold]
+
+ {:error, changeset} =
+ Goals.create(site, %{"page_path" => "/blog/post-1", "scroll_threshold" => 101})
+
+ assert {"Should be -1 (missing) or in range [0, 100]", _} =
+ changeset.errors[:scroll_threshold]
+
+ assert {:ok, _} =
+ Goals.create(site, %{"page_path" => "/blog/post-1", "scroll_threshold" => -1})
+
+ assert {:ok, _} =
+ Goals.create(site, %{"page_path" => "/blog/post-2", "scroll_threshold" => 50})
+ end
+
+ test "create/2 validates page path exists for scroll goals" do
+ site = new_site()
+
+ {:error, changeset} =
+ Goals.create(site, %{"event_name" => "Signup", "scroll_threshold" => 50})
+
+ assert {"page_path field missing for page scroll goal", _} =
+ changeset.errors[:scroll_threshold]
+ end
+
+ test "create/2 validates uniqueness across page_path and scroll_threshold" do
+ site = new_site()
+
+ {:ok, _} =
+ Goals.create(site, %{
+ "page_path" => "/blog/post-1",
+ "scroll_threshold" => 50,
+ "display_name" => "Scroll 50"
+ })
+
+ {:ok, _} =
+ Goals.create(site, %{
+ "page_path" => "/blog/post-1",
+ "scroll_threshold" => 75,
+ "display_name" => "Scroll 75"
+ })
+
+ {:error, changeset} =
+ Goals.create(site, %{
+ "page_path" => "/blog/post-1",
+ "scroll_threshold" => 50,
+ "display_name" => "Scroll 50 another"
+ })
+
+ assert {"has already been taken", _} =
+ changeset.errors[:page_path]
+ end
+
test "create/2 fails to create the same pageview goal twice" do
site = new_site()
{:ok, _} = Goals.create(site, %{"page_path" => "foo bar", "display_name" => "one"})
diff --git a/test/plausible_web/controllers/api/external_stats_controller/query_goal_dimension_test.exs b/test/plausible_web/controllers/api/external_stats_controller/query_goal_dimension_test.exs
index 64b517995f..fb6f6021ee 100644
--- a/test/plausible_web/controllers/api/external_stats_controller/query_goal_dimension_test.exs
+++ b/test/plausible_web/controllers/api/external_stats_controller/query_goal_dimension_test.exs
@@ -130,4 +130,420 @@ defmodule PlausibleWeb.Api.ExternalStatsController.QueryGoalDimensionTest do
]
end
end
+
+ describe "page scroll goals" do
+ test "a scroll depth of 255 (missing) is not considered a conversion (page scroll goal filter)",
+ %{
+ conn: conn,
+ site: site
+ } do
+ insert(:goal,
+ site: site,
+ page_path: "/blog",
+ scroll_threshold: 90,
+ display_name: "Scroll /blog 90"
+ )
+
+ populate_stats(site, [
+ build(:pageview, user_id: 12, pathname: "/blog", timestamp: ~N[2021-01-01 00:00:00]),
+ build(:pageleave,
+ user_id: 12,
+ pathname: "/blog",
+ timestamp: ~N[2021-01-01 00:00:00],
+ scroll_depth: 255
+ )
+ ])
+
+ conn =
+ post(conn, "/api/v2/query", %{
+ "site_id" => site.domain,
+ "metrics" => ["visitors"],
+ "filters" => [["is", "event:goal", ["Scroll /blog 90"]]],
+ "date_range" => "all"
+ })
+
+ assert json_response(conn, 200)["results"] == [
+ %{"dimensions" => [], "metrics" => [0]}
+ ]
+ end
+
+ test "a scroll depth of 255 (missing) is not considered a conversion (page scroll goal breakdown)",
+ %{
+ conn: conn,
+ site: site
+ } do
+ insert(:goal,
+ site: site,
+ page_path: "/blog",
+ scroll_threshold: 90,
+ display_name: "Scroll /blog 90"
+ )
+
+ populate_stats(site, [
+ build(:pageview, user_id: 12, pathname: "/blog", timestamp: ~N[2021-01-01 00:00:00]),
+ build(:pageleave,
+ user_id: 12,
+ pathname: "/blog",
+ timestamp: ~N[2021-01-01 00:00:00],
+ scroll_depth: 255
+ )
+ ])
+
+ conn =
+ post(conn, "/api/v2/query", %{
+ "site_id" => site.domain,
+ "metrics" => ["visitors"],
+ "date_range" => "all",
+ "dimensions" => ["event:goal"]
+ })
+
+ assert json_response(conn, 200)["results"] == []
+ end
+
+ test "returns page scroll goals and pageview goals in breakdown with page filter", %{
+ conn: conn,
+ site: site
+ } do
+ insert(:goal, site: site, page_path: "/blog")
+
+ insert(:goal,
+ site: site,
+ page_path: "/blog",
+ scroll_threshold: 25,
+ display_name: "Scroll /blog 25"
+ )
+
+ populate_stats(site, [
+ build(:pageview, user_id: 12, pathname: "/blog", timestamp: ~N[2021-01-01 00:00:00]),
+ build(:pageleave,
+ user_id: 12,
+ pathname: "/blog",
+ timestamp: ~N[2021-01-01 00:00:00],
+ scroll_depth: 10
+ ),
+ build(:pageview, user_id: 34, pathname: "/blog", timestamp: ~N[2021-01-01 00:00:00]),
+ build(:pageleave,
+ user_id: 34,
+ pathname: "/blog",
+ timestamp: ~N[2021-01-01 00:00:00],
+ scroll_depth: 30
+ ),
+ build(:pageview)
+ ])
+
+ conn =
+ post(conn, "/api/v2/query", %{
+ "site_id" => site.domain,
+ "metrics" => ["visitors", "events", "conversion_rate"],
+ "filters" => [["is", "event:page", ["/blog"]]],
+ "date_range" => "all",
+ "dimensions" => ["event:goal"]
+ })
+
+ assert json_response(conn, 200)["results"] == [
+ %{"dimensions" => ["Visit /blog"], "metrics" => [2, 2, 100.0]},
+ %{"dimensions" => ["Scroll /blog 25"], "metrics" => [1, 0, 50.0]}
+ ]
+ end
+
+ test "custom props and page scroll goals in a double-dimension breakdown", %{
+ conn: conn,
+ site: site
+ } do
+ for threshold <- [25, 50, 75] do
+ insert(:goal,
+ site: site,
+ page_path: "/blog**",
+ scroll_threshold: threshold,
+ display_name: "Scroll /blog #{threshold}"
+ )
+ end
+
+ populate_stats(site, [
+ build(:pageview,
+ pathname: "/blog/john-post",
+ "meta.key": ["author"],
+ "meta.value": ["john"]
+ ),
+ build(:pageview,
+ user_id: 12,
+ pathname: "/blog/john-post",
+ timestamp: ~N[2021-01-01 00:00:00],
+ "meta.key": ["author"],
+ "meta.value": ["john"]
+ ),
+ build(:pageleave,
+ user_id: 12,
+ pathname: "/blog/john-post",
+ timestamp: ~N[2021-01-01 00:00:10],
+ scroll_depth: 30,
+ "meta.key": ["author"],
+ "meta.value": ["john"]
+ ),
+ build(:pageview,
+ user_id: 34,
+ pathname: "/blog/jane-post",
+ timestamp: ~N[2021-01-01 00:00:00],
+ "meta.key": ["author"],
+ "meta.value": ["jane"]
+ ),
+ build(:pageleave,
+ user_id: 34,
+ pathname: "/blog/jane-post",
+ timestamp: ~N[2021-01-01 00:00:10],
+ scroll_depth: 50,
+ "meta.key": ["author"],
+ "meta.value": ["jane"]
+ ),
+ build(:pageview,
+ user_id: 56,
+ pathname: "/blog/jane-post",
+ timestamp: ~N[2021-01-01 00:00:00],
+ "meta.key": ["author"],
+ "meta.value": ["jane"]
+ ),
+ build(:pageleave,
+ user_id: 56,
+ pathname: "/blog/jane-post",
+ timestamp: ~N[2021-01-01 00:00:10],
+ scroll_depth: 75,
+ "meta.key": ["author"],
+ "meta.value": ["jane"]
+ )
+ ])
+
+ conn =
+ post(conn, "/api/v2/query", %{
+ "site_id" => site.domain,
+ "metrics" => ["visitors", "events", "conversion_rate"],
+ "date_range" => "all",
+ "dimensions" => ["event:goal", "event:props:author"]
+ })
+
+ assert json_response(conn, 200)["results"] == [
+ %{"dimensions" => ["Scroll /blog 25", "jane"], "metrics" => [2, 0, 50.0]},
+ %{"dimensions" => ["Scroll /blog 50", "jane"], "metrics" => [2, 0, 50.0]},
+ %{"dimensions" => ["Scroll /blog 25", "john"], "metrics" => [1, 0, 25.0]},
+ %{"dimensions" => ["Scroll /blog 75", "jane"], "metrics" => [1, 0, 25.0]}
+ ]
+ end
+
+ test "returns group_conversion_rate for a page scroll goal filter in source breakdown", %{
+ conn: conn,
+ site: site
+ } do
+ insert(:goal,
+ site: site,
+ page_path: "/blog",
+ scroll_threshold: 50,
+ display_name: "Scroll /blog 50"
+ )
+
+ populate_stats(site, [
+ build(:pageview, referrer_source: "Twitter"),
+ build(:pageview,
+ user_id: 12,
+ pathname: "/blog",
+ timestamp: ~N[2021-01-01 00:00:00],
+ referrer_source: "Google"
+ ),
+ build(:pageleave,
+ user_id: 12,
+ pathname: "/blog",
+ timestamp: ~N[2021-01-01 00:00:10],
+ scroll_depth: 30,
+ referrer_source: "Google"
+ ),
+ build(:pageview,
+ user_id: 34,
+ pathname: "/blog",
+ timestamp: ~N[2021-01-01 00:00:00],
+ referrer_source: "Twitter"
+ ),
+ build(:pageleave,
+ user_id: 34,
+ pathname: "/blog",
+ timestamp: ~N[2021-01-01 00:00:10],
+ scroll_depth: 50,
+ referrer_source: "Twitter"
+ )
+ ])
+
+ conn =
+ post(conn, "/api/v2/query", %{
+ "site_id" => site.domain,
+ "metrics" => ["visitors", "events", "group_conversion_rate"],
+ "date_range" => "all",
+ "filters" => [["is", "event:goal", ["Scroll /blog 50"]]],
+ "dimensions" => ["visit:source"]
+ })
+
+ assert json_response(conn, 200)["results"] == [
+ %{"dimensions" => ["Twitter"], "metrics" => [1, 0, 50.0]}
+ ]
+ end
+
+ test "breakdown by event:props:author with a page scroll goal filter", %{
+ conn: conn,
+ site: site
+ } do
+ for threshold <- [25, 50, 75] do
+ insert(:goal,
+ site: site,
+ page_path: "/blog**",
+ scroll_threshold: threshold,
+ display_name: "Scroll /blog #{threshold}"
+ )
+ end
+
+ populate_stats(site, [
+ build(:pageview,
+ pathname: "/blog/john-post",
+ "meta.key": ["author"],
+ "meta.value": ["john"]
+ ),
+ build(:pageview,
+ user_id: 12,
+ pathname: "/blog/john-post",
+ timestamp: ~N[2021-01-01 00:00:00],
+ "meta.key": ["author"],
+ "meta.value": ["john"]
+ ),
+ build(:pageleave,
+ user_id: 12,
+ pathname: "/blog/john-post",
+ timestamp: ~N[2021-01-01 00:00:10],
+ scroll_depth: 30,
+ "meta.key": ["author"],
+ "meta.value": ["john"]
+ ),
+ build(:pageview,
+ user_id: 34,
+ pathname: "/blog/jane-post",
+ timestamp: ~N[2021-01-01 00:00:00],
+ "meta.key": ["author"],
+ "meta.value": ["jane"]
+ ),
+ build(:pageleave,
+ user_id: 34,
+ pathname: "/blog/jane-post",
+ timestamp: ~N[2021-01-01 00:00:10],
+ scroll_depth: 50,
+ "meta.key": ["author"],
+ "meta.value": ["jane"]
+ ),
+ build(:pageview,
+ user_id: 56,
+ pathname: "/blog/jane-post",
+ timestamp: ~N[2021-01-01 00:00:00],
+ "meta.key": ["author"],
+ "meta.value": ["jane"]
+ ),
+ build(:pageleave,
+ user_id: 56,
+ pathname: "/blog/jane-post",
+ timestamp: ~N[2021-01-01 00:00:10],
+ scroll_depth: 75,
+ "meta.key": ["author"],
+ "meta.value": ["jane"]
+ )
+ ])
+
+ conn =
+ post(conn, "/api/v2/query", %{
+ "site_id" => site.domain,
+ "metrics" => ["visitors", "events", "conversion_rate"],
+ "date_range" => "all",
+ "filters" => [["is", "event:goal", ["Scroll /blog 25"]]],
+ "dimensions" => ["event:props:author"]
+ })
+
+ assert json_response(conn, 200)["results"] == [
+ %{"dimensions" => ["jane"], "metrics" => [2, 0, 50.0]},
+ %{"dimensions" => ["john"], "metrics" => [1, 0, 25.0]}
+ ]
+ end
+
+ test "breaks down page scroll goals with a custom prop filter", %{
+ conn: conn,
+ site: site
+ } do
+ for threshold <- [25, 50, 75] do
+ insert(:goal,
+ site: site,
+ page_path: "/blog**",
+ scroll_threshold: threshold,
+ display_name: "Scroll /blog #{threshold}"
+ )
+ end
+
+ populate_stats(site, [
+ build(:pageview,
+ pathname: "/blog/john-post",
+ "meta.key": ["author"],
+ "meta.value": ["john"]
+ ),
+ build(:pageview,
+ user_id: 12,
+ pathname: "/blog/john-post",
+ timestamp: ~N[2021-01-01 00:00:00],
+ "meta.key": ["author"],
+ "meta.value": ["john"]
+ ),
+ build(:pageleave,
+ user_id: 12,
+ pathname: "/blog/john-post",
+ timestamp: ~N[2021-01-01 00:00:10],
+ scroll_depth: 30,
+ "meta.key": ["author"],
+ "meta.value": ["john"]
+ ),
+ build(:pageview,
+ user_id: 34,
+ pathname: "/blog/jane-post",
+ timestamp: ~N[2021-01-01 00:00:00],
+ "meta.key": ["author"],
+ "meta.value": ["jane"]
+ ),
+ build(:pageleave,
+ user_id: 34,
+ pathname: "/blog/jane-post",
+ timestamp: ~N[2021-01-01 00:00:10],
+ scroll_depth: 50,
+ "meta.key": ["author"],
+ "meta.value": ["jane"]
+ ),
+ build(:pageview,
+ user_id: 56,
+ pathname: "/blog/jane-post",
+ timestamp: ~N[2021-01-01 00:00:00],
+ "meta.key": ["author"],
+ "meta.value": ["jane"]
+ ),
+ build(:pageleave,
+ user_id: 56,
+ pathname: "/blog/jane-post",
+ timestamp: ~N[2021-01-01 00:00:10],
+ scroll_depth: 75,
+ "meta.key": ["author"],
+ "meta.value": ["jane"]
+ )
+ ])
+
+ conn =
+ post(conn, "/api/v2/query", %{
+ "site_id" => site.domain,
+ "metrics" => ["visitors", "events", "conversion_rate"],
+ "date_range" => "all",
+ "filters" => [["is", "event:props:author", ["jane"]]],
+ "dimensions" => ["event:goal"]
+ })
+
+ assert json_response(conn, 200)["results"] == [
+ %{"dimensions" => ["Scroll /blog 25"], "metrics" => [2, 0, 50.0]},
+ %{"dimensions" => ["Scroll /blog 50"], "metrics" => [2, 0, 50.0]},
+ %{"dimensions" => ["Scroll /blog 75"], "metrics" => [1, 0, 25.0]}
+ ]
+ end
+ end
end
diff --git a/test/plausible_web/controllers/api/external_stats_controller/query_imported_test.exs b/test/plausible_web/controllers/api/external_stats_controller/query_imported_test.exs
index 47c2035f02..b6f12608bf 100644
--- a/test/plausible_web/controllers/api/external_stats_controller/query_imported_test.exs
+++ b/test/plausible_web/controllers/api/external_stats_controller/query_imported_test.exs
@@ -3,6 +3,9 @@ defmodule PlausibleWeb.Api.ExternalStatsController.QueryImportedTest do
@user_id Enum.random(1000..9999)
+ @unsupported_interval_warning "Imported stats are not included because the time dimension (i.e. the interval) is too short."
+ @unsupported_query_warning "Imported stats are not included in the results because query parameters are not supported. For more information, see: https://plausible.io/docs/stats-api#filtering-imported-stats"
+
setup [:create_user, :create_site, :create_api_key, :use_api_key]
describe "aggregation with imported data" do
@@ -880,8 +883,7 @@ defmodule PlausibleWeb.Api.ExternalStatsController.QueryImportedTest do
refute json_response(conn, 200)["meta"]["imports_included"]
assert json_response(conn, 200)["meta"]["imports_skip_reason"] == "unsupported_interval"
- assert json_response(conn, 200)["meta"]["imports_warning"] =~
- "Imported stats are not included because the time dimension (i.e. the interval) is too short."
+ assert json_response(conn, 200)["meta"]["imports_warning"] == @unsupported_interval_warning
end
test "adds a warning when query params are not supported for imported data", %{
@@ -920,8 +922,7 @@ defmodule PlausibleWeb.Api.ExternalStatsController.QueryImportedTest do
refute json_response(conn, 200)["meta"]["imports_included"]
assert json_response(conn, 200)["meta"]["imports_skip_reason"] == "unsupported_query"
- assert json_response(conn, 200)["meta"]["imports_warning"] =~
- "Imported stats are not included in the results because query parameters are not supported."
+ assert json_response(conn, 200)["meta"]["imports_warning"] == @unsupported_query_warning
end
test "does not add a warning when there are no site imports", %{conn: conn, site: site} do
@@ -1052,7 +1053,7 @@ defmodule PlausibleWeb.Api.ExternalStatsController.QueryImportedTest do
} = json_response(conn, 200)
refute json_response(conn, 200)["meta"]["imports_included"]
- assert meta["imports_warning"] =~ "Imported stats are not included in the results"
+ assert meta["imports_warning"] == @unsupported_query_warning
end
test "imported country, region and city data",
@@ -1322,8 +1323,7 @@ defmodule PlausibleWeb.Api.ExternalStatsController.QueryImportedTest do
assert json_response(conn, 200)["results"] == [%{"dimensions" => [], "metrics" => [2]}]
refute json_response(conn, 200)["meta"]["imports_included"]
- assert json_response(conn, 200)["meta"]["imports_warning"] =~
- "Imported stats are not included in the results"
+ assert json_response(conn, 200)["meta"]["imports_warning"] == @unsupported_query_warning
end
test "imports are skipped when has_not_done filter is used", %{
@@ -1358,8 +1358,161 @@ defmodule PlausibleWeb.Api.ExternalStatsController.QueryImportedTest do
assert json_response(conn, 200)["results"] == []
refute json_response(conn, 200)["meta"]["imports_included"]
- assert json_response(conn, 200)["meta"]["imports_warning"] =~
- "Imported stats are not included in the results"
+ assert json_response(conn, 200)["meta"]["imports_warning"] == @unsupported_query_warning
+ end
+ end
+
+ describe "page scroll goals" do
+ setup :create_site_import
+
+ test "rejects imported data with warning when page scroll goal is filtered by", %{
+ conn: conn,
+ site: site,
+ site_import: site_import
+ } do
+ insert(:goal,
+ site: site,
+ page_path: "/blog",
+ scroll_threshold: 50,
+ display_name: "Scroll /blog 50"
+ )
+
+ insert(:goal, site: site, page_path: "/blog**")
+
+ populate_stats(site, site_import.id, [
+ build(:pageview,
+ user_id: 12,
+ pathname: "/blog",
+ timestamp: ~N[2021-01-01 00:00:00]
+ ),
+ build(:pageleave,
+ user_id: 12,
+ pathname: "/blog",
+ timestamp: ~N[2021-01-01 00:00:10],
+ scroll_depth: 50
+ ),
+ build(:imported_pages, page: "/blog", date: ~D[2021-01-01])
+ ])
+
+ conn =
+ post(conn, "/api/v2/query", %{
+ "site_id" => site.domain,
+ "metrics" => ["visitors", "events", "conversion_rate"],
+ "date_range" => "all",
+ "filters" => [["is", "event:goal", ["Scroll /blog 50"]]],
+ "include" => %{"imports" => true}
+ })
+
+ assert %{
+ "results" => results,
+ "meta" => %{
+ "imports_included" => false,
+ "imports_skip_reason" => "unsupported_query",
+ "imports_warning" => @unsupported_query_warning
+ }
+ } = json_response(conn, 200)
+
+ assert results == [%{"dimensions" => [], "metrics" => [1, 0, 100.0]}]
+ end
+
+ test "rejects imported data with warning when page scroll goal is filtered by (IN filter)", %{
+ conn: conn,
+ site: site,
+ site_import: site_import
+ } do
+ insert(:goal,
+ site: site,
+ page_path: "/blog",
+ scroll_threshold: 50,
+ display_name: "Scroll /blog 50"
+ )
+
+ insert(:goal, site: site, page_path: "/blog**")
+
+ populate_stats(site, site_import.id, [
+ build(:pageview,
+ user_id: 12,
+ pathname: "/blog",
+ timestamp: ~N[2021-01-01 00:00:00]
+ ),
+ build(:pageleave,
+ user_id: 12,
+ pathname: "/blog",
+ timestamp: ~N[2021-01-01 00:00:10],
+ scroll_depth: 50
+ ),
+ build(:imported_pages, page: "/blog", date: ~D[2021-01-01])
+ ])
+
+ conn =
+ post(conn, "/api/v2/query", %{
+ "site_id" => site.domain,
+ "metrics" => ["visitors", "events", "conversion_rate"],
+ "date_range" => "all",
+ "filters" => [["is", "event:goal", ["Scroll /blog 50", "Visit /blog**"]]],
+ "include" => %{"imports" => true}
+ })
+
+ assert %{
+ "results" => results,
+ "meta" => %{
+ "imports_included" => false,
+ "imports_skip_reason" => "unsupported_query",
+ "imports_warning" => @unsupported_query_warning
+ }
+ } = json_response(conn, 200)
+
+ assert results == [%{"dimensions" => [], "metrics" => [1, 1, 100.0]}]
+ end
+
+ test "ignores imported data for page scroll goal conversions in goal breakdown", %{
+ conn: conn,
+ site: site,
+ site_import: site_import
+ } do
+ insert(:goal,
+ site: site,
+ page_path: "/blog",
+ scroll_threshold: 50,
+ display_name: "Scroll /blog 50"
+ )
+
+ insert(:goal, site: site, page_path: "/blog**")
+
+ populate_stats(site, site_import.id, [
+ build(:pageview,
+ user_id: 12,
+ pathname: "/blog",
+ timestamp: ~N[2021-01-01 00:00:00]
+ ),
+ build(:pageleave,
+ user_id: 12,
+ pathname: "/blog",
+ timestamp: ~N[2021-01-01 00:00:10],
+ scroll_depth: 50
+ ),
+ build(:imported_pages, page: "/blog"),
+ build(:imported_visitors)
+ ])
+
+ conn =
+ post(conn, "/api/v2/query", %{
+ "site_id" => site.domain,
+ "metrics" => ["visitors", "events", "conversion_rate"],
+ "date_range" => "all",
+ "include" => %{"imports" => true},
+ "dimensions" => ["event:goal"]
+ })
+
+ assert %{
+ "results" => results,
+ "meta" => %{"imports_included" => true}
+ } = json_response(conn, 200)
+
+ assert results == [
+ %{"dimensions" => ["Visit /blog**"], "metrics" => [2, 2, 100.0]},
+ %{"dimensions" => ["Scroll /blog 50"], "metrics" => [1, 0, 50.0]}
+ ]
end
end
end
diff --git a/test/plausible_web/controllers/api/stats_controller/conversions_test.exs b/test/plausible_web/controllers/api/stats_controller/conversions_test.exs
index 738c5b6e6b..fe72cb1f46 100644
--- a/test/plausible_web/controllers/api/stats_controller/conversions_test.exs
+++ b/test/plausible_web/controllers/api/stats_controller/conversions_test.exs
@@ -7,7 +7,10 @@ defmodule PlausibleWeb.Api.StatsController.ConversionsTest do
describe "GET /api/stats/:domain/conversions" do
setup [:create_user, :log_in, :create_site]
- test "returns mixed conversions in ordered by count", %{conn: conn, site: site} do
+ test "returns mixed pageview and custom event goal conversions ordered by count", %{
+ conn: conn,
+ site: site
+ } do
populate_stats(site, [
build(:pageview, pathname: "/"),
build(:pageview, pathname: "/"),
@@ -25,7 +28,8 @@ defmodule PlausibleWeb.Api.StatsController.ConversionsTest do
name: "Signup",
"meta.key": ["variant"],
"meta.value": ["B"]
- )
+ ),
+ build(:event, name: "Signup")
])
insert(:goal, %{site: site, page_path: "/register"})
@@ -36,15 +40,107 @@ defmodule PlausibleWeb.Api.StatsController.ConversionsTest do
assert json_response(conn, 200)["results"] == [
%{
"name" => "Signup",
- "visitors" => 2,
- "events" => 3,
- "conversion_rate" => 33.3
+ "visitors" => 3,
+ "events" => 4,
+ "conversion_rate" => 42.9
},
%{
"name" => "Visit /register",
"visitors" => 2,
"events" => 2,
- "conversion_rate" => 33.3
+ "conversion_rate" => 28.6
+ }
+ ]
+ end
+
+ test "returns page scroll goals ordered by count", %{conn: conn, site: site} do
+ populate_stats(site, [
+ # user 1: /blog -> /another -> blog/posts/1
+ build(:pageview, user_id: 1, pathname: "/blog", timestamp: ~N[2020-01-01 00:00:00]),
+ build(:pageleave,
+ user_id: 1,
+ pathname: "/blog",
+ timestamp: ~N[2020-01-01 00:01:00],
+ scroll_depth: 20
+ ),
+ build(:pageview, user_id: 1, pathname: "/another", timestamp: ~N[2020-01-01 00:01:00]),
+ build(:pageleave,
+ user_id: 1,
+ pathname: "/another",
+ timestamp: ~N[2020-01-01 00:02:00],
+ scroll_depth: 100
+ ),
+ build(:pageview,
+ user_id: 1,
+ pathname: "/blog/posts/1",
+ timestamp: ~N[2020-01-01 00:02:00]
+ ),
+ build(:pageleave,
+ user_id: 1,
+ pathname: "/blog/posts/1",
+ timestamp: ~N[2020-01-01 00:03:00],
+ scroll_depth: 55
+ ),
+ # user 2: /blog -> /blog/posts/1 -> /blog/posts/2
+ build(:pageview, user_id: 2, pathname: "/blog", timestamp: ~N[2020-01-01 00:00:00]),
+ build(:pageleave,
+ user_id: 2,
+ pathname: "/blog",
+ timestamp: ~N[2020-01-01 00:01:00],
+ scroll_depth: 60
+ ),
+ build(:pageview,
+ user_id: 2,
+ pathname: "/blog/posts/1",
+ timestamp: ~N[2020-01-01 00:02:00]
+ ),
+ build(:pageleave,
+ user_id: 2,
+ pathname: "/blog/posts/1",
+ timestamp: ~N[2020-01-01 00:03:00],
+ scroll_depth: 100
+ ),
+ build(:pageview,
+ user_id: 2,
+ pathname: "/blog/posts/2",
+ timestamp: ~N[2020-01-01 00:02:00]
+ ),
+ build(:pageleave,
+ user_id: 2,
+ pathname: "/blog/posts/2",
+ timestamp: ~N[2020-01-01 00:03:00],
+ scroll_depth: 100
+ )
+ ])
+
+ insert(:goal, %{
+ site: site,
+ page_path: "/blog/**",
+ scroll_threshold: 50,
+ display_name: "Scroll 50 /blog/**"
+ })
+
+ insert(:goal, %{
+ site: site,
+ page_path: "/blog/posts/1",
+ scroll_threshold: 75,
+ display_name: "Scroll 75 /blog/posts/1"
+ })
+
+ conn = get(conn, "/api/stats/#{site.domain}/conversions?period=day&date=2020-01-01")
+
+ assert json_response(conn, 200)["results"] == [
+ %{
+ "name" => "Scroll 50 /blog/**",
+ "visitors" => 2,
+ "events" => 0,
+ "conversion_rate" => 100.0
+ },
+ %{
+ "name" => "Scroll 75 /blog/posts/1",
+ "visitors" => 1,
+ "events" => 0,
+ "conversion_rate" => 50.0
}
]
end
diff --git a/test/plausible_web/controllers/api/stats_controller/top_stats_test.exs b/test/plausible_web/controllers/api/stats_controller/top_stats_test.exs
index 286a1073f6..5ec5aee0bf 100644
--- a/test/plausible_web/controllers/api/stats_controller/top_stats_test.exs
+++ b/test/plausible_web/controllers/api/stats_controller/top_stats_test.exs
@@ -1742,6 +1742,123 @@ defmodule PlausibleWeb.Api.StatsController.TopStatsTest do
refute "Average revenue" in metrics
refute "Total revenue" in metrics
end
+
+ test "page scroll goal filter", %{conn: conn, site: site} do
+ insert(:goal, site: site, page_path: "/blog", scroll_threshold: 50)
+
+ populate_stats(site, [
+ build(:pageview, user_id: 123, pathname: "/blog", timestamp: ~N[2021-01-01 00:00:00]),
+ build(:pageleave,
+ user_id: 123,
+ pathname: "/blog",
+ timestamp: ~N[2021-01-01 00:00:10],
+ scroll_depth: 60
+ ),
+ build(:pageview, user_id: 456, pathname: "/blog", timestamp: ~N[2021-01-01 00:00:00]),
+ build(:pageleave,
+ user_id: 456,
+ pathname: "/blog",
+ timestamp: ~N[2021-01-01 00:00:10],
+ scroll_depth: 40
+ )
+ ])
+
+ filters = Jason.encode!([[:is, "event:goal", ["Visit /blog"]]])
+ conn = get(conn, "/api/stats/#{site.domain}/top-stats?period=all&filters=#{filters}")
+ assert %{"top_stats" => top_stats} = json_response(conn, 200)
+
+ assert [unique_conversions, total_conversions, conversion_rate] = top_stats
+
+ assert %{"name" => "Unique conversions", "value" => 1} = unique_conversions
+ assert %{"name" => "Total conversions", "value" => 0} = total_conversions
+ assert %{"name" => "Conversion rate", "value" => 50.0} = conversion_rate
+ end
+
+ test "goal is page scroll OR custom event", %{conn: conn, site: site} do
+ insert(:goal, site: site, page_path: "/blog", scroll_threshold: 50)
+ insert(:goal, site: site, event_name: "Signup")
+
+ populate_stats(site, [
+ build(:pageview, user_id: 123, pathname: "/blog", timestamp: ~N[2021-01-01 00:00:00]),
+ build(:event,
+ user_id: 123,
+ pathname: "/blog",
+ name: "Signup",
+ timestamp: ~N[2021-01-01 00:00:05]
+ ),
+ build(:pageleave,
+ user_id: 123,
+ pathname: "/blog",
+ timestamp: ~N[2021-01-01 00:00:10],
+ scroll_depth: 60
+ ),
+ build(:pageview, user_id: 456, pathname: "/blog", timestamp: ~N[2021-01-01 00:00:00]),
+ build(:pageleave,
+ user_id: 456,
+ pathname: "/blog",
+ timestamp: ~N[2021-01-01 00:00:10],
+ scroll_depth: 40
+ ),
+ build(:pageview, user_id: 789, pathname: "/blog", timestamp: ~N[2021-01-01 00:00:00]),
+ build(:pageleave,
+ user_id: 789,
+ pathname: "/blog",
+ timestamp: ~N[2021-01-01 00:00:10],
+ scroll_depth: 100
+ ),
+ build(:event, name: "Signup", timestamp: ~N[2021-01-01 00:01:00]),
+ build(:pageview, timestamp: ~N[2021-01-01 00:01:00])
+ ])
+
+ filters = Jason.encode!([[:is, "event:goal", ["Visit /blog", "Signup"]]])
+ conn = get(conn, "/api/stats/#{site.domain}/top-stats?period=all&filters=#{filters}")
+ assert %{"top_stats" => top_stats} = json_response(conn, 200)
+
+ assert [unique_conversions, total_conversions, conversion_rate] = top_stats
+
+ assert %{"name" => "Unique conversions", "value" => 3} = unique_conversions
+ assert %{"name" => "Total conversions", "value" => 2} = total_conversions
+ assert %{"name" => "Conversion rate", "value" => 60.0} = conversion_rate
+ end
+
+ test "goal is page scroll OR pageview goal", %{conn: conn, site: site} do
+ insert(:goal,
+ site: site,
+ page_path: "/blog**",
+ scroll_threshold: 50,
+ display_name: "Scroll 50 /blog**"
+ )
+
+ insert(:goal, site: site, page_path: "/blog")
+
+ populate_stats(site, [
+ build(:pageview, user_id: 123, pathname: "/blog", timestamp: ~N[2021-01-01 00:00:00]),
+ build(:pageleave,
+ user_id: 123,
+ pathname: "/blog",
+ timestamp: ~N[2021-01-01 00:00:10],
+ scroll_depth: 60
+ ),
+ build(:pageview, user_id: 456, pathname: "/blog", timestamp: ~N[2021-01-01 00:00:00]),
+ build(:pageleave,
+ user_id: 456,
+ pathname: "/blog",
+ timestamp: ~N[2021-01-01 00:00:10],
+ scroll_depth: 40
+ ),
+ build(:pageview, timestamp: ~N[2021-01-01 00:01:00])
+ ])
+
+ filters = Jason.encode!([[:is, "event:goal", ["Visit /blog", "Scroll 50 /blog**"]]])
+ conn = get(conn, "/api/stats/#{site.domain}/top-stats?period=all&filters=#{filters}")
+ assert %{"top_stats" => top_stats} = json_response(conn, 200)
+
+ assert [unique_conversions, total_conversions, conversion_rate] = top_stats
+
+ assert %{"name" => "Unique conversions", "value" => 2} = unique_conversions
+ assert %{"name" => "Total conversions", "value" => 2} = total_conversions
+ assert %{"name" => "Conversion rate", "value" => 66.7} = conversion_rate
+ end
end
describe "GET /api/stats/top-stats - with comparisons" do
diff --git a/test/plausible_web/live/goal_settings/form_test.exs b/test/plausible_web/live/goal_settings/form_test.exs
index 177cae03f4..fee362210a 100644
--- a/test/plausible_web/live/goal_settings/form_test.exs
+++ b/test/plausible_web/live/goal_settings/form_test.exs
@@ -88,6 +88,26 @@ defmodule PlausibleWeb.Live.GoalSettings.FormTest do
]
end
+ test "renders scroll_threshold input in pageview goal form if scroll_depth feature visible for site/user",
+ %{conn: conn, site: site} do
+ Plausible.Sites.set_scroll_depth_visible_at(site)
+
+ lv = get_liveview(conn, site)
+ lv |> element(~s/a#pageview-tab/) |> render_click()
+ html = render(lv)
+ input_names = html |> find("#pageviews-form input") |> Enum.map(&name_of/1)
+ assert "goal[scroll_threshold]" in input_names
+ end
+
+ test "does not render scroll_threshold input in pageview goal form if scroll_depth feature not visible for site/user",
+ %{conn: conn, site: site} do
+ lv = get_liveview(conn, site)
+ lv |> element(~s/a#pageview-tab/) |> render_click()
+ html = render(lv)
+ input_names = html |> find("#pageviews-form input") |> Enum.map(&name_of/1)
+ refute "goal[scroll_threshold]" in input_names
+ end
+
test "renders error on empty submission", %{conn: conn, site: site} do
lv = get_liveview(conn, site)
lv |> element("#goals-form-modalseq0 form") |> render_submit()