diff --git a/assets/js/dashboard/stats/graph/graph-util.js b/assets/js/dashboard/stats/graph/graph-util.js
index 9f402097d..c517d27f6 100644
--- a/assets/js/dashboard/stats/graph/graph-util.js
+++ b/assets/js/dashboard/stats/graph/graph-util.js
@@ -1,21 +1,5 @@
import numberFormatter, {durationFormatter} from '../../util/number-formatter'
-export const METRIC_MAPPING = {
- 'Unique visitors (last 30 min)': 'visitors',
- 'Pageviews (last 30 min)': 'pageviews',
- 'Unique visitors': 'visitors',
- 'Visit duration': 'visit_duration',
- 'Total pageviews': 'pageviews',
- 'Views per visit': 'views_per_visit',
- 'Total visits': 'visits',
- 'Bounce rate': 'bounce_rate',
- 'Unique conversions': 'conversions',
- 'Total conversions': 'events',
- 'Conversion rate': 'conversion_rate',
- 'Average revenue': 'average_revenue',
- 'Total revenue': 'total_revenue',
-}
-
export const METRIC_LABELS = {
'visitors': 'Visitors',
'pageviews': 'Pageviews',
@@ -45,11 +29,9 @@ export const METRIC_FORMATTER = {
}
export const LoadingState = {
- loading: 'loading',
- refreshing: 'refreshing',
- loaded: 'loaded',
- isLoadingOrRefreshing: function (state) { return [this.loading, this.refreshing].includes(state) },
- isLoadedOrRefreshing: function (state) { return [this.loaded, this.refreshing].includes(state) }
+ loading: 0,
+ updatingGraph: 1,
+ loaded: 2
}
const buildComparisonDataset = function(comparisonPlot) {
diff --git a/assets/js/dashboard/stats/graph/line-graph.js b/assets/js/dashboard/stats/graph/line-graph.js
index ba5b55355..999eb53e6 100644
--- a/assets/js/dashboard/stats/graph/line-graph.js
+++ b/assets/js/dashboard/stats/graph/line-graph.js
@@ -28,7 +28,8 @@ class LineGraph extends React.Component {
}
regenerateChart() {
- const { graphData, metric, query } = this.props
+ const { graphData, query } = this.props
+ const metric = graphData.metric
const graphEl = document.getElementById("main-graph-canvas")
this.ctx = graphEl.getContext('2d');
const dataSet = buildDataSet(graphData.plot, graphData.comparison_plot, graphData.present_index, this.ctx, METRIC_LABELS[metric])
diff --git a/assets/js/dashboard/stats/graph/top-stats.js b/assets/js/dashboard/stats/graph/top-stats.js
index 125aa104e..bb1ffb515 100644
--- a/assets/js/dashboard/stats/graph/top-stats.js
+++ b/assets/js/dashboard/stats/graph/top-stats.js
@@ -3,7 +3,7 @@ import { Tooltip } from '../../util/tooltip'
import { SecondsSinceLastLoad } from '../../util/seconds-since-last-load'
import classNames from "classnames";
import numberFormatter, { durationFormatter } from '../../util/number-formatter'
-import { METRIC_MAPPING } from './graph-util'
+import * as storage from '../../util/storage'
import { formatDateRange } from '../../util/date.js'
function Maybe({condition, children}) {
@@ -86,15 +86,13 @@ export default class TopStats extends React.Component {
}
canMetricBeGraphed(stat) {
- const isTotalUniqueVisitors = this.props.query.filters.goal && stat.name === 'Unique visitors'
- const isKnownMetric = Object.keys(METRIC_MAPPING).includes(stat.name)
-
- return isKnownMetric && !isTotalUniqueVisitors
+ return !!stat.graph_metric
}
maybeUpdateMetric(stat) {
if (this.canMetricBeGraphed(stat)) {
- this.props.updateMetric(METRIC_MAPPING[stat.name])
+ storage.setItem(`metric__${this.props.site.domain}`, stat.graph_metric)
+ this.props.onMetricUpdate(stat.graph_metric)
}
}
@@ -104,9 +102,12 @@ export default class TopStats extends React.Component {
)
}
+ getStoredMetric() {
+ return storage.getItem(`metric__${this.props.site.domain}`)
+ }
+
renderStatName(stat) {
- const { metric } = this.props
- const isSelected = metric === METRIC_MAPPING[stat.name]
+ const isSelected = stat.graph_metric === this.getStoredMetric()
const [statDisplayName, statExtraName] = stat.name.split(/(\(.+\))/g)
@@ -140,7 +141,7 @@ export default class TopStats extends React.Component {
- {this.topStatNumberShort(stat.name, stat.value)}
+ {this.topStatNumberShort(stat.name, stat.value)}
{ this.renderPercentageComparison(stat.name, stat.change) }
diff --git a/assets/js/dashboard/stats/graph/visitor-graph.js b/assets/js/dashboard/stats/graph/visitor-graph.js
index 40e0c30c1..f707efcbe 100644
--- a/assets/js/dashboard/stats/graph/visitor-graph.js
+++ b/assets/js/dashboard/stats/graph/visitor-graph.js
@@ -3,7 +3,7 @@ import { Link } from 'react-router-dom'
import * as api from '../../api'
import * as storage from '../../util/storage'
import LazyLoader from '../../components/lazy-loader'
-import { METRIC_MAPPING, LoadingState } from './graph-util'
+import { LoadingState } from './graph-util'
import TopStats from './top-stats';
import { IntervalPicker, getCurrentInterval } from './interval-picker';
import FadeIn from '../../fade-in';
@@ -31,106 +31,90 @@ export default class VisitorGraph extends React.Component {
constructor(props) {
super(props)
this.state = {
- topStatsLoadingState: LoadingState.loading,
- mainGraphLoadingState: LoadingState.loading,
- metric: storage.getItem(`metric__${this.props.site.domain}`) || 'visitors',
+ loading: LoadingState.loading,
+ topStatData: null,
+ graphData: null,
exported: false
}
this.onVisible = this.onVisible.bind(this)
- this.updateMetric = this.updateMetric.bind(this)
- this.fetchTopStatData = this.fetchTopStatData.bind(this)
+ this.fetchTopStatsAndGraphData = this.fetchTopStatsAndGraphData.bind(this)
this.fetchGraphData = this.fetchGraphData.bind(this)
this.onIntervalUpdate = this.onIntervalUpdate.bind(this)
+ this.onMetricUpdate = this.onMetricUpdate.bind(this)
this.boundary = React.createRef()
}
- onIntervalUpdate(interval) {
- this.setState({ mainGraphLoadingState: LoadingState.refreshing, graphData: null }, () => this.fetchGraphData(interval))
- }
+ fetchTopStatsAndGraphData() {
+ const { site, query } = this.props
+
+ this.setState({loading: LoadingState.loading, topStatData: null, graphData: null})
- onVisible() {
- this.setState({ mainGraphLoadingState: LoadingState.loading }, this.fetchGraphData)
- this.fetchTopStatData()
- if (this.props.query.period === 'realtime') {
- document.addEventListener('tick', this.fetchGraphData)
- document.addEventListener('tick', this.fetchTopStatData)
- }
+ fetchTopStats(site, query)
+ .then((res) => {
+ this.setState({ topStatData: res, loading: LoadingState.updatingGraph }, () => {
+ this.storeTopStatsContainerHeight()
+ })
+
+ let metric = this.getStoredMetric()
+ const availableMetrics = res.top_stats.filter(stat => !!stat.graph_metric).map(stat => stat.graph_metric)
+
+ if (!availableMetrics.includes(metric)) {
+ metric = availableMetrics[0]
+ storage.setItem(`metric__${this.props.site.domain}`, metric)
+ }
+
+ const interval = getCurrentInterval(site, query)
+
+ return fetchMainGraph(site, query, metric, interval)
+ })
+ .then((res) => {
+ this.setState({ graphData: res, loading: LoadingState.loaded })
+ })
}
- componentDidUpdate(prevProps, prevState) {
- const { metric } = this.state;
- const { query } = this.props
-
- if (query !== prevProps.query) {
- this.setState({ mainGraphLoadingState: LoadingState.loading, topStatsLoadingState: LoadingState.loading, graphData: null, topStatData: null }, this.fetchGraphData)
- this.fetchTopStatData()
- }
+ fetchGraphData(metric, interval) {
+ const { site, query } = this.props
+
+ this.setState({loading: LoadingState.updatingGraph, graphData: null})
- if (metric !== prevState.metric) {
- this.setState({ mainGraphLoadingState: LoadingState.refreshing }, this.fetchGraphData)
- }
+ fetchMainGraph(site, query, metric, interval)
+ .then((res) => {
+ this.setState({graphData: res, loading: LoadingState.loaded})
+ })
}
- resetMetric() {
- const { topStatData } = this.state
- const { query, site } = this.props
-
- const savedMetric = storage.getItem(`metric__${site.domain}`)
- const selectableMetrics = topStatData && topStatData.top_stats.map(({ name }) => METRIC_MAPPING[name]).filter(name => name)
- const canSelectSavedMetric = selectableMetrics && selectableMetrics.includes(savedMetric)
-
- if (query.filters.goal && !['conversion_rate', 'events'].includes(savedMetric)) {
- this.setState({ metric: 'conversions' })
- } else if (canSelectSavedMetric) {
- this.setState({ metric: savedMetric })
- } else {
- this.setState({ metric: 'visitors' })
- }
+ getStoredMetric() {
+ return storage.getItem(`metric__${this.props.site.domain}`)
}
- componentWillUnmount() {
- document.removeEventListener('tick', this.fetchGraphData)
- document.removeEventListener('tick', this.fetchTopStatData)
+ onIntervalUpdate(newInterval) {
+ this.fetchGraphData(this.getStoredMetric(), newInterval)
}
- storeTopStatsContainerHeight() {
- storage.setItem(`topStatsHeight__${this.props.site.domain}`, document.getElementById('top-stats-container').clientHeight)
+ onMetricUpdate(newMetric) {
+ this.fetchGraphData(newMetric, getCurrentInterval(this.props.site, this.props.query))
}
- updateMetric(clickedMetric) {
- if (this.state.metric == clickedMetric) return
+ onVisible() {
+ this.fetchTopStatsAndGraphData()
- storage.setItem(`metric__${this.props.site.domain}`, clickedMetric)
- this.setState({ metric: clickedMetric, graphData: null })
+ if (this.props.query.period === 'realtime') {
+ document.addEventListener('tick', this.fetchTopStatsAndGraphData)
+ }
}
- fetchGraphData(interval) {
- const { site, query } = this.props
- const graphInterval = interval || getCurrentInterval(site, query)
- const metric = this.state.metric
-
- fetchMainGraph(site, query, metric, graphInterval)
- .then((res) => {
- this.setState({ mainGraphLoadingState: LoadingState.loaded, graphData: res })
- return res
- })
- .catch((err) => {
- console.log(err)
- this.setState({ mainGraphLoadingState: LoadingState.loaded, graphData: false })
- })
+ componentDidUpdate(prevProps, prevState) {
+ if (this.props.query !== prevProps.query) {
+ this.fetchTopStatsAndGraphData()
+ }
}
- fetchTopStatData() {
- const { site, query } = this.props
+ componentWillUnmount() {
+ document.removeEventListener('tick', this.fetchTopStatsAndGraphData)
+ }
- fetchTopStats(site, query)
- .then((res) => {
- this.setState({ topStatsLoadingState: LoadingState.loaded, topStatData: res }, () => {
- this.storeTopStatsContainerHeight()
- this.resetMetric()
- })
- return res
- })
+ storeTopStatsContainerHeight() {
+ storage.setItem(`topStatsHeight__${this.props.site.domain}`, document.getElementById('top-stats-container').clientHeight)
}
pollExportReady() {
@@ -235,51 +219,31 @@ export default class VisitorGraph extends React.Component {
}
}
- renderInner() {
+ render() {
const { query, site } = this.props;
- const { graphData, metric, topStatData, topStatsLoadingState, mainGraphLoadingState } = this.state;
+ const { graphData, topStatData, loading } = this.state;
const isDarkTheme = document.querySelector('html').classList.contains('dark') || false
- const mainGraphRefreshing = (mainGraphLoadingState === LoadingState.refreshing)
- const topStatAndGraphLoaded = !!(topStatData && graphData)
-
- const shouldShow =
- topStatsLoadingState === LoadingState.loaded &&
- LoadingState.isLoadedOrRefreshing(mainGraphLoadingState) &&
- (topStatData && mainGraphRefreshing || topStatAndGraphLoaded)
-
- return (
-
-
-
-
-
- {mainGraphRefreshing && renderLoader()}
-
- {this.downloadLink()}
- {this.samplingNotice()}
- {this.importedNotice()}
-
-
-
-
-
- )
- }
-
- render() {
- const { mainGraphLoadingState, topStatsLoadingState } = this.state
-
- const showLoader =
- [mainGraphLoadingState, topStatsLoadingState].includes(LoadingState.loading) &&
- mainGraphLoadingState !== LoadingState.refreshing
-
return (
- {showLoader && renderLoader()}
- {this.renderInner()}
+ {loading === LoadingState.loading && renderLoader()}
+
+
+
+
+
+ {loading === LoadingState.updatingGraph && renderLoader()}
+
+ {this.downloadLink()}
+ {this.samplingNotice()}
+ {this.importedNotice()}
+
+
+
+
+
)
diff --git a/lib/plausible_web/controllers/api/stats_controller.ex b/lib/plausible_web/controllers/api/stats_controller.ex
index fbfb7866e..080142c0e 100644
--- a/lib/plausible_web/controllers/api/stats_controller.ex
+++ b/lib/plausible_web/controllers/api/stats_controller.ex
@@ -129,6 +129,7 @@ defmodule PlausibleWeb.Api.StatsController do
full_intervals = build_full_intervals(query, labels)
json(conn, %{
+ metric: metric,
plot: plot_timeseries(timeseries_result, metric),
labels: labels,
comparison_plot: comparison_result && plot_timeseries(comparison_result, metric),