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 <uku.taht@gmail.com>
* 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 <uku.taht@gmail.com>
* Apply changes related to 242081e5
* Evaluate funnels through `Stats.funnel/3` entry point
---------
Co-authored-by: Uku Taht <uku.taht@gmail.com>
pull/3028/head
parent
c7b8a8ef27
commit
a356009af8
@ -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
|
||||
@ -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
|
||||
@ -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
|
||||
@ -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
|
||||
@ -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
|
||||
Loading…
Reference in New Issue