From ea658dcc467256357994cd9b506d0c579f6721a7 Mon Sep 17 00:00:00 2001 From: Adam Rutkowski Date: Mon, 17 Apr 2023 13:06:10 +0200 Subject: [PATCH] dirty wip --- lib/plausible/funnel.ex | 13 ++++ lib/plausible/funnel/goals.ex | 12 +++ lib/plausible/funnels.ex | 77 +++++++++++++++++++ .../20230417095029_init_funnels.exs | 18 +++++ test/plausible/funnels_test.exs | 71 +++++++++++++++++ 5 files changed, 191 insertions(+) create mode 100644 lib/plausible/funnel.ex create mode 100644 lib/plausible/funnel/goals.ex create mode 100644 lib/plausible/funnels.ex create mode 100644 priv/repo/migrations/20230417095029_init_funnels.exs 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..18f051759 --- /dev/null +++ b/lib/plausible/funnel.ex @@ -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 diff --git a/lib/plausible/funnel/goals.ex b/lib/plausible/funnel/goals.ex new file mode 100644 index 000000000..ab513c54a --- /dev/null +++ b/lib/plausible/funnel/goals.ex @@ -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 diff --git a/lib/plausible/funnels.ex b/lib/plausible/funnels.ex new file mode 100644 index 000000000..274dfb4c0 --- /dev/null +++ b/lib/plausible/funnels.ex @@ -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 diff --git a/priv/repo/migrations/20230417095029_init_funnels.exs b/priv/repo/migrations/20230417095029_init_funnels.exs new file mode 100644 index 000000000..e7dbc8e83 --- /dev/null +++ b/priv/repo/migrations/20230417095029_init_funnels.exs @@ -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 diff --git a/test/plausible/funnels_test.exs b/test/plausible/funnels_test.exs new file mode 100644 index 000000000..16d445336 --- /dev/null +++ b/test/plausible/funnels_test.exs @@ -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