Open source, privacy-first web analytics. Lightweight, cookie-free Google Analytics alternative. Self-hosted or cloud.
TypeScript analyticsanalytics-dashboardclickhouseelixirgoogle-analyticsmarketingopen-source-analyticsphoenixplausible-analyticspostgresqlprivacyprivacy-friendlyself-hostedsimple-analyticsstatisticstailwindcssweb-analyticswebsite-analyticswebsite-statswebsite-tracking
Autoconfigure event goals (#4093)
RobertJoonasa6dcd19ccc76
diff --git a/lib/plausible/goal/schema.ex b/lib/plausible/goal/schema.exindex 99e335f95e..0a8ca41cac 100644--- a/lib/plausible/goal/schema.ex+++ b/lib/plausible/goal/schema.ex@@ -24,6 +24,10 @@ defmodule Plausible.Goal do@fields [:id, :site_id, :event_name, :page_path] ++ on_ee(do: [:currency], else: [])+ @max_event_name_length 120++ def max_event_name_length(), do: @max_event_name_length+def changeset(goal, attrs \\ %{}) dogoal|> cast(attrs, @fields)@@ -35,7 +39,7 @@ defmodule Plausible.Goal do|> update_change(:page_path, &String.trim/1)|> unique_constraint(:event_name, name: :goals_event_name_unique)|> unique_constraint(:page_path, name: :goals_page_path_unique)- |> validate_length(:event_name, max: 120)+ |> validate_length(:event_name, max: @max_event_name_length)|> check_constraint(:event_name,name: :check_event_name_or_page_path,message: "cannot co-exist with page_path"diff --git a/lib/plausible/goals.ex b/lib/plausible/goals.exindex a0b2f8a3aa..cb9c3dcbd4 100644--- a/lib/plausible/goals.ex+++ b/lib/plausible/goals.ex@@ -114,6 +114,15 @@ defmodule Plausible.Goals doendend+ def batch_create_event_goals(names, site) do+ Enum.reduce(names, [], fn name, acc ->+ case insert_goal(site, %{event_name: name}, true) do+ {:ok, _, goal} -> acc ++ [goal]+ _ -> acc+ end+ end)+ end+@doc """If a goal belongs to funnel(s), we need to inspect their number of steps.diff --git a/lib/plausible/stats/goal_suggestions.ex b/lib/plausible/stats/goal_suggestions.exnew file mode 100644index 0000000000..7802478f5e--- /dev/null+++ b/lib/plausible/stats/goal_suggestions.ex@@ -0,0 +1,72 @@+defmodule Plausible.Stats.GoalSuggestions do+ @moduledoc false++ alias Plausible.{Repo, ClickhouseRepo}+ alias Plausible.Stats.Query+ import Plausible.Stats.Base+ import Ecto.Query++ defmacrop visitors(e) do+ quote do+ selected_as(+ fragment("toUInt64(round(uniq(?)*any(_sample_factor)))", unquote(e).user_id),+ :visitors+ )+ end+ end++ def suggest_event_names(site, search_input, opts \\ []) do+ matches = "%#{search_input}%"++ site =+ site+ |> Repo.preload(:goals)+ |> Plausible.Imported.load_import_data()++ excluded = Keyword.get(opts, :exclude, [])++ params = %{"with_imported" => "true", "period" => "6mo"}+ query = Query.from(site, params)++ native_q =+ from(e in base_event_query(site, query),+ where: fragment("? ilike ?", e.name, ^matches),+ where: e.name != "pageview",+ where: e.name not in ^excluded,+ select: %{+ name: e.name,+ visitors: visitors(e)+ },+ order_by: selected_as(:visitors),+ group_by: e.name,+ limit: 25+ )++ imported_q =+ from(i in "imported_custom_events",+ where: i.site_id == ^site.id,+ where: i.import_id in ^site.complete_import_ids,+ where: i.date >= ^query.date_range.first and i.date <= ^query.date_range.last,+ where: i.visitors > 0,+ where: fragment("? ilike ?", i.name, ^matches),+ where: i.name not in ^excluded,+ select: %{+ name: i.name,+ visitors: selected_as(sum(i.visitors), :visitors)+ },+ order_by: selected_as(:visitors),+ group_by: i.name,+ limit: 25+ )++ from(e in Ecto.Query.subquery(native_q),+ full_join: i in subquery(imported_q),+ on: e.name == i.name,+ select: selected_as(fragment("if(empty(?), ?, ?)", e.name, i.name, e.name), :name),+ order_by: [desc: e.visitors + i.visitors],+ limit: 25+ )+ |> ClickhouseRepo.all()+ |> Enum.reject(&(String.length(&1) > Plausible.Goal.max_event_name_length()))+ end+enddiff --git a/lib/plausible_web/live/goal_settings.ex b/lib/plausible_web/live/goal_settings.exindex 32d11eca27..58f498ea04 100644--- a/lib/plausible_web/live/goal_settings.ex+++ b/lib/plausible_web/live/goal_settings.ex@@ -21,6 +21,14 @@ defmodule PlausibleWeb.Live.GoalSettings do|> assign_new(:all_goals, fn %{site: site} ->Goals.for_site(site, preload_funnels?: true)end)+ |> assign_new(:event_name_options, fn %{site: site, all_goals: all_goals} ->+ exclude =+ all_goals+ |> Enum.reject(&is_nil(&1.event_name))+ |> Enum.map(& &1.event_name)++ Plausible.Stats.GoalSuggestions.suggest_event_names(site, "", exclude: exclude)+ end)|> assign_new(:current_user, fn ->Plausible.Repo.get(Plausible.Auth.User, user_id)end)@@ -44,6 +52,7 @@ defmodule PlausibleWeb.Live.GoalSettings do<.live_componentmodule={PlausibleWeb.Live.GoalSettings.Form}id="goals-form"+ event_name_options={@event_name_options}domain={@domain}site={@site}current_user={@current_user}@@ -53,6 +62,12 @@ defmodule PlausibleWeb.Live.GoalSettings doModal.close(socket, "goals-form-modal")end}+ on_autoconfigure={+ fn socket ->+ send(self(), :autoconfigure)+ Modal.close(socket, "goals-form-modal")+ end+ }/></.live_component><.live_component@@ -81,16 +96,24 @@ defmodule PlausibleWeb.Live.GoalSettings do{:noreply, assign(socket, displayed_goals: new_list, filter_text: filter_text)}end- def handle_event("delete-goal", %{"goal-id" => goal_id}, socket) do+ def handle_event("delete-goal", %{"goal-id" => goal_id} = params, socket) dogoal_id = String.to_integer(goal_id)case Plausible.Goals.delete(goal_id, socket.assigns.site_id) do:ok ->+ event_name_options =+ if goal_name = params["goal-name"] do+ [goal_name | socket.assigns.event_name_options]+ else+ socket.assigns.event_name_options+ end+socket =socket|> put_live_flash(:success, "Goal deleted successfully")|> assign(all_goals: Enum.reject(socket.assigns.all_goals, &(&1.id == goal_id)),+ event_name_options: event_name_options,displayed_goals: Enum.reject(socket.assigns.displayed_goals, &(&1.id == goal_id)))@@ -107,10 +130,33 @@ defmodule PlausibleWeb.Live.GoalSettings do|> assign(filter_text: "",all_goals: [goal | socket.assigns.all_goals],+ event_name_options:+ Enum.reject(socket.assigns.event_name_options, &(&1 == goal.event_name)),displayed_goals: [goal | socket.assigns.all_goals])|> put_live_flash(:success, "Goal saved successfully"){:noreply, socket}end++ def handle_info(:autoconfigure, socket) do+ %{event_name_options: names, site: site} = socket.assigns++ added_goals =+ names+ |> Plausible.Goals.batch_create_event_goals(site)+ |> Enum.map(&Map.put(&1, :funnels, []))++ socket =+ socket+ |> assign(+ filter_text: "",+ all_goals: added_goals ++ socket.assigns.all_goals,+ event_name_options: [],+ displayed_goals: added_goals ++ socket.assigns.all_goals+ )+ |> put_live_flash(:success, "All goals added successfully")++ {:noreply, socket}+ endenddiff --git a/lib/plausible_web/live/goal_settings/form.ex b/lib/plausible_web/live/goal_settings/form.exindex c6c41a3307..e87959814d 100644--- a/lib/plausible_web/live/goal_settings/form.ex+++ b/lib/plausible_web/live/goal_settings/form.ex@@ -28,12 +28,14 @@ defmodule PlausibleWeb.Live.GoalSettings.Form do|> assign(id: assigns.id,form: form,+ event_name_options_count: length(assigns.event_name_options),current_user: assigns.current_user,domain: assigns.domain,selected_tab: "custom_events",site: site,has_access_to_revenue_goals?: has_access_to_revenue_goals?,- on_save_goal: assigns.on_save_goal+ on_save_goal: assigns.on_save_goal,+ on_autoconfigure: assigns.on_autoconfigure){:ok, socket}@@ -80,6 +82,20 @@ defmodule PlausibleWeb.Live.GoalSettings.Form doAdd Goal →</PlausibleWeb.Components.Generic.button></div>++ <button+ :if={@selected_tab == "custom_events" && @event_name_options_count > 0}+ class="mt-2 text-sm hover:underline text-indigo-600 dark:text-indigo-400 text-left"+ phx-click="autoconfigure"+ phx-target={@myself}+ >+ <span :if={@event_name_options_count > 1}>+ 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.+ </span>+ <span :if={@event_name_options_count == 1}>+ 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.+ </span>+ </button></.form></div>"""@@ -250,7 +266,7 @@ defmodule PlausibleWeb.Live.GoalSettings.Form do!@selected? && "dark:text-gray-100 text-gray-800"]}id="event-tab"- x-on:click="tabSelectionInProgress = true"+ x-on:click={!@selected? && "tabSelectionInProgress = true"}phx-click="switch-tab"phx-value-tab="custom_events"phx-target={@myself}@@ -269,7 +285,7 @@ defmodule PlausibleWeb.Live.GoalSettings.Form do!@selected? && "dark:text-gray-100 text-gray-800"]}id="pageview-tab"- x-on:click="tabSelectionInProgress = true"+ x-on:click={!@selected? && "tabSelectionInProgress = true"}phx-click="switch-tab"phx-value-tab="pageviews"phx-target={@myself}@@ -298,6 +314,10 @@ defmodule PlausibleWeb.Live.GoalSettings.Form doendend+ def handle_event("autoconfigure", _params, socket) do+ {:noreply, socket.assigns.on_autoconfigure.(socket)}+ end+def suggest_page_paths(input, _options, site) doquery = Plausible.Stats.Query.from(site, %{})diff --git a/lib/plausible_web/live/goal_settings/list.ex b/lib/plausible_web/live/goal_settings/list.exindex 839a5d6c4b..8bbb8e723c 100644--- a/lib/plausible_web/live/goal_settings/list.ex+++ b/lib/plausible_web/live/goal_settings/list.ex@@ -86,6 +86,7 @@ defmodule PlausibleWeb.Live.GoalSettings.List doid={"delete-goal-#{goal.id}"}phx-click="delete-goal"phx-value-goal-id={goal.id}+ phx-value-goal-name={goal.event_name}class="text-sm text-red-600"data-confirm={delete_confirmation_text(goal)}>diff --git a/test/plausible/stats/goal_suggestions_test.exs b/test/plausible/stats/goal_suggestions_test.exsnew file mode 100644index 0000000000..16d3109cdc--- /dev/null+++ b/test/plausible/stats/goal_suggestions_test.exs@@ -0,0 +1,80 @@+defmodule Plausible.Stats.GoalSuggestionsTest do+ use Plausible.DataCase, async: true+ alias Plausible.Stats.GoalSuggestions++ describe "suggest_event_names/3" do+ setup [:create_user, :create_site]++ test "returns custom event goal suggestions including imported data, ordered by visitor count",+ %{site: site} do+ site_import = insert(:site_import, site: site)++ populate_stats(+ site,+ site_import.id,+ [+ build(:event, name: "Signup"),+ build(:event, name: "Signup"),+ build(:event, name: "Signup"),+ build(:event, name: "Signup"),+ build(:event, name: "Signup Newsletter"),+ build(:event, name: "Signup Newsletter"),+ build(:imported_custom_events, name: "Signup Newsletter", visitors: 3),+ build(:imported_custom_events, name: "Outbound Link: Click", visitors: 50)+ ] ++ build_list(10, :event, name: "Purchase", user_id: 123)+ )++ assert GoalSuggestions.suggest_event_names(site, "") == [+ "Outbound Link: Click",+ "Signup Newsletter",+ "Signup",+ "Purchase"+ ]+ end++ test "returns custom event goal suggestions with search input, including imported data",+ %{site: site} do+ site_import = insert(:site_import, site: site)++ populate_stats(site, site_import.id, [+ build(:event, name: "Some Signup"),+ build(:event, name: "Some Signup"),+ build(:event, name: "Some Signup"),+ build(:event, name: "sign"),+ build(:event, name: "A Sign"),+ build(:event, name: "A Sign"),+ build(:event, name: "Not Matching"),+ build(:imported_custom_events, name: "GA Signup", visitors: 4),+ build(:imported_custom_events, name: "Not Matching", visitors: 3)+ ])++ assert GoalSuggestions.suggest_event_names(site, "Sign") == [+ "GA Signup",+ "Some Signup",+ "A Sign",+ "sign"+ ]+ end++ test "ignores the 'pageview' event name", %{site: site} do+ populate_stats(site, [+ build(:event, name: "Signup"),+ build(:pageview)+ ])++ assert GoalSuggestions.suggest_event_names(site, "") == ["Signup"]+ end++ test "can exclude goals from being suggested", %{site: site} do+ populate_stats(site, [build(:event, name: "Signup")])++ assert GoalSuggestions.suggest_event_names(site, "", exclude: ["Signup"]) == []+ end++ test "does not suggest event names longer than schema allows", %{site: site} do+ populate_stats(site, [build(:event, name: String.duplicate("A", 121))])++ assert GoalSuggestions.suggest_event_names(site, "") == []+ end+ end+enddiff --git a/test/plausible_web/live/goal_settings_test.exs b/test/plausible_web/live/goal_settings_test.exsindex eb7baf4647..c580adabd1 100644--- a/test/plausible_web/live/goal_settings_test.exs+++ b/test/plausible_web/live/goal_settings_test.exs@@ -167,6 +167,53 @@ defmodule PlausibleWeb.Live.GoalSettingsTest do~s/div#goals-form form[phx-submit="save-goal"]/)end++ test "auto-configuring custom event goals", %{conn: conn, site: site} do+ populate_stats(site, [+ build(:event, name: "Signup"),+ build(:event, name: "Newsletter Signup"),+ build(:event, name: "Purchase")+ ])++ autoconfigure_button_selector = ~s/button[phx-click="autoconfigure"]/++ assert_suggested_event_name_count = fn html, number ->+ assert text_of_element(html, autoconfigure_button_selector) =~+ "found #{number} custom events from the last 6 months that are not yet configured as goals"+ end++ {lv, html} = get_liveview(conn, site, with_html?: true)++ # At first, 3 event names are suggested+ assert_suggested_event_name_count.(html, 3)++ # Add one goal+ lv+ |> element("#goals-form form")+ |> render_submit(%{goal: %{event_name: "Signup"}})++ html = render(lv)++ # Now two goals are suggested because one is already added+ assert_suggested_event_name_count.(html, 2)++ # Delete the goal+ goal = Plausible.Repo.get_by(Plausible.Goal, site_id: site.id, event_name: "Signup")+ html = lv |> element(~s/button#delete-goal-#{goal.id}/) |> render_click()++ # Suggested event name count should be 3 again+ assert_suggested_event_name_count.(html, 3)++ # Autoconfigure all custom event goals+ lv+ |> element(autoconfigure_button_selector)+ |> render_click()++ html = render(lv)++ # All possible goals exist - no suggestions anymore+ refute html =~ "from the last 6 months"+ endenddefp setup_goals(site) do