From e3b8067628baeab4aee12a481711ac2e0fc8f076 Mon Sep 17 00:00:00 2001 From: Adam Rutkowski Date: Mon, 15 May 2023 14:27:42 +0200 Subject: [PATCH] wip --- assets/js/app.js | 25 +++++- assets/package-lock.json | 45 +++++++--- assets/package.json | 5 +- config/config.exs | 1 + lib/plausible/funnel.ex | 2 +- lib/plausible_web/endpoint.ex | 19 ++-- lib/plausible_web/live/funnel_settings.ex | 43 ++++++++++ .../live/funnel_settings/form.ex | 86 +++++++++++++++++++ .../live/funnel_settings/list.ex | 25 ++++++ lib/plausible_web/router.ex | 6 +- .../templates/layout/app.html.eex | 1 + .../templates/site/settings_funnels.html.eex | 51 +++-------- 12 files changed, 237 insertions(+), 72 deletions(-) create mode 100644 lib/plausible_web/live/funnel_settings.ex create mode 100644 lib/plausible_web/live/funnel_settings/form.ex create mode 100644 lib/plausible_web/live/funnel_settings/list.ex diff --git a/assets/js/app.js b/assets/js/app.js index 9c2bb174d..9a684bc3c 100644 --- a/assets/js/app.js +++ b/assets/js/app.js @@ -5,6 +5,23 @@ import 'abortcontroller-polyfill/dist/polyfill-patch-fetch' import "phoenix_html" import 'alpinejs' +import { Socket } from "phoenix" +import { LiveSocket } from "phoenix_live_view" + +let csrfToken = document.querySelector("meta[name='csrf-token']").getAttribute("content") +let liveSocket = new LiveSocket("/live", Socket, { params: { _csrf_token: csrfToken } }) + +// Connect if there are any LiveViews on the page +liveSocket.connect() + +// Expose liveSocket on window for web console debug logs and latency simulation: +// >> liveSocket.enableDebug() +// >> liveSocket.enableLatencySim(1000) +// The latency simulator is enabled for the duration of the browser session. +// Call disableLatencySim() to disable: +// >> liveSocket.disableLatencySim() +window.liveSocket = liveSocket + const triggers = document.querySelectorAll('[data-dropdown-trigger]') for (const trigger of triggers) { @@ -49,7 +66,7 @@ if (registerForm) { } } /* eslint-disable-next-line no-undef */ - plausible('Signup', {callback: submitForm}); + plausible('Signup', { callback: submitForm }); }) } @@ -58,12 +75,12 @@ const changelogNotification = document.getElementById('changelog-notification') if (changelogNotification) { showChangelogNotification(changelogNotification) - fetch('https://plausible.io/changes.txt', {headers: {'Content-Type': 'text/plain'}}) + fetch('https://plausible.io/changes.txt', { headers: { 'Content-Type': 'text/plain' } }) .then((res) => res.text()) .then((res) => { localStorage.lastChangelogUpdate = new Date(res).getTime() showChangelogNotification(changelogNotification) - }) + }) } function showChangelogNotification(el) { @@ -71,7 +88,7 @@ function showChangelogNotification(el) { const lastChecked = Number(localStorage.lastChangelogClick) const hasNewUpdateSinceLastClicked = lastUpdated > lastChecked - const notOlderThanThreeDays = Date.now() - lastUpdated < 1000 * 60 * 60 * 72 + const notOlderThanThreeDays = Date.now() - lastUpdated < 1000 * 60 * 60 * 72 if ((!lastChecked || hasNewUpdateSinceLastClicked) && notOlderThanThreeDays) { el.innerHTML = ` diff --git a/assets/package-lock.json b/assets/package-lock.json index b8357d8b7..61326700a 100644 --- a/assets/package-lock.json +++ b/assets/package-lock.json @@ -35,8 +35,9 @@ "debounce-promise": "^3.1.2", "iframe-resizer": "^4.3.2", "mini-css-extract-plugin": "^1.6.0", - "phoenix": "1.5.0", - "phoenix_html": "2.12", + "phoenix": "^1.7.2", + "phoenix_html": "^3.3.1", + "phoenix_live_view": "^0.18.18", "postcss": "^8.3.0", "postcss-loader": "^6.1.1", "react": "^16.13.1", @@ -68,6 +69,12 @@ "webpack-bundle-analyzer": "^4.4.2" } }, + "../../../../../../deps/phoenix": { + "extraneous": true + }, + "../../../../../../deps/phoenix_html": { + "extraneous": true + }, "node_modules/@ampproject/remapping": { "version": "2.2.1", "resolved": "https://registry.npmjs.org/@ampproject/remapping/-/remapping-2.2.1.tgz", @@ -6805,14 +6812,19 @@ } }, "node_modules/phoenix": { - "version": "1.5.0", - "resolved": "https://registry.npmjs.org/phoenix/-/phoenix-1.5.0.tgz", - "integrity": "sha512-Vzz6eb64BEufIXTVkTcT5HiNdRyLY2DF/l2eN1LaeedRbBhGCsdRWdDISFjBSjxODjqY+w2NmSzBeHM8MieLRw==" + "version": "1.7.2", + "resolved": "https://registry.npmjs.org/phoenix/-/phoenix-1.7.2.tgz", + "integrity": "sha512-jwWDOsUNEQZkLNSwzfLJ6AMFUkyZ+ILU+NkBBtoAK020QPpV/dYEQHGMQoFScj89MFjJlLdkE1W97i8S+4FdGg==" }, "node_modules/phoenix_html": { - "version": "2.12.0", - "resolved": "https://registry.npmjs.org/phoenix_html/-/phoenix_html-2.12.0.tgz", - "integrity": "sha512-UulYUsew5h2qeCSIMs1VWINkkpCauEn8uEq0zt2Ky9FpfEW6LsasMJ+Vgi01tUBbBFEy73E+sUvucQ+okL69Aw==" + "version": "3.3.1", + "resolved": "https://registry.npmjs.org/phoenix_html/-/phoenix_html-3.3.1.tgz", + "integrity": "sha512-t/9Saqpe8vznZYHMDim7HS32Dd2/rKf3+uxuKKNRADLpGXVIDjselOY6pK8aNaLiY2gnlqsoz6yIpUBuoLT63w==" + }, + "node_modules/phoenix_live_view": { + "version": "0.18.18", + "resolved": "https://registry.npmjs.org/phoenix_live_view/-/phoenix_live_view-0.18.18.tgz", + "integrity": "sha512-KVWZPH4XRCWofBbxNv4jSQ5clxBV1Mo9pkw5CmcMZDYLzdsk47am9+FiiEj7fapgYKnPNtPNIHZNKj2/yzgzZw==" }, "node_modules/picocolors": { "version": "1.0.0", @@ -14775,14 +14787,19 @@ "integrity": "sha512-gDKb8aZMDeD/tZWs9P6+q0J9Mwkdl6xMV8TjnGP3qJVJ06bdMgkbBlLU8IdfOsIsFz2BW1rNVT3XuNEl8zPAvw==" }, "phoenix": { - "version": "1.5.0", - "resolved": "https://registry.npmjs.org/phoenix/-/phoenix-1.5.0.tgz", - "integrity": "sha512-Vzz6eb64BEufIXTVkTcT5HiNdRyLY2DF/l2eN1LaeedRbBhGCsdRWdDISFjBSjxODjqY+w2NmSzBeHM8MieLRw==" + "version": "1.7.2", + "resolved": "https://registry.npmjs.org/phoenix/-/phoenix-1.7.2.tgz", + "integrity": "sha512-jwWDOsUNEQZkLNSwzfLJ6AMFUkyZ+ILU+NkBBtoAK020QPpV/dYEQHGMQoFScj89MFjJlLdkE1W97i8S+4FdGg==" }, "phoenix_html": { - "version": "2.12.0", - "resolved": "https://registry.npmjs.org/phoenix_html/-/phoenix_html-2.12.0.tgz", - "integrity": "sha512-UulYUsew5h2qeCSIMs1VWINkkpCauEn8uEq0zt2Ky9FpfEW6LsasMJ+Vgi01tUBbBFEy73E+sUvucQ+okL69Aw==" + "version": "3.3.1", + "resolved": "https://registry.npmjs.org/phoenix_html/-/phoenix_html-3.3.1.tgz", + "integrity": "sha512-t/9Saqpe8vznZYHMDim7HS32Dd2/rKf3+uxuKKNRADLpGXVIDjselOY6pK8aNaLiY2gnlqsoz6yIpUBuoLT63w==" + }, + "phoenix_live_view": { + "version": "0.18.18", + "resolved": "https://registry.npmjs.org/phoenix_live_view/-/phoenix_live_view-0.18.18.tgz", + "integrity": "sha512-KVWZPH4XRCWofBbxNv4jSQ5clxBV1Mo9pkw5CmcMZDYLzdsk47am9+FiiEj7fapgYKnPNtPNIHZNKj2/yzgzZw==" }, "picocolors": { "version": "1.0.0", diff --git a/assets/package.json b/assets/package.json index 69d015442..f617f0eb4 100644 --- a/assets/package.json +++ b/assets/package.json @@ -37,8 +37,9 @@ "debounce-promise": "^3.1.2", "iframe-resizer": "^4.3.2", "mini-css-extract-plugin": "^1.6.0", - "phoenix": "1.5.0", - "phoenix_html": "2.12", + "phoenix": "^1.7.2", + "phoenix_html": "^3.3.1", + "phoenix_live_view": "^0.18.18", "postcss": "^8.3.0", "postcss-loader": "^6.1.1", "react": "^16.13.1", diff --git a/config/config.exs b/config/config.exs index b4689c9c8..995fda0ac 100644 --- a/config/config.exs +++ b/config/config.exs @@ -4,6 +4,7 @@ config :plausible, ecto_repos: [Plausible.Repo, Plausible.IngestRepo] config :plausible, PlausibleWeb.Endpoint, + live_view: [signing_salt: "F19F4RBTbQ66AiYD"], pubsub_server: Plausible.PubSub, render_errors: [ view: PlausibleWeb.ErrorView, diff --git a/lib/plausible/funnel.ex b/lib/plausible/funnel.ex index 7a42024d3..9bf77bcfa 100644 --- a/lib/plausible/funnel.ex +++ b/lib/plausible/funnel.ex @@ -37,7 +37,7 @@ defmodule Plausible.Funnel do timestamps() end - def changeset(funnel, attrs \\ %{}) do + def changeset(funnel \\ %__MODULE__{}, attrs \\ %{}) do funnel |> cast(attrs, [:name]) |> cast_assoc(:steps, with: &Step.changeset/2) diff --git a/lib/plausible_web/endpoint.ex b/lib/plausible_web/endpoint.ex index 90172f0c3..8bfba7f05 100644 --- a/lib/plausible_web/endpoint.ex +++ b/lib/plausible_web/endpoint.ex @@ -2,6 +2,15 @@ defmodule PlausibleWeb.Endpoint do use Sentry.PlugCapture use Phoenix.Endpoint, otp_app: :plausible + @session_options [ + store: :cookie, + key: "_plausible_key", + signing_salt: "3IL0ob4k", + # 5 years, this is super long but the SlidingSessionTimeout will log people out if they don't return for 2 weeks + max_age: 60 * 60 * 24 * 365 * 5, + extra: "SameSite=Lax" + ] + # Serve at "/" the static files from "priv/static" directory. # # You should set gzip to true if you are running phx.digest @@ -43,13 +52,9 @@ defmodule PlausibleWeb.Endpoint do plug Plug.MethodOverride plug Plug.Head - plug Plug.Session, - store: :cookie, - key: "_plausible_key", - signing_salt: "3IL0ob4k", - # 5 years, this is super long but the SlidingSessionTimeout will log people out if they don't return for 2 weeks - max_age: 60 * 60 * 24 * 365 * 5, - extra: "SameSite=Lax" + plug Plug.Session, @session_options + + socket "/live", Phoenix.LiveView.Socket, websocket: [connect_info: [session: @session_options]] plug CORSPlug plug PlausibleWeb.Router diff --git a/lib/plausible_web/live/funnel_settings.ex b/lib/plausible_web/live/funnel_settings.ex new file mode 100644 index 000000000..0abe10976 --- /dev/null +++ b/lib/plausible_web/live/funnel_settings.ex @@ -0,0 +1,43 @@ +defmodule PlausibleWeb.Live.FunnelSettings do + use Phoenix.LiveView + use Phoenix.HTML + + alias Plausible.{Sites, Goals, Funnels} + + def mount( + _params, + %{"site_id" => _site_id, "domain" => domain, "current_user_id" => user_id}, + socket + ) do + site = Sites.get_for_user!(user_id, domain, [:owner, :admin]) + + funnels = Funnels.list(site) + goals = Goals.for_site(site) + + {:ok, assign(socket, site: site, funnels: funnels, goals: goals, add_funnel?: false)} + end + + def render(assigns) do + ~H""" + <%= if @add_funnel? do %> + <.live_component module={PlausibleWeb.Live.FunnelSettings.Form} id="funnelForm" site={@site} form={to_form(Plausible.Funnel.changeset())} goals={@goals} /> + <% else %> + <.live_component module={PlausibleWeb.Live.FunnelSettings.List} id="funnelsList" funnels={@funnels} site={@site} /> + + <% end %> + """ + end + + def handle_event("add_funnel", _value, socket) do + {:noreply, assign(socket, :add_funnel?, true)} + end + + def handle_event("cancel_add_funnel", _value, socket) do + {:noreply, assign(socket, :add_funnel?, false)} + end + + def handle_event("validate", value, socket) do + IO.inspect(value, label: :validate) + {:noreply, socket} + end +end diff --git a/lib/plausible_web/live/funnel_settings/form.ex b/lib/plausible_web/live/funnel_settings/form.ex new file mode 100644 index 000000000..1095c10ee --- /dev/null +++ b/lib/plausible_web/live/funnel_settings/form.ex @@ -0,0 +1,86 @@ +defmodule PlausibleWeb.Live.FunnelSettings.Form do + use Phoenix.LiveComponent + use Phoenix.HTML + + # good + + def mount(socket) do + {:ok, assign(socket, step_count: 2)} + end + + def update(assigns, socket) do + {:ok, assign(socket, form: assigns.form, goals: assigns.goals)} + end + + def render(assigns) do + ~H""" +
+ +
+ """ + end + + attr :field, Phoenix.HTML.FormField + + def input(assigns) do + ~H""" + + """ + end + + attr :goals, :map, required: true + attr :myself, :any, required: true + + def select(assigns) do + ~H""" + + """ + end + + def handle_event("add-step", _value, socket) do + step_count = socket.assigns.step_count + + if step_count < 5 do + {:noreply, assign(socket, step_count: step_count + 1)} + else + {:noreply, socket} + end + end + + def handle_event(event, value, socket) do + IO.inspect(event, label: :event) + IO.inspect(value, label: :value) + [_ | except_first] = socket.assigns.goals + {:noreply, assign(socket, goals: except_first)} + end +end + +# goal_options = [{"N/A", ""} | Enum.map(@goals, fn goal -> {Plausible.Goal.display_name(goal), goal.id} end)] %> +# +# end diff --git a/lib/plausible_web/live/funnel_settings/list.ex b/lib/plausible_web/live/funnel_settings/list.ex new file mode 100644 index 000000000..ec3e1aeec --- /dev/null +++ b/lib/plausible_web/live/funnel_settings/list.ex @@ -0,0 +1,25 @@ +defmodule PlausibleWeb.Live.FunnelSettings.List do + use Phoenix.LiveComponent + use Phoenix.HTML + + def render(assigns) do + ~H""" +
+ <%= if Enum.count(@funnels) > 0 do %> +
+ <%= for funnel <- @funnels do %> +
+ <%= funnel.name %> + <%= button(to: "", method: :delete, class: "text-sm text-red-600", data: [confirm: "Are you sure you want to remove funnel '#{funnel.name}'? This will just affect the UI, all of your analytics data will stay intact."]) do %> + + <% end %> +
+ <% end %> +
+ <% else %> +
No funnels configured for this site yet
+ <% end %> +
+ """ + end +end diff --git a/lib/plausible_web/router.ex b/lib/plausible_web/router.ex index a02fa2cdd..9fd95e7ae 100644 --- a/lib/plausible_web/router.ex +++ b/lib/plausible_web/router.ex @@ -6,7 +6,7 @@ defmodule PlausibleWeb.Router do pipeline :browser do plug :accepts, ["html"] plug :fetch_session - plug :fetch_flash + plug :fetch_live_flash plug :put_secure_browser_headers plug PlausibleWeb.FirstLaunchPlug, redirect_to: "/register" plug PlausibleWeb.SessionTimeoutPlug, timeout_after_seconds: @two_weeks_in_seconds @@ -247,10 +247,10 @@ defmodule PlausibleWeb.Router do get "/:website/settings/visibility", SiteController, :settings_visibility get "/:website/settings/goals", SiteController, :settings_goals - # get "/:website/settings/funnels", SiteController, :settings_funnels + get "/:website/settings/funnels", SiteController, :settings_funnels # post "/:website/settings/funnels", SiteController, :save_funnel - live "/:website/settings/funnels", FunnelSettingsLive + # live "/:website/settings/funnels", Live.FunnelSettings get "/:website/settings/search-console", SiteController, :settings_search_console get "/:website/settings/email-reports", SiteController, :settings_email_reports diff --git a/lib/plausible_web/templates/layout/app.html.eex b/lib/plausible_web/templates/layout/app.html.eex index af8795294..ff0873031 100644 --- a/lib/plausible_web/templates/layout/app.html.eex +++ b/lib/plausible_web/templates/layout/app.html.eex @@ -5,6 +5,7 @@ + /> "> <%= assigns[:title] || "Plausible ยท Simple, privacy-friendly alternative to Google Analytics" %> diff --git a/lib/plausible_web/templates/site/settings_funnels.html.eex b/lib/plausible_web/templates/site/settings_funnels.html.eex index dc197cdf3..c93c8c9ef 100644 --- a/lib/plausible_web/templates/site/settings_funnels.html.eex +++ b/lib/plausible_web/templates/site/settings_funnels.html.eex @@ -1,41 +1,10 @@ -
-

Your Funnels

-
- - - -
-
-

New funnel

-
- -<% goal_options = [{"N/A", ""} | Enum.map(@goals, fn goal -> {Plausible.Goal.display_name(goal), goal.id} end)] %> - -<%= form_for @conn, "/#{@site.domain}/settings/funnels", [class: "max-w-sm"], fn f -> %> - <%= label f, :funnel_name, class: "block text-sm font-medium text-gray-700 dark:text-gray-300" %> - <%= text_input f, :funnel_name, class: "shadow-sm dark:bg-gray-900 dark:text-gray-300 focus:ring-indigo-500 focus:border-indigo-500 block w-full sm:text-sm border-gray-300 dark:border-gray-500 rounded-md dark:bg-gray-800" %> - - <%= label f, :step_1, class: "block text-sm font-medium text-gray-700 dark:text-gray-300" %> - - <%= select f, :step_1, goal_options, class: "dark:bg-gray-900 mt-1 block w-full pl-3 pr-10 py-2 text-base border-gray-300 dark:border-gray-500 focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm rounded-md dark:text-gray-100 cursor-pointer" %> - - <%= label f, :step_2, class: "block text-sm font-medium text-gray-700 dark:text-gray-300" %> - <%= select f, :step_2, goal_options, class: "dark:bg-gray-900 mt-1 block w-full pl-3 pr-10 py-2 text-base border-gray-300 dark:border-gray-500 focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm rounded-md dark:text-gray-100 cursor-pointer" %> - - - <%= label f, :step_3, class: "block text-sm font-medium text-gray-700 dark:text-gray-300" %> - <%= select f, :step_3, goal_options, class: "dark:bg-gray-900 mt-1 block w-full pl-3 pr-10 py-2 text-base border-gray-300 dark:border-gray-500 focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm rounded-md dark:text-gray-100 cursor-pointer" %> - - <%= label f, :step_4, class: "block text-sm font-medium text-gray-700 dark:text-gray-300" %> - <%= select f, :step_4, goal_options, class: "dark:bg-gray-900 mt-1 block w-full pl-3 pr-10 py-2 text-base border-gray-300 dark:border-gray-500 focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm rounded-md dark:text-gray-100 cursor-pointer" %> - - <%= label f, :step_5, class: "block text-sm font-medium text-gray-700 dark:text-gray-300" %> - <%= select f, :step_5, goal_options, class: "dark:bg-gray-900 mt-1 block w-full pl-3 pr-10 py-2 text-base border-gray-300 dark:border-gray-500 focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm rounded-md dark:text-gray-100 cursor-pointer" %> - - <%= submit "Save funnel", class: "button mt-4" %> -<% end %> - +
+
+

Funnels

+

Compose goals into funnels

+ <%= link(to: "https://plausible.io/docs/goal-conversions", target: "_blank", rel: "noreferrer") do %> + + <% end %> +
+ <%= live_render(@conn, PlausibleWeb.Live.FunnelSettings, session: %{"site_id" => @site.id, "domain" => @site.domain}) %> +