2023-06-22 10:00:07 +03:00
|
|
|
defmodule PlausibleWeb.Live.FunnelSettingsTest do
|
|
|
|
use PlausibleWeb.ConnCase, async: true
|
|
|
|
import Phoenix.LiveViewTest
|
|
|
|
import Plausible.Test.Support.HTML
|
|
|
|
|
|
|
|
describe "GET /:website/settings/funnels" do
|
|
|
|
setup [:create_user, :log_in, :create_site]
|
|
|
|
|
|
|
|
test "lists funnels for the site and renders help link", %{conn: conn, site: site} do
|
2023-06-28 14:20:19 +03:00
|
|
|
{:ok, _} = setup_funnels(site)
|
2023-06-22 10:00:07 +03:00
|
|
|
conn = get(conn, "/#{site.domain}/settings/funnels")
|
|
|
|
|
|
|
|
resp = html_response(conn, 200)
|
|
|
|
assert resp =~ "Compose goals into funnels"
|
|
|
|
assert resp =~ "From blog to signup"
|
|
|
|
assert resp =~ "From signup to blog"
|
|
|
|
assert element_exists?(resp, "a[href=\"https://plausible.io/docs/funnel-analysis\"]")
|
|
|
|
end
|
|
|
|
|
2023-06-28 14:20:19 +03:00
|
|
|
test "lists funnels with delete actions", %{conn: conn, site: site} do
|
|
|
|
{:ok, [f1_id, f2_id]} = setup_funnels(site)
|
|
|
|
conn = get(conn, "/#{site.domain}/settings/funnels")
|
|
|
|
|
|
|
|
resp = html_response(conn, 200)
|
|
|
|
|
|
|
|
assert element_exists?(
|
|
|
|
resp,
|
|
|
|
~s/button[phx-click="delete-funnel"][phx-value-funnel-id=#{f1_id}]#delete-funnel-#{f1_id}/
|
|
|
|
)
|
|
|
|
|
|
|
|
assert element_exists?(
|
|
|
|
resp,
|
|
|
|
~s/button[phx-click="delete-funnel"][phx-value-funnel-id=#{f2_id}]#delete-funnel-#{f2_id}/
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2023-06-22 10:00:07 +03:00
|
|
|
test "if goals are present, Add Funnel button is rendered", %{conn: conn, site: site} do
|
2023-06-28 14:20:19 +03:00
|
|
|
{:ok, _} = setup_funnels(site)
|
2023-06-22 10:00:07 +03:00
|
|
|
conn = get(conn, "/#{site.domain}/settings/funnels")
|
|
|
|
resp = conn |> html_response(200)
|
|
|
|
assert element_exists?(resp, ~S/button[phx-click="add-funnel"]/)
|
|
|
|
end
|
|
|
|
|
|
|
|
test "if not enough goals are present, a hint to create goals is rendered", %{
|
|
|
|
conn: conn,
|
|
|
|
site: site
|
|
|
|
} do
|
|
|
|
{:ok, _} = Plausible.Goals.create(site, %{"page_path" => "/go/to/blog/**"})
|
|
|
|
conn = get(conn, "/#{site.domain}/settings/funnels")
|
|
|
|
|
|
|
|
doc = conn |> html_response(200)
|
|
|
|
assert Floki.text(doc) =~ "You need to define at least two goals to create a funnel."
|
|
|
|
|
2023-09-04 14:44:22 +03:00
|
|
|
add_goals_path = Routes.site_path(conn, :settings_goals, site.domain)
|
2023-06-22 10:00:07 +03:00
|
|
|
assert element_exists?(doc, ~s/a[href="#{add_goals_path}"]/)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-09-04 14:44:22 +03:00
|
|
|
describe "FunnelSettings live view" do
|
2023-06-22 10:00:07 +03:00
|
|
|
setup [:create_user, :log_in, :create_site]
|
|
|
|
|
2023-06-28 14:20:19 +03:00
|
|
|
test "allows to delete funnels", %{conn: conn, site: site} do
|
|
|
|
{:ok, [f1_id, _f2_id]} = setup_funnels(site)
|
|
|
|
|
|
|
|
{lv, html} = get_liveview(conn, site, with_html?: true)
|
|
|
|
|
|
|
|
assert html =~ "From blog to signup"
|
|
|
|
assert html =~ "From signup to blog"
|
|
|
|
|
|
|
|
html = lv |> element(~s/button#delete-funnel-#{f1_id}/) |> render_click()
|
|
|
|
|
|
|
|
refute html =~ "From blog to signup"
|
|
|
|
assert html =~ "From signup to blog"
|
|
|
|
|
|
|
|
html = get(conn, "/#{site.domain}/settings/funnels") |> html_response(200)
|
|
|
|
|
|
|
|
refute html =~ "From blog to signup"
|
|
|
|
assert html =~ "From signup to blog"
|
|
|
|
end
|
|
|
|
|
2023-06-22 10:00:07 +03:00
|
|
|
test "renders the funnel form on clicking 'Add Funnel' button", %{conn: conn, site: site} do
|
|
|
|
setup_goals(site)
|
|
|
|
lv = get_liveview(conn, site)
|
|
|
|
doc = render_click(lv, "add-funnel")
|
|
|
|
|
|
|
|
assert element_exists?(doc, ~s/form[phx-change="validate"][phx-submit="save"]/)
|
|
|
|
assert element_exists?(doc, ~s/form input[type="text"][name="funnel[name]"]/)
|
|
|
|
|
|
|
|
assert element_exists?(
|
|
|
|
doc,
|
|
|
|
~s/input[type="hidden"][name="funnel[steps][][goal_id]"]#submit-step-1/
|
|
|
|
)
|
|
|
|
|
|
|
|
step_setup_controls = [
|
|
|
|
~s/input[type="hidden"][name="funnel[steps][][goal_id]"]#submit-step-1/,
|
|
|
|
~s/input[type="hidden"][name="funnel[steps][][goal_id]"]#submit-step-2/,
|
|
|
|
~s/input[type="text"][name="display-step-1"]#step-1/,
|
|
|
|
~s/input[type="text"][name="display-step-2"]#step-2/,
|
|
|
|
~s/a[phx-click="add-step"]/
|
|
|
|
]
|
|
|
|
|
|
|
|
Enum.each(step_setup_controls, &assert(element_exists?(doc, &1)))
|
|
|
|
end
|
|
|
|
|
|
|
|
test "clicking 'Add another step' adds a pair of inputs and renders remove step buttons", %{
|
|
|
|
conn: conn,
|
|
|
|
site: site
|
|
|
|
} do
|
|
|
|
setup_goals(site)
|
|
|
|
lv = get_liveview(conn, site)
|
|
|
|
|
|
|
|
lv |> element(~s/button[phx-click="add-funnel"]/) |> render_click()
|
|
|
|
|
|
|
|
assert lv = find_live_child(lv, "funnels-form")
|
|
|
|
lv |> element("form") |> render_change(%{funnel: %{name: "My test funnel"}})
|
|
|
|
doc = lv |> element(~s/a[phx-click="add-step"]/) |> render_click()
|
|
|
|
|
|
|
|
assert element_exists?(
|
|
|
|
doc,
|
|
|
|
~s/input[type="hidden"][name="funnel[steps][][goal_id]"]#submit-step-3/
|
|
|
|
)
|
|
|
|
|
|
|
|
assert element_exists?(doc, ~s/input[type="text"][name="display-step-1"]#step-1/)
|
|
|
|
|
|
|
|
assert element_exists?(
|
|
|
|
doc,
|
|
|
|
~s/svg#remove-step-1[phx-click="remove-step"][phx-value-step-idx="1"]/
|
|
|
|
)
|
|
|
|
|
|
|
|
assert element_exists?(
|
|
|
|
doc,
|
|
|
|
~s/svg#remove-step-2[phx-click="remove-step"][phx-value-step-idx="2"]/
|
|
|
|
)
|
|
|
|
|
|
|
|
assert element_exists?(
|
|
|
|
doc,
|
|
|
|
~s/svg#remove-step-3[phx-click="remove-step"][phx-value-step-idx="3"]/
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
test "clicking the 'remove step' button removes a step", %{site: site, conn: conn} do
|
|
|
|
setup_goals(site)
|
|
|
|
lv = get_liveview(conn, site)
|
|
|
|
|
|
|
|
lv |> element(~s/button[phx-click="add-funnel"]/) |> render_click()
|
|
|
|
assert lv = find_live_child(lv, "funnels-form")
|
|
|
|
lv |> element("form") |> render_change(%{funnel: %{name: "My test funnel"}})
|
|
|
|
lv |> element(~s/a[phx-click="add-step"]/) |> render_click()
|
|
|
|
|
|
|
|
doc = lv |> element(~s/#remove-step-2/) |> render_click()
|
|
|
|
|
|
|
|
assert element_exists?(doc, ~s/input#step-1/)
|
|
|
|
assert element_exists?(doc, ~s/input#step-3/)
|
|
|
|
refute element_exists?(doc, ~s/input#step-2/)
|
|
|
|
end
|
|
|
|
|
|
|
|
test "save button becomes active once at least two steps are selected", %{
|
|
|
|
conn: conn,
|
|
|
|
site: site
|
|
|
|
} do
|
|
|
|
setup_goals(site)
|
|
|
|
lv = get_liveview(conn, site)
|
|
|
|
lv |> element(~s/button[phx-click="add-funnel"]/) |> render_click()
|
|
|
|
|
|
|
|
assert lv = find_live_child(lv, "funnels-form")
|
|
|
|
|
2023-07-04 13:07:16 +03:00
|
|
|
lv
|
2023-09-04 15:56:31 +03:00
|
|
|
|> element("li#dropdown-step-1-option-1 a")
|
2023-07-04 13:07:16 +03:00
|
|
|
|> render_click()
|
|
|
|
|
2023-06-22 10:00:07 +03:00
|
|
|
doc =
|
|
|
|
lv
|
2023-09-04 15:56:31 +03:00
|
|
|
|> element("li#dropdown-step-2-option-1 a")
|
2023-07-04 13:07:16 +03:00
|
|
|
|> render_click()
|
2023-06-22 10:00:07 +03:00
|
|
|
|
|
|
|
save_inactive = ~s/form button#save.cursor-not-allowed/
|
|
|
|
save_active = ~s/form button#save[type="submit"]/
|
|
|
|
|
|
|
|
refute element_exists?(doc, save_active)
|
|
|
|
assert element_exists?(doc, save_inactive)
|
|
|
|
|
|
|
|
doc =
|
|
|
|
lv
|
|
|
|
|> element("form")
|
|
|
|
|> render_change(%{
|
|
|
|
funnel: %{
|
|
|
|
name: "My test funnel",
|
|
|
|
steps: [
|
|
|
|
%{goal_id: 1},
|
|
|
|
%{goal_id: 2}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
assert element_exists?(doc, save_active)
|
|
|
|
refute element_exists?(doc, save_inactive)
|
|
|
|
end
|
|
|
|
|
2023-07-04 13:07:16 +03:00
|
|
|
test "funnel gets evaluated on every select, assuming a second has passed between selections",
|
|
|
|
%{
|
|
|
|
conn: conn,
|
|
|
|
site: site
|
|
|
|
} do
|
|
|
|
setup_goals(site)
|
|
|
|
lv = get_liveview(conn, site)
|
|
|
|
lv |> element(~s/button[phx-click="add-funnel"]/) |> render_click()
|
|
|
|
|
|
|
|
assert lv = find_live_child(lv, "funnels-form")
|
|
|
|
|
2023-09-04 15:56:31 +03:00
|
|
|
lv |> element("li#dropdown-step-1-option-1 a") |> render_click()
|
2023-07-04 13:07:16 +03:00
|
|
|
|
|
|
|
:timer.sleep(1001)
|
|
|
|
|
2023-09-04 15:56:31 +03:00
|
|
|
lv |> element("li#dropdown-step-2-option-1 a") |> render_click()
|
2023-07-04 13:07:16 +03:00
|
|
|
|
|
|
|
doc = lv |> element("#step-eval-0") |> render()
|
|
|
|
assert text_of_element(doc, ~s/#step-eval-0/) =~ "Entering Visitors: 0"
|
|
|
|
|
|
|
|
doc = lv |> element("#step-eval-1") |> render()
|
|
|
|
assert text_of_element(doc, ~s/#step-eval-1/) =~ "Dropoff: 0%"
|
|
|
|
|
|
|
|
doc = lv |> element("#funnel-eval") |> render()
|
|
|
|
assert text_of_element(doc, ~s/#funnel-eval/) =~ "Last month conversion rate: 0%"
|
|
|
|
end
|
|
|
|
|
2023-06-22 10:00:07 +03:00
|
|
|
test "cancel buttons renders the funnel list", %{
|
|
|
|
conn: conn,
|
|
|
|
site: site
|
|
|
|
} do
|
|
|
|
setup_goals(site)
|
|
|
|
lv = get_liveview(conn, site)
|
|
|
|
doc = lv |> element(~s/button[phx-click="add-funnel"]/) |> render_click()
|
|
|
|
|
|
|
|
cancel_button = ~s/button#cancel[phx-click="cancel-add-funnel"]/
|
|
|
|
|
|
|
|
assert element_exists?(doc, cancel_button)
|
|
|
|
|
|
|
|
doc =
|
|
|
|
lv
|
|
|
|
|> element(cancel_button)
|
|
|
|
|> render_click()
|
|
|
|
|
|
|
|
assert doc =~ "No funnels configured for this site yet"
|
|
|
|
assert element_exists?(doc, ~S/button[phx-click="add-funnel"]/)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
defp setup_funnels(site) do
|
|
|
|
{:ok, [g1, g2]} = setup_goals(site)
|
|
|
|
|
2023-06-28 14:20:19 +03:00
|
|
|
{:ok, f1} =
|
2023-06-22 10:00:07 +03:00
|
|
|
Plausible.Funnels.create(
|
|
|
|
site,
|
|
|
|
"From blog to signup",
|
|
|
|
[%{"goal_id" => g1.id}, %{"goal_id" => g2.id}]
|
|
|
|
)
|
|
|
|
|
2023-06-28 14:20:19 +03:00
|
|
|
{:ok, f2} =
|
2023-06-22 10:00:07 +03:00
|
|
|
Plausible.Funnels.create(
|
|
|
|
site,
|
|
|
|
"From signup to blog",
|
|
|
|
[%{"goal_id" => g2.id}, %{"goal_id" => g1.id}]
|
|
|
|
)
|
|
|
|
|
2023-06-28 14:20:19 +03:00
|
|
|
{:ok, [f1.id, f2.id]}
|
2023-06-22 10:00:07 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
defp setup_goals(site) do
|
|
|
|
{:ok, g1} = Plausible.Goals.create(site, %{"page_path" => "/go/to/blog/**"})
|
|
|
|
{:ok, g2} = Plausible.Goals.create(site, %{"event_name" => "Signup"})
|
|
|
|
{:ok, [g1, g2]}
|
|
|
|
end
|
|
|
|
|
2023-06-28 14:20:19 +03:00
|
|
|
defp get_liveview(conn, site, opts \\ []) do
|
2023-06-22 10:00:07 +03:00
|
|
|
conn = assign(conn, :live_module, PlausibleWeb.Live.FunnelSettings)
|
2023-06-28 14:20:19 +03:00
|
|
|
{:ok, lv, html} = live(conn, "/#{site.domain}/settings/funnels")
|
|
|
|
|
|
|
|
if Keyword.get(opts, :with_html?) do
|
|
|
|
{lv, html}
|
|
|
|
else
|
|
|
|
lv
|
|
|
|
end
|
2023-06-22 10:00:07 +03:00
|
|
|
end
|
|
|
|
end
|