dirty wip
parent
6c907cbf42
commit
ea658dcc46
@ -0,0 +1,13 @@
|
||||
defmodule Plausible.Funnel do
|
||||
use Ecto.Schema
|
||||
|
||||
@type t() :: %__MODULE__{}
|
||||
schema "funnels" do
|
||||
field :name, :string
|
||||
belongs_to :site, Plausible.Site
|
||||
|
||||
has_many :funnel_goals, Plausible.Funnel.Goals
|
||||
has_many :goals, through: [:funnel_goals, :goal]
|
||||
timestamps()
|
||||
end
|
||||
end
|
||||
@ -0,0 +1,12 @@
|
||||
defmodule Plausible.Funnel.Goals do
|
||||
use Ecto.Schema
|
||||
|
||||
@type t() :: %__MODULE__{}
|
||||
schema "funnel_goals" do
|
||||
field :step_order, :integer
|
||||
|
||||
belongs_to :funnel, Plausible.Funnel
|
||||
belongs_to :goal, Plausible.Goal
|
||||
timestamps()
|
||||
end
|
||||
end
|
||||
@ -0,0 +1,77 @@
|
||||
defmodule Plausible.Funnels do
|
||||
@funnel_window_duration 86_400
|
||||
|
||||
alias Plausible.Funnel
|
||||
alias Plausible.Repo
|
||||
alias Plausible.ClickhouseRepo
|
||||
|
||||
import Ecto.Changeset
|
||||
import Ecto.Query
|
||||
|
||||
def create(site, name, goals) when is_list(goals) do
|
||||
funnel_goals =
|
||||
goals
|
||||
|> Enum.with_index(1)
|
||||
|> Enum.map(fn {goal, index} ->
|
||||
%{
|
||||
goal_id: goal.id,
|
||||
step_order: index
|
||||
}
|
||||
end)
|
||||
|
||||
change(%Funnel{
|
||||
site_id: site.id,
|
||||
name: name
|
||||
})
|
||||
|> put_assoc(:funnel_goals, funnel_goals)
|
||||
|> Repo.insert!()
|
||||
end
|
||||
|
||||
def list_funnels(site) do
|
||||
end
|
||||
|
||||
def evaluate(query, funnel) do
|
||||
# select count(1), chain from
|
||||
# (select
|
||||
# session_id, windowFunnel(5184000)(timestamp, pathname like '/blog%', name = 'Signup') AS chain
|
||||
# FROM events_v2
|
||||
# WHERE site_id = 2
|
||||
# GROUP BY session_id
|
||||
# having chain > 0 order by chain desc) group by chain;
|
||||
#
|
||||
|
||||
q_events =
|
||||
from e in "events_v2",
|
||||
select: %{
|
||||
session_id: e.session_id,
|
||||
step:
|
||||
fragment(
|
||||
"windowFunnel(?)(timestamp, pathname = '/go/to/blog/foo', name = 'Signup', pathname = '/checkout')",
|
||||
@funnel_window_duration
|
||||
)
|
||||
},
|
||||
where: e.site_id == ^funnel.site_id,
|
||||
group_by: e.session_id,
|
||||
having: fragment("step > 0"),
|
||||
order_by: [desc: fragment("step")]
|
||||
|
||||
query =
|
||||
from f in subquery(q_events),
|
||||
select: %{
|
||||
visitors: count(1),
|
||||
step: f.step
|
||||
},
|
||||
group_by: f.step
|
||||
|
||||
Logger.configure(level: :debug)
|
||||
|
||||
steps = ClickhouseRepo.all(query)
|
||||
|
||||
# funnel
|
||||
# |> Repo.preload([:goals])
|
||||
|
||||
%{
|
||||
steps: steps
|
||||
}
|
||||
end
|
||||
end
|
||||
@ -0,0 +1,18 @@
|
||||
defmodule Plausible.Repo.Migrations.InitFunnels do
|
||||
use Ecto.Migration
|
||||
|
||||
def change do
|
||||
create table(:funnels) do
|
||||
add :name, :string, null: false
|
||||
add :site_id, references(:sites), null: false
|
||||
timestamps()
|
||||
end
|
||||
|
||||
create table(:funnel_goals) do
|
||||
add :goal_id, references(:goals), null: false
|
||||
add :funnel_id, references(:funnels, on_delete: :delete_all), null: false
|
||||
add :step_order, :integer, null: false
|
||||
timestamps()
|
||||
end
|
||||
end
|
||||
end
|
||||
@ -0,0 +1,71 @@
|
||||
defmodule Plausible.GoalsTest do
|
||||
use Plausible.DataCase
|
||||
|
||||
alias Plausible.Goals
|
||||
alias Plausible.Funnels
|
||||
|
||||
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, %{site: site, goals: [g1, g2, g3]}}
|
||||
end
|
||||
|
||||
test "create and store a funnel given a set of goals", %{site: site, goals: [g1, g2, g3]} do
|
||||
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.funnel_goals
|
||||
|
||||
assert fg1.goal_id == g1.id
|
||||
assert fg2.goal_id == g2.id
|
||||
assert fg3.goal_id == g3.id
|
||||
|
||||
assert fg1.step_order == 1
|
||||
assert fg2.step_order == 2
|
||||
assert fg3.step_order == 3
|
||||
end
|
||||
|
||||
test "a funnel can be made of max n(TBD) goals" do
|
||||
end
|
||||
|
||||
test "a funnel can be deleted" do
|
||||
end
|
||||
|
||||
test "a goal can only appear once in a funnel" do
|
||||
end
|
||||
|
||||
test "funnels can be listed per site" do
|
||||
end
|
||||
|
||||
test "funnels can be evaluated per site within a time range", %{site: site, goals: goals} do
|
||||
funnel =
|
||||
Funnels.create(
|
||||
site,
|
||||
"From blog to signup and purchase",
|
||||
goals
|
||||
)
|
||||
|
||||
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)
|
||||
])
|
||||
|
||||
query = Plausible.Stats.Query.from(site, %{"period" => "all"})
|
||||
funnel_data = Funnels.evaluate(query, funnel)
|
||||
|
||||
assert %{
|
||||
steps: [%{step: 3, visitors: 1}]
|
||||
} = funnel_data
|
||||
end
|
||||
end
|
||||
Loading…
Reference in New Issue