Implement new goal settings
parent
e1f8287481
commit
fa5ed72cce
@ -0,0 +1,106 @@
|
||||
defmodule PlausibleWeb.Live.Components.Form do
|
||||
use Phoenix.Component
|
||||
|
||||
@doc """
|
||||
Renders an input with label and error messages.
|
||||
|
||||
A `Phoenix.HTML.FormField` may be passed as argument,
|
||||
which is used to retrieve the input name, id, and values.
|
||||
Otherwise all attributes may be passed explicitly.
|
||||
|
||||
## Examples
|
||||
|
||||
<.input field={@form[:email]} type="email" />
|
||||
<.input name="my-input" errors={["oh no!"]} />
|
||||
"""
|
||||
attr(:id, :any, default: nil)
|
||||
attr(:name, :any)
|
||||
attr(:label, :string, default: nil)
|
||||
attr(:value, :any)
|
||||
|
||||
attr(:type, :string,
|
||||
default: "text",
|
||||
values: ~w(checkbox color date datetime-local email file hidden month number password
|
||||
range radio search select tel text textarea time url week)
|
||||
)
|
||||
|
||||
attr(:field, Phoenix.HTML.FormField,
|
||||
doc: "a form field struct retrieved from the form, for example: @form[:email]"
|
||||
)
|
||||
|
||||
attr(:errors, :list, default: [])
|
||||
attr(:checked, :boolean, doc: "the checked flag for checkbox inputs")
|
||||
attr(:prompt, :string, default: nil, doc: "the prompt for select inputs")
|
||||
attr(:options, :list, doc: "the options to pass to Phoenix.HTML.Form.options_for_select/2")
|
||||
attr(:multiple, :boolean, default: false, doc: "the multiple flag for select inputs")
|
||||
|
||||
attr(:rest, :global,
|
||||
include: ~w(accept autocomplete capture cols disabled form list max maxlength min minlength
|
||||
multiple pattern placeholder readonly required rows size step)
|
||||
)
|
||||
|
||||
slot(:inner_block)
|
||||
|
||||
def input(%{field: %Phoenix.HTML.FormField{} = field} = assigns) do
|
||||
assigns
|
||||
|> assign(field: nil, id: assigns.id || field.id)
|
||||
|> assign(:errors, Enum.map(field.errors, &translate_error(&1)))
|
||||
|> assign_new(:name, fn -> if assigns.multiple, do: field.name <> "[]", else: field.name end)
|
||||
|> assign_new(:value, fn -> field.value end)
|
||||
|> input()
|
||||
end
|
||||
|
||||
# All other inputs text, datetime-local, url, password, etc. are handled here...
|
||||
def input(assigns) do
|
||||
~H"""
|
||||
<div phx-feedback-for={@name}>
|
||||
<.label for={@id}>
|
||||
<%= @label %>
|
||||
</.label>
|
||||
<input
|
||||
type={@type}
|
||||
name={@name}
|
||||
id={@id}
|
||||
value={Phoenix.HTML.Form.normalize_value(@type, @value)}
|
||||
{@rest}
|
||||
/>
|
||||
<.error :for={msg <- @errors}>
|
||||
<%= msg %>
|
||||
</.error>
|
||||
</div>
|
||||
"""
|
||||
end
|
||||
|
||||
@doc """
|
||||
Renders a label.
|
||||
"""
|
||||
attr(:for, :string, default: nil)
|
||||
slot(:inner_block, required: true)
|
||||
|
||||
def label(assigns) do
|
||||
~H"""
|
||||
<label for={@for} class="block font-medium dark:text-gray-100">
|
||||
<%= render_slot(@inner_block) %>
|
||||
</label>
|
||||
"""
|
||||
end
|
||||
|
||||
@doc """
|
||||
Generates a generic error message.
|
||||
"""
|
||||
slot(:inner_block, required: true)
|
||||
|
||||
def error(assigns) do
|
||||
~H"""
|
||||
<p class="flex gap-3 text-sm leading-6 text-red-500 phx-no-feedback:hidden">
|
||||
<%= render_slot(@inner_block) %>
|
||||
</p>
|
||||
"""
|
||||
end
|
||||
|
||||
def translate_error({msg, opts}) do
|
||||
Enum.reduce(opts, msg, fn {key, value}, acc ->
|
||||
String.replace(acc, "%{#{key}}", fn _ -> to_string(value) end)
|
||||
end)
|
||||
end
|
||||
end
|
||||
@ -0,0 +1,128 @@
|
||||
defmodule PlausibleWeb.Live.GoalSettings do
|
||||
@moduledoc """
|
||||
LiveView allowing listing, creating and deleting goals.
|
||||
"""
|
||||
use Phoenix.LiveView
|
||||
use Phoenix.HTML
|
||||
|
||||
use Plausible.Funnel
|
||||
|
||||
alias Plausible.{Sites, Goals}
|
||||
|
||||
def mount(
|
||||
_params,
|
||||
%{"site_id" => _site_id, "domain" => domain, "current_user_id" => user_id},
|
||||
socket
|
||||
) do
|
||||
site =
|
||||
if Plausible.Auth.is_super_admin?(user_id) do
|
||||
Sites.get_by_domain(domain)
|
||||
else
|
||||
Sites.get_for_user!(user_id, domain, [:owner, :admin])
|
||||
end
|
||||
|
||||
goals = Goals.for_site(site, preload_funnels?: true)
|
||||
|
||||
{:ok,
|
||||
assign(socket,
|
||||
site_id: site.id,
|
||||
domain: site.domain,
|
||||
goals: goals,
|
||||
list: goals,
|
||||
add_goal?: false,
|
||||
current_user_id: user_id,
|
||||
filter_text: ""
|
||||
)}
|
||||
end
|
||||
|
||||
# Flash sharing with live views within dead views can be done via re-rendering the flash partial.
|
||||
# Normally, we'd have to use live_patch which we can't do with views unmounted at the router it seems.
|
||||
def render(assigns) do
|
||||
~H"""
|
||||
<div id="goal-settings-main">
|
||||
<.live_component id="embedded_liveview_flash" module={PlausibleWeb.Live.Flash} flash={@flash} />
|
||||
<%= if @add_goal? do %>
|
||||
<%= live_render(
|
||||
@socket,
|
||||
PlausibleWeb.Live.GoalSettings.Form,
|
||||
id: "goals-form",
|
||||
session: %{
|
||||
"current_user_id" => @current_user_id,
|
||||
"domain" => @domain,
|
||||
"site_id" => @site_id,
|
||||
"rendered_by" => self()
|
||||
}
|
||||
) %>
|
||||
<% end %>
|
||||
<.live_component
|
||||
module={PlausibleWeb.Live.GoalSettings.List}
|
||||
id="goals-list"
|
||||
goals={@list}
|
||||
domain={@domain}
|
||||
filter_text={@filter_text}
|
||||
/>
|
||||
</div>
|
||||
"""
|
||||
end
|
||||
|
||||
def handle_event("reset-filter-text", _params, socket) do
|
||||
{:noreply, assign(socket, filter_text: "", list: socket.assigns.goals)}
|
||||
end
|
||||
|
||||
def handle_event("filter", %{"filter-text" => filter_text}, socket) do
|
||||
new_list =
|
||||
PlausibleWeb.Live.Components.ComboBox.StaticSearch.suggest(
|
||||
filter_text,
|
||||
socket.assigns.goals
|
||||
)
|
||||
|
||||
{:noreply, assign(socket, list: new_list, filter_text: filter_text)}
|
||||
end
|
||||
|
||||
def handle_event("add-goal", _value, socket) do
|
||||
{:noreply, assign(socket, add_goal?: true)}
|
||||
end
|
||||
|
||||
def handle_event("delete-goal", %{"goal-id" => goal_id}, socket) do
|
||||
goal_id = String.to_integer(goal_id)
|
||||
|
||||
case Plausible.Goals.delete(goal_id, socket.assigns.site_id) do
|
||||
:ok ->
|
||||
socket =
|
||||
socket
|
||||
|> put_flash(:success, "Goal deleted successfully")
|
||||
|> assign(
|
||||
goals: Enum.reject(socket.assigns.goals, &(&1.id == goal_id)),
|
||||
list: Enum.reject(socket.assigns.list, &(&1.id == goal_id))
|
||||
)
|
||||
|
||||
Process.send_after(self(), :clear_flash, 5000)
|
||||
{:noreply, socket}
|
||||
|
||||
_ ->
|
||||
{:noreply, socket}
|
||||
end
|
||||
end
|
||||
|
||||
def handle_info(:cancel_add_goal, socket) do
|
||||
{:noreply, assign(socket, add_goal?: false)}
|
||||
end
|
||||
|
||||
def handle_info({:goal_added, goal}, socket) do
|
||||
socket =
|
||||
socket
|
||||
|> assign(
|
||||
add_goal?: false,
|
||||
filter_text: "",
|
||||
goals: [goal | socket.assigns.goals],
|
||||
list: [goal | socket.assigns.goals]
|
||||
)
|
||||
|> put_flash(:success, "Goal saved successfully")
|
||||
|
||||
{:noreply, socket}
|
||||
end
|
||||
|
||||
def handle_info(:clear_flash, socket) do
|
||||
{:noreply, clear_flash(socket)}
|
||||
end
|
||||
end
|
||||
@ -0,0 +1,274 @@
|
||||
defmodule PlausibleWeb.Live.GoalSettings.Form do
|
||||
@moduledoc """
|
||||
Live view for the goal creation form
|
||||
"""
|
||||
use Phoenix.LiveView
|
||||
import PlausibleWeb.Live.Components.Form
|
||||
alias PlausibleWeb.Live.Components.ComboBox
|
||||
|
||||
alias Plausible.Repo
|
||||
|
||||
def mount(
|
||||
_params,
|
||||
%{
|
||||
"site_id" => _site_id,
|
||||
"current_user_id" => user_id,
|
||||
"domain" => domain,
|
||||
"rendered_by" => pid
|
||||
},
|
||||
socket
|
||||
) do
|
||||
form = to_form(Plausible.Goal.changeset(%Plausible.Goal{}))
|
||||
|
||||
site =
|
||||
if Plausible.Auth.is_super_admin?(user_id) do
|
||||
Plausible.Sites.get_by_domain(domain)
|
||||
else
|
||||
Plausible.Sites.get_for_user!(user_id, domain, [:owner, :admin])
|
||||
end
|
||||
|
||||
initial_suggestions = suggest("", nil, site)
|
||||
|
||||
{:ok,
|
||||
assign(socket,
|
||||
current_user: Repo.get(Plausible.Auth.User, user_id),
|
||||
form: form,
|
||||
suggestions: initial_suggestions,
|
||||
domain: domain,
|
||||
rendered_by: pid,
|
||||
tabs: %{custom_events: true, pageviews: false},
|
||||
site: site
|
||||
)}
|
||||
end
|
||||
|
||||
def render(assigns) do
|
||||
~H"""
|
||||
<div
|
||||
class="fixed inset-0 bg-gray-500 bg-opacity-75 transition-opacity"
|
||||
phx-window-keydown="cancel-add-goal"
|
||||
phx-key="Escape"
|
||||
>
|
||||
</div>
|
||||
<div class="fixed inset-0 flex items-center justify-center mt-32 z-50">
|
||||
<div class="w-1/2 h-full">
|
||||
<.form
|
||||
:let={f}
|
||||
for={@form}
|
||||
class="max-w-md w-full mx-auto bg-white dark:bg-gray-800 shadow-md rounded px-8 pt-6 pb-8 mb-4 mt-8"
|
||||
phx-submit="save-goal"
|
||||
phx-click-away="cancel-add-goal"
|
||||
>
|
||||
<h2 class="text-xl font-black dark:text-gray-100">Add goal for <%= @domain %></h2>
|
||||
|
||||
<.tabs tabs={@tabs} />
|
||||
|
||||
<.custom_event_fields :if={@tabs.custom_events} f={f} />
|
||||
<.pageview_fields :if={@tabs.pageviews} f={f} suggestions={@suggestions} site={@site} />
|
||||
|
||||
<div class="py-4">
|
||||
<button type="submit" class="button text-base font-bold w-full">
|
||||
Add goal →
|
||||
</button>
|
||||
</div>
|
||||
</.form>
|
||||
</div>
|
||||
</div>
|
||||
"""
|
||||
end
|
||||
|
||||
attr(:f, Phoenix.HTML.Form)
|
||||
attr(:suggestions, :list)
|
||||
attr(:site, Plausible.Site)
|
||||
|
||||
def pageview_fields(assigns) do
|
||||
~H"""
|
||||
<div class="py-2">
|
||||
<.label for="page_path_input">
|
||||
Page path
|
||||
</.label>
|
||||
|
||||
<.live_component
|
||||
id="page_path_input"
|
||||
submit_name="goal[page_path]"
|
||||
class={[
|
||||
"py-2"
|
||||
]}
|
||||
module={ComboBox}
|
||||
options={@suggestions}
|
||||
suggest_fun={fn input, options -> suggest(input, options, @site) end}
|
||||
creatable
|
||||
/>
|
||||
|
||||
<.error :for={{msg, opts} <- @f[:page_path].errors}>
|
||||
<%= Enum.reduce(opts, msg, fn {key, value}, acc ->
|
||||
String.replace(acc, "%{#{key}}", fn _ -> to_string(value) end)
|
||||
end) %>
|
||||
</.error>
|
||||
</div>
|
||||
"""
|
||||
end
|
||||
|
||||
attr(:f, Phoenix.HTML.Form)
|
||||
|
||||
def custom_event_fields(assigns) do
|
||||
~H"""
|
||||
<div class="my-6">
|
||||
<div id="event-fields">
|
||||
<div class="pb-6 text-xs text-gray-700 dark:text-gray-200 text-justify rounded-md">
|
||||
Custom events are not tracked by default - you have to configure them on your site to be sent to Plausible. See examples and learn more in <a
|
||||
class="text-indigo-500 hover:underline"
|
||||
target="_blank"
|
||||
rel="noreferrer"
|
||||
href="https://plausible.io/docs/custom-event-goals"
|
||||
> our docs</a>.
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<.input
|
||||
autofocus
|
||||
field={@f[:event_name]}
|
||||
label="Event name"
|
||||
class="focus:ring-indigo-500 focus:border-indigo-500 dark:bg-gray-900 dark:text-gray-300 block w-7/12 rounded-md sm:text-sm border-gray-300 dark:border-gray-500 w-full p-2 mt-2"
|
||||
placeholder="e.g. Signup"
|
||||
autocomplete="off"
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div
|
||||
class="mt-6 space-y-3"
|
||||
x-data={
|
||||
Jason.encode!(%{
|
||||
active: !!@f[:currency].value and @f[:currency].value != "",
|
||||
currency: @f[:currency].value
|
||||
})
|
||||
}
|
||||
>
|
||||
<div
|
||||
class="flex items-center w-max cursor-pointer"
|
||||
x-on:click="active = !active; currency = ''"
|
||||
>
|
||||
<button
|
||||
class="relative inline-flex h-6 w-11 flex-shrink-0 rounded-full border-2 border-transparent transition-colors duration-200 ease-in-out focus:outline-none focus:ring-2 focus:ring-indigo-600 focus:ring-offset-2"
|
||||
x-bind:class="active ? 'bg-indigo-600' : 'bg-gray-200'"
|
||||
x-bind:aria-checked="active"
|
||||
aria-labelledby="enable-revenue-tracking"
|
||||
role="switch"
|
||||
type="button"
|
||||
>
|
||||
<span
|
||||
aria-hidden="true"
|
||||
class="pointer-events-none inline-block h-5 w-5 transform rounded-full bg-white shadow ring-0 transition duration-200 ease-in-out"
|
||||
x-bind:class="active ? 'translate-x-5' : 'translate-x-0'"
|
||||
/>
|
||||
</button>
|
||||
<span
|
||||
class="ml-3 font-medium text-gray-900 dark:text-gray-200"
|
||||
id="enable-revenue-tracking"
|
||||
>
|
||||
Enable revenue tracking
|
||||
</span>
|
||||
</div>
|
||||
|
||||
<div class="rounded-md bg-yellow-50 dark:bg-yellow-900 p-4" x-show="active">
|
||||
<p class="text-xs text-yellow-700 dark:text-yellow-50 text-justify">
|
||||
Revenue tracking is an upcoming premium feature that's free-to-use
|
||||
during the private preview. Pricing will be announced soon. See
|
||||
examples and learn more in <a
|
||||
class="font-medium text-yellow underline hover:text-yellow-600"
|
||||
href="https://plausible.io/docs/ecommerce-revenue-tracking"
|
||||
>our docs</a>.
|
||||
</p>
|
||||
</div>
|
||||
|
||||
<div x-show="active">
|
||||
<.live_component
|
||||
id="currency_input"
|
||||
submit_name={@f[:currency].name}
|
||||
module={ComboBox}
|
||||
options={Plausible.Goal.currency_options()}
|
||||
suggest_fun={&PlausibleWeb.Live.Components.ComboBox.StaticSearch.suggest/2}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
"""
|
||||
end
|
||||
|
||||
def tabs(assigns) do
|
||||
~H"""
|
||||
<div class="mt-6 font-medium dark:text-gray-100">Goal trigger</div>
|
||||
<div class="my-3 w-full flex rounded border border-gray-300 dark:border-gray-500">
|
||||
<.custom_events_tab tabs={@tabs} />
|
||||
<.pageviews_tab tabs={@tabs} />
|
||||
</div>
|
||||
"""
|
||||
end
|
||||
|
||||
defp custom_events_tab(assigns) do
|
||||
~H"""
|
||||
<a
|
||||
class={[
|
||||
"w-1/2 text-center py-2 border-r dark:border-gray-500",
|
||||
"cursor-pointer",
|
||||
@tabs.custom_events && "shadow-inner font-bold bg-indigo-600 text-white",
|
||||
!@tabs.custom_events && "dark:text-gray-100 text-gray-800"
|
||||
]}
|
||||
id="event-tab"
|
||||
phx-click="switch-tab"
|
||||
>
|
||||
Custom event
|
||||
</a>
|
||||
"""
|
||||
end
|
||||
|
||||
def pageviews_tab(assigns) do
|
||||
~H"""
|
||||
<a
|
||||
class={[
|
||||
"w-1/2 text-center py-2 cursor-pointer",
|
||||
@tabs.pageviews && "shadow-inner font-bold bg-indigo-600 text-white",
|
||||
!@tabs.pageviews && "dark:text-gray-100 text-gray-800"
|
||||
]}
|
||||
id="pageview-tab"
|
||||
phx-click="switch-tab"
|
||||
>
|
||||
Pageview
|
||||
</a>
|
||||
"""
|
||||
end
|
||||
|
||||
def handle_event("switch-tab", _params, socket) do
|
||||
{:noreply,
|
||||
assign(socket,
|
||||
tabs: %{
|
||||
custom_events: !socket.assigns.tabs.custom_events,
|
||||
pageviews: !socket.assigns.tabs.pageviews
|
||||
}
|
||||
)}
|
||||
end
|
||||
|
||||
def handle_event("save-goal", %{"goal" => goal}, socket) do
|
||||
case Plausible.Goals.create(socket.assigns.site, goal) do
|
||||
{:ok, goal} ->
|
||||
send(socket.assigns.rendered_by, {:goal_added, Map.put(goal, :funnels, [])})
|
||||
{:noreply, socket}
|
||||
|
||||
{:error, changeset} ->
|
||||
{:noreply, assign(socket, form: to_form(changeset))}
|
||||
end
|
||||
end
|
||||
|
||||
def handle_event("cancel-add-goal", _value, socket) do
|
||||
send(socket.assigns.rendered_by, :cancel_add_goal)
|
||||
{:noreply, socket}
|
||||
end
|
||||
|
||||
def suggest(input, _options, site) do
|
||||
query = Plausible.Stats.Query.from(site, %{})
|
||||
|
||||
site
|
||||
|> Plausible.Stats.filter_suggestions(query, "page", input)
|
||||
|> Enum.map(fn %{label: label, value: value} -> {label, value} end)
|
||||
end
|
||||
end
|
||||
@ -0,0 +1,142 @@
|
||||
defmodule PlausibleWeb.Live.GoalSettings.List do
|
||||
@moduledoc """
|
||||
Phoenix LiveComponent module that renders a list of goals
|
||||
"""
|
||||
use Phoenix.LiveComponent
|
||||
use Phoenix.HTML
|
||||
|
||||
use Plausible.Funnel
|
||||
alias Phoenix.LiveView.JS
|
||||
|
||||
attr(:goals, :list, required: true)
|
||||
attr(:domain, :string, required: true)
|
||||
attr(:filter_text, :string)
|
||||
|
||||
def render(assigns) do
|
||||
~H"""
|
||||
<div>
|
||||
<div class="border-t border-gray-200 pt-4 sm:flex sm:items-center sm:justify-between">
|
||||
<form id="filter-form" phx-change="filter">
|
||||
<div class="text-gray-800 text-sm inline-flex items-center">
|
||||
<Heroicons.magnifying_glass
|
||||
class="feather mr-1 dark:text-gray-300"
|
||||
phx-click={JS.focus(to: "#filter-text")}
|
||||
/>
|
||||
<input
|
||||
type="text"
|
||||
name="filter-text"
|
||||
id="filter-text"
|
||||
class={[
|
||||
"border-none rounded-md px-1 py-2 focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm dark:text-gray-300 dark:bg-gray-800",
|
||||
String.trim(@filter_text) != "" &&
|
||||
"bg-gray-100 focus:bg-white dark:bg-gray-850 dark:focus:bg-gray-900"
|
||||
]}
|
||||
placeholder="Search Goals"
|
||||
value={@filter_text}
|
||||
/>
|
||||
<Heroicons.backspace
|
||||
:if={String.trim(@filter_text) != ""}
|
||||
class="feather ml-2 cursor-pointer hover:text-red-500 dark:text-gray-300 dark:hover:text-red-500"
|
||||
phx-click="reset-filter-text"
|
||||
id="reset-filter"
|
||||
/>
|
||||
</div>
|
||||
</form>
|
||||
<div class="mt-3 flex sm:ml-4 sm:mt-0">
|
||||
<button
|
||||
type="button"
|
||||
phx-click="add-goal"
|
||||
class="ml-3 inline-flex items-center rounded-md bg-indigo-600 px-3 py-2 text-sm font-semibold text-white shadow-sm hover:bg-indigo-500 focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-indigo-600"
|
||||
>
|
||||
+ Add Goal
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
<%= if Enum.count(@goals) > 0 do %>
|
||||
<div class="mt-12">
|
||||
<%= for goal <- @goals do %>
|
||||
<div class="border-b border-gray-300 dark:border-gray-500 py-3 flex justify-between">
|
||||
<span class="text-sm font-medium text-gray-900 dark:text-gray-100 w-3/4">
|
||||
<div class="flex">
|
||||
<span class="truncate">
|
||||
<%= goal %>
|
||||
<br />
|
||||
<span class="text-sm text-gray-400 block mt-1 font-normal">
|
||||
<span :if={goal.page_path}>Pageview</span>
|
||||
<span :if={goal.event_name && !goal.currency}>Custom Event</span>
|
||||
<span :if={goal.currency}>
|
||||
Revenue Goal: <%= goal.currency %>
|
||||
</span>
|
||||
<span :if={not Enum.empty?(goal.funnels)}> - belongs to funnel(s)</span>
|
||||
</span>
|
||||
</span>
|
||||
</div>
|
||||
</span>
|
||||
<button
|
||||
id={"delete-goal-#{goal.id}"}
|
||||
phx-click="delete-goal"
|
||||
phx-value-goal-id={goal.id}
|
||||
class="text-sm text-red-600"
|
||||
data-confirm={delete_confirmation_text(goal)}
|
||||
>
|
||||
<svg
|
||||
class="feather feather-sm"
|
||||
xmlns="http://www.w3.org/2000/svg"
|
||||
viewBox="0 0 24 24"
|
||||
fill="none"
|
||||
stroke="currentColor"
|
||||
stroke-width="2"
|
||||
stroke-linecap="round"
|
||||
stroke-linejoin="round"
|
||||
>
|
||||
<polyline points="3 6 5 6 21 6"></polyline>
|
||||
<path d="M19 6v14a2 2 0 0 1-2 2H7a2 2 0 0 1-2-2V6m3 0V4a2 2 0 0 1 2-2h4a2 2 0 0 1 2 2v2">
|
||||
</path>
|
||||
<line x1="10" y1="11" x2="10" y2="17"></line>
|
||||
<line x1="14" y1="11" x2="14" y2="17"></line>
|
||||
</svg>
|
||||
</button>
|
||||
</div>
|
||||
<% end %>
|
||||
</div>
|
||||
<% else %>
|
||||
<p class="text-sm text-gray-800 dark:text-gray-200 mt-12 mb-8 text-center">
|
||||
<span :if={String.trim(@filter_text) != ""}>
|
||||
No goals found for this site. Please refine or
|
||||
<a
|
||||
class="text-indigo-500 cursor-pointer underline"
|
||||
phx-click="reset-filter-text"
|
||||
id="reset-filter-hint"
|
||||
>
|
||||
reset your search.
|
||||
</a>
|
||||
</span>
|
||||
<span :if={String.trim(@filter_text) == "" && Enum.empty?(@goals)}>
|
||||
No goals configured for this site.
|
||||
</span>
|
||||
</p>
|
||||
<% end %>
|
||||
</div>
|
||||
"""
|
||||
end
|
||||
|
||||
defp delete_confirmation_text(goal) do
|
||||
if Enum.empty?(goal.funnels) do
|
||||
"""
|
||||
Are you sure you want to remove the following goal:
|
||||
|
||||
#{goal}
|
||||
|
||||
This will just affect the UI, all of your analytics data will stay intact.
|
||||
"""
|
||||
else
|
||||
"""
|
||||
The goal:
|
||||
|
||||
#{goal}
|
||||
|
||||
is part of some funnel(s). If you are going to delete it, the associated funnels will be either reduced or deleted completely. Are you sure you want to remove the goal?
|
||||
"""
|
||||
end
|
||||
end
|
||||
end
|
||||
@ -0,0 +1,166 @@
|
||||
defmodule PlausibleWeb.Live.GoalSettings.FormTest do
|
||||
use PlausibleWeb.ConnCase, async: true
|
||||
import Phoenix.LiveViewTest
|
||||
import Plausible.Test.Support.HTML
|
||||
|
||||
describe "integration - live rendering" do
|
||||
setup [:create_user, :log_in, :create_site]
|
||||
|
||||
test "tabs switching", %{conn: conn, site: site} do
|
||||
setup_goals(site)
|
||||
lv = get_liveview(conn, site)
|
||||
|
||||
html = lv |> render()
|
||||
|
||||
assert element_exists?(html, ~s/a#pageview-tab/)
|
||||
assert element_exists?(html, ~s/a#event-tab/)
|
||||
|
||||
pageview_tab = lv |> element(~s/a#pageview-tab/) |> render_click()
|
||||
assert pageview_tab =~ "Page path"
|
||||
|
||||
event_tab = lv |> element(~s/a#event-tab/) |> render_click()
|
||||
assert event_tab =~ "Event name"
|
||||
end
|
||||
|
||||
test "escape closes the form", %{conn: conn, site: site} do
|
||||
{parent, lv} = get_liveview(conn, site, with_parent?: true)
|
||||
html = render(parent)
|
||||
assert html =~ "Goal trigger"
|
||||
render_keydown(lv, "cancel-add-goal")
|
||||
html = render(parent)
|
||||
refute html =~ "Goal trigger"
|
||||
end
|
||||
end
|
||||
|
||||
describe "Goal submission" do
|
||||
setup [:create_user, :log_in, :create_site]
|
||||
|
||||
test "renders form fields", %{conn: conn, site: site} do
|
||||
lv = get_liveview(conn, site)
|
||||
html = render(lv)
|
||||
|
||||
[event_name, currency_display, currency_submit] = find(html, "input")
|
||||
|
||||
assert name_of(event_name) == "goal[event_name]"
|
||||
assert name_of(currency_display) == "display-currency_input"
|
||||
assert name_of(currency_submit) == "goal[currency]"
|
||||
|
||||
html = lv |> element(~s/a#pageview-tab/) |> render_click()
|
||||
|
||||
[page_path_display, page_path] = find(html, "input")
|
||||
assert name_of(page_path_display) == "display-page_path_input"
|
||||
assert name_of(page_path) == "goal[page_path]"
|
||||
end
|
||||
|
||||
test "renders error on empty submission", %{conn: conn, site: site} do
|
||||
lv = get_liveview(conn, site)
|
||||
html = lv |> element("form") |> render_submit()
|
||||
assert html =~ "this field is required and cannot be blank"
|
||||
|
||||
pageview_tab = lv |> element(~s/a#pageview-tab/) |> render_click()
|
||||
assert pageview_tab =~ "this field is required and must start with a /"
|
||||
end
|
||||
|
||||
test "creates a custom event", %{conn: conn, site: site} do
|
||||
{parent, lv} = get_liveview(conn, site, with_parent?: true)
|
||||
refute render(parent) =~ "Foo"
|
||||
lv |> element("form") |> render_submit(%{goal: %{event_name: "Foo"}})
|
||||
parent_html = render(parent)
|
||||
assert parent_html =~ "Foo"
|
||||
assert parent_html =~ "Custom Event"
|
||||
end
|
||||
|
||||
test "creates a revenue goal", %{conn: conn, site: site} do
|
||||
{parent, lv} = get_liveview(conn, site, with_parent?: true)
|
||||
refute render(parent) =~ "Foo"
|
||||
lv |> element("form") |> render_submit(%{goal: %{event_name: "Foo", currency: "EUR"}})
|
||||
parent_html = render(parent)
|
||||
assert parent_html =~ "Foo"
|
||||
assert parent_html =~ "Revenue Goal: EUR"
|
||||
end
|
||||
|
||||
test "creates a pageview goal", %{conn: conn, site: site} do
|
||||
{parent, lv} = get_liveview(conn, site, with_parent?: true)
|
||||
refute render(parent) =~ "Foo"
|
||||
lv |> element("form") |> render_submit(%{goal: %{page_path: "/page/**"}})
|
||||
parent_html = render(parent)
|
||||
assert parent_html =~ "Visit /page/**"
|
||||
assert parent_html =~ "Pageview"
|
||||
end
|
||||
end
|
||||
|
||||
describe "Combos integration" do
|
||||
setup [:create_user, :log_in, :create_site]
|
||||
|
||||
test "currency combo works", %{conn: conn, site: site} do
|
||||
lv = get_liveview(conn, site)
|
||||
|
||||
html = type_into_combo(lv, "currency_input", "Polish")
|
||||
|
||||
assert element_exists?(html, ~s/a[phx-value-display-value="PLN - Polish Zloty"]/)
|
||||
refute element_exists?(html, ~s/a[phx-value-display-value="EUR - Euro"]/)
|
||||
|
||||
html = type_into_combo(lv, "currency_input", "Euro")
|
||||
|
||||
refute element_exists?(html, ~s/a[phx-value-display-value="PLN - Polish Zloty"]/)
|
||||
assert element_exists?(html, ~s/a[phx-value-display-value="EUR - Euro"]/)
|
||||
end
|
||||
|
||||
test "pageview combo works", %{conn: conn, site: site} do
|
||||
lv = get_liveview(conn, site)
|
||||
lv |> element(~s/a#pageview-tab/) |> render_click()
|
||||
|
||||
html = type_into_combo(lv, "page_path_input", "/hello")
|
||||
|
||||
assert html =~ "Create "/hello""
|
||||
end
|
||||
|
||||
test "pageview combo uses filter suggestions", %{conn: conn, site: site} do
|
||||
populate_stats(site, [
|
||||
build(:pageview, pathname: "/go/to/page/1"),
|
||||
build(:pageview, pathname: "/go/home")
|
||||
])
|
||||
|
||||
lv = get_liveview(conn, site)
|
||||
lv |> element(~s/a#pageview-tab/) |> render_click()
|
||||
|
||||
html = type_into_combo(lv, "page_path_input", "/go/to/p")
|
||||
assert html =~ "Create "/go/to/p""
|
||||
assert html =~ "/go/to/page/1"
|
||||
refute html =~ "/go/home"
|
||||
|
||||
html = type_into_combo(lv, "page_path_input", "/go/h")
|
||||
assert html =~ "/go/home"
|
||||
refute html =~ "/go/to/page/1"
|
||||
end
|
||||
end
|
||||
|
||||
defp type_into_combo(lv, id, text) do
|
||||
lv
|
||||
|> element("input##{id}")
|
||||
|> render_change(%{
|
||||
"_target" => ["display-#{id}"],
|
||||
"display-#{id}" => "#{text}"
|
||||
})
|
||||
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, g3} = Plausible.Goals.create(site, %{"event_name" => "Purchase", "currency" => "EUR"})
|
||||
{:ok, [g1, g2, g3]}
|
||||
end
|
||||
|
||||
defp get_liveview(conn, site, opts \\ []) do
|
||||
conn = assign(conn, :live_module, PlausibleWeb.Live.GoalSettings)
|
||||
{:ok, lv, _html} = live(conn, "/#{site.domain}/settings/goals")
|
||||
lv |> element(~s/button[phx-click="add-goal"]/) |> render_click()
|
||||
assert form_view = find_live_child(lv, "goals-form")
|
||||
|
||||
if opts[:with_parent?] do
|
||||
{lv, form_view}
|
||||
else
|
||||
form_view
|
||||
end
|
||||
end
|
||||
end
|
||||
@ -0,0 +1,177 @@
|
||||
defmodule PlausibleWeb.Live.GoalSettingsTest do
|
||||
use PlausibleWeb.ConnCase, async: true
|
||||
import Phoenix.LiveViewTest
|
||||
import Plausible.Test.Support.HTML
|
||||
|
||||
describe "GET /:website/settings/goals" do
|
||||
setup [:create_user, :log_in, :create_site]
|
||||
|
||||
test "lists goals for the site and renders links", %{conn: conn, site: site} do
|
||||
{:ok, [g1, g2, g3]} = setup_goals(site)
|
||||
conn = get(conn, "/#{site.domain}/settings/goals")
|
||||
|
||||
resp = html_response(conn, 200)
|
||||
assert resp =~ "Define actions that you want your users to take"
|
||||
assert resp =~ "compose goals into funnels"
|
||||
assert resp =~ "/#{site.domain}/settings/funnels"
|
||||
assert element_exists?(resp, ~s|a[href="https://plausible.io/docs/goal-conversions"]|)
|
||||
|
||||
assert resp =~ to_string(g1)
|
||||
assert resp =~ "Pageview"
|
||||
assert resp =~ to_string(g2)
|
||||
assert resp =~ "Custom Event"
|
||||
assert resp =~ to_string(g3)
|
||||
assert resp =~ "Revenue Goal: EUR"
|
||||
end
|
||||
|
||||
test "lists goals with delete actions", %{conn: conn, site: site} do
|
||||
{:ok, goals} = setup_goals(site)
|
||||
conn = get(conn, "/#{site.domain}/settings/goals")
|
||||
resp = html_response(conn, 200)
|
||||
|
||||
for g <- goals do
|
||||
assert element_exists?(
|
||||
resp,
|
||||
~s/button[phx-click="delete-goal"][phx-value-goal-id=#{g.id}]#delete-goal-#{g.id}/
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
test "if no goals are present, a proper info is displayed", %{conn: conn, site: site} do
|
||||
conn = get(conn, "/#{site.domain}/settings/goals")
|
||||
resp = html_response(conn, 200)
|
||||
assert resp =~ "No goals configured for this site"
|
||||
end
|
||||
|
||||
test "if goals are present, no info about missing goals is displayed", %{
|
||||
conn: conn,
|
||||
site: site
|
||||
} do
|
||||
{:ok, _goals} = setup_goals(site)
|
||||
conn = get(conn, "/#{site.domain}/settings/goals")
|
||||
resp = html_response(conn, 200)
|
||||
refute resp =~ "No goals configured for this site"
|
||||
end
|
||||
|
||||
test "add goal button is rendered", %{conn: conn, site: site} do
|
||||
conn = get(conn, "/#{site.domain}/settings/goals")
|
||||
resp = html_response(conn, 200)
|
||||
assert element_exists?(resp, ~s/button[phx-click="add-goal"]/)
|
||||
end
|
||||
|
||||
test "search goals input is rendered", %{conn: conn, site: site} do
|
||||
conn = get(conn, "/#{site.domain}/settings/goals")
|
||||
resp = html_response(conn, 200)
|
||||
assert element_exists?(resp, ~s/input[type="text"]#filter-text/)
|
||||
assert element_exists?(resp, ~s/form[phx-change="filter"]#filter-form/)
|
||||
end
|
||||
end
|
||||
|
||||
describe "GoalSettings live view" do
|
||||
setup [:create_user, :log_in, :create_site]
|
||||
|
||||
test "allows goal deletion", %{conn: conn, site: site} do
|
||||
{:ok, [g1, g2 | _]} = setup_goals(site)
|
||||
{lv, html} = get_liveview(conn, site, with_html?: true)
|
||||
|
||||
assert html =~ to_string(g1)
|
||||
assert html =~ to_string(g2)
|
||||
|
||||
html = lv |> element(~s/button#delete-goal-#{g1.id}/) |> render_click()
|
||||
|
||||
refute html =~ to_string(g1)
|
||||
assert html =~ to_string(g2)
|
||||
|
||||
html = get(conn, "/#{site.domain}/settings/goals") |> html_response(200)
|
||||
|
||||
refute html =~ to_string(g1)
|
||||
assert html =~ to_string(g2)
|
||||
end
|
||||
|
||||
test "allows list filtering / search", %{conn: conn, site: site} do
|
||||
{:ok, [g1, g2, g3]} = setup_goals(site)
|
||||
{lv, html} = get_liveview(conn, site, with_html?: true)
|
||||
|
||||
assert html =~ to_string(g1)
|
||||
assert html =~ to_string(g2)
|
||||
assert html =~ to_string(g3)
|
||||
|
||||
html = type_into_search(lv, to_string(g3))
|
||||
|
||||
refute html =~ to_string(g1)
|
||||
refute html =~ to_string(g2)
|
||||
assert html =~ to_string(g3)
|
||||
end
|
||||
|
||||
test "allows resetting filter text via backspace icon", %{conn: conn, site: site} do
|
||||
{:ok, [g1, g2, g3]} = setup_goals(site)
|
||||
{lv, html} = get_liveview(conn, site, with_html?: true)
|
||||
|
||||
refute element_exists?(html, ~s/svg[phx-click="reset-filter-text"]#reset-filter/)
|
||||
|
||||
html = type_into_search(lv, to_string(g3))
|
||||
|
||||
assert element_exists?(html, ~s/svg[phx-click="reset-filter-text"]#reset-filter/)
|
||||
|
||||
html = lv |> element(~s/svg#reset-filter/) |> render_click()
|
||||
|
||||
assert html =~ to_string(g1)
|
||||
assert html =~ to_string(g2)
|
||||
assert html =~ to_string(g3)
|
||||
end
|
||||
|
||||
test "allows resetting filter text via no match link", %{conn: conn, site: site} do
|
||||
{:ok, _goals} = setup_goals(site)
|
||||
lv = get_liveview(conn, site)
|
||||
html = type_into_search(lv, "Definitely this is not going to render any matches")
|
||||
|
||||
assert html =~ "No goals found for this site. Please refine or"
|
||||
assert html =~ "reset your search"
|
||||
|
||||
assert element_exists?(html, ~s/a[phx-click="reset-filter-text"]#reset-filter-hint/)
|
||||
html = lv |> element(~s/a#reset-filter-hint/) |> render_click()
|
||||
|
||||
refute html =~ "No goals found for this site. Please refine or"
|
||||
end
|
||||
|
||||
test "clicking Add Goal button renders the form view", %{conn: conn, site: site} do
|
||||
{:ok, _goals} = setup_goals(site)
|
||||
lv = get_liveview(conn, site)
|
||||
html = lv |> element(~s/button[phx-click="add-goal"]/) |> render_click()
|
||||
|
||||
assert html =~ "Add goal for #{site.domain}"
|
||||
|
||||
assert element_exists?(
|
||||
html,
|
||||
~s/div#goals-form form[phx-submit="save-goal"][phx-click-away="cancel-add-goal"]/
|
||||
)
|
||||
end
|
||||
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, g3} = Plausible.Goals.create(site, %{"event_name" => "Purchase", "currency" => "EUR"})
|
||||
{:ok, [g1, g2, g3]}
|
||||
end
|
||||
|
||||
defp get_liveview(conn, site, opts \\ []) do
|
||||
conn = assign(conn, :live_module, PlausibleWeb.Live.GoalSettings)
|
||||
{:ok, lv, html} = live(conn, "/#{site.domain}/settings/goals")
|
||||
|
||||
if Keyword.get(opts, :with_html?) do
|
||||
{lv, html}
|
||||
else
|
||||
lv
|
||||
end
|
||||
end
|
||||
|
||||
defp type_into_search(lv, text) do
|
||||
lv
|
||||
|> element("form#filter-form")
|
||||
|> render_change(%{
|
||||
"_target" => ["filter-text"],
|
||||
"filter-text" => "#{text}"
|
||||
})
|
||||
end
|
||||
end
|
||||
Loading…
Reference in New Issue