From f4298328e6310e42dad9e39b5578da16d0089c0f Mon Sep 17 00:00:00 2001 From: Robert Joonas Date: Mon, 1 Apr 2024 17:08:12 +0100 Subject: [PATCH] Make sure revenue metrics can remain on the graph --- assets/js/dashboard.js | 1 + assets/js/dashboard/stats/graph/graph-util.js | 32 +++++++++++++++---- .../js/dashboard/stats/graph/visitor-graph.js | 2 +- lib/plausible/goals.ex | 8 +++++ .../controllers/stats_controller.ex | 11 +++++-- .../templates/stats/stats.html.heex | 1 + test/plausible/goals_test.exs | 18 +++++++++++ 7 files changed, 63 insertions(+), 10 deletions(-) diff --git a/assets/js/dashboard.js b/assets/js/dashboard.js index c733c0905..227514849 100644 --- a/assets/js/dashboard.js +++ b/assets/js/dashboard.js @@ -22,6 +22,7 @@ if (container) { conversionsOptedOut: container.dataset.conversionsOptedOut === 'true', funnelsOptedOut: container.dataset.funnelsOptedOut === 'true', propsOptedOut: container.dataset.propsOptedOut === 'true', + revenueGoals: JSON.parse(container.dataset.revenueGoals), funnels: JSON.parse(container.dataset.funnels), statsBegin: container.dataset.statsBegin, nativeStatsBegin: container.dataset.nativeStatsBegin, diff --git a/assets/js/dashboard/stats/graph/graph-util.js b/assets/js/dashboard/stats/graph/graph-util.js index 728cd6bdd..b0f3d4a53 100644 --- a/assets/js/dashboard/stats/graph/graph-util.js +++ b/assets/js/dashboard/stats/graph/graph-util.js @@ -1,23 +1,43 @@ import numberFormatter, {durationFormatter} from '../../util/number-formatter' +import { parsePrefix } from '../../util/filters' -export function getGraphableMetrics(query) { +export function getGraphableMetrics(query, site) { const isRealtime = query.period === 'realtime' - const isGoalFilter = !!query.filters.goal - const isPageFilter = !!query.filters.page + const goalFilter = query.filters.goal + const pageFilter = query.filters.page - if (isRealtime && isGoalFilter) { + if (isRealtime && !!goalFilter) { return ["visitors"] } else if (isRealtime) { return ["visitors", "pageviews"] - } else if (isGoalFilter) { + } else if (!!goalFilter && canGraphRevenueMetrics(goalFilter, site)) { + return ["visitors", "events", "average_revenue", "total_revenue", "conversion_rate"] + } else if (!!goalFilter) { return ["visitors", "events", "conversion_rate"] - } else if (isPageFilter) { + } else if (!!pageFilter) { return ["visitors", "visits", "pageviews", "bounce_rate", "time_on_page"] } else { return ["visitors", "visits", "pageviews", "views_per_visit", "bounce_rate", "visit_duration"] } } +// Revenue metrics can only be graphed if: +// * The query is filtered by at least one revenue goal +// * All revenue goals in filter have the same currency +function canGraphRevenueMetrics(goalFilter, site) { + const goalsInFilter = parsePrefix(goalFilter).values + + const revenueGoalsInFilter = site.revenueGoals.filter((rg) => { + return goalsInFilter.includes(rg.event_name) + }) + + const singleCurrency = revenueGoalsInFilter.every((rg) => { + return rg.currency === revenueGoalsInFilter[0].currency + }) + + return revenueGoalsInFilter.length > 0 && singleCurrency +} + export const METRIC_LABELS = { 'visitors': 'Visitors', 'pageviews': 'Pageviews', diff --git a/assets/js/dashboard/stats/graph/visitor-graph.js b/assets/js/dashboard/stats/graph/visitor-graph.js index 667840502..a9b51f7ae 100644 --- a/assets/js/dashboard/stats/graph/visitor-graph.js +++ b/assets/js/dashboard/stats/graph/visitor-graph.js @@ -86,7 +86,7 @@ export default function VisitorGraph(props) { }) let metric = getStoredMetric() - const availableMetrics = getGraphableMetrics(query) + const availableMetrics = getGraphableMetrics(query, site) if (!availableMetrics.includes(metric)) { metric = availableMetrics[0] diff --git a/lib/plausible/goals.ex b/lib/plausible/goals.ex index 32e88419c..11118ff84 100644 --- a/lib/plausible/goals.ex +++ b/lib/plausible/goals.ex @@ -80,6 +80,14 @@ defmodule Plausible.Goals do def find_or_create(_, %{"goal_type" => "page"}), do: {:missing, "page_path"} + def list_revenue_goals(site) do + from(g in Plausible.Goal, + where: g.site_id == ^site.id and not is_nil(g.currency), + select: %{event_name: g.event_name, currency: g.currency} + ) + |> Plausible.Repo.all() + end + def for_site(site, opts \\ []) do site |> for_site_query(opts) diff --git a/lib/plausible_web/controllers/stats_controller.ex b/lib/plausible_web/controllers/stats_controller.ex index a2f3f997c..683c34a84 100644 --- a/lib/plausible_web/controllers/stats_controller.ex +++ b/lib/plausible_web/controllers/stats_controller.ex @@ -64,6 +64,7 @@ defmodule PlausibleWeb.StatsController do |> render("stats.html", site: site, has_goals: Plausible.Sites.has_goals?(site), + revenue_goals: list_revenue_goals(site), funnels: list_funnels(site), has_props: Plausible.Props.configured?(site), stats_start_date: stats_start_date, @@ -92,10 +93,13 @@ defmodule PlausibleWeb.StatsController do defp list_funnels(site) do Plausible.Funnels.list(site) end - else - defp list_funnels(_site) do - [] + + defp list_revenue_goals(site) do + Plausible.Goals.list_revenue_goals(site) end + else + defp list_revenue_goals(_site), do: [] + defp list_revenue_goals(_site), do: [] end @doc """ @@ -319,6 +323,7 @@ defmodule PlausibleWeb.StatsController do |> render("stats.html", site: shared_link.site, has_goals: Sites.has_goals?(shared_link.site), + revenue_goals: list_revenue_goals(shared_link.site), funnels: list_funnels(shared_link.site), has_props: Plausible.Props.configured?(shared_link.site), stats_start_date: stats_start_date, diff --git a/lib/plausible_web/templates/stats/stats.html.heex b/lib/plausible_web/templates/stats/stats.html.heex index 2a09599a9..4a2d69ab5 100644 --- a/lib/plausible_web/templates/stats/stats.html.heex +++ b/lib/plausible_web/templates/stats/stats.html.heex @@ -27,6 +27,7 @@ data-props-available={ to_string(Plausible.Billing.Feature.Props.check_availability(@site.owner) == :ok) } + data-revenue-goals={Jason.encode!(@revenue_goals)} data-funnels={Jason.encode!(@funnels)} data-has-props={to_string(@has_props)} data-logged-in={to_string(!!@conn.assigns[:current_user])} diff --git a/test/plausible/goals_test.exs b/test/plausible/goals_test.exs index c799afd9a..15a237804 100644 --- a/test/plausible/goals_test.exs +++ b/test/plausible/goals_test.exs @@ -92,6 +92,24 @@ defmodule Plausible.GoalsTest do assert [currency: {"is invalid", _}] = changeset.errors end + @tag :full_build_only + test "list_revenue_goals/1 lists event_names and currencies for each revenue goal" do + site = insert(:site) + + Goals.create(site, %{"event_name" => "One", "currency" => "EUR"}) + Goals.create(site, %{"event_name" => "Two", "currency" => "EUR"}) + Goals.create(site, %{"event_name" => "Three", "currency" => "USD"}) + Goals.create(site, %{"event_name" => "Four"}) + Goals.create(site, %{"page_path" => "/some-page"}) + + revenue_goals = Goals.list_revenue_goals(site) + + assert length(revenue_goals) == 3 + assert %{event_name: "One", currency: :EUR} in revenue_goals + assert %{event_name: "Two", currency: :EUR} in revenue_goals + assert %{event_name: "Three", currency: :USD} in revenue_goals + end + test "create/2 clears currency for pageview goals" do site = insert(:site) {:ok, goal} = Goals.create(site, %{"page_path" => "/purchase", "currency" => "EUR"})