Skip to content

Commit 98e0f72

Browse files
sanne-sanaerosol
andauthored
Bring autoconfigure notification a step forwards in custom events cre… (#5912)
* Bring autoconfigure notification a step forwards in custom events creation flow - Rather than showing a notification that custom events have been detected at the bottom of the form, we now show a modal prior to the form, that allows the user to add them instantly or set them up manually. * Added tests for autoconfigure modal * Clean it up a little --------- Co-authored-by: Adam Rutkowski <hq@mtod.org>
1 parent 12d818a commit 98e0f72

3 files changed

Lines changed: 179 additions & 78 deletions

File tree

lib/plausible_web/live/goal_settings/form.ex

Lines changed: 66 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,24 @@ defmodule PlausibleWeb.Live.GoalSettings.Form do
3030
assigns[:goal_type] || "custom_events"
3131
end
3232

33+
event_name_options_count = length(assigns.event_name_options)
34+
35+
show_autoconfigure_modal? =
36+
case form_type do
37+
"custom_events" when event_name_options_count > 0 and is_nil(assigns.goal) ->
38+
true
39+
40+
_ ->
41+
Map.get(socket.assigns, :show_autoconfigure_modal?, false)
42+
end
43+
3344
socket =
3445
socket
3546
|> assign(
3647
id: assigns.id,
3748
context_unique_id: assigns.context_unique_id,
3849
form: form,
39-
event_name_options_count: length(assigns.event_name_options),
50+
event_name_options_count: event_name_options_count,
4051
event_name_options: Enum.map(assigns.event_name_options, &{&1, &1}),
4152
current_user: assigns.current_user,
4253
site_team: assigns.site_team,
@@ -48,7 +59,8 @@ defmodule PlausibleWeb.Live.GoalSettings.Form do
4859
on_save_goal: assigns.on_save_goal,
4960
on_autoconfigure: assigns.on_autoconfigure,
5061
goal: assigns.goal,
51-
goal_type: assigns[:goal_type]
62+
goal_type: assigns[:goal_type],
63+
show_autoconfigure_modal?: show_autoconfigure_modal?
5264
)
5365

5466
{:ok, socket}
@@ -61,7 +73,8 @@ defmodule PlausibleWeb.Live.GoalSettings.Form do
6173
~H"""
6274
<div id={@id}>
6375
{if @goal, do: edit_form(assigns)}
64-
{if is_nil(@goal), do: create_form(assigns)}
76+
{if is_nil(@goal) && @show_autoconfigure_modal?, do: autoconfigure_modal(assigns)}
77+
{if is_nil(@goal) && not @show_autoconfigure_modal?, do: create_form(assigns)}
6578
</div>
6679
"""
6780
end
@@ -105,6 +118,41 @@ defmodule PlausibleWeb.Live.GoalSettings.Form do
105118
"""
106119
end
107120

121+
def autoconfigure_modal(assigns) do
122+
~H"""
123+
<div data-test-id="autoconfigure-modal">
124+
<.title>
125+
We detected {@event_name_options_count} custom {if @event_name_options_count == 1,
126+
do: "event",
127+
else: "events"}.
128+
</.title>
129+
130+
<p class="mt-2 py-2 text-sm text-gray-600 dark:text-gray-400 text-pretty">
131+
These events have been sent from your site in the past 6 months but aren't yet configured as goals. Add them instantly or set one up manually.
132+
</p>
133+
134+
<div class="flex justify-end gap-3">
135+
<.button
136+
theme="secondary"
137+
phx-click="add-manually"
138+
phx-target={@myself}
139+
>
140+
Add manually
141+
</.button>
142+
<.button
143+
phx-click="autoconfigure"
144+
phx-target={@myself}
145+
>
146+
<Heroicons.plus class="size-4" />
147+
Add {@event_name_options_count} {if @event_name_options_count == 1,
148+
do: "event",
149+
else: "events"}
150+
</.button>
151+
</div>
152+
</div>
153+
"""
154+
end
155+
108156
def create_form(assigns) do
109157
~H"""
110158
<.form :let={f} for={@form} phx-submit="save-goal" phx-target={@myself}>
@@ -139,20 +187,6 @@ defmodule PlausibleWeb.Live.GoalSettings.Form do
139187
<.button type="submit" class="w-full">
140188
Add goal
141189
</.button>
142-
143-
<button
144-
:if={@form_type == "custom_events" && @event_name_options_count > 0}
145-
class="mt-4 text-sm hover:underline text-indigo-600 dark:text-indigo-400 text-left"
146-
phx-click="autoconfigure"
147-
phx-target={@myself}
148-
>
149-
<span :if={@event_name_options_count > 1}>
150-
Already sending custom events? We've found {@event_name_options_count} custom events from the last 6 months that are not yet configured as goals. Click here to add them.
151-
</span>
152-
<span :if={@event_name_options_count == 1}>
153-
Already sending custom events? We've found 1 custom event from the last 6 months that is not yet configured as a goal. Click here to add it.
154-
</span>
155-
</button>
156190
</.form>
157191
"""
158192
end
@@ -166,13 +200,14 @@ defmodule PlausibleWeb.Live.GoalSettings.Form do
166200
def pageview_fields(assigns) do
167201
~H"""
168202
<div id="pageviews-form" class="py-2" {@rest}>
169-
<div class="text-sm pb-6 text-gray-500 dark:text-gray-400 text-justify rounded-md">
170-
Pageview goals allow you to measure how many people visit a specific page or section of your site. Learn more in <.styled_link
203+
<div class="text-sm pb-6 text-gray-600 dark:text-gray-400 text-pretty">
204+
Pageview goals allow you to measure how many people visit a specific page or section of your site.
205+
<.styled_link
171206
href="https://plausible.io/docs/pageview-goals"
172207
new_tab={true}
173208
>
174-
our docs
175-
</.styled_link>.
209+
Learn more
210+
</.styled_link>
176211
</div>
177212
178213
<.label for={"page_path_input_#{@suffix}"}>
@@ -245,12 +280,13 @@ defmodule PlausibleWeb.Live.GoalSettings.Form do
245280
~H"""
246281
<div id="scroll-form" class="py-2" x-data={@js} {@rest}>
247282
<div class="text-sm pb-6 text-gray-500 dark:text-gray-400 text-justify rounded-md">
248-
Scroll Depth goals allow you to see how many people scroll beyond your desired scroll depth percentage threshold. Learn more in <.styled_link
283+
Scroll Depth goals allow you to see how many people scroll beyond your desired scroll depth percentage threshold.
284+
<.styled_link
249285
href="https://plausible.io/docs/scroll-depth"
250286
new_tab={true}
251287
>
252-
our docs
253-
</.styled_link>.
288+
Learn more
289+
</.styled_link>
254290
</div>
255291
256292
<.label for={"scroll_threshold_input_#{@suffix}"}>
@@ -460,9 +496,14 @@ defmodule PlausibleWeb.Live.GoalSettings.Form do
460496
end
461497

462498
def handle_event("autoconfigure", _params, socket) do
499+
socket = assign(socket, show_autoconfigure_modal?: false)
463500
{:noreply, socket.assigns.on_autoconfigure.(socket)}
464501
end
465502

503+
def handle_event("add-manually", _params, socket) do
504+
{:noreply, assign(socket, show_autoconfigure_modal?: false)}
505+
end
506+
466507
def suggest_page_paths(input, site) do
467508
query =
468509
Plausible.Stats.Query.parse_and_build!(
@@ -523,7 +564,7 @@ defmodule PlausibleWeb.Live.GoalSettings.Form do
523564
disabled={not @has_access_to_revenue_goals?}
524565
/>
525566
<span class={[
526-
"ml-3 text-sm font-medium",
567+
"ml-3 text-sm/6 font-medium",
527568
if(@has_access_to_revenue_goals?,
528569
do: "text-gray-900 dark:text-gray-100",
529570
else: "text-gray-500 dark:text-gray-400"

test/plausible_web/live/goal_settings/form_test.exs

Lines changed: 113 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ defmodule PlausibleWeb.Live.GoalSettings.FormTest do
125125
end
126126

127127
test "creates a custom event", %{conn: conn, site: site} do
128-
lv = get_liveview(conn, site)
128+
lv = get_liveview(conn, site) |> open_modal_with_goal_type("custom_events")
129129
refute render(lv) =~ "SampleCustomEvent"
130130

131131
lv = open_modal_with_goal_type(lv, "custom_events")
@@ -148,7 +148,7 @@ defmodule PlausibleWeb.Live.GoalSettings.FormTest do
148148
new_site(team: team)
149149
site = new_consolidated_view(team)
150150

151-
lv = get_liveview(conn, site)
151+
lv = get_liveview(conn, site) |> open_modal_with_goal_type("custom_events")
152152

153153
assert render(lv) =~ "Add goal for consolidated view"
154154
refute element_exists?(render(lv), @revenue_goal_settings)
@@ -165,7 +165,7 @@ defmodule PlausibleWeb.Live.GoalSettings.FormTest do
165165

166166
@tag :ee_only
167167
test "creates a revenue goal", %{conn: conn, site: site} do
168-
lv = get_liveview(conn, site)
168+
lv = get_liveview(conn, site) |> open_modal_with_goal_type("custom_events")
169169
refute render(lv) =~ "SampleRevenueGoal"
170170

171171
assert element_exists?(render(lv), @revenue_goal_settings)
@@ -188,7 +188,7 @@ defmodule PlausibleWeb.Live.GoalSettings.FormTest do
188188
end
189189

190190
test "creates a pageview goal", %{conn: conn, site: site} do
191-
lv = get_liveview(conn, site)
191+
lv = get_liveview(conn, site) |> open_modal_with_goal_type("pageviews")
192192
refute render(lv) =~ "Visit /page/**"
193193

194194
lv
@@ -205,7 +205,7 @@ defmodule PlausibleWeb.Live.GoalSettings.FormTest do
205205
test "fails to create a goal above limit", %{conn: conn, site: site} do
206206
for i <- 1..10, do: {:ok, _} = Plausible.Goals.create(site, %{"event_name" => "G#{i}"})
207207

208-
lv = get_liveview(conn, site)
208+
lv = get_liveview(conn, site) |> open_modal_with_goal_type("pageviews")
209209
refute render(lv) =~ "Visit /page/**"
210210

211211
lv
@@ -423,6 +423,10 @@ defmodule PlausibleWeb.Live.GoalSettings.FormTest do
423423

424424
lv = get_liveview(conn, site) |> open_modal_with_goal_type("custom_events")
425425

426+
lv
427+
|> element("button[phx-click='add-manually']")
428+
|> render_click()
429+
426430
type_into_combo(lv, "event_name_input_modalseq0", "One")
427431
html = render(lv)
428432

@@ -446,14 +450,117 @@ defmodule PlausibleWeb.Live.GoalSettings.FormTest do
446450

447451
# Delete the goal
448452
goal = Plausible.Repo.get_by(Plausible.Goal, site_id: site.id, event_name: "EventOne")
449-
html = lv |> element(~s/button#delete-goal-#{goal.id}/) |> render_click()
453+
454+
lv |> element(~s/button#delete-goal-#{goal.id}/) |> render_click()
455+
lv |> element("button[phx-click='add-manually']") |> render_click()
456+
457+
html = render(lv)
450458

451459
assert text_of_element(html, "#goals-form-modalseq0") =~ "EventOne"
452460
refute text_of_element(html, "#goals-form-modalseq0") =~ "EventTwo"
453461
refute text_of_element(html, "#goals-form-modalseq0") =~ "EventThree"
454462
end
455463
end
456464

465+
describe "Autoconfigure goals from custom events modal" do
466+
setup [:create_user, :log_in, :create_site]
467+
468+
test "shows autoconfigure modal when opening custom events modal with available events", %{
469+
conn: conn,
470+
site: site
471+
} do
472+
populate_stats(site, [
473+
build(:pageview, pathname: "/go/home"),
474+
build(:event, name: "Signup"),
475+
build(:event, name: "Newsletter Signup"),
476+
build(:event, name: "Purchase")
477+
])
478+
479+
lv = get_liveview(conn, site) |> open_modal_with_goal_type("custom_events")
480+
481+
html = render(lv)
482+
assert html =~ "We detected 3 custom"
483+
assert html =~ "These events have been sent from your site in the past 6 months"
484+
end
485+
486+
test "clicking 'Add manually' shows the regular form", %{conn: conn, site: site} do
487+
populate_stats(site, [
488+
build(:pageview, pathname: "/go/home"),
489+
build(:event, name: "Signup"),
490+
build(:event, name: "Newsletter Signup"),
491+
build(:event, name: "Purchase")
492+
])
493+
494+
lv = get_liveview(conn, site) |> open_modal_with_goal_type("custom_events")
495+
496+
html = render(lv)
497+
assert html =~ "We detected 3 custom"
498+
499+
lv
500+
|> element("button[phx-click='add-manually']")
501+
|> render_click()
502+
503+
html = render(lv)
504+
refute html =~ "We detected"
505+
assert html =~ "Add goal for"
506+
end
507+
508+
test "autoconfigure button adds all events", %{conn: conn, site: site} do
509+
populate_stats(site, [
510+
build(:pageview, pathname: "/go/home"),
511+
build(:event, name: "Signup"),
512+
build(:event, name: "Newsletter Signup"),
513+
build(:event, name: "Purchase")
514+
])
515+
516+
lv = get_liveview(conn, site) |> open_modal_with_goal_type("custom_events")
517+
518+
lv
519+
|> element("button[phx-click='autoconfigure']")
520+
|> render_click()
521+
522+
# Render again to process the async :autoconfigure message
523+
_html = render(lv)
524+
525+
goals = Plausible.Goals.for_site(site)
526+
assert length(goals) == 3
527+
assert Enum.any?(goals, &(&1.event_name == "Signup"))
528+
assert Enum.any?(goals, &(&1.event_name == "Newsletter Signup"))
529+
assert Enum.any?(goals, &(&1.event_name == "Purchase"))
530+
end
531+
532+
test "autoconfigure modal does not show when all events are already goals", %{
533+
conn: conn,
534+
site: site
535+
} do
536+
populate_stats(site, [
537+
build(:pageview, pathname: "/go/home"),
538+
build(:event, name: "Signup"),
539+
build(:event, name: "Newsletter Signup"),
540+
build(:event, name: "Purchase")
541+
])
542+
543+
lv = get_liveview(conn, site)
544+
html = render(lv)
545+
546+
assert element_exists?(html, "[data-test-id='autoconfigure-modal']")
547+
548+
lv
549+
|> element("button[phx-click='autoconfigure']")
550+
|> render_click()
551+
552+
html = render(lv)
553+
refute element_exists?(html, "[data-test-id='autoconfigure-modal']")
554+
555+
lv = open_modal_with_goal_type(lv, "custom_events")
556+
html = render(lv)
557+
558+
refute element_exists?(html, "[data-test-id='autoconfigure-modal']")
559+
refute html =~ "We detected"
560+
refute html =~ "from the last 6 months"
561+
end
562+
end
563+
457564
defp type_into_combo(lv, id, text) do
458565
lv
459566
|> element("input##{id}")

test/plausible_web/live/goal_settings_test.exs

Lines changed: 0 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -246,53 +246,6 @@ defmodule PlausibleWeb.Live.GoalSettingsTest do
246246

247247
refute html =~ "No goals found for this site. Please refine or"
248248
end
249-
250-
test "auto-configuring custom event goals", %{conn: conn, site: site} do
251-
populate_stats(site, [
252-
build(:event, name: "Signup"),
253-
build(:event, name: "Newsletter Signup"),
254-
build(:event, name: "Purchase")
255-
])
256-
257-
autoconfigure_button_selector = ~s/button[phx-click="autoconfigure"]/
258-
259-
assert_suggested_event_name_count = fn html, number ->
260-
assert text_of_element(html, autoconfigure_button_selector) =~
261-
"found #{number} custom events from the last 6 months that are not yet configured as goals"
262-
end
263-
264-
{lv, html} = get_liveview(conn, site, with_html?: true)
265-
266-
# At first, 3 event names are suggested
267-
assert_suggested_event_name_count.(html, 3)
268-
269-
# Add one goal
270-
lv
271-
|> element("#goals-form-modal form")
272-
|> render_submit(%{goal: %{event_name: "Signup"}})
273-
274-
html = render(lv)
275-
276-
# Now two goals are suggested because one is already added
277-
assert_suggested_event_name_count.(html, 2)
278-
279-
# Delete the goal
280-
goal = Plausible.Repo.get_by(Plausible.Goal, site_id: site.id, event_name: "Signup")
281-
html = lv |> element(~s/button#delete-goal-#{goal.id}/) |> render_click()
282-
283-
# Suggested event name count should be 3 again
284-
assert_suggested_event_name_count.(html, 3)
285-
286-
# Autoconfigure all custom event goals
287-
lv
288-
|> element(autoconfigure_button_selector)
289-
|> render_click()
290-
291-
html = render(lv)
292-
293-
# All possible goals exist - no suggestions anymore
294-
refute html =~ "from the last 6 months"
295-
end
296249
end
297250

298251
on_ee do

0 commit comments

Comments
 (0)