Open source, privacy-first web analytics. Lightweight, cookie-free Google Analytics alternative. Self-hosted or cloud.
TypeScript analyticsanalytics-dashboardclickhouseelixirgoogle-analyticsmarketingopen-source-analyticsphoenixplausible-analyticspostgresqlprivacyprivacy-friendlyself-hostedsimple-analyticsstatisticstailwindcssweb-analyticswebsite-analyticswebsite-statswebsite-tracking
Provide top stats graphable metrics from BE (#5160)
Artur Pata2db60c90af4c
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 535604784a..0c416f84be 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -39,6 +39,7 @@ All notable changes to this project will be documented in this file.- Fix database mismatch between event and session user_ids after rotating salts- `/api/v2/query` no longer returns a 500 when querying percentage metric without `visitors`- Fix current visitors loading when viewing a dashboard with a shared link+- Fix Conversion Rate graph being unselectable when "Goal is ..." filter is within a segment## v2.1.5-rc.1 - 2025-01-17diff --git a/assets/js/dashboard/stats/graph/graph-util.js b/assets/js/dashboard/stats/graph/graph-util.jsindex ad4b5b7d0f..743ab00c39 100644--- a/assets/js/dashboard/stats/graph/graph-util.js+++ b/assets/js/dashboard/stats/graph/graph-util.js@@ -1,27 +1,3 @@-import { getFiltersByKeyPrefix, hasConversionGoalFilter } from '../../util/filters'-import { revenueAvailable } from '../../query'--export function getGraphableMetrics(query, site) {- const isRealtime = query.period === 'realtime'- const isGoalFilter = hasConversionGoalFilter(query)- const isPageFilter = getFiltersByKeyPrefix(query, "page").length > 0-- if (isRealtime && isGoalFilter) {- return ["visitors"]- } else if (isRealtime) {- return ["visitors", "pageviews"]- } else if (isGoalFilter && revenueAvailable(query, site)) {- return ["visitors", "events", "average_revenue", "total_revenue", "conversion_rate"]- } else if (isGoalFilter) {- return ["visitors", "events", "conversion_rate"]- } else if (isPageFilter) {- const pageFilterMetrics = ["visitors", "visits", "pageviews", "bounce_rate"]- return site.scrollDepthVisible ? [...pageFilterMetrics, "scroll_depth"] : pageFilterMetrics- } else {- return ["visitors", "visits", "pageviews", "views_per_visit", "bounce_rate", "visit_duration"]- }-}-export const METRIC_LABELS = {'visitors': 'Visitors','pageviews': 'Pageviews',diff --git a/assets/js/dashboard/stats/graph/top-stats.js b/assets/js/dashboard/stats/graph/top-stats.jsindex cbbb7d39c3..576102ab35 100644--- a/assets/js/dashboard/stats/graph/top-stats.js+++ b/assets/js/dashboard/stats/graph/top-stats.js@@ -6,7 +6,6 @@ import { SecondsSinceLastLoad } from '../../util/seconds-since-last-load'import classNames from 'classnames'import * as storage from '../../util/storage'import { formatDateRange } from '../../util/date'-import { getGraphableMetrics } from './graph-util'import { useQueryContext } from '../../query-context'import { useSiteContext } from '../../site-context'import { useLastLoadContext } from '../../last-load-context'@@ -26,7 +25,12 @@ function topStatNumberLong(metric, value) {return formatter(value)}-export default function TopStats({ data, onMetricUpdate, tooltipBoundary }) {+export default function TopStats({+ data,+ onMetricUpdate,+ tooltipBoundary,+ graphableMetrics+}) {const { query } = useQueryContext()const lastLoadTimestamp = useLastLoadContext()const site = useSiteContext()@@ -77,7 +81,6 @@ export default function TopStats({ data, onMetricUpdate, tooltipBoundary }) {}function canMetricBeGraphed(stat) {- const graphableMetrics = getGraphableMetrics(query, site)return graphableMetrics.includes(stat.graph_metric)}diff --git a/assets/js/dashboard/stats/graph/visitor-graph.js b/assets/js/dashboard/stats/graph/visitor-graph.jsindex 434bc09e30..7930d41df0 100644--- a/assets/js/dashboard/stats/graph/visitor-graph.js+++ b/assets/js/dashboard/stats/graph/visitor-graph.js@@ -2,7 +2,6 @@import React, { useState, useEffect, useRef, useCallback } from 'react';import * as api from '../../api';import * as storage from '../../util/storage';-import { getGraphableMetrics } from './graph-util';import TopStats from './top-stats';import { IntervalPicker, getCurrentInterval } from './interval-picker';import StatsExport from './stats-export';@@ -84,18 +83,11 @@ export default function VisitorGraph({ updateImportedDataInView }) {if (topStatData) { storeTopStatsContainerHeight() }}, [topStatData])- function fetchTopStatsAndGraphData() {- fetchTopStats(site, query)- .then((res) => {- if (updateImportedDataInView) {- updateImportedDataInView(res.includes_imported)- }- setTopStatData(res)- setTopStatsLoading(false)- })-+ async function fetchTopStatsAndGraphData() {+ const response = await fetchTopStats(site, query)+let metric = getStoredMetric()- const availableMetrics = getGraphableMetrics(query, site)+ const availableMetrics = response.graphable_metricsif (!availableMetrics.includes(metric)) {metric = availableMetrics[0]@@ -103,6 +95,13 @@ export default function VisitorGraph({ updateImportedDataInView }) {}const interval = getCurrentInterval(site, query)++ if (response.updateImportedDataInView) {+ updateImportedDataInView(response.includes_imported)+ }++ setTopStatData(response)+ setTopStatsLoading(false)fetchGraphData(metric, interval)}@@ -159,6 +158,7 @@ export default function VisitorGraph({ updateImportedDataInView }) {<FadeIn show={!(topStatsLoading || graphLoading)}><div id="top-stats-container" className="flex flex-wrap" ref={topStatsBoundary} style={{ height: getTopStatsHeight() }}><TopStats+ graphableMetrics={topStatData?.graphable_metrics || []}data={topStatData}onMetricUpdate={onMetricUpdate}tooltipBoundary={topStatsBoundary.current}diff --git a/lib/plausible_web/controllers/api/stats_controller.ex b/lib/plausible_web/controllers/api/stats_controller.exindex 15f5aefaa0..c773948b1b 100644--- a/lib/plausible_web/controllers/api/stats_controller.ex+++ b/lib/plausible_web/controllers/api/stats_controller.ex@@ -194,7 +194,8 @@ defmodule PlausibleWeb.Api.StatsController do%{top_stats: top_stats,meta: meta,- sample_percent: sample_percent+ sample_percent: sample_percent,+ graphable_metrics: graphable_metrics} = fetch_top_stats(site, query, current_user)comparison_query = comparison_query(query)@@ -202,6 +203,7 @@ defmodule PlausibleWeb.Api.StatsController dojson(conn, %{top_stats: top_stats,meta: meta,+ graphable_metrics: graphable_metrics,interval: query.interval,sample_percent: sample_percent,with_imported_switch: with_imported_switch_info(meta),@@ -276,7 +278,8 @@ defmodule PlausibleWeb.Api.StatsController doenddefp fetch_top_stats(site, query, current_user) do- goal_filter? = toplevel_goal_filter?(query)+ goal_filter? =+ toplevel_goal_filter?(query)cond doquery.period == "30m" && goal_filter? ->@@ -322,7 +325,12 @@ defmodule PlausibleWeb.Api.StatsController do}]- %{top_stats: top_stats, meta: meta, sample_percent: 100}+ %{+ top_stats: top_stats,+ meta: meta,+ graphable_metrics: [:visitors, :events],+ sample_percent: 100+ }enddefp fetch_realtime_top_stats(site, query) do@@ -354,7 +362,12 @@ defmodule PlausibleWeb.Api.StatsController do}]- %{top_stats: top_stats, meta: meta, sample_percent: 100}+ %{+ top_stats: top_stats,+ meta: meta,+ sample_percent: 100,+ graphable_metrics: [:visitors, :pageviews]+ }enddefp fetch_goal_top_stats(site, query) do@@ -377,7 +390,7 @@ defmodule PlausibleWeb.Api.StatsController do]|> Enum.reject(&is_nil/1)- %{top_stats: top_stats, meta: meta, sample_percent: 100}+ %{top_stats: top_stats, meta: meta, graphable_metrics: metrics, sample_percent: 100}enddefp fetch_other_top_stats(site, query, current_user) do@@ -428,7 +441,12 @@ defmodule PlausibleWeb.Api.StatsController dosample_percent = results[:sample_percent][:value]- %{top_stats: top_stats, meta: meta, sample_percent: sample_percent}+ %{+ top_stats: top_stats,+ meta: meta,+ graphable_metrics: metrics -- [:time_on_page],+ sample_percent: sample_percent+ }enddefp top_stats_entry(current_results, name, key, opts \\ []) do@@ -1656,6 +1674,9 @@ defmodule PlausibleWeb.Api.StatsController dodefp realtime_period_to_30m(params), do: paramsdefp toplevel_goal_filter?(query) do- Filters.filtering_on_dimension?(query, "event:goal", max_depth: 0)+ Filters.filtering_on_dimension?(query, "event:goal",+ max_depth: 0,+ behavioral_filters: :ignore+ )endend