diff --git a/assets/js/dashboard/stats/behaviours/funnel-tooltip.js b/assets/js/dashboard/stats/behaviours/funnel-tooltip.js
new file mode 100644
index 000000000..bbec21a16
--- /dev/null
+++ b/assets/js/dashboard/stats/behaviours/funnel-tooltip.js
@@ -0,0 +1,90 @@
+export default function FunnelTooltip(palette, funnel) {
+ return (context) => {
+ const tooltipModel = context.tooltip
+ const dataIndex = tooltipModel.dataPoints[0].dataIndex
+ const offset = document.getElementById("funnel").getBoundingClientRect()
+ let tooltipEl = document.getElementById('chartjs-tooltip')
+
+ if (!tooltipEl) {
+ tooltipEl = document.createElement('div')
+ tooltipEl.id = 'chartjs-tooltip'
+ tooltipEl.style.display = 'none'
+ tooltipEl.style.opacity = 0
+ document.body.appendChild(tooltipEl)
+ }
+
+ if (tooltipEl && offset && window.innerWidth < 768) {
+ tooltipEl.style.top = offset.y + offset.height + window.scrollY + 15 + 'px'
+ tooltipEl.style.left = offset.x + 'px'
+ tooltipEl.style.right = null
+ tooltipEl.style.opacity = 1
+ }
+
+ if (tooltipModel.opacity === 0) {
+ tooltipEl.style.display = 'none'
+ return
+ }
+
+
+ if (tooltipModel.body) {
+ const currentStep = funnel.steps[dataIndex]
+ const previousStep = (dataIndex > 0) ? funnel.steps[dataIndex - 1] : null
+
+ tooltipEl.innerHTML = `
+
+ `
+ }
+ tooltipEl.style.display = null
+ }
+}
+
+const formatPercentage = (value) => {
+ const decimalNumber = parseFloat(value);
+ return decimalNumber % 1 === 0 ? decimalNumber.toFixed(0) : value;
+}
diff --git a/assets/js/dashboard/stats/behaviours/funnel.js b/assets/js/dashboard/stats/behaviours/funnel.js
index 4d0ad135b..34105e3f1 100644
--- a/assets/js/dashboard/stats/behaviours/funnel.js
+++ b/assets/js/dashboard/stats/behaviours/funnel.js
@@ -1,4 +1,356 @@
-export default function Funnel(_props) {
- // TODO
- return null
-}
\ No newline at end of file
+import React, { useEffect, useState, useRef } from 'react'
+import FlipMove from 'react-flip-move';
+import Chart from 'chart.js/auto'
+import FunnelTooltip from './funnel-tooltip.js'
+import ChartDataLabels from 'chartjs-plugin-datalabels'
+import numberFormatter from '../../util/number-formatter'
+import Bar from '../bar'
+
+import RocketIcon from '../modals/rocket-icon'
+
+import * as api from '../../api'
+import LazyLoader from '../../components/lazy-loader'
+
+
+export default function Funnel(props) {
+ console.info('funnah')
+ const [loading, setLoading] = useState(true)
+ const [visible, setVisible] = useState(false)
+ const [error, setError] = useState(undefined)
+ const [funnel, setFunnel] = useState(null)
+ const [isSmallScreen, setSmallScreen] = useState(false)
+ const chartRef = useRef(null)
+ const canvasRef = useRef(null)
+
+ useEffect(() => {
+ if (visible) {
+ setLoading(true)
+ fetchFunnel()
+ .then((res) => {
+ setFunnel(res)
+ setError(undefined)
+ })
+ .catch((error) => {
+ setError(error)
+ })
+ .finally(() => {
+ setLoading(false)
+ })
+
+ return () => {
+ if (chartRef.current) {
+ chartRef.current.destroy()
+ }
+ }
+ }
+ }, [props.query, props.funnelName, visible, isSmallScreen])
+
+ useEffect(() => {
+ if (canvasRef.current && funnel && visible && !isSmallScreen) {
+ initialiseChart()
+ }
+ }, [funnel, visible])
+
+ useEffect(() => {
+ const mediaQuery = window.matchMedia('(max-width: 600px)')
+ setSmallScreen(mediaQuery.matches)
+ const handleScreenChange = (e) => {
+ setSmallScreen(e.matches);
+ }
+ mediaQuery.addEventListener("change", handleScreenChange);
+ return () => {
+ mediaQuery.removeEventListener("change", handleScreenChange)
+ }
+ }, [])
+
+ const isDarkMode = () => {
+ return document.querySelector('html').classList.contains('dark') || false
+ }
+
+ const getPalette = () => {
+ if (isDarkMode()) {
+ return {
+ dataLabelBackground: 'rgba(25, 30, 56, 0.97)',
+ dataLabelTextColor: 'rgb(243, 244, 246)',
+ visitorsBackground: 'rgb(99, 102, 241)',
+ dropoffBackground: '#2F3949',
+ dropoffStripes: 'rgb(25, 30, 56)',
+ stepNameLegendColor: 'rgb(228, 228, 231)',
+ visitorsLegendClass: 'bg-indigo-500',
+ dropoffLegendClass: 'bg-gray-600',
+ smallBarClass: 'bg-indigo-500'
+ }
+ } else {
+ return {
+ dataLabelBackground: 'rgba(25, 30, 56, 0.97)',
+ dataLabelTextColor: 'rgb(243, 244, 246)',
+ visitorsBackground: 'rgb(99, 102, 241)',
+ dropoffBackground: 'rgb(224, 231, 255)',
+ dropoffStripes: 'rgb(255, 255, 255)',
+ stepNameLegendColor: 'rgb(12, 24, 39)',
+ visitorsLegendClass: 'bg-indigo-500',
+ dropoffLegendClass: 'bg-indigo-100',
+ smallBarClass: 'bg-indigo-300'
+ }
+ }
+ }
+
+ const formatDataLabel = (visitors, ctx) => {
+ if (ctx.dataset.label === 'Visitors') {
+ const conversionRate = funnel.steps[ctx.dataIndex].conversion_rate
+ return `${formatPercentage(conversionRate)}% \n(${numberFormatter(visitors)} Visitors)`
+ } else {
+ return null
+ }
+ }
+
+ const calcOffset = (ctx) => {
+ const conversionRate = parseFloat(funnel.steps[ctx.dataIndex].conversion_rate)
+ if (conversionRate > 90) {
+ return -64
+ } else if (conversionRate > 20) {
+ return -28
+ } else {
+ return 8
+ }
+ }
+
+ const getFunnel = () => {
+ return props.site.funnels.find((funnel) => funnel.name === props.funnelName)
+ }
+
+ const fetchFunnel = async () => {
+ const funnelMeta = getFunnel()
+ if (typeof funnelMeta === 'undefined') {
+ throw new Error('Could not fetch the funnel. Perhaps it was deleted?')
+ } else {
+ return api.get(`/api/stats/${encodeURIComponent(props.site.domain)}/funnels/${funnelMeta.id}`, props.query)
+ }
+ }
+
+ const initialiseChart = () => {
+ if (chartRef.current) {
+ chartRef.current.destroy()
+ }
+
+ const palette = getPalette()
+
+ const createDiagonalPattern = (color1, color2) => {
+ // create a 10x10 px canvas for the pattern's base shape
+ let shape = document.createElement('canvas')
+ shape.width = 10
+ shape.height = 10
+ let c = shape.getContext('2d')
+
+ c.fillStyle = color1
+ c.strokeStyle = color2
+ c.fillRect(0, 0, shape.width, shape.height);
+
+ c.beginPath()
+ c.moveTo(2, 0)
+ c.lineTo(10, 8)
+ c.stroke()
+
+ c.beginPath()
+ c.moveTo(0, 8)
+ c.lineTo(2, 10)
+ c.stroke()
+
+ return c.createPattern(shape, 'repeat')
+ }
+
+ const labels = funnel.steps.map((step) => step.label)
+ const stepData = funnel.steps.map((step) => step.visitors)
+
+ const dropOffData = funnel.steps.map((step) => step.dropoff)
+ const ctx = canvasRef.current.getContext("2d")
+
+ // 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"'
+
+ var gradient = ctx.createLinearGradient(900, 0, 900, 900);
+ gradient.addColorStop(1, palette.dropoffBackground);
+ gradient.addColorStop(0, palette.visitorsBackground);
+
+ const data = {
+ labels: labels,
+ datasets: [
+ {
+ label: 'Visitors',
+ data: stepData,
+ backgroundColor: gradient,
+ hoverBackgroundColor: gradient,
+ borderRadius: 4,
+ stack: 'Stack 0',
+ },
+ {
+ label: 'Dropoff',
+ data: dropOffData,
+ backgroundColor: createDiagonalPattern(palette.dropoffBackground, palette.dropoffStripes),
+ hoverBackgroundColor: palette.dropoffBackground,
+ borderRadius: 4,
+ stack: 'Stack 0',
+ },
+ ],
+ }
+
+ const config = {
+ plugins: [ChartDataLabels],
+ type: 'bar',
+ data: data,
+ options: {
+ responsive: true,
+ barThickness: 120,
+ plugins: {
+ legend: {
+ display: false,
+ },
+ tooltip: {
+ enabled: false,
+ mode: 'index',
+ intersect: true,
+ position: 'average',
+ external: FunnelTooltip(palette, funnel)
+ },
+ datalabels: {
+ formatter: formatDataLabel,
+ anchor: 'end',
+ align: 'end',
+ offset: calcOffset,
+ backgroundColor: palette.dataLabelBackground,
+ color: palette.dataLabelTextColor,
+ borderRadius: 4,
+ clip: true,
+ font: { size: 12, weight: 'normal', lineHeight: 1.6, family: fontFamily },
+ textAlign: 'center',
+ padding: { top: 8, bottom: 8, right: 8, left: 8 },
+ },
+ },
+ scales: {
+ y: { display: false },
+ x: {
+ position: 'bottom',
+ display: true,
+ border: { display: false },
+ grid: { drawBorder: false, display: false },
+ ticks: {
+ padding: 8,
+ font: { weight: 'bold', family: fontFamily, size: 14 },
+ color: palette.stepNameLegendColor
+ },
+ },
+ },
+ },
+ }
+
+ chartRef.current = new Chart(ctx, config)
+ }
+
+ const header = () => {
+ return (
+
+
{props.funnelName}
+ {props.tabs}
+
+ )
+ }
+
+ const renderError = () => {
+ if (error.name === 'AbortError') return
+ if (error.payload && error.payload.level === 'normal') {
+ return (
+ <>
+ {header()}
+ {error.message}
+ >
+ )
+ } else {
+ return (
+ <>
+ {header()}
+
+
+
Oops! Something went wrong
+
{error.message ? error.message : 'Failed to render funnel'}
+
Please try refreshing your browser or selecting the funnel again.
+
+ >
+ )
+ }
+ }
+
+ const renderInner = () => {
+ if (loading) {
+ return
+ } else if (error) {
+ return renderError()
+ } else if (funnel) {
+ const conversionRate = funnel.steps[funnel.steps.length - 1].conversion_rate
+
+ return (
+
+ {header()}
+
{funnel.steps.length}-step funnel • {conversionRate}% conversion rate
+ {isSmallScreen &&
{renderBars(funnel)}
}
+
+ )
+ }
+ }
+
+ const renderBar = (step) => {
+ const palette = getPalette()
+
+ return (
+ <>
+
+
+
+
+ {step.label}
+
+
+
+
+ {numberFormatter(step.visitors)}
+
+
+ >
+ )
+ }
+
+ const renderBars = (funnel) => {
+ return (
+ <>
+
+
+
+ Visitors
+
+
+
+ {funnel.steps.map(renderBar)}
+
+ >
+ )
+ }
+
+ const formatPercentage = (value) => {
+ const decimalNumber = parseFloat(value);
+ return decimalNumber % 1 === 0 ? decimalNumber.toFixed(0) : value;
+ }
+
+ return (
+
+ setVisible(true)}>
+ {renderInner()}
+
+ {!isSmallScreen && }
+
+ )
+}
diff --git a/assets/js/dashboard/stats/behaviours/index.js b/assets/js/dashboard/stats/behaviours/index.js
index d5eabf5fe..fc6a77e77 100644
--- a/assets/js/dashboard/stats/behaviours/index.js
+++ b/assets/js/dashboard/stats/behaviours/index.js
@@ -82,7 +82,7 @@ export default function Behaviours(props) {
className={classNames(
active ? 'bg-gray-100 dark:bg-gray-900 text-gray-900 dark:text-gray-200 cursor-pointer' : 'text-gray-700 dark:text-gray-200',
'block px-4 py-2 text-sm',
- mode === funnelName ? 'font-bold' : ''
+ selectedFunnel === funnelName ? 'font-bold text-gray-500' : ''
)}
>
{funnelName}
diff --git a/assets/js/liveview/live_socket.js b/assets/js/liveview/live_socket.js
index b83099922..bbf1ea747 100644
--- a/assets/js/liveview/live_socket.js
+++ b/assets/js/liveview/live_socket.js
@@ -2,17 +2,20 @@ import "phoenix_html"
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 }, hooks: {}, dom: {
- // for alpinejs integration
- onBeforeElUpdated(from, to) {
- if (from.__x) {
- window.Alpine.clone(from.__x, to);
- }
- },
- }
-})
+let csrfToken = document.querySelector("meta[name='csrf-token']")
+if (csrfToken) {
+ let token = csrfToken.getAttribute("content")
+ let liveSocket = new LiveSocket("/live", Socket, {
+ params: { _csrf_token: token }, hooks: {}, dom: {
+ // for alpinejs integration
+ onBeforeElUpdated(from, to) {
+ if (from.__x) {
+ window.Alpine.clone(from.__x, to);
+ }
+ },
+ }
+ })
-liveSocket.connect()
-window.liveSocket = liveSocket
+ liveSocket.connect()
+ window.liveSocket = liveSocket
+}
diff --git a/lib/plausible/funnels.ex b/lib/plausible/funnels.ex
index fc4b67084..bf874fe36 100644
--- a/lib/plausible/funnels.ex
+++ b/lib/plausible/funnels.ex
@@ -18,7 +18,7 @@ defmodule Plausible.Funnels do
FunWithFlags.enabled?(:funnels, for: actor)
end
- @spec create(Plausible.Site.t(), String.t(), [Plausible.Goal.t()]) ::
+ @spec create(Plausible.Site.t(), String.t(), [map()]) ::
{:ok, Funnel.t()}
| {:error, Ecto.Changeset.t() | :invalid_funnel_size}
def create(site, name, steps)
@@ -32,13 +32,13 @@ defmodule Plausible.Funnels do
{:error, :invalid_funnel_size}
end
- @spec create_changeset(Plausible.Site.t(), String.t(), [Plausible.Goal.t()]) ::
+ @spec create_changeset(Plausible.Site.t(), String.t(), [map()]) ::
Ecto.Changeset.t()
def create_changeset(site, name, steps) do
Funnel.changeset(%Funnel{site_id: site.id}, %{name: name, steps: steps})
end
- @spec ephemeral_definition(Plausible.Site.t(), String.t(), [Plausible.Goal.t()]) :: Funnel.t()
+ @spec ephemeral_definition(Plausible.Site.t(), String.t(), [map()]) :: Funnel.t()
def ephemeral_definition(site, name, steps) do
site
|> create_changeset(name, steps)
diff --git a/lib/plausible/stats/funnel.ex b/lib/plausible/stats/funnel.ex
index 51b0ab9ca..85c2bfc45 100644
--- a/lib/plausible/stats/funnel.ex
+++ b/lib/plausible/stats/funnel.ex
@@ -15,7 +15,7 @@ defmodule Plausible.Stats.Funnel do
alias Plausible.Stats.Base
@spec funnel(Plausible.Site.t(), Plausible.Stats.Query.t(), Funnel.t() | pos_integer()) ::
- {:ok, Funnel.t()} | {:error, :funnel_not_found}
+ {:ok, map()} | {:error, :funnel_not_found}
def funnel(site, query, funnel_id) when is_integer(funnel_id) do
case Funnels.get(site.id, funnel_id) do
%Funnel{} = funnel ->
@@ -27,34 +27,51 @@ defmodule Plausible.Stats.Funnel do
end
def funnel(site, query, %Funnel{} = funnel) do
- steps =
+ funnel_data =
site
|> Base.base_event_query(query)
|> query_funnel(funnel)
- |> backfill_steps(funnel)
+
+ # Funnel definition steps are 1-indexed, if there's index 0 in the resulting query,
+ # it signifies the number of visitors that haven't entered the funnel.
+ not_entering_visitors =
+ case funnel_data do
+ [{0, count} | _] -> count
+ _ -> 0
+ end
+
+ all_visitors = Enum.reduce(funnel_data, 0, fn {_, n}, acc -> acc + n end)
+ steps = backfill_steps(funnel_data, funnel)
+
+ visitors_at_first_step = List.first(steps).visitors
{:ok,
%{
name: funnel.name,
- steps: steps
+ steps: steps,
+ all_visitors: all_visitors,
+ entering_visitors: visitors_at_first_step,
+ entering_visitors_percentage: percentage(visitors_at_first_step, all_visitors),
+ never_entering_visitors: all_visitors - visitors_at_first_step,
+ never_entering_visitors_percentage: percentage(not_entering_visitors, all_visitors)
}}
end
defp query_funnel(query, funnel_definition) do
q_events =
from(e in query,
- select: %{session_id: e.session_id},
+ select: %{user_id: e.user_id},
where: e.site_id == ^funnel_definition.site_id,
- group_by: e.session_id,
- having: fragment("step > 0"),
+ group_by: e.user_id,
order_by: [desc: fragment("step")]
)
|> select_funnel(funnel_definition)
query =
- from f in subquery(q_events),
+ from(f in subquery(q_events),
select: {f.step, count(1)},
group_by: f.step
+ )
ClickhouseRepo.all(query)
end
@@ -62,12 +79,12 @@ defmodule Plausible.Stats.Funnel do
defp select_funnel(db_query, funnel_definition) do
window_funnel_steps =
Enum.reduce(funnel_definition.steps, nil, fn step, acc ->
- step_condition = step_condition(step.goal)
+ goal_condition = goal_condition(step.goal)
if acc do
- dynamic([q], fragment("?, ?", ^acc, ^step_condition))
+ dynamic([q], fragment("?, ?", ^acc, ^goal_condition))
else
- dynamic([q], fragment("?", ^step_condition))
+ dynamic([q], fragment("?", ^goal_condition))
end
end)
@@ -77,14 +94,15 @@ defmodule Plausible.Stats.Funnel do
fragment("windowFunnel(?)(timestamp, ?)", @funnel_window_duration, ^window_funnel_steps)
)
- from q in db_query,
+ from(q in db_query,
select_merge:
^%{
step: dynamic_window_funnel
}
+ )
end
- defp step_condition(goal) do
+ defp goal_condition(goal) do
case goal do
%Plausible.Goal{event_name: event} when is_binary(event) ->
dynamic([], fragment("name = ?", ^event))
@@ -104,6 +122,9 @@ defmodule Plausible.Stats.Funnel do
# but no totals including previous steps are aggregated.
# Hence we need to perform the appropriate backfill
# and also calculate dropoff and conversion rate for each step.
+ # In case ClickHouse returns 0-index funnel result, we're going to ignore it
+ # anyway, since we fold over steps as per definition, that are always
+ # indexed starting from 1.
funnel_result = Enum.into(funnel_result, %{})
max_step = Enum.max_by(funnel.steps, & &1.step_order).step_order
@@ -128,20 +149,15 @@ defmodule Plausible.Stats.Funnel do
# Dropoff is 0 for the first step, otherwise we subtract current from previous
dropoff = if visitors_at_previous, do: visitors_at_previous - current_visitors, else: 0
- conversion_rate =
- if current_visitors == 0 or total_visitors == 0 do
- "0.00"
- else
- current_visitors
- |> Decimal.div(total_visitors)
- |> Decimal.mult(100)
- |> Decimal.round(2)
- |> Decimal.to_string()
- end
+ dropoff_percentage = percentage(dropoff, visitors_at_previous)
+ conversion_rate = percentage(current_visitors, total_visitors)
+ conversion_rate_step = percentage(current_visitors, visitors_at_previous)
step = %{
dropoff: dropoff,
+ dropoff_percentage: dropoff_percentage,
conversion_rate: conversion_rate,
+ conversion_rate_step: conversion_rate_step,
visitors: visitors_at_step,
label: to_string(step.goal)
}
@@ -151,4 +167,16 @@ defmodule Plausible.Stats.Funnel do
|> elem(2)
|> Enum.reverse()
end
+
+ defp percentage(x, y) when x in [0, nil] or y in [0, nil] do
+ "0.00"
+ end
+
+ defp percentage(x, y) do
+ x
+ |> Decimal.div(y)
+ |> Decimal.mult(100)
+ |> Decimal.round(2)
+ |> Decimal.to_string()
+ end
end
diff --git a/lib/plausible_web/controllers/api/stats_controller.ex b/lib/plausible_web/controllers/api/stats_controller.ex
index bdc306fb1..84c09a90a 100644
--- a/lib/plausible_web/controllers/api/stats_controller.ex
+++ b/lib/plausible_web/controllers/api/stats_controller.ex
@@ -7,7 +7,7 @@ defmodule PlausibleWeb.Api.StatsController do
require Logger
- plug :validate_common_input
+ plug(:validate_common_input)
@doc """
Returns a time-series based on given parameters.
@@ -506,6 +506,52 @@ defmodule PlausibleWeb.Api.StatsController do
end
end
+ def funnel(conn, %{"id" => funnel_id} = params) do
+ site = conn.assigns[:site]
+
+ with :ok <- validate_params(params),
+ query <- Query.from(site, params) |> Filters.add_prefix(),
+ :ok <- validate_funnel_query(query),
+ {funnel_id, ""} <- Integer.parse(funnel_id),
+ {:ok, funnel} <- Stats.funnel(site, query, funnel_id) do
+ json(conn, funnel)
+ else
+ {:error, {:invalid_funnel_query, due_to}} ->
+ bad_request(
+ conn,
+ "We are unable to show funnels when the dashboard is filtered by #{due_to}",
+ %{
+ level: :normal
+ }
+ )
+
+ {:error, :funnel_not_found} ->
+ conn
+ |> put_status(404)
+ |> json(%{error: "Funnel not found"})
+ |> halt()
+
+ _ ->
+ bad_request(conn, "There was an error with your request")
+ end
+ end
+
+ defp validate_funnel_query(query) do
+ case query do
+ _ when is_map_key(query.filters, "event:goal") ->
+ {:error, {:invalid_funnel_query, "goals"}}
+
+ _ when is_map_key(query.filters, "event:page") ->
+ {:error, {:invalid_funnel_query, "pages"}}
+
+ _ when query.period == "realtime" ->
+ {:error, {:invalid_funnel_query, "realtime period"}}
+
+ _ ->
+ :ok
+ end
+ end
+
def utm_mediums(conn, params) do
site = conn.assigns[:site]
@@ -1339,10 +1385,12 @@ defmodule PlausibleWeb.Api.StatsController do
end
end
- defp bad_request(conn, message) do
+ defp bad_request(conn, message, extra \\ %{}) do
+ payload = Map.merge(extra, %{error: message})
+
conn
|> put_status(400)
- |> json(%{error: message})
+ |> json(payload)
|> halt()
end
diff --git a/lib/plausible_web/controllers/stats_controller.ex b/lib/plausible_web/controllers/stats_controller.ex
index 0e9ec1d50..72b6ba9a4 100644
--- a/lib/plausible_web/controllers/stats_controller.ex
+++ b/lib/plausible_web/controllers/stats_controller.ex
@@ -64,7 +64,7 @@ defmodule PlausibleWeb.StatsController do
|> render("stats.html",
site: site,
has_goals: Plausible.Sites.has_goals?(site),
- funnels: [],
+ funnels: Plausible.Funnels.list(site),
stats_start_date: stats_start_date,
native_stats_start_date: NaiveDateTime.to_date(site.native_stats_start_at),
title: "Plausible · " <> site.domain,
@@ -292,7 +292,7 @@ defmodule PlausibleWeb.StatsController do
|> render("stats.html",
site: shared_link.site,
has_goals: Sites.has_goals?(shared_link.site),
- funnels: [],
+ funnels: Plausible.Funnels.list(shared_link.site),
stats_start_date: shared_link.site.stats_start_date,
native_stats_start_date: NaiveDateTime.to_date(shared_link.site.native_stats_start_at),
title: "Plausible · " <> shared_link.site.domain,
diff --git a/lib/plausible_web/live/funnel_settings/form.ex b/lib/plausible_web/live/funnel_settings/form.ex
index 615319b5c..dcb9b6f60 100644
--- a/lib/plausible_web/live/funnel_settings/form.ex
+++ b/lib/plausible_web/live/funnel_settings/form.ex
@@ -31,7 +31,7 @@ defmodule PlausibleWeb.Live.FunnelSettings.Form do
form: to_form(Plausible.Funnels.create_changeset(site, "", [])),
goals: goals,
site: site,
- already_selected: Map.new()
+ selections_made: Map.new()
)}
end
@@ -63,7 +63,7 @@ defmodule PlausibleWeb.Live.FunnelSettings.Form do
submit_name="funnel[steps][][goal_id]"
module={PlausibleWeb.Live.FunnelSettings.ComboBox}
id={"step-#{step_idx}"}
- options={reject_alrady_selected("step-#{step_idx}", @goals, @already_selected)}
+ options={reject_alrady_selected("step-#{step_idx}", @goals, @selections_made)}
/>
@@ -72,7 +72,7 @@ defmodule PlausibleWeb.Live.FunnelSettings.Form do
<.add_step_button :if={
length(@step_ids) < Funnel.max_steps() and
- map_size(@already_selected) < length(@goals)
+ map_size(@selections_made) < length(@goals)
} />
@@ -202,13 +202,9 @@ defmodule PlausibleWeb.Live.FunnelSettings.Form do
def handle_event("remove-step", %{"step-idx" => idx}, socket) do
idx = String.to_integer(idx)
step_ids = List.delete(socket.assigns.step_ids, idx)
- already_selected = socket.assigns.already_selected
+ selections_made = drop_selection(socket.assigns.selections_made, idx)
- step_input_id = "step-#{idx}"
-
- new_already_selected = Map.delete(already_selected, step_input_id)
-
- {:noreply, assign(socket, step_ids: step_ids, already_selected: new_already_selected)}
+ {:noreply, assign(socket, step_ids: step_ids, selections_made: selections_made)}
end
def handle_event("validate", %{"funnel" => params}, socket) do
@@ -242,14 +238,12 @@ defmodule PlausibleWeb.Live.FunnelSettings.Form do
end
def handle_info({:selection_made, %{submit_value: goal_id, by: combo_box}}, socket) do
- already_selected = Map.put(socket.assigns.already_selected, combo_box, goal_id)
- {:noreply, assign(socket, already_selected: already_selected)}
- end
+ selections_made = store_selection(socket.assigns, combo_box, goal_id)
- defp reject_alrady_selected(combo_box, goals, already_selected) do
- result = Enum.reject(goals, fn {goal_id, _} -> goal_id in Map.values(already_selected) end)
- send_update(PlausibleWeb.Live.FunnelSettings.ComboBox, id: combo_box, suggestions: result)
- result
+ {:noreply,
+ assign(socket,
+ selections_made: selections_made
+ )}
end
defp find_sequence_break(input) do
@@ -268,4 +262,34 @@ defmodule PlausibleWeb.Live.FunnelSettings.Form do
defp has_steps_errors?(f) do
not f.source.valid?
end
+
+ defp get_goal(assigns, id) do
+ assigns
+ |> Map.fetch!(:goals)
+ |> Enum.find_value(fn
+ {goal_id, goal} when goal_id == id -> goal
+ _ -> nil
+ end)
+ end
+
+ defp store_selection(assigns, combo_box, goal_id) do
+ Map.put(assigns.selections_made, combo_box, get_goal(assigns, goal_id))
+ end
+
+ defp drop_selection(selections_made, step_idx) do
+ step_input_id = "step-#{step_idx}"
+ Map.delete(selections_made, step_input_id)
+ end
+
+ defp reject_alrady_selected(combo_box, goals, selections_made) do
+ selection_ids =
+ Enum.map(selections_made, fn
+ {_, %{id: goal_id}} -> goal_id
+ end)
+
+ result = Enum.reject(goals, fn {goal_id, _} -> goal_id in selection_ids end)
+
+ send_update(PlausibleWeb.Live.FunnelSettings.ComboBox, id: combo_box, suggestions: result)
+ result
+ end
end
diff --git a/lib/plausible_web/router.ex b/lib/plausible_web/router.ex
index 62882c15b..018ab227f 100644
--- a/lib/plausible_web/router.ex
+++ b/lib/plausible_web/router.ex
@@ -59,7 +59,7 @@ defmodule PlausibleWeb.Router do
scope "/api/stats", PlausibleWeb.Api do
pipe_through :internal_stats_api
-
+ get "/:domain//funnels/:id", StatsController, :funnel
get "/:domain/current-visitors", StatsController, :current_visitors
get "/:domain/main-graph", StatsController, :main_graph
get "/:domain/top-stats", StatsController, :top_stats
diff --git a/lib/plausible_web/templates/site/settings_funnels.html.heex b/lib/plausible_web/templates/site/settings_funnels.html.heex
index 33c1058be..5ac5032bc 100644
--- a/lib/plausible_web/templates/site/settings_funnels.html.heex
+++ b/lib/plausible_web/templates/site/settings_funnels.html.heex
@@ -4,6 +4,7 @@
Compose goals into funnels
+
<%= link(to: "https://plausible.io/docs/funnel-analysis", target: "_blank", rel: "noreferrer") do %>