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
Implement core logic of dynamic funnels support (#6762)
Adrian Gruntkowski4becfc1f5214
diff --git a/.credo.exs b/.credo.exsindex 381ca5ae2f..b1627bd311 100644--- a/.credo.exs+++ b/.credo.exs@@ -97,7 +97,6 @@{Credo.Check.Readability.ModuleDoc, []},{Credo.Check.Readability.ModuleNames, []},{Credo.Check.Readability.ParenthesesInCondition, []},- {Credo.Check.Readability.ParenthesesOnZeroArityDefs, []},{Credo.Check.Readability.PipeIntoAnonymousFunctions, []},{Credo.Check.Readability.PredicateFunctionNames, []},{Credo.Check.Readability.PreferImplicitTry, []},diff --git a/extra/lib/plausible/funnel.ex b/extra/lib/plausible/funnel.exindex 1f504fa6ad..d6803274af 100644--- a/extra/lib/plausible/funnel.ex+++ b/extra/lib/plausible/funnel.ex@@ -10,14 +10,15 @@ defmodule Plausible.Funnel doThis module defines the database schema for storing funnelsand changeset helpers for enumerating the steps within.- Each step references a goal (either a Custom Event or Visit)- - see: `Plausible.Goal`.+ Each step can reference a goal (either a Custom Event or Visit;+ see `Plausible.Goal`) or it can be defined fully dynamically."""use Ecto.Schemaimport Ecto.Changesetalias Plausible.Funnel.Step+ alias Plausible.Funnel.DynamicStep@funnel_types [:sequential, :flexible, :strict]@@ -62,6 +63,8 @@ defmodule Plausible.Funnel dobelongs_to :site, Plausible.Site+ embeds_many :dynamic_steps, DynamicStep, on_replace: :delete+has_many :steps, Step,preload_order: [asc: :step_order@@ -72,25 +75,103 @@ defmodule Plausible.Funnel dotimestamps()end+ def goals(funnel) do+ funnel+ |> steps()+ |> Enum.map(&as_goal/1)+ end++ def steps(funnel) do+ funnel.steps+ |> Enum.concat(funnel.dynamic_steps)+ |> Enum.sort_by(& &1.step_order)+ end++ def as_goal(%Step{} = step), do: Step.as_goal(step)+ def as_goal(%DynamicStep{} = step), do: DynamicStep.as_goal(step)+def changeset(funnel \\ %__MODULE__{}, attrs \\ %{}) dofunnel|> cast(attrs, [:name, :funnel_type])|> validate_required([:name])|> set_funnel_type()|> put_steps(attrs[:steps] || attrs["steps"])- |> validate_length(:steps, min: @min_steps, max: @max_steps)+ |> validate_steps_length()|> unique_constraint(:name,name: :funnels_name_site_id_index)end- def put_steps(changeset, steps) do- steps- |> Enum.map(&Step.changeset(%Step{}, &1))- |> Enum.with_index(fn step, step_order ->- Ecto.Changeset.put_change(step, :step_order, step_order + 1)- end)- |> then(&Ecto.Changeset.put_assoc(changeset, :steps, &1))+ @spec swap(t(), Keyword.t()) :: t()+ def swap(funnel, opts) do+ funnel_type = Keyword.get(opts, :funnel_type, funnel.funnel_type)++ funnel+ |> Map.put(:funnel_type, funnel_type)+ |> swap_steps(Keyword.get(opts, :steps))+ end++ defp swap_steps(funnel, nil), do: funnel++ defp swap_steps(funnel, steps) when length(steps) >= @min_steps do+ {static_steps, dynamic_steps} =+ steps+ |> Enum.with_index(1)+ |> Enum.map(fn {step, idx} -> %{step | step_order: idx} end)+ |> Enum.split_with(fn+ %Step{} -> true+ _ -> false+ end)++ %{funnel | steps: static_steps, dynamic_steps: dynamic_steps}+ end++ defp put_steps(changeset, steps) do+ {static_steps, dynamic_steps} =+ steps+ |> Enum.with_index(1)+ |> Enum.map(fn {input, idx} ->+ input+ |> schema_by_input()+ |> build_step(input, idx)+ end)+ |> Enum.split_with(fn+ %{data: %Step{}} -> true+ _ -> false+ end)++ changeset+ |> Ecto.Changeset.put_assoc(:steps, static_steps)+ |> Ecto.Changeset.put_embed(:dynamic_steps, dynamic_steps)+ end++ defp validate_steps_length(changeset) do+ steps_count =+ length(Ecto.Changeset.get_assoc(changeset, :steps)) ++ length(Ecto.Changeset.get_embed(changeset, :dynamic_steps))++ cond do+ steps_count < @min_steps ->+ add_error(changeset, :steps, "should have at least #{@min_steps} item(s)")++ steps_count > @max_steps ->+ add_error(changeset, :steps, "should have no more than #{@max_steps} item(s)")++ true ->+ changeset+ end+ end++ defp schema_by_input(%Plausible.Goal{}), do: Step+ defp schema_by_input(%Step{}), do: Step+ defp schema_by_input(%{goal_id: _}), do: Step+ defp schema_by_input(%{"goal_id" => _}), do: Step+ defp schema_by_input(_), do: DynamicStep++ defp build_step(step_schema, params, step_order) do+ params+ |> step_schema.changeset()+ |> Ecto.Changeset.put_change(:step_order, step_order)enddefp set_funnel_type(%{valid?: true} = changeset) dodiff --git a/extra/lib/plausible/funnel/dynamic_step.ex b/extra/lib/plausible/funnel/dynamic_step.exnew file mode 100644index 0000000000..7dfd61d8cd--- /dev/null+++ b/extra/lib/plausible/funnel/dynamic_step.ex@@ -0,0 +1,73 @@+defmodule Plausible.Funnel.DynamicStep do+ @moduledoc """+ This module defines the database schema for a single Dynamic Funnel step.+ See: `Plausible.Funnel` for more information.+ """++ use Ecto.Schema+ import Ecto.Changeset++ @type t() :: %__MODULE__{}++ @fields [+ :event_name,+ :page_path,+ :scroll_threshold,+ :custom_props,+ :currency+ ]++ @primary_key false+ embedded_schema do+ field :step_order, :integer+ field :event_name, :string+ field :page_path, :string+ field :scroll_threshold, :integer, default: -1+ field :currency, Ecto.Enum, values: Money.Currency.known_current_currencies()++ field :custom_props, :map, default: %{}+ end++ @spec changeset(map() | t()) :: Ecto.Changeset.t()+ def changeset(step_or_attrs \\ %{})++ def changeset(%__MODULE__{} = step), do: change(step)++ def changeset(attrs) do+ %__MODULE__{}+ |> cast(attrs, @fields)+ |> Plausible.Goal.base_changeset()+ |> validate_page_or_custom_goal()+ end++ @spec as_goal(t()) :: Plausible.Goal.t()+ def as_goal(step) do+ %Plausible.Goal{+ id: 0,+ display_name: display_name(step),+ event_name: step.event_name,+ page_path: step.page_path,+ scroll_threshold: step.scroll_threshold,+ custom_props: step.custom_props,+ currency: step.currency+ }+ end++ defp validate_page_or_custom_goal(changeset) do+ if get_field(changeset, :event_name) && get_field(changeset, :page_path) do+ add_error(changeset, :event_name, "cannot co-exist with page_path")+ else+ changeset+ end+ end++ defp display_name(step) do+ case step do+ %{page_path: page_path} when is_binary(page_path) ->+ "Visit " <> page_path++ %{event_name: event_name} when is_binary(event_name) ->+ event_name+ end+ end+enddiff --git a/extra/lib/plausible/funnel/step.ex b/extra/lib/plausible/funnel/step.exindex a25014cf76..01d63701df 100644--- a/extra/lib/plausible/funnel/step.ex+++ b/extra/lib/plausible/funnel/step.ex@@ -15,14 +15,17 @@ defmodule Plausible.Funnel.Step dotimestamps()end- def changeset(step, goal_or_attrs \\ %{})+ @spec changeset(map() | Plausible.Goal.t() | t()) :: Ecto.Changeset.t()+ def changeset(goal_step_or_attrs \\ %{})- def changeset(step, %Plausible.Goal{id: goal_id}) do- changeset(step, %{goal_id: goal_id})+ def changeset(%__MODULE__{} = step), do: change(step)++ def changeset(%Plausible.Goal{id: goal_id}) do+ changeset(%{goal_id: goal_id})end- def changeset(step, attrs) do- step+ def changeset(attrs) do+ %__MODULE__{}|> cast(attrs, [:goal_id])|> cast_assoc(:goal)|> validate_required([:goal_id])@@ -30,4 +33,15 @@ defmodule Plausible.Funnel.Step doname: :funnel_steps_goal_id_funnel_id_index)end++ @spec as_goal(t()) :: Plausible.Goal.t()+ def as_goal(step) do+ case step.goal do+ %Plausible.Goal{} = g ->+ g++ other ->+ raise "Expected goal, got: #{inspect(other)}"+ end+ endenddiff --git a/extra/lib/plausible/funnels.ex b/extra/lib/plausible/funnels.exindex a089dbf7e6..fd5d896751 100644--- a/extra/lib/plausible/funnels.ex+++ b/extra/lib/plausible/funnels.ex@@ -83,9 +83,13 @@ defmodule Plausible.Funnels do@spec ephemeral_definition(Plausible.Site.t(), String.t(), [map()], Keyword.t()) :: Funnel.t()def ephemeral_definition(site, name, steps, opts \\ []) do- site- |> create_changeset(name, steps, opts)- |> Ecto.Changeset.apply_changes()+ changeset = create_changeset(site, name, steps, opts)++ # Sanity check to ensure the definition is valid.+ # Should be ensured earlier already.+ true = changeset.valid?++ Ecto.Changeset.apply_changes(changeset)end@spec list(Plausible.Site.t()) :: [@@ -94,11 +98,16 @@ defmodule Plausible.Funnels dodef list(%Plausible.Site{} = site) doq =from(f in Funnel,- inner_join: steps in assoc(f, :steps),+ left_join: steps in assoc(f, :steps),where: f.site_id == ^site.id,group_by: f.id,order_by: [desc: :id],- select: %{name: f.name, id: f.id, steps_count: count(steps)}+ select: %{+ name: f.name,+ id: f.id,+ steps_count:+ count(steps) + fragment("coalesce(jsonb_array_length(?), 0)", f.dynamic_steps)+ })Repo.all(q)@@ -137,9 +146,7 @@ defmodule Plausible.Funnels do@spec with_goals_query(Plausible.Site.t()) :: Ecto.Query.t()def with_goals_query(site) dofrom(f in Funnel,- inner_join: steps in assoc(f, :steps),where: f.site_id == ^site.id,- group_by: f.id,order_by: [desc: :id],preload: [steps: :goal])@@ -148,8 +155,8 @@ defmodule Plausible.Funnels dodefp base_get_query(site_id) dofrom(f in Funnel,where: f.site_id == ^site_id,- inner_join: steps in assoc(f, :steps),- inner_join: goal in assoc(steps, :goal),+ left_join: steps in assoc(f, :steps),+ left_join: goal in assoc(steps, :goal),order_by: steps.step_order,preload: [steps: {steps, goal: goal}diff --git a/extra/lib/plausible/stats/funnel.ex b/extra/lib/plausible/stats/funnel.exindex e572bf4822..ba18393754 100644--- a/extra/lib/plausible/stats/funnel.ex+++ b/extra/lib/plausible/stats/funnel.ex@@ -30,7 +30,7 @@ defmodule Plausible.Stats.Funnel doenddef funnel(site, query, %Funnel{} = funnel) do- revenue_steps = revenue_steps(site, funnel.steps)+ revenue_steps = revenue_steps(site, Funnel.steps(funnel))comparison_funnel =if query.comparison_utc_time_range do@@ -61,9 +61,9 @@ defmodule Plausible.Stats.Funnel doenddefp recompute_flexible(final_funnel, query, site, %{funnel_type: :flexible} = funnel) do- if length(funnel.steps) > 2 do+ if length(Funnel.steps(funnel)) > 2 doshort_funnel = convert_to_first_and_last(funnel)- revenue_steps = revenue_steps(site, short_funnel.steps)+ revenue_steps = revenue_steps(site, Funnel.steps(short_funnel))[_, last_step] =query@@ -82,22 +82,20 @@ defmodule Plausible.Stats.Funnel dodo: final_funneldefp convert_to_first_and_last(funnel) do- first = List.first(funnel.steps)- last = %{List.last(funnel.steps) | step_order: 2}-- %{funnel | funnel_type: :sequential, steps: [first, last]}+ steps = Funnel.steps(funnel)+ Funnel.swap(funnel, funnel_type: :sequential, steps: [List.first(steps), List.last(steps)])enddefp revenue_steps(site, steps) doif Revenue.available?(site) do- Enum.reject(steps, &is_nil(&1.goal.currency))+ Enum.reject(steps, &is_nil(Funnel.as_goal(&1).currency))else[]endenddefp compute(query, funnel, revenue_steps) do- goals = Enum.map(funnel.steps, & &1.goal)+ goals = Funnel.goals(funnel)funnel_data =query@@ -155,7 +153,7 @@ defmodule Plausible.Stats.Funnel dodefp select_user_revenue(db_query, revenue_steps) dosums =Map.new(revenue_steps, fn step ->- goal_condition = Plausible.Stats.Goals.goal_condition(step.goal)+ goal_condition = Plausible.Stats.Goals.goal_condition(Funnel.as_goal(step)){revenue_key(step),dynamic([e], fragment("sumIf(?, ?)", e.revenue_reporting_amount, ^goal_condition))}@@ -203,8 +201,8 @@ defmodule Plausible.Stats.Funnel dodefp select_funnel(db_query, funnel_definition) dowindow_funnel_steps =- Enum.reduce(funnel_definition.steps, nil, fn step, acc ->- goal_condition = Plausible.Stats.Goals.goal_condition(step.goal)+ Enum.reduce(Funnel.goals(funnel_definition), nil, fn goal, acc ->+ goal_condition = Plausible.Stats.Goals.goal_condition(goal)if acc dodynamic([q], fragment("?, ?", ^acc, ^goal_condition))@@ -246,10 +244,10 @@ defmodule Plausible.Stats.Funnel do# In case ClickHouse returns 0-index funnel result, we're going to ignore it# anyway, since we fold over steps as per definition, that are always# indexed starting from 1.- max_step = Enum.max_by(funnel.steps, & &1.step_order).step_order+ max_step = Enum.max_by(Funnel.steps(funnel), & &1.step_order).step_orderfunnel- |> Map.fetch!(:steps)+ |> Funnel.steps()|> Enum.reduce({nil, nil, []}, fn step, {total_visitors, visitors_at_previous, acc} -># first step contains the total number of all visitors qualifying for the funnel,# with each subsequent step needing to accumulate sum of the previous one(s)@@ -280,7 +278,7 @@ defmodule Plausible.Stats.Funnel doconversion_rate: conversion_rate,conversion_rate_step: conversion_rate_step,visitors: visitors_at_step,- label: to_string(step.goal)+ label: to_string(Funnel.as_goal(step))}|> Map.merge(revenue_metrics(step, revenue_by_step, visitors_at_step))@@ -293,7 +291,7 @@ defmodule Plausible.Stats.Funnel dodefp revenue_metrics(step, revenue_by_step, visitors_at_step) docase Map.fetch(revenue_by_step, step.step_order) do{:ok, revenue} ->- currency = step.goal.currency+ currency = Funnel.as_goal(step).currency%{revenue: Revenue.format_revenue_metric(revenue, currency),diff --git a/extra/lib/plausible_web/live/funnel_settings/form.ex b/extra/lib/plausible_web/live/funnel_settings/form.exindex de4450c71e..faa6c1d895 100644--- a/extra/lib/plausible_web/live/funnel_settings/form.ex+++ b/extra/lib/plausible_web/live/funnel_settings/form.ex@@ -34,7 +34,15 @@ defmodule PlausibleWeb.Live.FunnelSettings.Form do{goal.id,struct!(Plausible.Goal,- Map.take(goal, [:id, :display_name, :event_name, :page_path, :currency])+ Map.take(goal, [+ :id,+ :display_name,+ :event_name,+ :page_path,+ :currency,+ :scroll_threshold,+ :custom_props+ ]))}end)@@ -415,7 +423,10 @@ defmodule PlausibleWeb.Live.FunnelSettings.Form do"goal" => %{"id" => goal.id,"event_name" => goal.event_name,- "page_path" => goal.page_path+ "page_path" => goal.page_path,+ "scroll_threshold" => goal.scroll_threshold,+ "currency" => goal.currency,+ "custom_props" => goal.custom_props}}end)@@ -482,8 +493,8 @@ defmodule PlausibleWeb.Live.FunnelSettings.Form doenddefp find_preselected(%Funnel{} = funnel, false, idx) do- if step = Enum.at(funnel.steps, idx - 1) do- {step.goal.id, to_string(step.goal)}+ if goal = Enum.at(Funnel.goals(funnel), idx - 1) do+ {goal.id, to_string(goal)}endend@@ -491,18 +502,16 @@ defmodule PlausibleWeb.Live.FunnelSettings.Form dodefp prepare_socket(socket, site, funnel_id) when is_integer(funnel_id) dofunnel = Funnels.get(site.id, funnel_id)+ goals = Funnel.goals(funnel)form =funnel- |> Funnels.edit_changeset(- funnel.name,- Enum.map(funnel.steps, &%{goal_id: &1.goal.id})- )+ |> Funnels.edit_changeset(funnel.name, goals)|> to_form()selections_made =- Enum.reduce(Enum.with_index(funnel.steps, 1), %{}, fn {step, idx}, acc ->- Map.put(acc, "step-#{idx}", step.goal)+ Enum.reduce(Enum.with_index(goals, 1), %{}, fn {goal, idx}, acc ->+ Map.put(acc, "step-#{idx}", goal)end)socket =@@ -513,7 +522,7 @@ defmodule PlausibleWeb.Live.FunnelSettings.Form dofunnel_type: funnel.funnel_type,funnel_modified?: false,selections_made: selections_made,- step_ids: Enum.to_list(1..Enum.count(funnel.steps))+ step_ids: Enum.to_list(1..Enum.count(goals)))evaluate_funnel(socket)diff --git a/extra/lib/plausible_web/plugins/api/views/funnel.ex b/extra/lib/plausible_web/plugins/api/views/funnel.exindex 6b544c2942..f5e960e63b 100644--- a/extra/lib/plausible_web/plugins/api/views/funnel.ex+++ b/extra/lib/plausible_web/plugins/api/views/funnel.ex@@ -46,7 +46,7 @@ defmodule PlausibleWeb.Plugins.API.Views.Funnel doauthorized_site: site}) do- goals = Enum.map(funnel.steps, & &1.goal)+ goals = Plausible.Funnel.goals(funnel)%{funnel: %{diff --git a/lib/plausible/goal.ex b/lib/plausible/goal.exindex 1f1a58a087..202b6bf851 100644--- a/lib/plausible/goal.ex+++ b/lib/plausible/goal.ex@@ -74,29 +74,34 @@ defmodule Plausible.Goal dodef changeset(goal, attrs \\ %{}) dogoal|> cast(attrs, @fields)- |> update_leading_slash()- |> validate_event_name_and_page_path()- |> validate_page_path_for_scroll_goal()+ |> base_changeset()|> maybe_put_display_name()- |> validate_change(:custom_props, &validate_custom_props/2)|> unique_constraint(:display_name, name: :goals_display_name_unique)|> unique_constraint(:event_name, name: :goals_event_config_unique)|> unique_constraint([:page_path, :scroll_threshold],name: :goals_pageview_config_unique)+ |> check_constraint(:event_name,+ name: :check_event_name_or_page_path,+ message: "cannot co-exist with page_path"+ )+ |> prevent_currency_change()+ |> prevent_special_goal_renames()+ end++ def base_changeset(changeset) do+ changeset+ |> update_leading_slash()+ |> validate_event_name_and_page_path()+ |> validate_page_path_for_scroll_goal()+ |> validate_change(:custom_props, &validate_custom_props/2)|> 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"- )|> maybe_drop_currency()- |> prevent_currency_change()- |> prevent_special_goal_renames()end@spec display_name(t()) :: String.t()diff --git a/lib/plausible/goals/goals.ex b/lib/plausible/goals/goals.exindex 337e2918bf..192e6a5fc6 100644--- a/lib/plausible/goals/goals.ex+++ b/lib/plausible/goals/goals.ex@@ -9,9 +9,9 @@ defmodule Plausible.Goals doimport Ecto.Query- alias Plausible.Goal- alias Ecto.Multialias Ecto.Changeset+ alias Ecto.Multi+ alias Plausible.Goal@max_goals_per_site 1_000@spec max_goals_per_site(Keyword.t()) :: pos_integer()@@ -239,40 +239,50 @@ defmodule Plausible.Goals dodefp delete_matching(base_query) dogoal_query = on_ee(do: preload(base_query, funnels: :steps), else: base_query)- result =+ multi =Multi.new()|> Multi.one(:goal,goal_query)- |> Multi.run(:funnel_ids_to_wipe, fn- _, %{goal: nil} ->- {:error, :not_found}-- _, %{goal: %{funnels: []}} ->- {:ok, []}-- _, %{goal: %{funnels: funnels}} ->- funnels_to_wipe =- funnels- |> Enum.filter(&(Enum.count(&1.steps) <= Funnel.Const.min_steps()))- |> Enum.map(& &1.id)-- {:ok, funnels_to_wipe}- end)- |> Multi.merge(fn- %{funnel_ids_to_wipe: []} ->- Ecto.Multi.new()-- %{funnel_ids_to_wipe: [_ | _] = funnel_ids} ->- Ecto.Multi.new()- |> Multi.delete_all(- :delete_funnels,- from(f in "funnels",- where: f.id in ^funnel_ids++ multi =+ on_ee do+ multi+ |> Multi.run(:funnel_ids_to_wipe, fn+ _, %{goal: nil} ->+ {:error, :not_found}++ _, %{goal: %{funnels: []}} ->+ {:ok, []}++ _, %{goal: %{funnels: funnels}} ->+ funnels_to_wipe =+ funnels+ |> Enum.filter(&(length(Funnel.steps(&1)) <= Funnel.Const.min_steps()))+ |> Enum.map(& &1.id)++ {:ok, funnels_to_wipe}+ end)+ |> Multi.merge(fn+ %{funnel_ids_to_wipe: []} ->+ Ecto.Multi.new()++ %{funnel_ids_to_wipe: [_ | _] = funnel_ids} ->+ Ecto.Multi.new()+ |> Multi.delete_all(+ :delete_funnels,+ from(f in "funnels",+ where: f.id in ^funnel_ids+ ))- )- end)+ end)+ else+ multi+ end++ result =+ multi|> Multi.delete_all(:delete_goals, fn _ -> base_query end)|> Repo.transaction()diff --git a/test/plausible/funnel/dynamic_step_test.exs b/test/plausible/funnel/dynamic_step_test.exsnew file mode 100644index 0000000000..349a7805a1--- /dev/null+++ b/test/plausible/funnel/dynamic_step_test.exs@@ -0,0 +1,143 @@+defmodule Plausible.Funnel.DynamicStepTest do+ use Plausible.DataCase, async: true++ @moduletag :ee_only++ on_ee do+ alias Plausible.Funnel+ alias Plausible.Funnel.DynamicStep+ alias Plausible.Funnels++ describe "changeset/1" do+ test "accepts existing struct and turns it to a changeset" do+ site = new_site()++ {:ok, funnel} =+ Funnels.create(site, "Example funnel", [+ %{"event_name" => "Signup"},+ %{"page_path" => "/checkout"}+ ])++ [s1, _] = Funnel.steps(funnel)++ assert %Ecto.Changeset{data: ^s1, valid?: true} = DynamicStep.changeset(s1)+ end++ test "accepts the same params as goal along with step_order" do+ changeset = DynamicStep.changeset(%{"event_name" => "CustomEvent"})+ assert changeset.valid?+ assert %DynamicStep{} == changeset.data+ assert get_change(changeset, :event_name) == "CustomEvent"+ assert is_nil(get_change(changeset, :page_path))++ changeset = DynamicStep.changeset(%{"event_name" => "Purchase", "currency" => "USD"})+ assert changeset.valid?+ assert %DynamicStep{} == changeset.data+ assert get_change(changeset, :event_name) == "Purchase"+ assert get_change(changeset, :currency) == :USD+ assert is_nil(get_change(changeset, :page_path))++ assert changeset =+ DynamicStep.changeset(%{+ "page_path" => "/blog",+ "custom_props" => %{"lang" => "de"}+ })++ assert changeset.valid?+ assert %DynamicStep{} == changeset.data+ assert get_change(changeset, :page_path) == "/blog"+ assert get_change(changeset, :custom_props) == %{"lang" => "de"}+ assert is_nil(get_change(changeset, :event_name))++ changeset = DynamicStep.changeset(%{"page_path" => "/blog", "scroll_threshold" => "90"})+ assert changeset.valid?+ assert %DynamicStep{} == changeset.data+ assert get_change(changeset, :page_path) == "/blog"+ assert get_change(changeset, :scroll_threshold) == 90+ assert is_nil(get_change(changeset, :event_name))+ end++ test "validates the input params" do+ changeset =+ DynamicStep.changeset(%{"event_name" => "CustomEvent", "page_path" => "/blog"})++ refute changeset.valid?+ assert {"cannot co-exist with page_path", _} = changeset.errors[:event_name]++ changeset =+ DynamicStep.changeset(%{"event_name" => "Purchase", "currency" => "WRONG"})++ refute changeset.valid?+ assert {"is invalid", _} = changeset.errors[:currency]++ changeset =+ DynamicStep.changeset(%{"page_path" => "/blog", "scroll_threshold" => "-100"})++ refute changeset.valid?+ assert {_, _} = changeset.errors[:scroll_threshold]++ changeset =+ DynamicStep.changeset(%{"page_path" => "/blog", "custom_props" => %{"bad" => -1}})++ refute changeset.valid?+ assert {_, _} = changeset.errors[:custom_props]+ end+ end++ describe "as_goal/1" do+ test "converts a dynamic step to a goal" do+ site = new_site()++ {:ok, funnel} =+ Funnels.create(site, "Example funnel", [+ %{"page_path" => "/blog", "scroll_threshold" => 90},+ %{"event_name" => "Signup"},+ %{"page_path" => "/checkout", "custom_props" => %{"lang" => "de"}},+ %{"event_name" => "Purchase", "currency" => "USD"}+ ])++ [s1, s2, s3, s4] = Funnel.steps(funnel)++ assert DynamicStep.as_goal(s1) == %Plausible.Goal{+ id: 0,+ display_name: "Visit /blog",+ event_name: nil,+ page_path: "/blog",+ scroll_threshold: 90,+ custom_props: %{},+ currency: nil+ }++ assert DynamicStep.as_goal(s2) == %Plausible.Goal{+ id: 0,+ display_name: "Signup",+ event_name: "Signup",+ page_path: nil,+ scroll_threshold: -1,+ custom_props: %{},+ currency: nil+ }++ assert DynamicStep.as_goal(s3) == %Plausible.Goal{+ id: 0,+ display_name: "Visit /checkout",+ event_name: nil,+ page_path: "/checkout",+ scroll_threshold: -1,+ custom_props: %{"lang" => "de"},+ currency: nil+ }++ assert DynamicStep.as_goal(s4) == %Plausible.Goal{+ id: 0,+ display_name: "Purchase",+ event_name: "Purchase",+ page_path: nil,+ scroll_threshold: -1,+ custom_props: %{},+ currency: :USD+ }+ end+ end+ end+enddiff --git a/test/plausible/funnel_test.exs b/test/plausible/funnel_test.exsnew file mode 100644index 0000000000..1b94d5e894--- /dev/null+++ b/test/plausible/funnel_test.exs@@ -0,0 +1,358 @@+defmodule Plausible.FunnelTest do+ use Plausible.DataCase, async: true++ @moduletag :ee_only++ on_ee do+ alias Plausible.Funnel+ alias Plausible.Funnels+ alias Plausible.Goal+ alias Plausible.Goals+ alias Plausible.Repo++ describe "changeset/2" do+ test "accepts and splits a set of static and dynamic steps" do+ site = new_site()++ {:ok, g1} =+ Goals.create(site, %{+ "page_path" => "/go/to/blog/**",+ "custom_props" => %{"lang" => "de"}+ })++ {:ok, g2} =+ Goals.create(site, %{+ "event_name" => "Purchase",+ "currency" => "USD"+ })++ params = %{+ name: "Example funnel",+ steps: [+ %{+ "goal_id" => g1.id,+ "goal" => %{+ "id" => g1.id,+ "event_name" => g1.event_name,+ "page_path" => g1.page_path,+ "custom_props" => %{"lang" => "de"}+ }+ },+ %{"event_name" => "Signup"},+ %{"page_path" => "/checkout", "scroll_threshold" => 90},+ %{+ "goal_id" => g2.id,+ "goal" => %{+ "id" => g2.id,+ "event_name" => g2.event_name,+ "page_path" => g2.page_path+ }+ }+ ]+ }++ changeset = Funnel.changeset(%Funnel{site_id: site.id}, params)++ assert changeset.valid?++ funnel = apply_changes(changeset)++ assert_matches [+ %Funnel.Step{+ step_order: 1,+ goal_id: ^g1.id,+ goal: %{+ event_name: ^g1.event_name,+ page_path: ^g1.page_path,+ custom_props: %{"lang" => "de"}+ }+ },+ %Funnel.Step{+ step_order: 4,+ goal_id: ^g2.id,+ goal: %{event_name: ^g2.event_name, page_path: ^g2.page_path}+ }+ ] = funnel.steps++ assert_matches [+ %Funnel.DynamicStep{+ step_order: 2,+ event_name: "Signup",+ page_path: nil+ },+ %Funnel.DynamicStep{+ step_order: 3,+ event_name: nil,+ page_path: "/checkout",+ scroll_threshold: 90+ }+ ] = funnel.dynamic_steps+ end++ test "handles empty steps list gracefully" do+ site = new_site()+ params = %{name: "Example funnel", steps: []}++ changeset = Funnel.changeset(%Funnel{site_id: site.id}, params)++ refute changeset.valid?+ end++ test "handles static steps only" do+ site = new_site()+ {:ok, g1} = Goals.create(site, %{"page_path" => "/go/to/blog/**"})+ {:ok, g2} = Goals.create(site, %{"event_name" => "Signup"})+ {:ok, g3} = Goals.create(site, %{"page_path" => "/checkout"})++ params = %{+ name: "Example funnel",+ steps: [+ %{+ "goal_id" => g1.id,+ "goal" => %{+ "id" => g1.id,+ "event_name" => g1.event_name,+ "page_path" => g1.page_path+ }+ },+ %{+ "goal_id" => g2.id,+ "goal" => %{+ "id" => g2.id,+ "event_name" => g2.event_name,+ "page_path" => g2.page_path+ }+ },+ %{+ "goal_id" => g3.id,+ "goal" => %{+ "id" => g3.id,+ "event_name" => g3.event_name,+ "page_path" => g3.page_path+ }+ }+ ]+ }++ changeset = Funnel.changeset(%Funnel{site_id: site.id}, params)++ assert changeset.valid?++ funnel = apply_changes(changeset)++ assert_matches [+ %Funnel.Step{+ step_order: 1,+ goal_id: ^g1.id,+ goal: %{event_name: ^g1.event_name, page_path: ^g1.page_path}+ },+ %Funnel.Step{+ step_order: 2,+ goal_id: ^g2.id,+ goal: %{event_name: ^g2.event_name, page_path: ^g2.page_path}+ },+ %Funnel.Step{+ step_order: 3,+ goal_id: ^g3.id,+ goal: %{event_name: ^g3.event_name, page_path: ^g3.page_path}+ }+ ] = funnel.steps++ assert_matches [] = funnel.dynamic_steps+ end++ test "handles dynamic steps only" do+ site = new_site()++ params = %{+ name: "Example funnel",+ steps: [+ %{"page_path" => "/go/to/blog/**"},+ %{"event_name" => "Signup"},+ %{"page_path" => "/checkout"}+ ]+ }++ changeset = Funnel.changeset(%Funnel{site_id: site.id}, params)++ assert changeset.valid?++ funnel = apply_changes(changeset)++ assert_matches [] = funnel.steps++ assert_matches [+ %Funnel.DynamicStep{+ step_order: 1,+ event_name: nil,+ page_path: "/go/to/blog/**"+ },+ %Funnel.DynamicStep{+ step_order: 2,+ event_name: "Signup",+ page_path: nil+ },+ %Funnel.DynamicStep{+ step_order: 3,+ event_name: nil,+ page_path: "/checkout"+ }+ ] = funnel.dynamic_steps+ end++ test "properly handles validation errors in steps" do+ site = new_site()++ params = %{+ name: "Example funnel",+ steps: [+ %{"page_path" => "/go/to/blog/**"},+ %{"event_name" => "Signup", "page_path" => "/should/be/empty"},+ %{"page_path" => "/checkout"}+ ]+ }++ changeset = Funnel.changeset(%Funnel{site_id: site.id}, params)++ refute changeset.valid?++ assert [_, %Ecto.Changeset{valid?: false}, _] = get_change(changeset, :dynamic_steps)+ end+ end++ describe "steps/1" do+ test "returns a continuous and ordered list of mixed dynamic and static steps" do+ site = new_site()+ {:ok, g} = Goals.create(site, %{"page_path" => "/go/to/blog/**"})++ {:ok, funnel} =+ Funnels.create(site, "Example funnel", [+ %{"goal_id" => g.id},+ %{"event_name" => "Signup"},+ %{"page_path" => "/checkout"}+ ])++ assert [s1, s2, s3] = Funnel.steps(funnel)++ assert_matches %Funnel.Step{goal_id: ^g.id, step_order: 1} = s1+ assert %Funnel.DynamicStep{event_name: "Signup", step_order: 2} = s2+ assert %Funnel.DynamicStep{page_path: "/checkout", step_order: 3} = s3+ end+ end++ describe "goals/1" do+ test "returns a continuous and ordered list of goals extracted from mixed dynamic and static steps" do+ site = new_site()+ {:ok, g} = Goals.create(site, %{"page_path" => "/go/to/blog/**"})++ {:ok, funnel} =+ Funnels.create(site, "Example funnel", [+ %{"goal_id" => g.id},+ %{"event_name" => "Signup"},+ %{"page_path" => "/checkout"}+ ])++ funnel = Repo.preload(funnel, steps: :goal)++ assert [g1, g2, g3] = Funnel.goals(funnel)++ assert_matches %Goal{id: ^g.id, event_name: ^g.event_name, page_path: ^g.page_path} = g1+ assert_matches %Goal{id: 0, event_name: "Signup", page_path: nil} = g2+ assert_matches %Goal{id: 0, event_name: nil, page_path: "/checkout"} = g3+ end++ test "crashes when static steps do not have goals preloaded" do+ site = new_site()+ {:ok, g} = Goals.create(site, %{"page_path" => "/go/to/blog/**"})++ {:ok, funnel} =+ Funnels.create(site, "Example funnel", [+ %{"goal_id" => g.id},+ %{"event_name" => "Signup"}+ ])++ assert_raise RuntimeError, ~r/Expected goal, got:/, fn ->+ Funnel.goals(funnel)+ end+ end+ end++ describe "swap/2" do+ setup do+ site = new_site()+ {:ok, g1} = Goals.create(site, %{"page_path" => "/go/to/blog/**"})+ {:ok, g2} = Goals.create(site, %{"event_name" => "Purchase"})++ {:ok, funnel} =+ Funnels.create(site, "Example funnel", [+ %{"goal_id" => g1.id},+ %{"event_name" => "Signup"},+ %{"page_path" => "/checkout"},+ %{"goal_id" => g2.id}+ ])++ {:ok, funnel: funnel}+ end++ test "swaps a mix of static and dynamic steps in the funnel", %{funnel: funnel} do+ [s1, _s2, s3, _s4] = Funnel.steps(funnel)++ swapped = Funnel.swap(funnel, steps: [s1, s3])++ assert [sw1] = swapped.steps+ assert [sw2] = swapped.dynamic_steps++ assert sw1.goal_id == s1.goal_id+ assert sw1.step_order == 1+ assert sw2.page_path == "/checkout"+ assert sw2.step_order == 2+ end++ test "handles no steps to swap gracefully", %{funnel: funnel} do+ swapped = Funnel.swap(funnel, [])++ assert funnel == swapped+ end++ test "crashes on too few steps passed for swapping", %{funnel: funnel} do+ [s1, _s2, _s3, _s4] = Funnel.steps(funnel)++ assert_raise FunctionClauseError, fn ->+ Funnel.swap(funnel, steps: [s1])+ end++ assert_raise FunctionClauseError, fn ->+ Funnel.swap(funnel, steps: [])+ end+ end++ test "handles static steps only", %{funnel: funnel} do+ [s1, _s2, _s3, s4] = Funnel.steps(funnel)++ swapped = Funnel.swap(funnel, steps: [s1, s4])++ assert [sw1, sw2] = swapped.steps+ assert [] = swapped.dynamic_steps++ assert sw1.goal_id == s1.goal_id+ assert sw1.step_order == 1+ assert sw2.goal_id == s4.goal_id+ assert sw2.step_order == 2+ end++ test "handles dynamic steps only", %{funnel: funnel} do+ [_s1, s2, s3, _s4] = Funnel.steps(funnel)++ swapped = Funnel.swap(funnel, steps: [s2, s3])++ assert [] = swapped.steps+ assert [sw1, sw2] = swapped.dynamic_steps++ assert sw1.event_name == "Signup"+ assert sw1.step_order == 1+ assert sw2.page_path == "/checkout"+ assert sw2.step_order == 2+ end+ end+ end+enddiff --git a/test/plausible/funnels_test.exs b/test/plausible/funnels_test.exsindex c69d0617d1..20f11ec269 100644--- a/test/plausible/funnels_test.exs+++ b/test/plausible/funnels_test.exs@@ -3,8 +3,9 @@ defmodule Plausible.FunnelsTest do@moduletag :ee_onlyon_ee do- alias Plausible.Goals+ alias Plausible.Funnelalias Plausible.Funnels+ alias Plausible.Goalsalias Plausible.Statsalias Plausible.Stats.QueryBuilder@@ -46,7 +47,7 @@ defmodule Plausible.FunnelsTest doassert funnel.funnel_type == :sequentialassert funnel.strict_order == falseassert funnel.first_and_last == false- assert [fg1, fg2, fg3] = funnel.steps+ assert [fg1, fg2, fg3] = Funnel.steps(funnel)assert fg1.goal_id == g1["goal_id"]assert fg2.goal_id == g2["goal_id"]@@ -57,6 +58,33 @@ defmodule Plausible.FunnelsTest doassert fg3.step_order == 3end+ test "create a mixed static/dynamic funnel", %{site: site, steps: [g1 | _]} do+ s2 = %{"event_name" => "Signup"}+ s3 = %{"page_path" => "/checkout"}++ {:ok, funnel} =+ Funnels.create(+ site,+ "From blog to signup and purchase",+ [g1, s2, s3]+ )++ assert funnel.inserted_at+ assert funnel.name == "From blog to signup and purchase"+ assert funnel.strict_order == false++ assert [%Funnel.Step{} = fg1, %Funnel.DynamicStep{} = fg2, %Funnel.DynamicStep{} = fg3] =+ Funnel.steps(funnel)++ assert fg1.goal_id == g1["goal_id"]+ assert fg2.event_name == s2["event_name"]+ assert fg3.page_path == s3["page_path"]++ assert fg1.step_order == 1+ assert fg2.step_order == 2+ assert fg3.step_order == 3+ end+test "update funnel", %{site: site, steps: [g1, g2, g3, g4, g5 | _]} do{:ok, funnel1} =Funnels.create(@@ -68,7 +96,7 @@ defmodule Plausible.FunnelsTest do{:ok, funnel2} = Funnels.update(funnel1, "Updated funnel", [g4, g5])assert funnel2.name == "Updated funnel"- assert [fg1, fg2] = funnel2.steps+ assert [fg1, fg2] = Funnel.steps(funnel2)assert fg1.goal_id == g4["goal_id"]assert fg2.goal_id == g5["goal_id"]@@ -225,7 +253,7 @@ defmodule Plausible.FunnelsTest doassert funnel = Funnels.get(site, funnel.id)assert funnel.name == "Lorem ipsum"- assert [%{step_order: 1}, %{step_order: 2}, %{step_order: 3}] = funnel.steps+ assert [%{step_order: 1}, %{step_order: 2}, %{step_order: 3}] = Funnel.steps(funnel)endtest "funnels can be deleted by id and a site", %{site: site, steps: [g1, g2 | _]} do@@ -288,6 +316,19 @@ defmodule Plausible.FunnelsTest doassert [%{name: "Funnel 2", steps_count: 3}, %{name: "Funnel 1", steps_count: 2}] =funnels_listend++ test "funnels with dynamic steps are listed correctly", %{+ site: site,+ steps: [g1, _g2, g3 | _]+ } do+ Funnels.create(site, "Funnel 1", [g3, %{"page_path" => "/go/to/blog/**"}])+ Funnels.create(site, "Funnel 2", [%{"event_name" => "Signup"}, g1, g3])++ funnels_list = Funnels.list(site)++ assert [%{name: "Funnel 2", steps_count: 3}, %{name: "Funnel 1", steps_count: 2}] =+ funnels_list+ endenddescribe "Plausible.Stats.Funnel" do@@ -377,6 +418,84 @@ defmodule Plausible.FunnelsTest do}} = funnel_dataend+ test "ephemeral definition creation crashes when passed data are invalid", %{site: site} do+ assert_raise MatchError, fn ->+ Funnels.ephemeral_definition(site, "Empty", [])+ end+ end++ test "mixed funnels can be evaluated per site within a time range against an ephemeral definition",+ %{+ site: site,+ goals: [g1, g2, g3 | _]+ } do+ funnel_definition =+ Funnels.ephemeral_definition(+ site,+ "From blog to signup and purchase",+ [+ %{+ "goal_id" => "#{g1.id}",+ "goal" => %{+ "id" => "#{g1.id}",+ "event_name" => g1.event_name,+ "page_path" => g1.page_path+ }+ },+ %{"event_name" => g2.event_name},+ %{"page_path" => g3.page_path}+ ]+ )++ populate_stats(site, [+ build(:pageview, pathname: "/irrelevant/page/not/in/funnel", user_id: 999),+ build(:pageview, pathname: "/go/to/blog/foo", user_id: 123),+ build(:event, name: "Signup", user_id: 123),+ build(:pageview, pathname: "/checkout", user_id: 123),+ build(:pageview, pathname: "/go/to/blog/bar", user_id: 666),+ build(:event, name: "Signup", user_id: 666)+ ])++ query = QueryBuilder.build!(site, input_date_range: :all)++ funnel_data = Stats.funnel(site, query, funnel_definition)++ assert {:ok,+ %{+ all_visitors: 3,+ entering_visitors: 2,+ entering_visitors_percentage: "66.67",+ never_entering_visitors: 1,+ never_entering_visitors_percentage: "33.33",+ steps: [+ %{+ label: "Visit /go/to/blog/**",+ visitors: 2,+ conversion_rate: "100",+ conversion_rate_step: "0",+ dropoff: 0,+ dropoff_percentage: "0"+ },+ %{+ label: "Signup",+ visitors: 2,+ conversion_rate: "100",+ conversion_rate_step: "100",+ dropoff: 0,+ dropoff_percentage: "0"+ },+ %{+ label: "Visit /checkout",+ visitors: 1,+ conversion_rate: "50",+ conversion_rate_step: "50",+ dropoff: 1,+ dropoff_percentage: "50"+ }+ ]+ }} = funnel_data+ end+test "funnels can be evaluated per site within a time range", %{site: site,steps: [g1, g2, g3 | _]@@ -537,6 +656,58 @@ defmodule Plausible.FunnelsTest doassert flexible_funnel_data.funnel_type == :flexibleend+ test "flexible mixed funnels count everyone who reaches first and last step in the last step",+ %{+ site: site,+ steps: [g1 | _]+ } do+ s2 = %{"event_name" => "Signup"}+ s3 = %{"page_path" => "/checkout"}++ {:ok, non_flexible_funnel} =+ Funnels.create(+ site,+ "From blog to signup and purchase",+ [g1, s2, s3]+ )++ {:ok, flexible_funnel} =+ Funnels.create(+ site,+ "Flexible from blog to signup and purchase",+ [g1, s2, s3],+ funnel_type: :flexible+ )++ populate_stats(site, [+ build(:pageview,+ pathname: "/go/to/blog/foo",+ user_id: 123,+ timestamp: ~N[2021-01-01 00:00:00]+ ),+ build(:pageview,+ pathname: "/checkout",+ user_id: 123,+ timestamp: ~N[2021-01-01 00:00:03]+ )+ ])++ query = QueryBuilder.build!(site, input_date_range: :all)++ {:ok, non_flexible_funnel_data} = Stats.funnel(site, query, non_flexible_funnel.id)+ {:ok, flexible_funnel_data} = Stats.funnel(site, query, flexible_funnel.id)++ assert Enum.at(non_flexible_funnel_data.steps, 0).visitors == 1+ assert Enum.at(non_flexible_funnel_data.steps, 1).visitors == 0+ assert Enum.at(non_flexible_funnel_data.steps, 2).visitors == 0+ assert Enum.at(flexible_funnel_data.steps, 0).visitors == 1+ assert Enum.at(flexible_funnel_data.steps, 1).visitors == 0+ assert Enum.at(flexible_funnel_data.steps, 2).visitors == 1++ assert non_flexible_funnel_data.funnel_type == :sequential+ assert flexible_funnel_data.funnel_type == :flexible+ end+test "funnels can be evaluated even where there are no visits yet", %{site: site,steps: [g1, g2, g3 | _]diff --git a/test/plausible/goals_test.exs b/test/plausible/goals_test.exsindex b8011e76dc..93d6657436 100644--- a/test/plausible/goals_test.exs+++ b/test/plausible/goals_test.exs@@ -749,6 +749,44 @@ defmodule Plausible.GoalsTest doassert_matches [%{id: ^g3.id}, %{id: ^g2.id}] = Goals.for_site(site)end+ test "funnel reduction accounts for dynamic funnel steps" do+ site = new_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, f1} =+ Plausible.Funnels.create(+ site,+ "Funnel 2 steps mixed",+ [+ %{"goal_id" => g1.id},+ %{"page_path" => "/5"}+ ]+ )++ {:ok, f2} =+ Plausible.Funnels.create(+ site,+ "Funnel 3 steps mixed",+ [+ %{"goal_id" => g1.id},+ %{"page_path" => "/4"},+ %{"event_name" => "Some event"}+ ]+ )++ :ok = Goals.delete(g1.id, site)++ refute Plausible.Funnels.get(site.id, f1.id)+ assert Repo.all(from(fs in Plausible.Funnel.Step, where: fs.funnel_id == ^f1.id)) == []++ assert f2 = Plausible.Funnels.get(site.id, f2.id)+ assert Enum.count(Plausible.Funnel.steps(f2)) == 2++ assert_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" dosite = new_site(){:ok, g1} = Goals.create(site, %{"page_path" => "/1"})diff --git a/test/plausible_web/controllers/api/stats_controller/funnels_test.exs b/test/plausible_web/controllers/api/stats_controller/funnels_test.exsindex 69f114b0d3..28f0021fe5 100644--- a/test/plausible_web/controllers/api/stats_controller/funnels_test.exs+++ b/test/plausible_web/controllers/api/stats_controller/funnels_test.exs@@ -618,6 +618,45 @@ defmodule PlausibleWeb.Api.StatsController.FunnelsTest do} = purchase_stepend+ test "reports revenue for a dynamic funnel without goals", %{conn: conn, site: site} do+ {:ok, funnel} =+ Plausible.Funnels.create(+ site,+ "Test funnel",+ [%{"page_path" => "/checkout"}, %{"event_name" => "Purchase", "currency" => "USD"}]+ )++ funnel = Plausible.Repo.reload!(funnel)++ populate_stats(site, [+ build(:pageview, pathname: "/checkout", user_id: @user_id),+ build(:pageview, pathname: "/checkout", user_id: @other_user_id),+ purchase(@user_id, "100"),+ purchase(@other_user_id, "300")+ ])++ assert [checkout_step, purchase_step] = funnel_steps(conn, site, funnel)++ refute Map.has_key?(checkout_step, "revenue")+ refute Map.has_key?(checkout_step, "revenue_per_visitor")++ assert %{+ "visitors" => 2,+ "revenue" => %{+ "short" => "$400.0",+ "long" => "$400.00",+ "value" => 400.0,+ "currency" => "USD"+ },+ "revenue_per_visitor" => %{+ "short" => "$200.0",+ "long" => "$200.00",+ "value" => 200.0,+ "currency" => "USD"+ }+ } = purchase_step+ end+test "divides the revenue by visitors, not by orders", %{conn: conn, site: site} do{:ok, funnel} = setup_checkout_funnel(site)diff --git a/test/plausible_web/live/funnel_settings_test.exs b/test/plausible_web/live/funnel_settings_test.exsindex c3a0c66c34..ee70571723 100644--- a/test/plausible_web/live/funnel_settings_test.exs+++ b/test/plausible_web/live/funnel_settings_test.exs@@ -538,6 +538,87 @@ defmodule PlausibleWeb.Live.FunnelSettingsTest doassert text_of_element(render(lv), ~s/#funnel-eval/) =~ "Last month conversion rate: 100%"end+ test "funnel evaluation respects additional properties like scroll threshold and custom props",+ %{+ conn: conn,+ site: site+ } do+ {:ok, g1} =+ Plausible.Goals.create(site, %{+ "page_path" => "/go/to/blog/**",+ "scroll_threshold" => 90+ })++ {:ok, g3} = Plausible.Goals.create(site, %{"event_name" => "Purchase"})++ {:ok, funnel} =+ Plausible.Funnels.create(+ site,+ "Mixed funnel",+ [+ %{"goal_id" => g1.id},+ %{"event_name" => "Signup", "custom_props" => %{"method" => "oauth"}},+ %{"goal_id" => g3.id}+ ]+ )++ ts =+ NaiveDateTime.utc_now()+ |> NaiveDateTime.truncate(:second)+ |> NaiveDateTime.add(-600, :second)++ populate_stats(site, [+ build(:pageview, pathname: "/go/to/blog/foo", user_id: 122, timestamp: ts),+ build(:engagement,+ pathname: "/go/to/blog/foo",+ scroll_depth: 92,+ user_id: 122,+ timestamp: NaiveDateTime.add(ts, 1, :second)+ ),+ build(:event,+ name: "Signup",+ "meta.key": ["method"],+ "meta.value": ["oauth"],+ user_id: 122,+ timestamp: NaiveDateTime.add(ts, 2, :second)+ ),+ build(:event,+ name: "Purchase",+ user_id: 122,+ timestamp: NaiveDateTime.add(ts, 3, :second)+ ),+ build(:pageview, pathname: "/go/to/blog/foo", user_id: 123, timestamp: ts),+ build(:engagement,+ pathname: "/go/to/blog/foo",+ scroll_depth: 98,+ user_id: 123,+ timestamp: NaiveDateTime.add(ts, 1, :second)+ ),+ build(:event,+ name: "Signup",+ "meta.key": ["method"],+ "meta.value": ["email"],+ user_id: 123,+ timestamp: NaiveDateTime.add(ts, 2, :second)+ ),+ build(:event,+ name: "Purchase",+ user_id: 123,+ timestamp: NaiveDateTime.add(ts, 3, :second)+ )+ ])++ lv = get_liveview(conn, site)++ lv+ |> element(~s/button[phx-click="edit-funnel"][phx-value-funnel-id="#{funnel.id}"]/)+ |> render_click()++ assert lv = find_live_child(lv, "funnels-form")++ assert text_of_element(render(lv), ~s/#funnel-eval/) =~ "Last month conversion rate: 50%"+ end+test "clicking save after editing the funnel, updates it", %{conn: conn,site: sitediff --git a/test/plausible_web/plugins/api/controllers/funnels_test.exs b/test/plausible_web/plugins/api/controllers/funnels_test.exsindex 90d0e68b4d..184302505a 100644--- a/test/plausible_web/plugins/api/controllers/funnels_test.exs+++ b/test/plausible_web/plugins/api/controllers/funnels_test.exs@@ -100,6 +100,40 @@ defmodule PlausibleWeb.Plugins.API.Controllers.FunnelsTest doassert_schema(s2, "Goal.Revenue", spec())assert_schema(s3, "Goal.CustomEvent", spec())end++ test "retrieves mixed funnel by ID", %{conn: conn, site: site, token: token} do+ {:ok, g1} = Plausible.Goals.create(site, %{"page_path" => "/product/123"})++ {:ok, funnel} =+ Plausible.Funnels.create(+ site,+ "Peek & buy",+ [+ g1,+ %{"event_name" => "Purchase", "currency" => "EUR"},+ %{"event_name" => "FiveStarReview"}+ ]+ )++ url = url(~p"/api/plugins/v1/funnels/#{funnel.id}")++ resp =+ conn+ |> authenticate(site.domain, token)+ |> get(url)+ |> json_response(200)++ schema = assert_schema(resp, "Funnel", spec())++ assert schema.funnel.id == funnel.id+ assert schema.funnel.name == "Peek & buy"+ [_, _, _] = schema.funnel.steps+ [s1, s2, s3] = resp |> Map.fetch!("funnel") |> Map.fetch!("steps")++ assert_schema(s1, "Goal.Pageview", spec())+ assert_schema(s2, "Goal.Revenue", spec())+ assert_schema(s3, "Goal.CustomEvent", spec())+ endenddescribe "get /funnels" do@@ -152,6 +186,32 @@ defmodule PlausibleWeb.Plugins.API.Controllers.FunnelsTest doassert Enum.count(resp.funnels) == 3end+ test "retrieves funnels with dynamic steps", %{conn: conn, site: site, token: token} do+ {:ok, g1} = Plausible.Goals.create(site, %{"page_path" => "/product/123"})++ {:ok, g3} = Plausible.Goals.create(site, %{"event_name" => "FiveStarReview"})++ for i <- 1..3 do+ {:ok, _} =+ Plausible.Funnels.create(+ site,+ "Funnel #{i}",+ [g1, %{"event_name" => "Purchase", "currency" => "EUR"}, g3]+ )+ end++ url = url(~p"/api/plugins/v1/funnels")++ resp =+ conn+ |> authenticate(site.domain, token)+ |> get(url)+ |> json_response(200)+ |> assert_schema("Funnel.ListResponse", spec())++ assert Enum.count(resp.funnels) == 3+ end+test "retrieves funnels with pagination", %{conn: conn, site: site, token: token} do{:ok, g1} = Plausible.Goals.create(site, %{"page_path" => "/product/123"})