Dirty funnel save

pull/2859/head
Adam Rutkowski 4 years ago
parent c7a6124c3f
commit 637d7ec548

@ -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,

@ -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}

@ -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(&params[&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]

@ -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

@ -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

@ -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

Loading…
Cancel
Save