From a356009af8cbdbf3c37670c9c9566cf76e640f4e Mon Sep 17 00:00:00 2001 From: hq1 Date: Tue, 13 Jun 2023 14:01:39 +0200 Subject: [PATCH] Implement backend core for funnels (#3023) * Implement Funnel schema * Implement Funnels context module lib/plausible/funnels.ex - restored from: dca93621 Mark Vini's suggestion applied 7d1eec75 Update lib/plausible/funnels.ex 028036ad Review comments b694ccd5 Express the guard nicely acd9c4f2 Prepare ephemeral funnel definitions so that users can test funnels aea4ebc4 Access Funnel min/max steps via the __using__/1 macro 51f0397d Implement deleting funnels cf228630 wip 1f6fe25d Add number of steps to funnels list 89f10ecb wip 950a18d9 Dirty funnel save e62bfce5 Refactor funnel result evaluation 88e38a33 Update funnel tests bf47f654 Calculate conversion rate/dropoff in the backend 9e62ffb1 Make existing tests pass 7a88fe44 Outline basic error handling c8ae3eaf Move Funnels to StatsController and use base query 2929bfd4 Minor rework b490403b !ifxup ae44db77 Add Funnels.list/1 function 4f3216b3 Unique steps 6e0c3efc Ensure step ordering works cea93ba1 It works I think c50ac0c2 WIP a466ec4c Dirty wip fbd14e23 Absolute dirty wip 3ddd15ad wip 34ceb3f5 dirty wip test/plausible_web/controllers/api/stats_controller/funnels_test.exs - restored from: 9b532273 Test funnel stats controller Co-authored-by: Uku Taht * Update Goals implementation lib/plausible/goal/schema.ex - restored from: 028036ad Review comments acd9c4f2 Prepare ephemeral funnel definitions so that users can test funnels 4a4ddbdc Optionally preload funnels for goals and stub funnel-goal deletion fbd14e23 Absolute dirty wip lib/plausible/goals.ex - restored from: aea4ebc4 Access Funnel min/max steps via the __using__/1 macro a418c039 Implement funnel reduction logic on goal deletion 4a4ddbdc Optionally preload funnels for goals and stub funnel-goal deletion ebdc4333 Extend the prompt in case of funnel-goal deletion 950a18d9 Dirty funnel save f06c3fcf wip test/plausible/goals_test.exs - restored from: a418c039 Implement funnel reduction logic on goal deletion 4a4ddbdc Optionally preload funnels for goals and stub funnel-goal deletion dee5bc9e Fixup tests * Update seeds * Split funnel schema modules * Add moduledoc to Funnel schema * Update Funnel.Step moduledoc * Add typespecs to Plausible.Funnels * Implement Phoenix.HTML.Safe for Funnel schema * Fixup a test after ordering goals by desc:id test/plausible_web/controllers/api/stats_controller/conversions_test.exs - restored from: 26a493cb Fixup a test after ordering goals by desc:id * Annotate Goal schema with type t() * Reword schema module docs * Fix up typespecs * Stop tagging Funnels.get/2 result * Nuke Goals.by_id!/2 * Test Funnels module test/plausible/funnels_test.exs - restored from: b7a6d3ad ya, rly 028036ad Review comments acd9c4f2 Prepare ephemeral funnel definitions so that users can test funnels 51f0397d Implement deleting funnels 1f6fe25d Add number of steps to funnels list dee5bc9e Fixup tests 950a18d9 Dirty funnel save 88e38a33 Update funnel tests bf47f654 Calculate conversion rate/dropoff in the backend 9e62ffb1 Make existing tests pass 2929bfd4 Minor rework b490403b !ifxup ae44db77 Add Funnels.list/1 function 4f3216b3 Unique steps 6e0c3efc Ensure step ordering works cea93ba1 It works I think c50ac0c2 WIP a466ec4c Dirty wip 34ceb3f5 dirty wip Co-authored-by: Uku Taht * Apply changes related to 242081e5 * Evaluate funnels through `Stats.funnel/3` entry point --------- Co-authored-by: Uku Taht --- lib/plausible/funnel.ex | 76 ++++++ lib/plausible/funnel/step.ex | 27 ++ lib/plausible/funnels.ex | 88 ++++++ lib/plausible/goal/schema.ex | 59 ++-- lib/plausible/goals.ex | 86 +++++- lib/plausible/{stats/mod.ex => stats.ex} | 1 + lib/plausible/stats/funnel.ex | 154 +++++++++++ priv/repo/seeds.exs | 20 +- test/plausible/funnels_test.exs | 252 ++++++++++++++++++ test/plausible/goals_test.exs | 84 +++++- .../api/stats_controller/conversions_test.exs | 10 +- 11 files changed, 820 insertions(+), 37 deletions(-) create mode 100644 lib/plausible/funnel.ex create mode 100644 lib/plausible/funnel/step.ex create mode 100644 lib/plausible/funnels.ex rename lib/plausible/{stats/mod.ex => stats.ex} (87%) create mode 100644 lib/plausible/stats/funnel.ex create mode 100644 test/plausible/funnels_test.exs diff --git a/lib/plausible/funnel.ex b/lib/plausible/funnel.ex new file mode 100644 index 000000000..4d8e24c8b --- /dev/null +++ b/lib/plausible/funnel.ex @@ -0,0 +1,76 @@ +defmodule Plausible.Funnel do + @min_steps 2 + @max_steps 5 + + @moduledoc """ + A funnel is a marketing term used to capture and describe the journey + that users go through, from initial step to conversion. + A funnel consists of several steps (here: #{@min_steps}..#{@max_steps}). + + This module defines the database schema for storing funnels + and changeset helpers for enumerating the steps within. + + Each step references a goal (either a Custom Event or Visit) + - see: `Plausible.Goal`. + """ + + use Ecto.Schema + import Ecto.Changeset + + alias Plausible.Funnel.Step + + defmacro min_steps() do + quote do + unquote(@min_steps) + end + end + + defmacro max_steps() do + quote do + unquote(@max_steps) + end + end + + defmacro __using__(_opts \\ []) do + quote do + require Plausible.Funnel + alias Plausible.Funnel + end + end + + @type t() :: %__MODULE__{} + schema "funnels" do + field :name, :string + belongs_to :site, Plausible.Site + + has_many :steps, Step, + preload_order: [ + asc: :step_order + ] + + has_many :goals, through: [:steps, :goal] + timestamps() + end + + def changeset(funnel \\ %__MODULE__{}, attrs \\ %{}) do + funnel + |> cast(attrs, [:name]) + |> validate_required([:name]) + |> cast_assoc(:steps, with: &Step.changeset/2, required: true) + |> validate_length(:steps, min: @min_steps, max: @max_steps) + |> put_step_orders() + |> unique_constraint(:name, + name: :funnels_name_site_id_index + ) + end + + def put_step_orders(changeset) do + if steps = Ecto.Changeset.get_change(changeset, :steps) do + steps + |> Enum.with_index(fn step, step_order -> + Ecto.Changeset.put_change(step, :step_order, step_order + 1) + end) + |> then(&Ecto.Changeset.put_change(changeset, :steps, &1)) + end + end +end diff --git a/lib/plausible/funnel/step.ex b/lib/plausible/funnel/step.ex new file mode 100644 index 000000000..b57d22c40 --- /dev/null +++ b/lib/plausible/funnel/step.ex @@ -0,0 +1,27 @@ +defmodule Plausible.Funnel.Step do + @moduledoc """ + This module defines the database schema for a single Funnel step. + See: `Plausible.Funnel` for more information. + """ + + use Ecto.Schema + import Ecto.Changeset + + @type t() :: %__MODULE__{} + schema "funnel_steps" do + field :step_order, :integer + belongs_to :funnel, Plausible.Funnel + belongs_to :goal, Plausible.Goal + timestamps() + end + + def changeset(step, attrs \\ %{}) do + step + |> cast(attrs, [:goal_id]) + |> cast_assoc(:goal) + |> validate_required([:goal_id]) + |> unique_constraint(:goal, + name: :funnel_steps_goal_id_funnel_id_index + ) + end +end diff --git a/lib/plausible/funnels.ex b/lib/plausible/funnels.ex new file mode 100644 index 000000000..ce290c6d9 --- /dev/null +++ b/lib/plausible/funnels.ex @@ -0,0 +1,88 @@ +defmodule Plausible.Funnels do + @moduledoc """ + This module implements contextual Funnel interface, allowing listing, + creating and deleting funnel definitions. + + For brief explanation of what a Funnel is, please see `Plausible.Funnel` schema. + See `Plausible.Stats.Funnel` for the evaluation logic. + """ + + use Plausible.Funnel + + alias Plausible.Repo + + import Ecto.Query + + @spec create(Plausible.Site.t(), String.t(), [Plausible.Goal.t()]) :: + {:ok, Funnel.t()} + | {:error, Ecto.Changeset.t() | :invalid_funnel_size} + def create(site, name, steps) + when is_list(steps) and length(steps) in Funnel.min_steps()..Funnel.max_steps() do + site + |> create_changeset(name, steps) + |> Repo.insert() + end + + def create(_site, _name, _goals) do + {:error, :invalid_funnel_size} + end + + @spec create_changeset(Plausible.Site.t(), String.t(), [Plausible.Goal.t()]) :: + Ecto.Changeset.t() + def create_changeset(site, name, steps) do + Funnel.changeset(%Funnel{site_id: site.id}, %{name: name, steps: steps}) + end + + @spec ephemeral_definition(Plausible.Site.t(), String.t(), [Plausible.Goal.t()]) :: Funnel.t() + def ephemeral_definition(site, name, steps) do + site + |> create_changeset(name, steps) + |> Ecto.Changeset.apply_changes() + end + + @spec list(Plausible.Site.t()) :: [ + %{name: String.t(), id: pos_integer(), steps_count: pos_integer()} + ] + def list(%Plausible.Site{id: site_id}) do + Repo.all( + from f in Funnel, + inner_join: steps in assoc(f, :steps), + where: f.site_id == ^site_id, + select: %{name: f.name, id: f.id, steps_count: count(steps)}, + group_by: f.id, + order_by: [desc: :id] + ) + end + + @spec delete(Plausible.Site.t(), pos_integer()) :: :ok + def delete(%Plausible.Site{id: site_id}, funnel_id) do + Repo.delete_all( + from f in Funnel, + where: f.site_id == ^site_id, + where: f.id == ^funnel_id + ) + + :ok + end + + @spec get(Plausible.Site.t() | pos_integer(), pos_integer()) :: + Funnel.t() | nil + def get(%Plausible.Site{id: site_id}, by) do + get(site_id, by) + end + + def get(site_id, funnel_id) when is_integer(site_id) and is_integer(funnel_id) do + q = + from f in Funnel, + where: f.site_id == ^site_id, + where: f.id == ^funnel_id, + inner_join: steps in assoc(f, :steps), + inner_join: goal in assoc(steps, :goal), + order_by: steps.step_order, + preload: [ + steps: {steps, goal: goal} + ] + + Repo.one(q) + end +end diff --git a/lib/plausible/goal/schema.ex b/lib/plausible/goal/schema.ex index 02a58ad1d..59b711dc6 100644 --- a/lib/plausible/goal/schema.ex +++ b/lib/plausible/goal/schema.ex @@ -1,31 +1,16 @@ -defimpl Jason.Encoder, for: Plausible.Goal do - def encode(value, opts) do - goal_type = - cond do - value.event_name -> :event - value.page_path -> :page - end - - domain = value.site.domain - - value - |> Map.put(:goal_type, goal_type) - |> Map.take([:id, :goal_type, :event_name, :page_path]) - |> Map.put(:domain, domain) - |> Jason.Encode.map(opts) - end -end - defmodule Plausible.Goal do use Ecto.Schema import Ecto.Changeset + @type t() :: %__MODULE__{} + schema "goals" do field :event_name, :string field :page_path, :string - field :currency, Ecto.Enum, values: Money.Currency.known_current_currencies() + many_to_many :funnels, Plausible.Funnel, join_through: Plausible.Funnel.Step + belongs_to :site, Plausible.Site timestamps() @@ -50,7 +35,7 @@ defmodule Plausible.Goal do def changeset(goal, attrs \\ %{}) do goal - |> cast(attrs, [:site_id, :event_name, :page_path, :currency]) + |> cast(attrs, [:id, :site_id, :event_name, :page_path, :currency]) |> validate_required([:site_id]) |> cast_assoc(:site) |> validate_event_name_and_page_path() @@ -88,3 +73,37 @@ defmodule Plausible.Goal do end end end + +defimpl Jason.Encoder, for: Plausible.Goal do + def encode(value, opts) do + goal_type = + cond do + value.event_name -> :event + value.page_path -> :page + end + + domain = value.site.domain + + value + |> Map.put(:goal_type, goal_type) + |> Map.take([:id, :goal_type, :event_name, :page_path]) + |> Map.put(:domain, domain) + |> Jason.Encode.map(opts) + end +end + +defimpl String.Chars, for: Plausible.Goal do + def to_string(%{page_path: page_path}) when is_binary(page_path) do + "Visit " <> page_path + end + + def to_string(%{event_name: name}) when is_binary(name) do + name + end +end + +defimpl Phoenix.HTML.Safe, for: Plausible.Goal do + def to_iodata(data) do + data |> to_string() |> Phoenix.HTML.Engine.html_escape() + end +end diff --git a/lib/plausible/goals.ex b/lib/plausible/goals.ex index 44a43e21e..20c6fc78c 100644 --- a/lib/plausible/goals.ex +++ b/lib/plausible/goals.ex @@ -1,6 +1,9 @@ defmodule Plausible.Goals do use Plausible.Repo alias Plausible.Goal + alias Ecto.Multi + + use Plausible.Funnel def create(site, params) do params = Map.merge(params, %{"site_id" => site.id}) @@ -47,26 +50,93 @@ defmodule Plausible.Goals do def find_or_create(_, %{"goal_type" => "page"}), do: {:missing, "page_path"} - def for_site(site) do + def for_site(site, opts \\ []) do query = from g in Goal, inner_join: assoc(g, :site), where: g.site_id == ^site.id, + order_by: [desc: g.id], preload: [:site] + query = + if opts[:preload_funnels?] do + from g in query, + left_join: assoc(g, :funnels), + group_by: g.id, + preload: [:funnels] + else + query + end + query |> Repo.all() |> Enum.map(&maybe_trim/1) end + @doc """ + If a goal belongs to funnel(s), we need to inspect their number of steps. + + If it exceeds the minimum allowed (defined via `Plausible.Funnel.min_steps/0`), + the funnel will be reduced (i.e. a step associated with the goal to be deleted + is removed), so that the minimum number of steps is preserved. This is done + implicitly, by postgres, as per on_delete: :delete_all. + + Otherwise, for associated funnel(s) consisting of minimum number steps only, + funnel record(s) are removed completely along with the targeted goal. + """ def delete(id, site) do - case Repo.delete_all( - from g in Goal, - where: g.id == ^id, - where: g.site_id == ^site.id - ) do - {1, _} -> :ok - {0, _} -> {:error, :not_found} + result = + Multi.new() + |> Multi.one( + :goal, + from(g in Goal, + where: g.id == ^id, + where: g.site_id == ^site.id, + preload: [funnels: :steps] + ) + ) + |> 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.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 Funnel, + where: f.id in ^funnel_ids + ) + ) + end) + |> Multi.delete_all( + :delete_goals, + fn _ -> + from(g in Goal, + where: g.id == ^id, + where: g.site_id == ^site.id + ) + end + ) + |> Repo.transaction() + + case result do + {:ok, _} -> :ok + {:error, _step, reason, _context} -> {:error, reason} end end diff --git a/lib/plausible/stats/mod.ex b/lib/plausible/stats.ex similarity index 87% rename from lib/plausible/stats/mod.ex rename to lib/plausible/stats.ex index 704e05cb4..178955f30 100644 --- a/lib/plausible/stats/mod.ex +++ b/lib/plausible/stats.ex @@ -3,6 +3,7 @@ defmodule Plausible.Stats do defdelegate aggregate(site, query, metrics), to: Plausible.Stats.Aggregate defdelegate timeseries(site, query, metrics), to: Plausible.Stats.Timeseries defdelegate current_visitors(site), to: Plausible.Stats.CurrentVisitors + defdelegate funnel(site, query, funnel), to: Plausible.Stats.Funnel defdelegate filter_suggestions(site, query, filter_name, filter_search), to: Plausible.Stats.FilterSuggestions diff --git a/lib/plausible/stats/funnel.ex b/lib/plausible/stats/funnel.ex new file mode 100644 index 000000000..51b0ab9ca --- /dev/null +++ b/lib/plausible/stats/funnel.ex @@ -0,0 +1,154 @@ +defmodule Plausible.Stats.Funnel do + @moduledoc """ + Module responsible for funnel evaluation, i.e. building and executing + ClickHouse funnel query based on `Plausible.Funnel` definition. + """ + + @funnel_window_duration 86_400 + + alias Plausible.Funnel + alias Plausible.Funnels + + import Ecto.Query + + alias Plausible.ClickhouseRepo + alias Plausible.Stats.Base + + @spec funnel(Plausible.Site.t(), Plausible.Stats.Query.t(), Funnel.t() | pos_integer()) :: + {:ok, Funnel.t()} | {:error, :funnel_not_found} + def funnel(site, query, funnel_id) when is_integer(funnel_id) do + case Funnels.get(site.id, funnel_id) do + %Funnel{} = funnel -> + funnel(site, query, funnel) + + nil -> + {:error, :funnel_not_found} + end + end + + def funnel(site, query, %Funnel{} = funnel) do + steps = + site + |> Base.base_event_query(query) + |> query_funnel(funnel) + |> backfill_steps(funnel) + + {:ok, + %{ + name: funnel.name, + steps: steps + }} + end + + defp query_funnel(query, funnel_definition) do + q_events = + from(e in query, + select: %{session_id: e.session_id}, + where: e.site_id == ^funnel_definition.site_id, + group_by: e.session_id, + having: fragment("step > 0"), + order_by: [desc: fragment("step")] + ) + |> select_funnel(funnel_definition) + + query = + from f in subquery(q_events), + select: {f.step, count(1)}, + group_by: f.step + + ClickhouseRepo.all(query) + end + + defp select_funnel(db_query, funnel_definition) do + window_funnel_steps = + Enum.reduce(funnel_definition.steps, nil, fn step, acc -> + step_condition = step_condition(step.goal) + + if acc do + dynamic([q], fragment("?, ?", ^acc, ^step_condition)) + else + dynamic([q], fragment("?", ^step_condition)) + end + end) + + dynamic_window_funnel = + dynamic( + [q], + fragment("windowFunnel(?)(timestamp, ?)", @funnel_window_duration, ^window_funnel_steps) + ) + + from q in db_query, + select_merge: + ^%{ + step: dynamic_window_funnel + } + end + + defp step_condition(goal) do + case goal do + %Plausible.Goal{event_name: event} when is_binary(event) -> + dynamic([], fragment("name = ?", ^event)) + + %Plausible.Goal{page_path: pathname} when is_binary(pathname) -> + if String.contains?(pathname, "*") do + regex = Plausible.Stats.Base.page_regex(pathname) + dynamic([], fragment("match(pathname, ?)", ^regex)) + else + dynamic([], fragment("pathname = ?", ^pathname)) + end + end + end + + defp backfill_steps(funnel_result, funnel) do + # Directly from ClickHouse we only get {step_idx(), visitor_count()} tuples. + # but no totals including previous steps are aggregated. + # Hence we need to perform the appropriate backfill + # and also calculate dropoff and conversion rate for each step. + funnel_result = Enum.into(funnel_result, %{}) + max_step = Enum.max_by(funnel.steps, & &1.step_order).step_order + + funnel + |> Map.fetch!(: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) + visitors_at_step = + step.step_order..max_step + |> Enum.map(&Map.get(funnel_result, &1, 0)) + |> Enum.sum() + + # accumulate current_visitors for the next iteration + current_visitors = visitors_at_step + + # First step contains the total number of visitors that we base percentage dropoff on + total_visitors = + total_visitors || + current_visitors + + # Dropoff is 0 for the first step, otherwise we subtract current from previous + dropoff = if visitors_at_previous, do: visitors_at_previous - current_visitors, else: 0 + + conversion_rate = + if current_visitors == 0 or total_visitors == 0 do + "0.00" + else + current_visitors + |> Decimal.div(total_visitors) + |> Decimal.mult(100) + |> Decimal.round(2) + |> Decimal.to_string() + end + + step = %{ + dropoff: dropoff, + conversion_rate: conversion_rate, + visitors: visitors_at_step, + label: to_string(step.goal) + } + + {total_visitors, current_visitors, [step | acc]} + end) + |> elem(2) + |> Enum.reverse() + end +end diff --git a/priv/repo/seeds.exs b/priv/repo/seeds.exs index b92dc0901..79ae8b786 100644 --- a/priv/repo/seeds.exs +++ b/priv/repo/seeds.exs @@ -12,6 +12,8 @@ user = Plausible.Factory.insert(:user, email: "user@plausible.test", password: "plausible") +FunWithFlags.enable(:funnels) + native_stats_range = Date.range( Date.add(Date.utc_today(), -720), @@ -31,6 +33,17 @@ site = stats_start_date: NaiveDateTime.new!(imported_stats_range.first, ~T[00:00:00]) ) +{:ok, goal1} = Plausible.Goals.create(site, %{"page_path" => "/"}) +{:ok, goal2} = Plausible.Goals.create(site, %{"page_path" => "/register"}) +{:ok, goal3} = Plausible.Goals.create(site, %{"page_path" => "/login"}) + +{:ok, _funnel} = + Plausible.Funnels.create(site, "From homepage to login", [ + %{"goal_id" => goal1.id}, + %{"goal_id" => goal2.id}, + %{"goal_id" => goal3.id} + ]) + _membership = Plausible.Factory.insert(:site_membership, user: user, site: site, role: :owner) put_random_time = fn @@ -94,7 +107,7 @@ geolocations = [ native_stats_range |> Enum.with_index() |> Enum.flat_map(fn {date, index} -> - Enum.map(0..:rand.uniform(500), fn _ -> + Enum.map(0..Enum.random(1..500), fn _ -> geolocation = Enum.random(geolocations) [ @@ -108,7 +121,8 @@ native_stats_range operating_system: Enum.random(["Windows", "macOS", "Linux"]), operating_system_version: to_string(Enum.random(0..15)), pathname: - Enum.random(["/", "/login", "/settings", "/register", "/docs", "/docs/1", "/docs/2"]) + Enum.random(["/", "/login", "/settings", "/register", "/docs", "/docs/1", "/docs/2"]), + user_id: Enum.random(1..1200) ] |> Keyword.merge(geolocation) |> then(&Plausible.Factory.build(:pageview, &1)) @@ -127,7 +141,7 @@ site = imported_stats_range |> Enum.flat_map(fn date -> - Enum.flat_map(0..:rand.uniform(500), fn _ -> + Enum.flat_map(0..Enum.random(1..500), fn _ -> [ Plausible.Factory.build(:imported_visitors, date: date, diff --git a/test/plausible/funnels_test.exs b/test/plausible/funnels_test.exs new file mode 100644 index 000000000..52b5513b5 --- /dev/null +++ b/test/plausible/funnels_test.exs @@ -0,0 +1,252 @@ +defmodule Plausible.FunnelsTest do + use Plausible.DataCase + + alias Plausible.Goals + alias Plausible.Funnels + alias Plausible.Stats + + setup do + site = insert(: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"}) + {:ok, g4} = Goals.create(site, %{"event_name" => "Leave feedback"}) + {:ok, g5} = Goals.create(site, %{"page_path" => "/recommend"}) + {:ok, g6} = Goals.create(site, %{"event_name" => "Extra event"}) + + {:ok, + %{ + site: site, + goals: [g1, g2, g3], + steps: [g1, g2, g3, g4, g5, g6] |> Enum.map(&%{"goal_id" => &1.id}) + }} + end + + describe "Common context" do + test "create and store a funnel given a set of goals", %{site: site, steps: [g1, g2, g3 | _]} do + {:ok, funnel} = + Funnels.create( + site, + "From blog to signup and purchase", + [g1, g2, g3] + ) + + assert funnel.inserted_at + assert funnel.name == "From blog to signup and purchase" + assert [fg1, fg2, fg3] = funnel.steps + + assert fg1.goal_id == g1["goal_id"] + assert fg2.goal_id == g2["goal_id"] + assert fg3.goal_id == g3["goal_id"] + + assert fg1.step_order == 1 + assert fg2.step_order == 2 + assert fg3.step_order == 3 + end + + test "retrieve a funnel by id and site, get steps in order", %{ + site: site, + steps: [g1, g2, g3 | _] + } do + {:ok, funnel} = + Funnels.create( + site, + "Lorem ipsum", + [g3, g1, g2] + ) + + assert funnel = Funnels.get(site, funnel.id) + assert funnel.name == "Lorem ipsum" + assert [%{step_order: 1}, %{step_order: 2}, %{step_order: 3}] = funnel.steps + end + + test "funnels can be deleted by id and a site", %{site: site, steps: [g1, g2 | _]} do + {:ok, funnel} = + Funnels.create( + site, + "Sample funnel", + [g1, g2] + ) + + assert :ok = Funnels.delete(site, funnel.id) + # deletion is idempotent + assert :ok = Funnels.delete(site, funnel.id) + refute Funnels.get(site, funnel.id) + end + + test "a funnel cannot be made of < 2 steps", %{site: site, steps: [g1 | _]} do + assert {:error, :invalid_funnel_size} = + Funnels.create( + site, + "Lorem ipsum", + [g1] + ) + end + + test "a funnel can be made of 5 steps maximum", %{site: site, steps: too_many_steps} do + assert {:error, :invalid_funnel_size} = + Funnels.create( + site, + "Lorem ipsum", + too_many_steps + ) + end + + test "a goal can only appear once in a funnel", %{steps: [g1 | _], site: site} do + {:error, _changeset} = + Funnels.create( + site, + "Lorem ipsum", + [g1, g1] + ) + end + + test "funnels can be listed per site, starting from most recently added", %{ + site: site, + steps: [g1, g2, g3 | _] + } do + Funnels.create(site, "Funnel 1", [g3, g1]) + Funnels.create(site, "Funnel 2", [g2, g1, g3]) + + funnels_list = Funnels.list(site) + + assert [%{name: "Funnel 2", steps_count: 3}, %{name: "Funnel 1", steps_count: 2}] = + funnels_list + end + end + + describe "Plausible.Stats.Funnel" do + test "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 + } + }, + %{ + "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 + } + } + ] + ) + + populate_stats(site, [ + 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 = Plausible.Stats.Query.from(site, %{"period" => "all"}) + + funnel_data = Stats.funnel(site, query, funnel_definition) + + assert {:ok, + %{ + steps: [ + %{ + label: "Visit /go/to/blog/**", + visitors: 2, + conversion_rate: "100.00", + dropoff: 0 + }, + %{label: "Signup", visitors: 2, conversion_rate: "100.00", dropoff: 0}, + %{label: "Visit /checkout", visitors: 1, conversion_rate: "50.00", dropoff: 1} + ] + }} = funnel_data + end + + test "funnels can be evaluated per site within a time range", %{ + site: site, + steps: [g1, g2, g3 | _] + } do + {:ok, funnel} = + Funnels.create( + site, + "From blog to signup and purchase", + [g1, g2, g3] + ) + + populate_stats(site, [ + 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 = Plausible.Stats.Query.from(site, %{"period" => "all"}) + + funnel_data = Stats.funnel(site, query, funnel.id) + + assert {:ok, + %{ + steps: [ + %{ + label: "Visit /go/to/blog/**", + visitors: 2, + conversion_rate: "100.00", + dropoff: 0 + }, + %{label: "Signup", visitors: 2, conversion_rate: "100.00", dropoff: 0}, + %{label: "Visit /checkout", visitors: 1, conversion_rate: "50.00", dropoff: 1} + ] + }} = funnel_data + end + + test "funnels can be evaluated even where there are no visits yet", %{ + site: site, + steps: [g1, g2, g3 | _] + } do + {:ok, funnel} = + Funnels.create( + site, + "From blog to signup and purchase", + [g1, g2, g3] + ) + + query = Plausible.Stats.Query.from(site, %{"period" => "all"}) + + funnel_data = Stats.funnel(site, query, funnel.id) + + assert {:ok, + %{ + steps: [ + %{ + label: "Visit /go/to/blog/**", + visitors: 0, + conversion_rate: "0.00", + dropoff: 0 + }, + %{label: "Signup", visitors: 0, conversion_rate: "0.00", dropoff: 0}, + %{label: "Visit /checkout", visitors: 0, conversion_rate: "0.00", dropoff: 0} + ] + }} = funnel_data + end + end +end diff --git a/test/plausible/goals_test.exs b/test/plausible/goals_test.exs index 4e3f20dcd..2df4c2b78 100644 --- a/test/plausible/goals_test.exs +++ b/test/plausible/goals_test.exs @@ -25,7 +25,7 @@ defmodule Plausible.GoalsTest do goals = Goals.for_site(site) - assert [%{event_name: "Signup"}, %{page_path: "/Signup"}] = goals + assert [%{page_path: "/Signup"}, %{event_name: "Signup"}] = goals end test "goals are present after domain change" do @@ -47,4 +47,86 @@ defmodule Plausible.GoalsTest do assert [] = Goals.for_site(site) end + + test "goals can be deleted" do + site = insert(:site) + goal = insert(:goal, %{site: site, event_name: " Signup "}) + :ok = Goals.delete(goal.id, site) + assert [] = Goals.for_site(site) + end + + test "goals can be fetched with funnel count preloaded" do + site = insert(:site) + + goals = + Enum.map(1..4, fn i -> + {:ok, g} = Goals.create(site, %{"page_path" => "/#{i}"}) + g + end) + + {:ok, %{id: funnel_id1}} = + Plausible.Funnels.create( + site, + "Funnel1", + [ + %{"goal_id" => Enum.at(goals, 1).id}, + %{"goal_id" => Enum.at(goals, 2).id}, + %{"goal_id" => Enum.at(goals, 3).id} + ] + ) + + {:ok, %{id: funnel_id2}} = + Plausible.Funnels.create( + site, + "Funnel2", + [ + %{"goal_id" => Enum.at(goals, 1).id}, + %{"goal_id" => Enum.at(goals, 3).id} + ] + ) + + assert [goal, _, _, _] = Goals.for_site(site, preload_funnels?: false) + assert %Ecto.Association.NotLoaded{} = goal.funnels + + assert [goal, _, _, _] = Goals.for_site(site, preload_funnels?: true) + assert [%{id: ^funnel_id1}, %{id: ^funnel_id2}] = goal.funnels + end + + test "deleting goals with funnels triggers funnel reduction" do + site = insert(: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 3 steps", + [ + %{"goal_id" => g1.id}, + %{"goal_id" => g2.id}, + %{"goal_id" => g3.id} + ] + ) + + {:ok, f2} = + Plausible.Funnels.create( + site, + "Funnel 2 steps", + [ + %{"goal_id" => g1.id}, + %{"goal_id" => g2.id} + ] + ) + + :ok = Goals.delete(g1.id, site) + + assert f1 = Plausible.Funnels.get(site.id, f1.id) + assert Enum.count(f1.steps) == 2 + + refute Plausible.Funnels.get(site.id, f2.id) + assert Repo.all(from fs in Plausible.Funnel.Step, where: fs.funnel_id == ^f2.id) == [] + + assert [^g3, ^g2] = Goals.for_site(site) + 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 abbf82ada..b939035ca 100644 --- a/test/plausible_web/controllers/api/stats_controller/conversions_test.exs +++ b/test/plausible_web/controllers/api/stats_controller/conversions_test.exs @@ -1399,14 +1399,14 @@ defmodule PlausibleWeb.Api.StatsController.ConversionsTest do %{ "conversion_rate" => 37.5, "unique_conversions" => 3, - "name" => "Visit /*", + "name" => "Visit /signup/**", "total_conversions" => 3, "prop_names" => [] }, %{ "conversion_rate" => 37.5, "unique_conversions" => 3, - "name" => "Visit /signup/**", + "name" => "Visit /*", "total_conversions" => 3, "prop_names" => [] }, @@ -1427,21 +1427,21 @@ defmodule PlausibleWeb.Api.StatsController.ConversionsTest do %{ "conversion_rate" => 12.5, "unique_conversions" => 1, - "name" => "Visit /billing*/success", + "name" => "Visit /signup/*", "total_conversions" => 1, "prop_names" => [] }, %{ "conversion_rate" => 12.5, "unique_conversions" => 1, - "name" => "Visit /register", + "name" => "Visit /billing*/success", "total_conversions" => 1, "prop_names" => [] }, %{ "conversion_rate" => 12.5, "unique_conversions" => 1, - "name" => "Visit /signup/*", + "name" => "Visit /register", "total_conversions" => 1, "prop_names" => [] }