diff --git a/assets/js/dashboard/stats/behaviours/funnel.js b/assets/js/dashboard/stats/behaviours/funnel.js
index a16f4af41..b24b8bb6b 100644
--- a/assets/js/dashboard/stats/behaviours/funnel.js
+++ b/assets/js/dashboard/stats/behaviours/funnel.js
@@ -3,13 +3,15 @@ import Chart from 'chart.js/auto';
import ChartDataLabels from 'chartjs-plugin-datalabels';
Chart.register(ChartDataLabels);
+import RocketIcon from '../modals/rocket-icon'
+
import * as api from '../../api'
import LazyLoader from '../../components/lazy-loader'
export default class Funnel extends React.Component {
constructor(props) {
super(props)
- this.state = { loading: true }
+ this.state = { loading: true, error: null }
this.onVisible = this.onVisible.bind(this)
this.fetchFunnel = this.fetchFunnel.bind(this)
}
@@ -23,7 +25,7 @@ export default class Funnel extends React.Component {
const funnelChanged = this.props.funnel !== prevProps.funnel
if (queryChanged || funnelChanged) {
- this.setState({loading: true})
+ this.setState({ loading: true, error: null })
this.fetchFunnel()
}
}
@@ -39,9 +41,18 @@ export default class Funnel extends React.Component {
}
fetchFunnel() {
- api.get(`/api/stats/${encodeURIComponent(this.props.site.domain)}/funnels/${this.props.funnel.id}`, this.props.query)
- .then((res) => this.setState({ loading: false, funnel: res }))
- .then(this.initialiseChart.bind(this))
+ if (this.props.funnel) {
+ api.get(`/api/stats/${encodeURIComponent(this.props.site.domain)}/funnels/${this.props.funnel.id}`, this.props.query)
+ .then((res) => {
+ this.setState({ loading: false, funnel: res, error: null })
+ })
+ .then(this.initialiseChart.bind(this))
+ .catch((error) => {
+ this.setState({ loading: false, error: error })
+ })
+ } else {
+ this.setState({ loading: false, error: { message: "Failed to locate funnel" } })
+ }
}
initialiseChart() {
@@ -103,14 +114,14 @@ export default class Funnel extends React.Component {
backgroundColor: 'rgba(25, 30, 56)',
color: 'rgb(243, 244, 246)',
borderRadius: 4,
- font: {size: 14, weight: 'bold'},
+ font: { size: 14, weight: 'bold' },
textAlign: 'center',
- padding: {top: 4, bottom: 4, right: 8, left: 8}
+ padding: { top: 4, bottom: 4, right: 8, left: 8 }
}
},
scales: {
- y: { display: false},
- x: { position: 'top', display: true, border: { display: false }, grid: {drawBorder: false, display: false}, ticks: {padding: 24, font: { weight: 'bold', size: 14 }, color: 'rgb(17, 24, 39)'}}
+ y: { display: false },
+ x: { position: 'top', display: true, border: { display: false }, grid: { drawBorder: false, display: false }, ticks: { padding: 24, font: { weight: 'bold', size: 14 }, color: 'rgb(17, 24, 39)' } }
}
},
};
@@ -118,9 +129,37 @@ export default class Funnel extends React.Component {
this.chart = new Chart(ctx, config);
}
+ tabs() {
+ return (
+
+
Funnels
+ {this.props.tabs}
+
+ )
+ }
+
renderInner() {
if (this.state.loading) {
return
+ } else if (this.state.error) {
+ if (this.state.error && this.state.error.payload.level === "normal") {
+ return (
+ {this.tabs()}
+ {this.state.error.message}
+ )
+ } else {
+ return (
+
+ {this.tabs()}
+
+
+
Oops! Something went wrong
+
{this.state.error.message ? this.state.error.message : "Failed to render funnel"}
+
Please try refreshing your browser and selecting the funnel again.
+
+
+ )
+ }
} else if (this.state.funnel) {
const firstStep = this.state.funnel.steps[0].visitors
const lastStep = this.state.funnel.steps[this.state.funnel.steps.length - 1].visitors
@@ -128,10 +167,7 @@ export default class Funnel extends React.Component {
return (
-
-
{this.state.funnel.name}
- {this.props.tabs}
-
+ {this.tabs()}
{this.state.funnel.steps.length}-step funnel • {conversionRate}% conversion rate
@@ -141,7 +177,7 @@ export default class Funnel extends React.Component {
render() {
return (
-
+
{this.renderInner()}
)
diff --git a/lib/plausible/funnels.ex b/lib/plausible/funnels.ex
index 608cef4f6..82c22f6a4 100644
--- a/lib/plausible/funnels.ex
+++ b/lib/plausible/funnels.ex
@@ -51,38 +51,44 @@ defmodule Plausible.Funnels do
steps: {steps, goal: goal}
]
- Repo.one(q)
+ funnel = Repo.one(q)
+
+ if funnel do
+ {:ok, funnel}
+ else
+ {:error, "Funnel not found"}
+ end
end
def evaluate(query, funnel_id, site) do
- # TODO: handle non-existing funnel
- funnel = get(site.id, funnel_id)
-
- q_events =
- from(e in Base.base_event_query(site, query),
- select: %{session_id: e.session_id},
- where: e.site_id == ^funnel.site_id,
- group_by: e.session_id,
- having: fragment("step > 0"),
- order_by: [desc: fragment("step")]
- )
- |> select_funnel(funnel)
-
- query =
- from f in subquery(q_events),
- select: {f.step, count(1)},
- group_by: f.step
-
- steps =
- query
- |> ClickhouseRepo.all()
- |> Enum.into(%{})
- |> backfill_steps(funnel)
-
- %{
- name: funnel.name,
- steps: steps
- }
+ with {:ok, funnel} <- get(site.id, funnel_id) do
+ q_events =
+ from(e in Base.base_event_query(site, query),
+ select: %{session_id: e.session_id},
+ where: e.site_id == ^funnel.site_id,
+ group_by: e.session_id,
+ having: fragment("step > 0"),
+ order_by: [desc: fragment("step")]
+ )
+ |> select_funnel(funnel)
+
+ query =
+ from f in subquery(q_events),
+ select: {f.step, count(1)},
+ group_by: f.step
+
+ steps =
+ query
+ |> ClickhouseRepo.all()
+ |> Enum.into(%{})
+ |> backfill_steps(funnel)
+
+ {:ok,
+ %{
+ name: funnel.name,
+ steps: steps
+ }}
+ end
end
defp select_funnel(db_query, funnel) do
diff --git a/lib/plausible_web/controllers/api/stats_controller.ex b/lib/plausible_web/controllers/api/stats_controller.ex
index c28c3baff..c121002ef 100644
--- a/lib/plausible_web/controllers/api/stats_controller.ex
+++ b/lib/plausible_web/controllers/api/stats_controller.ex
@@ -851,23 +851,43 @@ defmodule PlausibleWeb.Api.StatsController do
with :ok <- validate_params(params),
query <- Query.from(site, params) |> Filters.add_prefix(),
- :ok <- validate_funnel_query(query) do
- {funnel_id, ""} = Integer.parse(funnel_id)
- funnel = Plausible.Funnels.evaluate(query, funnel_id, site)
-
+ :ok <- validate_funnel_query(query),
+ {funnel_id, ""} <- Integer.parse(funnel_id),
+ {:ok, funnel} <- Plausible.Funnels.evaluate(query, funnel_id, site) do
json(conn, funnel)
else
- {:error, message} when is_binary(message) -> bad_request(conn, message)
+ {: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, message} when is_binary(message) ->
+ bad_request(conn, message)
+
+ _ ->
+ bad_request(conn, "There was an error with your request")
end
end
defp validate_funnel_query(query) do
- if query.filters["event:page"] || query.filters["event:goal"] || query.period == "realtime" do
- raise "Implement me properly"
- {:error, "Funnels unavailable for current set of filters"}
- else
- :ok
+ 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
+ |> IO.inspect(label: :query_valid?)
end
defp calculate_cr(nil, _converted_visitors), do: nil
@@ -1267,10 +1287,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