From 55dfe1d788e20eb0557067789e25cec59be641e3 Mon Sep 17 00:00:00 2001 From: Adam Rutkowski Date: Tue, 30 May 2023 10:27:16 +0200 Subject: [PATCH] Prepare ephemeral funnel definitions so that users can test funnels --- lib/plausible/funnel.ex | 26 +++---- lib/plausible/funnels.ex | 36 ++++++---- lib/plausible/goal/schema.ex | 44 ++++++------ lib/plausible_web/live/funnel_settings.ex | 15 ++-- .../live/funnel_settings/input_picker.ex | 13 +++- test/plausible/funnels_test.exs | 70 ++++++++++++++++++- 6 files changed, 150 insertions(+), 54 deletions(-) diff --git a/lib/plausible/funnel.ex b/lib/plausible/funnel.ex index 381f15d7c..9f9e23fd4 100644 --- a/lib/plausible/funnel.ex +++ b/lib/plausible/funnel.ex @@ -38,8 +38,9 @@ defmodule Plausible.Funnel do def changeset(step, attrs \\ %{}) do step - |> cast(attrs, [:step_order, :goal_id]) - |> validate_required([:goal_id, :step_order]) + |> cast(attrs, [:goal_id]) + |> cast_assoc(:goal) + |> validate_required([:goal_id]) |> unique_constraint(:goal, name: :funnel_steps_goal_id_funnel_id_index ) @@ -61,23 +62,24 @@ defmodule Plausible.Funnel do end def changeset(funnel \\ %__MODULE__{}, attrs \\ %{}) do - step_attrs = - attrs - |> Map.get(:steps, []) - |> Enum.with_index(1) - |> Enum.map(fn {goal_params, step_order} -> - Map.put(goal_params, "step_order", step_order) - end) - - attrs = Map.replace(attrs, :steps, step_attrs) - 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/funnels.ex b/lib/plausible/funnels.ex index fdf75ed7a..b2580f214 100644 --- a/lib/plausible/funnels.ex +++ b/lib/plausible/funnels.ex @@ -25,6 +25,12 @@ defmodule Plausible.Funnels do Funnel.changeset(%Funnel{site_id: site.id}, %{name: name, steps: steps}) end + def ephemeral_definition(site, name, steps) do + site + |> create_changeset(name, steps) + |> Ecto.Changeset.apply_changes() + end + def list(%Plausible.Site{id: site_id}) do Repo.all( from(f in Funnel, @@ -73,22 +79,26 @@ defmodule Plausible.Funnels do end end - def evaluate(query, funnel_id, site) do + def evaluate(query, funnel_id, site) when is_integer(funnel_id) do with {:ok, funnel_definition} <- get(site.id, funnel_id) do - steps = - site - |> Base.base_event_query(query) - |> query_funnel(funnel_definition) - |> backfill_steps(funnel_definition) - - {:ok, - %{ - name: funnel_definition.name, - steps: steps - }} + evaluate(query, funnel_definition, site) end end + def evaluate(query, funnel_definition, site) do + steps = + site + |> Base.base_event_query(query) + |> query_funnel(funnel_definition) + |> backfill_steps(funnel_definition) + + {:ok, + %{ + name: funnel_definition.name, + steps: steps + }} + end + defp query_funnel(query, funnel_definition) do q_events = from(e in query, @@ -191,7 +201,7 @@ defmodule Plausible.Funnels do dropoff: dropoff, conversion_rate: conversion_rate, visitors: visitors_at_step, - label: Plausible.Goal.display_name(step.goal) + label: to_string(step.goal) } {total_visitors, current_visitors, [step | acc]} diff --git a/lib/plausible/goal/schema.ex b/lib/plausible/goal/schema.ex index 3d57082dc..e12c12088 100644 --- a/lib/plausible/goal/schema.ex +++ b/lib/plausible/goal/schema.ex @@ -1,21 +1,3 @@ -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 @@ -51,7 +33,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,12 +70,32 @@ defmodule Plausible.Goal do changeset 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 - def display_name(%__MODULE__{page_path: page_path}) when is_binary(page_path) do +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 display_name(%__MODULE__{event_name: name}) when is_binary(name) do + def to_string(%{event_name: name}) when is_binary(name) do name end end diff --git a/lib/plausible_web/live/funnel_settings.ex b/lib/plausible_web/live/funnel_settings.ex index 7643a0ec2..cd70212fd 100644 --- a/lib/plausible_web/live/funnel_settings.ex +++ b/lib/plausible_web/live/funnel_settings.ex @@ -13,9 +13,16 @@ defmodule PlausibleWeb.Live.FunnelSettings do funnels = Funnels.list(site) + # We'll have the options trimmed to only the data we care about, to keep + # it minimal at the socket assigns, yet, we want to retain specific %Goal{} + # fields, so that `String.Chars` protocol and `Funnels.ephemeral_definition/3` + # are applicable downstream. goals = - Goals.for_site(site) - |> Enum.map(fn goal -> {goal.id, Plausible.Goal.display_name(goal)} end) + site + |> Goals.for_site() + |> Enum.map(fn goal -> + {goal.id, struct!(Plausible.Goal, Map.take(goal, [:id, :event_name, :page_path]))} + end) {:ok, assign(socket, site: site, funnels: funnels, goals: goals, add_funnel?: false)} end @@ -25,7 +32,7 @@ defmodule PlausibleWeb.Live.FunnelSettings do <%= if @add_funnel? do %> <.live_component module={PlausibleWeb.Live.FunnelSettings.Form} - id="funnelForm" + id="funnel-form" site={@site} form={to_form(Plausible.Funnels.create_changeset(@site, "", []))} goals={@goals} @@ -34,7 +41,7 @@ defmodule PlausibleWeb.Live.FunnelSettings do
= 2}> <.live_component module={PlausibleWeb.Live.FunnelSettings.List} - id="funnelsList" + id="funnels-list" funnels={@funnels} site={@site} /> diff --git a/lib/plausible_web/live/funnel_settings/input_picker.ex b/lib/plausible_web/live/funnel_settings/input_picker.ex index db3ee278c..2a1eed997 100644 --- a/lib/plausible_web/live/funnel_settings/input_picker.ex +++ b/lib/plausible_web/live/funnel_settings/input_picker.ex @@ -106,7 +106,13 @@ defmodule PlausibleWeb.Live.FunnelSettings.InputPicker do class="dropdown z-50 absolute mt-1 max-h-60 overflow-auto rounded-md bg-white py-1 text-base shadow-lg ring-1 ring-black ring-opacity-5 focus:outline-none sm:text-sm dark:bg-gray-900" > <.option - :for={{{submit_value, display_value}, idx} <- Enum.with_index(@suggestions)} + :for={ + {{submit_value, display_value}, idx} <- + Enum.with_index( + @suggestions, + fn {option_value, option}, idx -> {{option_value, to_string(option)}, idx} end + ) + } :if={@suggestions != []} idx={idx} submit_value={submit_value} @@ -191,13 +197,14 @@ defmodule PlausibleWeb.Live.FunnelSettings.InputPicker do options |> Enum.reject(fn {_, value} -> - input_len > String.length(value) + input_len > String.length(to_string(value)) end) |> Enum.sort_by( fn {_, value} -> - if value == input do + if to_string(value) == input do 3 else + value = to_string(value) input = String.downcase(input) value = String.downcase(value) weight = if String.contains?(value, input), do: 1, else: 0 diff --git a/test/plausible/funnels_test.exs b/test/plausible/funnels_test.exs index 485a3fb8b..a9780b16d 100644 --- a/test/plausible/funnels_test.exs +++ b/test/plausible/funnels_test.exs @@ -14,7 +14,12 @@ defmodule Plausible.FunnelsTest do {:ok, g5} = Goals.create(site, %{"page_path" => "/recommend"}) {:ok, g6} = Goals.create(site, %{"event_name" => "Extra event"}) - {:ok, %{site: site, steps: [g1, g2, g3, g4, g5, g6] |> Enum.map(&%{"goal_id" => &1.id})}} + {:ok, + %{ + site: site, + goals: [g1, g2, g3], + steps: [g1, g2, g3, g4, g5, g6] |> Enum.map(&%{"goal_id" => &1.id}) + }} end test "create and store a funnel given a set of goals", %{site: site, steps: [g1, g2, g3 | _]} do @@ -108,6 +113,69 @@ defmodule Plausible.FunnelsTest do funnels_list end + test "funnels can be evaluated per site within a time range against an interim 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 = Funnels.evaluate(query, funnel_definition, site) + + 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 | _]