From 637d7ec548a3aad33f7fa4ca4369c81a433b7ed3 Mon Sep 17 00:00:00 2001 From: Adam Rutkowski Date: Thu, 18 May 2023 07:31:21 +0200 Subject: [PATCH] Dirty funnel save --- lib/plausible/funnels.ex | 16 ++++++---- lib/plausible/goals.ex | 21 ++++++++++--- .../controllers/site_controller.ex | 16 ---------- lib/plausible_web/live/funnel_settings.ex | 18 +++++++++-- .../live/funnel_settings/form.ex | 2 -- test/plausible/funnels_test.exs | 30 +++++++++++++++++++ 6 files changed, 74 insertions(+), 29 deletions(-) diff --git a/lib/plausible/funnels.ex b/lib/plausible/funnels.ex index ab7b9d94b..8ef0f50f8 100644 --- a/lib/plausible/funnels.ex +++ b/lib/plausible/funnels.ex @@ -166,16 +166,22 @@ defmodule Plausible.Funnels do 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 + 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 = - (current_visitors / total_visitors * 100) - |> Decimal.from_float() - |> Decimal.round(2) - |> Decimal.to_string() + if current_visitors == 0 or total_visitors == 0 do + "0.00" + else + (current_visitors / total_visitors * 100) + |> Decimal.from_float() + |> Decimal.round(2) + |> Decimal.to_string() + end step = %{ dropoff: dropoff, diff --git a/lib/plausible/goals.ex b/lib/plausible/goals.ex index 97ace6957..22e426737 100644 --- a/lib/plausible/goals.ex +++ b/lib/plausible/goals.ex @@ -13,11 +13,12 @@ defmodule Plausible.Goals do def find_or_create(site, %{"goal_type" => "event", "event_name" => event_name}) do query = - from g in Goal, + from(g in Goal, inner_join: assoc(g, :site), where: g.site_id == ^site.id, where: g.event_name == ^event_name, preload: [:site] + ) goal = Repo.one(query) @@ -31,11 +32,12 @@ defmodule Plausible.Goals do def find_or_create(site, %{"goal_type" => "page", "page_path" => page_path}) do query = - from g in Goal, + from(g in Goal, inner_join: assoc(g, :site), where: g.site_id == ^site.id, where: g.page_path == ^page_path, preload: [:site] + ) goal = Repo.one(query) @@ -49,22 +51,33 @@ defmodule Plausible.Goals do def for_site(site) do query = - from g in Goal, + from(g in Goal, inner_join: assoc(g, :site), where: g.site_id == ^site.id, order_by: [desc: g.id], preload: [:site] + ) query |> Repo.all() |> Enum.map(&maybe_trim/1) end + def by_id!(site, id) do + Repo.one( + from(g in Goal, + where: g.id == ^id, + where: g.site_id == ^site.id + ) + ) + end + def delete(id, site) do case Repo.delete_all( - from g in Goal, + from(g in Goal, where: g.id == ^id, where: g.site_id == ^site.id + ) ) do {1, _} -> :ok {0, _} -> {:error, :not_found} diff --git a/lib/plausible_web/controllers/site_controller.ex b/lib/plausible_web/controllers/site_controller.ex index c4155fee8..33789af5f 100644 --- a/lib/plausible_web/controllers/site_controller.ex +++ b/lib/plausible_web/controllers/site_controller.ex @@ -251,22 +251,6 @@ defmodule PlausibleWeb.SiteController do ) end - def save_funnel(conn, params) do - site = conn.assigns[:site] - - sanitized_params = - ["step_1", "step_2", "step_3", "step_4", "step_5"] - |> Enum.map(¶ms[&1]) - |> Enum.reject(&(&1 == "")) - |> Enum.map(fn goal_id -> %{id: String.to_integer(goal_id)} end) - - funnel_name = params["funnel_name"] - - {:ok, _funnel} = Plausible.Funnels.create(site, funnel_name, sanitized_params) - - redirect(conn, to: Routes.site_path(conn, :settings_funnels, site.domain)) - end - def settings_search_console(conn, _params) do site = conn.assigns[:site] diff --git a/lib/plausible_web/live/funnel_settings.ex b/lib/plausible_web/live/funnel_settings.ex index 0fb28fe5c..0ccc87957 100644 --- a/lib/plausible_web/live/funnel_settings.ex +++ b/lib/plausible_web/live/funnel_settings.ex @@ -53,8 +53,22 @@ defmodule PlausibleWeb.Live.FunnelSettings do end def handle_event("save", data, socket) do - IO.inspect(data, label: :save) - {:noreply, socket} + site = socket.assigns.site + step_keys = for i <- 1..5, do: "step-#{i}" + + goals = + Map.take(data, step_keys) + |> Enum.reject(fn {_key, value} -> String.trim(value) == "" end) + |> Enum.sort_by(&elem(&1, 0)) + |> Enum.map(&Goals.by_id!(site, String.to_integer(elem(&1, 1)))) + + funnel_name = data["funnel"]["name"] + + {:ok, _funnel} = Plausible.Funnels.create(site, funnel_name, goals) + + funnels = Funnels.list(site) + + {:noreply, assign(socket, add_funnel?: false, funnels: funnels)} end def handle_event("add_funnel", _value, socket) do diff --git a/lib/plausible_web/live/funnel_settings/form.ex b/lib/plausible_web/live/funnel_settings/form.ex index eec8867b6..21d1227ec 100644 --- a/lib/plausible_web/live/funnel_settings/form.ex +++ b/lib/plausible_web/live/funnel_settings/form.ex @@ -2,8 +2,6 @@ defmodule PlausibleWeb.Live.FunnelSettings.Form do use Phoenix.LiveComponent use Phoenix.HTML - # good - def mount(socket) do {:ok, assign(socket, step_count: 2)} end diff --git a/test/plausible/funnels_test.exs b/test/plausible/funnels_test.exs index 5a6c7b7df..6388087a3 100644 --- a/test/plausible/funnels_test.exs +++ b/test/plausible/funnels_test.exs @@ -126,4 +126,34 @@ defmodule Plausible.FunnelsTest do ] }} = funnel_data end + + test "funnels can be evaluated even where there are no visits yet", %{ + site: site, + goals: [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 = Funnels.evaluate(query, funnel.id, site) + + 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