From 01fba1f34bd209b226a6d9fd4447a9997cf2d49c Mon Sep 17 00:00:00 2001 From: hq1 Date: Tue, 4 Jul 2023 12:07:24 +0200 Subject: [PATCH] Bump funnel max size to 8 steps (#3094) * Bump max funnel size to 8 steps * Improve chart rendering for small screens/large funnels --- assets/js/dashboard/stats/behaviours/funnel.js | 12 ++++++++++-- lib/plausible/funnel.ex | 2 +- test/plausible/funnels_test.exs | 14 ++++++++++++-- 3 files changed, 23 insertions(+), 5 deletions(-) diff --git a/assets/js/dashboard/stats/behaviours/funnel.js b/assets/js/dashboard/stats/behaviours/funnel.js index 30b2ad1ed..e044422f3 100644 --- a/assets/js/dashboard/stats/behaviours/funnel.js +++ b/assets/js/dashboard/stats/behaviours/funnel.js @@ -51,7 +51,7 @@ export default function Funnel(props) { }, [funnel, visible]) useEffect(() => { - const mediaQuery = window.matchMedia('(max-width: 600px)') + const mediaQuery = window.matchMedia('(max-width: 768px)') setSmallScreen(mediaQuery.matches) const handleScreenChange = (e) => { setSmallScreen(e.matches); @@ -164,6 +164,14 @@ export default function Funnel(props) { const dropOffData = funnel.steps.map((step) => step.dropoff) const ctx = canvasRef.current.getContext("2d") + const calcBarThickness = (ctx) => { + if (ctx.dataset.data.length <= 3) { + return 160 + } else { + return Math.floor(650 / ctx.dataset.data.length) + } + } + // passing those verbatim to make sure canvas rendering picks them up var fontFamily = 'ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, "Noto Sans", sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol", "Noto Color Emoji"' @@ -199,7 +207,7 @@ export default function Funnel(props) { data: data, options: { responsive: true, - barThickness: 120, + barThickness: calcBarThickness, plugins: { legend: { display: false, diff --git a/lib/plausible/funnel.ex b/lib/plausible/funnel.ex index 4d8e24c8b..208fec2af 100644 --- a/lib/plausible/funnel.ex +++ b/lib/plausible/funnel.ex @@ -1,6 +1,6 @@ defmodule Plausible.Funnel do @min_steps 2 - @max_steps 5 + @max_steps 8 @moduledoc """ A funnel is a marketing term used to capture and describe the journey diff --git a/test/plausible/funnels_test.exs b/test/plausible/funnels_test.exs index c600791f8..dbb0e0c76 100644 --- a/test/plausible/funnels_test.exs +++ b/test/plausible/funnels_test.exs @@ -14,12 +14,15 @@ defmodule Plausible.FunnelsTest do {:ok, g4} = Goals.create(site, %{"event_name" => "Leave feedback"}) {:ok, g5} = Goals.create(site, %{"page_path" => "/recommend"}) {:ok, g6} = Goals.create(site, %{"event_name" => "Extra event"}) + {:ok, g7} = Goals.create(site, %{"event_name" => "Extra event 2"}) + {:ok, g8} = Goals.create(site, %{"event_name" => "Extra event 3"}) + {:ok, g9} = Goals.create(site, %{"event_name" => "Extra event 4"}) {:ok, %{ site: site, goals: [g1, g2, g3], - steps: [g1, g2, g3, g4, g5, g6] |> Enum.map(&%{"goal_id" => &1.id}) + steps: [g1, g2, g3, g4, g5, g6, g7, g8, g9] |> Enum.map(&%{"goal_id" => &1.id}) }} end @@ -84,7 +87,14 @@ defmodule Plausible.FunnelsTest do ) end - test "a funnel can be made of 5 steps maximum", %{site: site, steps: too_many_steps} do + test "a funnel can be made of 8 steps maximum", %{site: site, steps: too_many_steps} do + assert {:ok, _} = + Funnels.create( + site, + "Lorem ipsum", + Enum.take(too_many_steps, 8) + ) + assert {:error, :invalid_funnel_size} = Funnels.create( site,