Skip to content

📈 Open source, privacy-first web analytics — a cookie-less Google Analytics alternative with built-in error tracking and performance monitoring. Self-host it or use our cloud. Get started for free!

add new line for graphics

Maksim Mrugd071341bebdc
diff --git a/src/pages/Project/View/ViewProject.helpers.jsx b/src/pages/Project/View/ViewProject.helpers.jsx
index a3a37cc41..d3181d79b 100644
--- a/src/pages/Project/View/ViewProject.helpers.jsx
+++ b/src/pages/Project/View/ViewProject.helpers.jsx
@@ -5,7 +5,7 @@ import {
} from '@heroicons/react/24/outline'
import * as d3 from 'd3'
import dayjs from 'dayjs'
-import { area } from 'billboard.js'
+import { area, spline } from 'billboard.js'
import _forEach from 'lodash/forEach'
import _map from 'lodash/map'
import _split from 'lodash/split'
@@ -82,17 +82,23 @@ const CHART_METRICS_MAPPING = {
unique: 'unique',
views: 'views',
bounce: 'bounce',
+ viewsPerUnique: 'viewsPerUnique',
}
// function to filter the data for the chart
const getColumns = (chart, activeChartMetrics) => {
- const { views, bounce } = activeChartMetrics
+ const {
+ views, bounce, viewsPerUnique, unique,
+ } = activeChartMetrics
const columns = [
['x', ..._map(chart.x, el => dayjs(el).toDate())],
- ['unique', ...chart.uniques],
]
+ if (unique) {
+ columns.push(['unique', ...chart.uniques])
+ }
+
if (views) {
columns.push(['total', ...chart.visits])
}
@@ -101,7 +107,19 @@ const getColumns = (chart, activeChartMetrics) => {
const bounceArray = _map(chart.uniques, (el, i) => {
return _round((el * 100) / chart.visits[i], 1)
})
- columns.push(['bounce', ...bounceArray])
+ columns.push(
+ ['bounce', ...bounceArray],
+ )
+ }
+
+ if (viewsPerUnique) {
+ const viewsPerUniqueArray = _map(chart.visits, (el, i) => {
+ if (chart.uniques[i] === 0 || chart.uniques[i] === undefined) {
+ return 0
+ }
+ return _round(el / chart.uniques[i], 1)
+ })
+ columns.push(['viewsPerUnique', ...viewsPerUniqueArray])
}
return columns
@@ -149,6 +167,14 @@ const getSettings = (chart, timeBucket, activeChartMetrics, applyRegions) => {
},
},
],
+ viewsPerUnique: [
+ {
+ start: regionStart,
+ style: {
+ dasharray: '6 2',
+ },
+ },
+ ],
}
}
@@ -156,13 +182,22 @@ const getSettings = (chart, timeBucket, activeChartMetrics, applyRegions) => {
data: {
x: 'x',
columns: getColumns(chart, activeChartMetrics),
- type: area(),
+ types: {
+ unique: area(),
+ total: area(),
+ bounce: spline(),
+ viewsPerUnique: spline(),
+ },
colors: {
unique: '#2563EB',
total: '#D97706',
bounce: '#2AC4B3',
+ viewsPerUnique: '#F87171',
},
regions,
+ axes: {
+ bounce: 'y2',
+ },
},
axis: {
x: {
@@ -171,6 +206,15 @@ const getSettings = (chart, timeBucket, activeChartMetrics, applyRegions) => {
},
type: 'timeseries',
},
+ y2: {
+ show: activeChartMetrics.bounce,
+ tick: {
+ format: (d) => `${d}%`,
+ },
+ min: 10,
+ max: 90,
+ default: [0, 100],
+ },
},
tooltip: {
format: {
diff --git a/src/pages/Project/View/ViewProject.jsx b/src/pages/Project/View/ViewProject.jsx
index 766c4b50b..76a8f9900 100644
--- a/src/pages/Project/View/ViewProject.jsx
+++ b/src/pages/Project/View/ViewProject.jsx
@@ -92,6 +92,7 @@ const ViewProject = ({
[CHART_METRICS_MAPPING.unique]: true,
[CHART_METRICS_MAPPING.views]: false,
[CHART_METRICS_MAPPING.bounce]: false,
+ [CHART_METRICS_MAPPING.viewsPerUnique]: false,
})
const checkIfAllMetricsAreDisabled = useMemo(() => !_some(activeChartMetrics, (value) => value), [activeChartMetrics])
const [filters, setFilters] = useState([])
@@ -125,6 +126,11 @@ const ViewProject = ({
label: t('dashboard.bounceRate'),
active: activeChartMetrics[CHART_METRICS_MAPPING.bounce],
},
+ {
+ id: CHART_METRICS_MAPPING.viewsPerUnique,
+ label: t('dashboard.viewsPerUnique'),
+ active: activeChartMetrics[CHART_METRICS_MAPPING.viewsPerUnique],
+ },
]
}, [t, activeChartMetrics])
@@ -330,23 +336,49 @@ const ViewProject = ({
useEffect(() => {
if (!isLoading && !_isEmpty(chartData) && !_isEmpty(mainChart)) {
- mainChart.data.names({ unique: t('project.unique'), total: t('project.total'), bounce: `${t('dashboard.bounceRate')} (%)` })
+ mainChart.data.names({
+ unique: t('project.unique'), total: t('project.total'), bounce: `${t('dashboard.bounceRate')} (%)`, viewsPerUnique: `${t('dashboard.viewsPerUnique')} (%)`,
+ })
- if (activeChartMetrics.views || activeChartMetrics.bounce || activeChartMetrics.unique) {
+ if (activeChartMetrics.views || activeChartMetrics.unique || activeChartMetrics.viewsPerUnique) {
mainChart.load({
columns: getColumns(chartData, activeChartMetrics),
})
}
- if (!activeChartMetrics.views) {
- mainChart.unload({
- ids: 'total',
+ if (activeChartMetrics.bounce) {
+ const applyRegions = !_includes(noRegionPeriods, activePeriod.period)
+ const bbSettings = getSettings(chartData, timeBucket, activeChartMetrics, applyRegions)
+
+ if (!_isEmpty(mainChart)) {
+ mainChart.destroy()
+ }
+
+ setMainChart(() => {
+ const generete = bb.generate(bbSettings)
+ generete.data.names({ unique: `${t('project.unique')} ` })
+ return generete
})
}
if (!activeChartMetrics.bounce) {
+ const applyRegions = !_includes(noRegionPeriods, activePeriod.period)
+ const bbSettings = getSettings(chartData, timeBucket, activeChartMetrics, applyRegions)
+
+ if (!_isEmpty(mainChart)) {
+ mainChart.destroy()
+ }
+
+ setMainChart(() => {
+ const generete = bb.generate(bbSettings)
+ generete.data.names({ unique: `${t('project.unique')} ` })
+ return generete
+ })
+ }
+
+ if (!activeChartMetrics.views) {
mainChart.unload({
- ids: 'bounce',
+ ids: 'total',
})
}
@@ -355,8 +387,14 @@ const ViewProject = ({
ids: 'unique',
})
}
+
+ if (!activeChartMetrics.viewsPerUnique) {
+ mainChart.unload({
+ ids: 'viewsPerUnique',
+ })
+ }
}
- }, [isLoading, activeChartMetrics, chartData, mainChart, t])
+ }, [isLoading, activeChartMetrics, chartData])
// Initialising Swetrix SDK instance
useEffect(() => {