Skip to content

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

Implement open funnel with first and last steps required (#6665)

Adrian Gruntkowski2a5eefa7ad8e
diff --git a/assets/js/dashboard/extra/funnel/index.tsx b/assets/js/dashboard/extra/funnel/index.tsx
index 17c661afe7..b7889cc65d 100644
--- a/assets/js/dashboard/extra/funnel/index.tsx
+++ b/assets/js/dashboard/extra/funnel/index.tsx
@@ -23,6 +23,7 @@ import { useDashboardStateContext } from '../../dashboard-state-context'
import { useSiteContext } from '../../site-context'
import {
FunnelResponse,
+ FunnelType,
StepMetrics,
StepValues,
conversionRateChange,
@@ -379,6 +380,21 @@ function ConversionRateSummary({
)
}
+const funnelTypeLabels: { [t: string]: string } = {
+ [FunnelType.sequential]: 'Sequential',
+ [FunnelType.flexible]: 'Flexible',
+ [FunnelType.strict]: 'Strict'
+}
+
+const funnelTypeDescriptions: { [t: string]: string } = {
+ [FunnelType.sequential]:
+ 'All steps are required. Other activity is allowed between steps.',
+ [FunnelType.flexible]:
+ 'Only the first and last steps are required. Middle steps can be skipped.',
+ [FunnelType.strict]:
+ 'All steps are required. No other activity is allowed between steps.'
+}
+
function FunnelHeader({
funnelName,
funnel,
@@ -418,16 +434,12 @@ function FunnelHeader({
</span>
<div className="flex items-center gap-1">
- {funnel.strict_order ? 'Strict' : 'Sequential'}
+ {funnelTypeLabels[funnel.funnel_type]}
<Tooltip
className="flex"
containerRef={{ current: document.body }}
info={
- <span>
- {funnel.strict_order
- ? 'No other activity is allowed between steps.'
- : 'Other activity is allowed between steps.'}
- </span>
+ <span> {funnelTypeDescriptions[funnel.funnel_type]} </span>
}
>
<InformationCircleIcon className="size-3.5" />
diff --git a/assets/js/dashboard/extra/funnel/metrics.test.ts b/assets/js/dashboard/extra/funnel/metrics.test.ts
index c18cc81230..0c36d70b2f 100644
--- a/assets/js/dashboard/extra/funnel/metrics.test.ts
+++ b/assets/js/dashboard/extra/funnel/metrics.test.ts
@@ -1,8 +1,13 @@
-import { FunnelResponse, conversionRateChange, stepMetrics } from './metrics'
+import {
+ FunnelResponse,
+ FunnelType,
+ conversionRateChange,
+ stepMetrics
+} from './metrics'
const funnel: FunnelResponse = {
name: 'Checkout',
- strict_order: false,
+ funnel_type: FunnelType.sequential,
all_visitors: 250,
entering_visitors: 100,
entering_visitors_percentage: '40',
diff --git a/assets/js/dashboard/extra/funnel/metrics.ts b/assets/js/dashboard/extra/funnel/metrics.ts
index edd5b261fe..719a2df027 100644
--- a/assets/js/dashboard/extra/funnel/metrics.ts
+++ b/assets/js/dashboard/extra/funnel/metrics.ts
@@ -20,9 +20,15 @@ type FunnelPeriod = {
never_entering_visitors_percentage: string
}
+export enum FunnelType {
+ sequential = 'sequential',
+ flexible = 'flexible',
+ strict = 'strict'
+}
+
export type FunnelResponse = FunnelPeriod & {
name: string
- strict_order: boolean
+ funnel_type: FunnelType
comparison?: FunnelPeriod | null
date_range?: [string, string]
comparison_date_range?: [string, string] | null
diff --git a/extra/lib/plausible/data_migration/backfill_funnel_type.ex b/extra/lib/plausible/data_migration/backfill_funnel_type.ex
new file mode 100644
index 0000000000..7f8d7e856f
--- /dev/null
+++ b/extra/lib/plausible/data_migration/backfill_funnel_type.ex
@@ -0,0 +1,60 @@
+defmodule Plausible.DataMigration.BackfillFunnelType do
+ @moduledoc """
+ Backfill and sync funnel_type for all funnels out of sync.
+ """
+
+ import Ecto.Query
+
+ alias Plausible.Funnel
+ alias Plausible.Repo
+
+ def run(opts \\ []) do
+ dry_run? = Keyword.get(opts, :dry_run?, true)
+
+ log("DRY RUN: #{dry_run?}")
+
+ backfill(dry_run?)
+ end
+
+ defp backfill(dry_run?) do
+ out_of_sync_funnels =
+ from(
+ f in Funnel,
+ where:
+ (f.strict_order == false and f.first_and_last == false and f.funnel_type != :sequential) or
+ (f.strict_order == true and f.funnel_type != :strict) or
+ (f.first_and_last == true and f.funnel_type != :flexible)
+ )
+ |> Repo.all()
+
+ log("Found #{length(out_of_sync_funnels)} funnels with funnel_type out of sync...")
+
+ if not dry_run? do
+ Enum.each(out_of_sync_funnels, fn funnel ->
+ funnel
+ |> set_funnel_type()
+ |> Repo.update!()
+ end)
+
+ log("Finished syncing funnel type for #{length(out_of_sync_funnels)} funnels!")
+ end
+
+ :ok
+ end
+
+ defp set_funnel_type(%{strict_order: false, first_and_last: false} = funnel) do
+ Ecto.Changeset.change(funnel, funnel_type: :sequential)
+ end
+
+ defp set_funnel_type(%{strict_order: true, first_and_last: false} = funnel) do
+ Ecto.Changeset.change(funnel, funnel_type: :strict)
+ end
+
+ defp set_funnel_type(%{strict_order: false, first_and_last: true} = funnel) do
+ Ecto.Changeset.change(funnel, funnel_type: :flexible)
+ end
+
+ defp log(msg) do
+ IO.puts("[#{DateTime.utc_now(:second)}] #{msg}")
+ end
+end
diff --git a/extra/lib/plausible/funnel.ex b/extra/lib/plausible/funnel.ex
index f30d295276..1f504fa6ad 100644
--- a/extra/lib/plausible/funnel.ex
+++ b/extra/lib/plausible/funnel.ex
@@ -19,6 +19,18 @@ defmodule Plausible.Funnel do
alias Plausible.Funnel.Step
+ @funnel_types [:sequential, :flexible, :strict]
+
+ @default_funnel_type :sequential
+
+ @type funnel_type() :: unquote(Enum.reduce(@funnel_types, &{:|, [], [&1, &2]}))
+
+ @spec funnel_types() :: [funnel_type()]
+ def funnel_types(), do: @funnel_types
+
+ @spec default_funnel_type() :: funnel_type()
+ def default_funnel_type(), do: @default_funnel_type
+
defmacro min_steps() do
quote do
unquote(@min_steps)
@@ -42,6 +54,12 @@ defmodule Plausible.Funnel do
schema "funnels" do
field :name, :string
field :strict_order, :boolean, default: false
+ field :first_and_last, :boolean, default: false
+
+ field :funnel_type, Ecto.Enum,
+ default: @default_funnel_type,
+ values: @funnel_types
+
belongs_to :site, Plausible.Site
has_many :steps, Step,
@@ -56,8 +74,9 @@ defmodule Plausible.Funnel do
def changeset(funnel \\ %__MODULE__{}, attrs \\ %{}) do
funnel
- |> cast(attrs, [:name, :strict_order])
+ |> 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)
|> unique_constraint(:name,
@@ -73,4 +92,24 @@ defmodule Plausible.Funnel do
end)
|> then(&Ecto.Changeset.put_assoc(changeset, :steps, &1))
end
+
+ defp set_funnel_type(%{valid?: true} = changeset) do
+ {changed?, strict_order?, first_and_last?} =
+ case get_change(changeset, :funnel_type) do
+ :strict -> {true, true, false}
+ :flexible -> {true, false, true}
+ :sequential -> {true, false, false}
+ nil -> {false, nil, nil}
+ end
+
+ if changed? do
+ changeset
+ |> put_change(:strict_order, strict_order?)
+ |> put_change(:first_and_last, first_and_last?)
+ else
+ changeset
+ end
+ end
+
+ defp set_funnel_type(changeset), do: changeset
end
diff --git a/extra/lib/plausible/funnels.ex b/extra/lib/plausible/funnels.ex
index feebbf65e1..a089dbf7e6 100644
--- a/extra/lib/plausible/funnels.ex
+++ b/extra/lib/plausible/funnels.ex
@@ -49,9 +49,7 @@ defmodule Plausible.Funnels do
:ok ->
funnel
- |> edit_changeset(name, steps,
- strict_order?: Keyword.get(opts, :strict_order?, !!funnel.strict_order)
- )
+ |> edit_changeset(name, steps, opts)
|> Repo.update()
end
end
@@ -59,21 +57,28 @@ defmodule Plausible.Funnels do
@spec create_changeset(Plausible.Site.t(), String.t(), [map()], Keyword.t()) ::
Ecto.Changeset.t()
def create_changeset(site, name, steps, opts \\ []) do
+ funnel_type = Keyword.get(opts, :funnel_type, Funnel.default_funnel_type())
+
Funnel.changeset(%Funnel{site_id: site.id}, %{
name: name,
steps: steps,
- strict_order: Keyword.get(opts, :strict_order?, false)
+ funnel_type: funnel_type
})
end
@spec edit_changeset(Plausible.Funnel.t(), String.t(), [map()], Keyword.t()) ::
Ecto.Changeset.t()
- def edit_changeset(funnel, name, steps, opts) do
- Funnel.changeset(funnel, %{
- name: name,
- steps: steps,
- strict_order: Keyword.get(opts, :strict_order?, false)
- })
+ def edit_changeset(funnel, name, steps, opts \\ []) do
+ params = %{name: name, steps: steps}
+
+ params =
+ if funnel_type = Keyword.get(opts, :funnel_type) do
+ Map.put(params, :funnel_type, funnel_type)
+ else
+ params
+ end
+
+ Funnel.changeset(funnel, params)
end
@spec ephemeral_definition(Plausible.Site.t(), String.t(), [map()], Keyword.t()) :: Funnel.t()
@@ -148,7 +153,19 @@ defmodule Plausible.Funnels do
order_by: steps.step_order,
preload: [
steps: {steps, goal: goal}
- ]
+ ],
+ select: %{
+ f
+ | funnel_type:
+ type(
+ fragment(
+ "case when ? = true then 'strict' when ? = true then 'flexible' else 'sequential' end",
+ f.strict_order,
+ f.first_and_last
+ ),
+ f.funnel_type
+ )
+ }
)
end
end
diff --git a/extra/lib/plausible/stats/funnel.ex b/extra/lib/plausible/stats/funnel.ex
index ef006aa2bb..e572bf4822 100644
--- a/extra/lib/plausible/stats/funnel.ex
+++ b/extra/lib/plausible/stats/funnel.ex
@@ -30,22 +30,28 @@ defmodule Plausible.Stats.Funnel do
end
def funnel(site, query, %Funnel{} = funnel) do
- revenue_steps = revenue_steps(site, funnel)
+ revenue_steps = revenue_steps(site, funnel.steps)
- comparison =
+ comparison_funnel =
if query.comparison_utc_time_range do
- query
- |> Comparisons.get_comparison_query()
+ comparison_query = Comparisons.get_comparison_query(query)
+
+ comparison_query
|> compute(funnel, revenue_steps)
+ |> recompute_flexible(comparison_query, site, funnel)
end
+ final_funnel =
+ query
+ |> compute(funnel, revenue_steps)
+ |> recompute_flexible(query, site, funnel)
+
{:ok,
- query
- |> compute(funnel, revenue_steps)
+ final_funnel
|> Map.merge(%{
name: funnel.name,
- strict_order: funnel.strict_order,
- comparison: comparison,
+ funnel_type: funnel.funnel_type,
+ comparison: comparison_funnel,
date_range: tz_date_range(query.utc_time_range, query.timezone),
comparison_date_range:
if(query.comparison_utc_time_range,
@@ -54,9 +60,37 @@ defmodule Plausible.Stats.Funnel do
})}
end
- defp revenue_steps(site, funnel) do
+ defp recompute_flexible(final_funnel, query, site, %{funnel_type: :flexible} = funnel) do
+ if length(funnel.steps) > 2 do
+ short_funnel = convert_to_first_and_last(funnel)
+ revenue_steps = revenue_steps(site, short_funnel.steps)
+
+ [_, last_step] =
+ query
+ |> compute(short_funnel, revenue_steps)
+ |> Map.fetch!(:steps)
+
+ steps = List.replace_at(final_funnel.steps, -1, last_step)
+
+ %{final_funnel | steps: steps}
+ else
+ final_funnel
+ end
+ end
+
+ defp recompute_flexible(final_funnel, _query, _site, _funnel),
+ do: final_funnel
+
+ defp 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]}
+ end
+
+ defp revenue_steps(site, steps) do
if Revenue.available?(site) do
- Enum.reject(funnel.steps, &is_nil(&1.goal.currency))
+ Enum.reject(steps, &is_nil(&1.goal.currency))
else
[]
end
@@ -180,7 +214,7 @@ defmodule Plausible.Stats.Funnel do
end)
dynamic_window_funnel =
- if funnel_definition.strict_order do
+ if funnel_definition.funnel_type == :strict do
dynamic(
[q],
fragment(
diff --git a/extra/lib/plausible_web/live/funnel_settings/form.ex b/extra/lib/plausible_web/live/funnel_settings/form.ex
index f7d8740af6..f4fd262170 100644
--- a/extra/lib/plausible_web/live/funnel_settings/form.ex
+++ b/extra/lib/plausible_web/live/funnel_settings/form.ex
@@ -55,7 +55,7 @@ defmodule PlausibleWeb.Live.FunnelSettings.Form do
>
</div>
<div class="fixed inset-0 flex items-center justify-center mt-16 z-50 overlofw-y-auto overflow-x-hidden">
- <div class="md:w-2/3 max-w-lg h-full">
+ <div class="md:w-2/3 max-w-xl h-full">
<div id="funnel-form">
<.form
:let={f}
@@ -80,17 +80,59 @@ defmodule PlausibleWeb.Live.FunnelSettings.Form do
label="Funnel name"
/>
- <div class="mt-6 flex items-center justify-between gap-4">
- <span class="text-sm font-medium text-gray-900 dark:text-gray-100">
- Allow other activity between funnel steps
- </span>
- <div class="flex items-center gap-3">
- <.toggle_switch
- id="toggle-strict-order"
- id_suffix="switch"
- checked={!@strict_order?}
- phx-click="toggle-strict-order"
- />
+ <div class="flex flex-col gap-y-1 mt-8">
+ <.label>
+ Funnel type
+ </.label>
+ <div class="flex items-center justify-between gap-4 mt-2">
+ <.input
+ type="radio"
+ id={f[:funnel_type].id <> "_sequential"}
+ name={f[:funnel_type].name}
+ value="sequential"
+ checked={@funnel_type == :sequential}
+ phx-click="switch-type"
+ phx-value-type="sequential"
+ label="Sequential"
+ >
+ <:help_content>
+ All steps are required. Other activity is allowed between steps.
+ </:help_content>
+ </.input>
+ </div>
+
+ <div class="flex items-center justify-between gap-4">
+ <.input
+ type="radio"
+ id={f[:funnel_type].id <> "_flexible"}
+ name={f[:funnel_type].name}
+ value="flexible"
+ checked={@funnel_type == :flexible}
+ phx-click="switch-type"
+ phx-value-type="flexible"
+ label="Flexible"
+ >
+ <:help_content>
+ Only the first and last steps are required. Middle steps can be skipped.
+ </:help_content>
+ </.input>
+ </div>
+
+ <div class="flex items-center justify-between gap-4">
+ <.input
+ type="radio"
+ id={f[:funnel_type].id <> "_strict"}
+ name={f[:funnel_type].name}
+ value="strict"
+ checked={@funnel_type == :strict}
+ phx-click="switch-type"
+ phx-value-type="strict"
+ label="Strict"
+ >
+ <:help_content>
+ All steps are required. No other activity is allowed between steps.
+ </:help_content>
+ </.input>
</div>
</div>
@@ -99,7 +141,7 @@ defmodule PlausibleWeb.Live.FunnelSettings.Form do
Funnel steps
</.label>
- <div :for={step_idx <- @step_ids} class="flex my-3">
+ <div :for={step_idx <- @step_ids} class="flex items-center my-3">
<div class="w-2/5 flex-1">
<.live_component
selected={find_preselected(@funnel, @funnel_modified?, step_idx)}
@@ -123,7 +165,7 @@ defmodule PlausibleWeb.Live.FunnelSettings.Form do
/>
</div>
- <div class="w-4/12 mt-1 ml-4 text-gray-500 dark:text-gray-400">
+ <div class="w-4/12 ml-4 text-gray-500 dark:text-gray-400">
<.evaluation
:if={@evaluation_result}
result={@evaluation_result}
@@ -132,7 +174,7 @@ defmodule PlausibleWeb.Live.FunnelSettings.Form do
</div>
</div>
- <div class="flex flex-col gap-y-4 mt-6">
+ <div class="flex flex-col gap-y-2 mt-2">
<.add_step_button :if={
length(@step_ids) < Funnel.max_steps() and
map_size(@selections_made) < length(@goals)
@@ -246,15 +288,15 @@ defmodule PlausibleWeb.Live.FunnelSettings.Form do
assign(socket, step_ids: step_ids, selections_made: selections_made, funnel_modified?: true)}
end
- def handle_event("toggle-strict-order", _params, socket) do
- strict_order? = !socket.assigns.strict_order?
+ def handle_event("switch-type", %{"type" => funnel_type_str}, socket) do
+ funnel_type = String.to_existing_atom(funnel_type_str)
send(self(), :evaluate_funnel)
- {:noreply, assign(socket, strict_order?: strict_order?)}
+ {:noreply, assign(socket, funnel_type: funnel_type)}
end
def handle_event("validate", %{"funnel" => params}, socket) do
- strict_order? = socket.assigns.strict_order?
+ funnel_type = socket.assigns.funnel_type
steps_from_assigns =
socket.assigns.step_ids
@@ -269,7 +311,7 @@ defmodule PlausibleWeb.Live.FunnelSettings.Form do
|> Funnels.create_changeset(
params["name"],
steps_from_assigns,
- strict_order?: strict_order?
+ funnel_type: funnel_type
)
|> Map.put(:action, :validate)
@@ -279,17 +321,27 @@ defmodule PlausibleWeb.Live.FunnelSettings.Form do
def handle_event(
"save",
%{"funnel" => params},
- %{assigns: %{site: site, funnel: funnel, strict_order?: strict_order?}} = socket
+ %{
+ assigns: %{
+ site: site,
+ funnel: funnel,
+ funnel_type: funnel_type
+ }
+ } = socket
) do
steps = Enum.map(params["steps"], fn {_idx, payload} -> payload end)
save_fn =
case funnel do
%Plausible.Funnel{} ->
- fn -> Funnels.update(funnel, params["name"], steps, strict_order?: strict_order?) end
+ fn ->
+ Funnels.update(funnel, params["name"], steps, funnel_type: funnel_type)
+ end
nil ->
- fn -> Funnels.create(site, params["name"], steps, strict_order?: strict_order?) end
+ fn ->
+ Funnels.create(site, params["name"], steps, funnel_type: funnel_type)
+ end
end
case save_fn.() do
@@ -339,12 +391,12 @@ defmodule PlausibleWeb.Live.FunnelSettings.Form do
assigns: %{
site: site,
selections_made: selections_made,
- strict_order?: strict_order?
+ funnel_type: funnel_type
}
} = socket
) do
with {:ok, {definition, query}} <-
- build_ephemeral_funnel(site, selections_made, strict_order?: strict_order?),
+ build_ephemeral_funnel(site, selections_made, funnel_type: funnel_type),
{:ok, funnel} <- Plausible.Stats.funnel(site, query, definition) do
assign(socket, evaluation_result: funnel)
else
@@ -444,8 +496,7 @@ defmodule PlausibleWeb.Live.FunnelSettings.Form do
funnel
|> Funnels.edit_changeset(
funnel.name,
- Enum.map(funnel.steps, &%{goal_id: &1.goal.id}),
- strict_order?: funnel.strict_order
+ Enum.map(funnel.steps, &%{goal_id: &1.goal.id})
)
|> to_form()
@@ -459,7 +510,7 @@ defmodule PlausibleWeb.Live.FunnelSettings.Form do
socket,
form: form,
funnel: funnel,
- strict_order?: funnel.strict_order,
+ funnel_type: funnel.funnel_type,
funnel_modified?: false,
selections_made: selections_made,
step_ids: Enum.to_list(1..Enum.count(funnel.steps))
@@ -475,7 +526,7 @@ defmodule PlausibleWeb.Live.FunnelSettings.Form do
socket,
form: form,
funnel: nil,
- strict_order?: false,
+ funnel_type: :sequential,
funnel_modified?: false,
selections_made: Map.new(),
step_ids: Enum.to_list(1..Funnel.min_steps())
diff --git a/lib/plausible_web/live/components/form.ex b/lib/plausible_web/live/components/form.ex
index 079bef4241..9f3278350a 100644
--- a/lib/plausible_web/live/components/form.ex
+++ b/lib/plausible_web/live/components/form.ex
@@ -145,7 +145,7 @@ defmodule PlausibleWeb.Live.Components.Form do
<span
:if={@help_text || @help_content != []}
- class="text-gray-500 dark:text-gray-400 mb-2 text-sm text-pretty"
+ class="font-normal text-gray-500 dark:text-gray-400 mb-2 text-sm text-pretty"
>
{@help_text}
{render_slot(@help_content)}
diff --git a/lib/plausible_web/templates/settings/new_api_key.html.heex b/lib/plausible_web/templates/settings/new_api_key.html.heex
index df0322f999..06fd913209 100644
--- a/lib/plausible_web/templates/settings/new_api_key.html.heex
+++ b/lib/plausible_web/templates/settings/new_api_key.html.heex
@@ -15,7 +15,6 @@
<.input
x-on:click="submitDisabled = false"
type="radio"
- class="block h-5 w-5 dark:bg-gray-700 border-gray-300 text-indigo-600 focus:ring-indigo-600"
id={f[:type].id <> "_0"}
name={f[:type].name}
value="stats_api"
diff --git a/lib/plausible_web/templates/site/membership/invite_member_form.html.heex b/lib/plausible_web/templates/site/membership/invite_member_form.html.heex
index c983643e6a..89478f07a5 100644
--- a/lib/plausible_web/templates/site/membership/invite_member_form.html.heex
+++ b/lib/plausible_web/templates/site/membership/invite_member_form.html.heex
@@ -48,8 +48,6 @@
value="editor"
x-model="selectedOption"
required="true"
- width="w-4"
- class="block dark:bg-gray-900 h-4 w-4 mt-0.5 cursor-pointer text-indigo-600 border-gray-300 dark:border-gray-500 focus:ring-indigo-500"
/>
<div class="ml-3 flex flex-col">
<span
@@ -78,8 +76,6 @@
value="viewer"
x-model="selectedOption"
required="true"
- width="w-4"
- class="block dark:bg-gray-900 h-4 w-4 mt-0.5 cursor-pointer text-indigo-600 border-gray-300 dark:border-gray-500 focus:ring-indigo-500"
/>
<div class="ml-3 flex flex-col">
<span
diff --git a/test/plausible/data_migration/backfill_funnel_type_test.exs b/test/plausible/data_migration/backfill_funnel_type_test.exs
new file mode 100644
index 0000000000..ceb45754ce
--- /dev/null
+++ b/test/plausible/data_migration/backfill_funnel_type_test.exs
@@ -0,0 +1,102 @@
+defmodule Plausible.DataMigration.BackfillFunnelTypeTest do
+ use Plausible
+
+ on_ee do
+ use Plausible.DataCase, async: true
+
+ import ExUnit.CaptureIO
+
+ alias Plausible.DataMigration.BackfillFunnelType
+ alias Plausible.Funnels
+ alias Plausible.Goals
+ alias Plausible.Repo
+
+ describe "run/1" do
+ test "runs for empty dataset" do
+ dry_run_output =
+ capture_io(fn ->
+ assert :ok = BackfillFunnelType.run()
+ end)
+
+ assert dry_run_output =~ "DRY RUN: true"
+ assert dry_run_output =~ "Found 0 funnels with funnel_type out of sync"
+ refute dry_run_output =~ "Finished syncing"
+
+ real_run_output =
+ capture_io(fn ->
+ assert :ok = BackfillFunnelType.run(dry_run?: false)
+ end)
+
+ assert real_run_output =~ "DRY RUN: false"
+ assert real_run_output =~ "Found 0 funnels with funnel_type out of sync"
+ assert real_run_output =~ "Finished syncing funnel type for 0 funnels!"
+ end
+
+ test "updates out of sync funnels" 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"})
+
+ {:ok, valid_sequential} =
+ Funnels.create(site, "Valid sequential", [g1, g2, g3], funnel_type: :sequential)
+
+ {:ok, valid_strict} =
+ Funnels.create(site, "Valid strict", [g1, g2, g3], funnel_type: :strict)
+
+ {:ok, valid_flexible} =
+ Funnels.create(site, "Valid flexible", [g1, g2, g3], funnel_type: :flexible)
+
+ {:ok, invalid_sequential} =
+ Funnels.create(site, "Invalid sequential", [g1, g2, g3], funnel_type: :strict)
+
+ invalid_sequential =
+ invalid_sequential
+ |> Ecto.Changeset.change(strict_order: false, first_and_last: false)
+ |> Repo.update!()
+
+ {:ok, invalid_strict} =
+ Funnels.create(site, "Invalid strict", [g1, g2, g3], funnel_type: :sequential)
+
+ invalid_strict =
+ invalid_strict
+ |> Ecto.Changeset.change(strict_order: true, first_and_last: false)
+ |> Repo.update!()
+
+ {:ok, invalid_flexible} =
+ Funnels.create(site, "Invalid flexible", [g1, g2, g3], funnel_type: :sequential)
+
+ invalid_flexible =
+ invalid_flexible
+ |> Ecto.Changeset.change(strict_order: false, first_and_last: true)
+ |> Repo.update!()
+
+ real_run_output =
+ capture_io(fn ->
+ assert :ok = BackfillFunnelType.run(dry_run?: false)
+ end)
+
+ assert real_run_output =~ "DRY RUN: false"
+ assert real_run_output =~ "Found 3 funnels with funnel_type out of sync"
+ assert real_run_output =~ "Finished syncing funnel type for 3 funnels!"
+
+ assert Repo.reload!(valid_sequential).funnel_type == :sequential
+ assert Repo.reload!(valid_strict).funnel_type == :strict
+ assert Repo.reload!(valid_flexible).funnel_type == :flexible
+ assert Repo.reload!(invalid_sequential).funnel_type == :sequential
+ assert Repo.reload!(invalid_strict).funnel_type == :strict
+ assert Repo.reload!(invalid_flexible).funnel_type == :flexible
+
+ real_run_output_repeat =
+ capture_io(fn ->
+ assert :ok = BackfillFunnelType.run(dry_run?: false)
+ end)
+
+ assert real_run_output_repeat =~ "DRY RUN: false"
+ assert real_run_output_repeat =~ "Found 0 funnels with funnel_type out of sync"
+ assert real_run_output_repeat =~ "Finished syncing funnel type for 0 funnels!"
+ end
+ end
+ end
+end
diff --git a/test/plausible/funnels_test.exs b/test/plausible/funnels_test.exs
index 222e56a2ed..c69d0617d1 100644
--- a/test/plausible/funnels_test.exs
+++ b/test/plausible/funnels_test.exs
@@ -43,7 +43,9 @@ defmodule Plausible.FunnelsTest do
assert funnel.inserted_at
assert funnel.name == "From blog to signup and purchase"
+ assert funnel.funnel_type == :sequential
assert funnel.strict_order == false
+ assert funnel.first_and_last == false
assert [fg1, fg2, fg3] = funnel.steps
assert fg1.goal_id == g1["goal_id"]
@@ -77,7 +79,31 @@ defmodule Plausible.FunnelsTest do
assert funnel1.id == funnel2.id
end
- test "update funnel strict_order", %{site: site, steps: [g1, g2, g3 | _]} do
+ test "updating strict funnel with out of sync funnel type does not alter the type", %{
+ site: site,
+ steps: [g1, g2, g3, g4, g5 | _]
+ } do
+ {:ok, funnel1} =
+ Funnels.create(
+ site,
+ "Sample funnel",
+ [g1, g2, g3],
+ funnel_type: :strict
+ )
+
+ funnel1 = funnel1 |> Ecto.Changeset.change(funnel_type: :sequential) |> Repo.update!()
+
+ {:ok, funnel2} = Funnels.update(funnel1, "Updated funnel", [g4, g5])
+
+ assert funnel2.name == "Updated funnel"
+ assert funnel1.id == funnel2.id
+
+ assert funnel2.strict_order == true
+ assert funnel2.first_and_last == false
+ assert funnel2.funnel_type == :sequential
+ end
+
+ test "update funnel to strict", %{site: site, steps: [g1, g2, g3 | _]} do
{:ok, funnel} =
Funnels.create(
site,
@@ -85,17 +111,21 @@ defmodule Plausible.FunnelsTest do
[g1, g2]
)
+ assert funnel.funnel_type == :sequential
assert funnel.strict_order == false
+ assert funnel.first_and_last == false
{:ok, strict_funnel} =
Funnels.update(
funnel,
"Sample funnel",
[g1, g2, g3],
- strict_order?: true
+ funnel_type: :strict
)
+ assert strict_funnel.funnel_type == :strict
assert strict_funnel.strict_order == true
+ assert strict_funnel.first_and_last == false
{:ok, preserved_funnel} =
Funnels.update(
@@ -104,7 +134,82 @@ defmodule Plausible.FunnelsTest do
[g1, g2]
)
+ assert preserved_funnel.funnel_type == :strict
assert preserved_funnel.strict_order == true
+ assert preserved_funnel.first_and_last == false
+ end
+
+ test "update funnel to flexible", %{site: site, steps: [g1, g2, g3 | _]} do
+ {:ok, funnel} =
+ Funnels.create(
+ site,
+ "Sample funnel",
+ [g1, g2]
+ )
+
+ assert funnel.funnel_type == :sequential
+ assert funnel.strict_order == false
+ assert funnel.first_and_last == false
+
+ {:ok, flexible_funnel} =
+ Funnels.update(
+ funnel,
+ "Sample funnel",
+ [g1, g2, g3],
+ funnel_type: :flexible
+ )
+
+ assert flexible_funnel.funnel_type == :flexible
+ assert flexible_funnel.strict_order == false
+ assert flexible_funnel.first_and_last == true
+
+ {:ok, preserved_funnel} =
+ Funnels.update(
+ flexible_funnel,
+ "Sample funnel",
+ [g1, g2]
+ )
+
+ assert preserved_funnel.funnel_type == :flexible
+ assert preserved_funnel.strict_order == false
+ assert preserved_funnel.first_and_last == true
+ end
+
+ test "update funnel to sequential", %{site: site, steps: [g1, g2, g3 | _]} do
+ {:ok, funnel} =
+ Funnels.create(
+ site,
+ "Sample funnel",
+ [g1, g2],
+ funnel_type: :strict
+ )
+
+ assert funnel.funnel_type == :strict
+ assert funnel.strict_order == true
+ assert funnel.first_and_last == false
+
+ {:ok, sequential_funnel} =
+ Funnels.update(
+ funnel,
+ "Sample funnel",
+ [g1, g2, g3],
+ funnel_type: :sequential
+ )
+
+ assert sequential_funnel.funnel_type == :sequential
+ assert sequential_funnel.strict_order == false
+ assert sequential_funnel.first_and_last == false
+
+ {:ok, preserved_funnel} =
+ Funnels.update(
+ sequential_funnel,
+ "Sample funnel",
+ [g1, g2]
+ )
+
+ assert preserved_funnel.funnel_type == :sequential
+ assert preserved_funnel.strict_order == false
+ assert preserved_funnel.first_and_last == false
end
test "retrieve a funnel by id and site, get steps in order", %{
@@ -346,7 +451,7 @@ defmodule Plausible.FunnelsTest do
site,
"Strict from blog to signup and purchase",
[g1, g2, g3],
- strict_order?: true
+ funnel_type: :strict
)
populate_stats(site, [
@@ -380,8 +485,56 @@ defmodule Plausible.FunnelsTest do
assert Enum.at(non_strict_funnel_data.steps, 2).visitors == 1
assert Enum.at(strict_funnel_data.steps, 2).visitors == 0
- assert non_strict_funnel_data.strict_order == false
- assert strict_funnel_data.strict_order == true
+ assert non_strict_funnel_data.funnel_type == :sequential
+ assert strict_funnel_data.funnel_type == :strict
+ end
+
+ test "flexible funnels count everyone who reaches first and last step in the last step", %{
+ site: site,
+ steps: [g1, g2, g3 | _]
+ } do
+ {:ok, non_flexible_funnel} =
+ Funnels.create(
+ site,
+ "From blog to signup and purchase",
+ [g1, g2, g3]
+ )
+
+ {:ok, flexible_funnel} =
+ Funnels.create(
+ site,
+ "Flexible from blog to signup and purchase",
+ [g1, g2, g3],
+ 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", %{
diff --git a/test/plausible_web/controllers/api/stats_controller/funnels_test.exs b/test/plausible_web/controllers/api/stats_controller/funnels_test.exs
index 604a10444e..69f114b0d3 100644
--- a/test/plausible_web/controllers/api/stats_controller/funnels_test.exs
+++ b/test/plausible_web/controllers/api/stats_controller/funnels_test.exs
@@ -36,7 +36,7 @@ defmodule PlausibleWeb.Api.StatsController.FunnelsTest do
assert %{
"name" => "Test funnel",
- "strict_order" => false,
+ "funnel_type" => "sequential",
"comparison" => nil,
"all_visitors" => 3,
"entering_visitors" => 2,
@@ -81,7 +81,7 @@ defmodule PlausibleWeb.Api.StatsController.FunnelsTest do
end
test "computes a strict-order funnel for a day", %{conn: conn, site: site} do
- {:ok, funnel} = setup_funnel(site, @build_funnel_with, strict_order?: true)
+ {:ok, funnel} = setup_funnel(site, @build_funnel_with, funnel_type: :strict)
populate_stats(site, [
build(:pageview, pathname: "/blog/announcement", user_id: @user_id),
@@ -95,10 +95,43 @@ defmodule PlausibleWeb.Api.StatsController.FunnelsTest do
assert %{
"name" => "Test funnel",
- "strict_order" => true,
+ "funnel_type" => "strict",
"all_visitors" => 1,
- "entering_visitors" => 1
+ "entering_visitors" => 1,
+ "steps" => steps
} = resp
+
+ assert Enum.at(steps, 0)["visitors"] == 1
+ assert Enum.at(steps, 1)["visitors"] == 1
+ assert Enum.at(steps, 2)["visitors"] == 0
+ assert Enum.at(steps, 3)["visitors"] == 0
+ end
+
+ test "computes a flexible funnel for a day", %{conn: conn, site: site} do
+ {:ok, funnel} = setup_funnel(site, @build_funnel_with, funnel_type: :flexible)
+
+ populate_stats(site, [
+ build(:pageview, pathname: "/blog/announcement", user_id: @user_id),
+ build(:event, name: "Purchase", user_id: @user_id)
+ ])
+
+ resp =
+ conn
+ |> get("/api/stats/#{site.domain}/funnels/#{funnel.id}/?period=day")
+ |> json_response(200)
+
+ assert %{
+ "name" => "Test funnel",
+ "funnel_type" => "flexible",
+ "all_visitors" => 1,
+ "entering_visitors" => 1,
+ "steps" => steps
+ } = resp
+
+ assert Enum.at(steps, 0)["visitors"] == 1
+ assert Enum.at(steps, 1)["visitors"] == 0
+ assert Enum.at(steps, 2)["visitors"] == 0
+ assert Enum.at(steps, 3)["visitors"] == 1
end
test "404 for unknown funnel", %{site: site, conn: conn} do
@@ -761,7 +794,9 @@ defmodule PlausibleWeb.Api.StatsController.FunnelsTest do
setup_goals(site, [{"page_path", "/checkout"}, {"event_name", "Signup"}])
purchase = insert(:goal, site: site, event_name: "Purchase", currency: "USD")
- {:ok, funnel} = funnel_with_goals(site, [checkout, signup, purchase], strict_order?: true)
+
+ {:ok, funnel} =
+ funnel_with_goals(site, [checkout, signup, purchase], funnel_type: :strict)
populate_stats(site, [
build(:pageview,
@@ -786,6 +821,38 @@ defmodule PlausibleWeb.Api.StatsController.FunnelsTest do
"revenue_per_visitor" => %{"long" => "$0.00"}
} = purchase_step
end
+
+ test "reports revenue when the payment is in the last step of a flexible funnel",
+ %{
+ conn: conn,
+ site: site
+ } do
+ {:ok, [checkout, signup]} =
+ setup_goals(site, [{"page_path", "/checkout"}, {"event_name", "Signup"}])
+
+ purchase = insert(:goal, site: site, event_name: "Purchase", currency: "USD")
+
+ {:ok, funnel} =
+ funnel_with_goals(site, [checkout, signup, purchase], funnel_type: :flexible)
+
+ populate_stats(site, [
+ build(:pageview,
+ pathname: "/checkout",
+ user_id: @user_id,
+ timestamp: ~N[2021-01-01 12:00:00]
+ ),
+ purchase(@user_id, "100", timestamp: ~N[2021-01-01 12:01:00])
+ ])
+
+ assert [_checkout_step, _signup_step, purchase_step] =
+ funnel_steps(conn, site, funnel, "period=day&date=2021-01-01")
+
+ assert %{
+ "visitors" => 1,
+ "revenue" => %{"long" => "$100.00"},
+ "revenue_per_visitor" => %{"long" => "$100.00"}
+ } = purchase_step
+ end
end
defp setup_checkout_funnel(site) do
diff --git a/test/plausible_web/live/funnel_settings_test.exs b/test/plausible_web/live/funnel_settings_test.exs
index d2cee0bd19..c3a0c66c34 100644
--- a/test/plausible_web/live/funnel_settings_test.exs
+++ b/test/plausible_web/live/funnel_settings_test.exs
@@ -326,7 +326,7 @@ defmodule PlausibleWeb.Live.FunnelSettingsTest do
|> element(~s/form/)
|> render_submit()
- assert %Plausible.Funnel{steps: [_, _], strict_order: false} =
+ assert %Plausible.Funnel{steps: [_, _], funnel_type: :sequential} =
Plausible.Funnels.get(site, "My test funnel")
end
@@ -336,12 +336,12 @@ defmodule PlausibleWeb.Live.FunnelSettingsTest do
} do
{:ok, [g1, g2]} = setup_goals(site)
- {:ok, %Plausible.Funnel{strict_order: false} = funnel} =
+ {:ok, %Plausible.Funnel{funnel_type: :sequential} = funnel} =
Plausible.Funnels.create(
site,
"Strict funnel",
[%{"goal_id" => g1.id}, %{"goal_id" => g2.id}],
- strict_order?: false
+ funnel_type: :sequential
)
lv = get_liveview(conn, site)
@@ -354,26 +354,80 @@ defmodule PlausibleWeb.Live.FunnelSettingsTest do
assert element_exists?(
render(lv),
- ~s/button#toggle-strict-order-switch[aria-checked="true"]/
+ ~s/input#funnel_funnel_type_sequential[checked]/
)
lv
- |> element(~s/button#toggle-strict-order-switch[phx-click="toggle-strict-order"]/)
+ |> element(~s/input#funnel_funnel_type_strict[phx-click="switch-type"]/)
|> render_click()
+ refute element_exists?(
+ render(lv),
+ ~s/input#funnel_funnel_type_sequential[checked]/
+ )
+
assert element_exists?(
render(lv),
- ~s/button#toggle-strict-order-switch[aria-checked="false"]/
+ ~s/input#funnel_funnel_type_strict[checked]/
)
lv
|> element(~s/form/)
|> render_submit()
- assert %Plausible.Funnel{strict_order: true} =
+ assert %Plausible.Funnel{funnel_type: :strict} =
Plausible.Funnels.get(site, "Strict funnel")
end
+ test "save button saves flexible setting", %{
+ conn: conn,
+ site: site
+ } do
+ {:ok, [g1, g2]} = setup_goals(site)
+
+ {:ok, %Plausible.Funnel{funnel_type: :sequential} = funnel} =
+ Plausible.Funnels.create(
+ site,
+ "Flexible funnel",
+ [%{"goal_id" => g1.id}, %{"goal_id" => g2.id}],
+ funnel_type: :sequential
+ )
+
+ 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 element_exists?(
+ render(lv),
+ ~s/input#funnel_funnel_type_sequential[checked]/
+ )
+
+ lv
+ |> element(~s/input#funnel_funnel_type_flexible[phx-click="switch-type"]/)
+ |> render_click()
+
+ refute element_exists?(
+ render(lv),
+ ~s/input#funnel_funnel_type_sequential[checked]/
+ )
+
+ assert element_exists?(
+ render(lv),
+ ~s/input#funnel_funnel_type_flexible[checked]/
+ )
+
+ lv
+ |> element(~s/form/)
+ |> render_submit()
+
+ assert %Plausible.Funnel{funnel_type: :flexible} =
+ Plausible.Funnels.get(site, "Flexible funnel")
+ end
+
test "editing a funnel pre-renders it", %{
conn: conn,
site: site
@@ -434,13 +488,56 @@ defmodule PlausibleWeb.Live.FunnelSettingsTest do
assert text_of_element(render(lv), ~s/#funnel-eval/) =~ "Last month conversion rate: 100%"
lv
- |> element(~s/button#toggle-strict-order-switch[phx-click="toggle-strict-order"]/)
+ |> element(~s/input#funnel_funnel_type_strict[phx-click="switch-type"]/)
|> render_click()
assert text_of_element(render(lv), ~s/#step-eval-1/) =~ "Dropoff: 100%"
assert text_of_element(render(lv), ~s/#funnel-eval/) =~ "Last month conversion rate: 0%"
end
+ test "funnel is re-evaluated when flexible is set", %{
+ conn: conn,
+ site: site
+ } do
+ {:ok, g1} = Plausible.Goals.create(site, %{"page_path" => "/go/to/blog/**"})
+ {:ok, g2} = Plausible.Goals.create(site, %{"event_name" => "Signup"})
+ {:ok, g3} = Plausible.Goals.create(site, %{"event_name" => "Purchase"})
+
+ {:ok, funnel} =
+ Plausible.Funnels.create(
+ site,
+ "Flexible switch",
+ [%{"goal_id" => g1.id}, %{"goal_id" => g2.id}, %{"goal_id" => g3.id}]
+ )
+
+ ts = NaiveDateTime.utc_now() |> NaiveDateTime.truncate(:second)
+
+ populate_stats(site, [
+ build(:pageview, pathname: "/go/to/blog/foo", user_id: 123, timestamp: ts),
+ build(:event,
+ name: "Purchase",
+ user_id: 123,
+ timestamp: NaiveDateTime.add(ts, 2, :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: 0%"
+
+ lv
+ |> element(~s/input#funnel_funnel_type_flexible[phx-click="switch-type"]/)
+ |> render_click()
+
+ assert text_of_element(render(lv), ~s/#funnel-eval/) =~ "Last month conversion rate: 100%"
+ end
+
test "clicking save after editing the funnel, updates it", %{
conn: conn,
site: site
@@ -491,7 +588,7 @@ defmodule PlausibleWeb.Live.FunnelSettingsTest do
site,
"Editable strict funnel",
[%{"goal_id" => g1.id}, %{"goal_id" => g2.id}],
- strict_order?: false
+ funnel_type: :sequential
)
lv = get_liveview(conn, site)
@@ -503,17 +600,51 @@ defmodule PlausibleWeb.Live.FunnelSettingsTest do
assert lv = find_live_child(lv, "funnels-form")
lv
- |> element(~s/button#toggle-strict-order-switch[phx-click="toggle-strict-order"]/)
+ |> element(~s/input#funnel_funnel_type_strict[phx-click="switch-type"]/)
|> render_click()
lv
|> element(~s/form/)
|> render_submit()
- assert %Plausible.Funnel{strict_order: true} =
+ assert %Plausible.Funnel{funnel_type: :strict} =
Plausible.Funnels.get(site, "Editable strict funnel")
end
+ test "clicking save after editing the funnel, updates flexible setting", %{
+ conn: conn,
+ site: site
+ } do
+ {:ok, [g1, g2]} = setup_goals(site)
+
+ {:ok, funnel} =
+ Plausible.Funnels.create(
+ site,
+ "Editable flexible funnel",
+ [%{"goal_id" => g1.id}, %{"goal_id" => g2.id}],
+ funnel_type: :sequential
+ )
+
+ 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")
+
+ lv
+ |> element(~s/input#funnel_funnel_type_flexible[phx-click="switch-type"]/)
+ |> render_click()
+
+ lv
+ |> element(~s/form/)
+ |> render_submit()
+
+ assert %Plausible.Funnel{funnel_type: :flexible} =
+ Plausible.Funnels.get(site, "Editable flexible funnel")
+ end
+
test "funnel gets evaluated on every select, assuming a second has passed between selections",
%{
conn: conn,