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!

added a new secret feature ;)

Blue Mousee01d4b880478
diff --git a/public/locales/en.json b/public/locales/en.json
index 9e98ea9e2..5876d219c 100644
--- a/public/locales/en.json
+++ b/public/locales/en.json
@@ -465,7 +465,8 @@
"month": "Month",
"resetFilters": "Reset filters",
"exportImgError": "Exporting data as image failed",
- "codeExample": "Or insert the following code into your website to track your events:",
+ "codeExample": "Or insert the following code into your website to track your events:",
+ "metricVis": "Metrics visualisation",
"settings": {
"verify": "Please, verify your email address first",
"updated": "The project's settings were updated",
diff --git a/src/pages/Project/View/ViewProject.helpers.jsx b/src/pages/Project/View/ViewProject.helpers.jsx
index ea33301c0..e7924972f 100644
--- a/src/pages/Project/View/ViewProject.helpers.jsx
+++ b/src/pages/Project/View/ViewProject.helpers.jsx
@@ -78,27 +78,39 @@ const onCSVExportClick = (data, pid, tnMapping, language) => {
})
}
+const CHART_METRICS_MAPPING = {
+ views: 'views',
+ bounce: 'bounce',
+}
+
// function to filter the data for the chart
-const getColumns = (chart, showTotal) => {
- if (showTotal) {
- return [
- ['x', ..._map(chart.x, el => dayjs(el).toDate())],
- ['unique', ...chart.uniques],
- ['total', ...chart.visits],
- ]
- }
+const getColumns = (chart, activeChartMetrics) => {
+ const { views, bounce } = activeChartMetrics
- return [
+ const columns = [
['x', ..._map(chart.x, el => dayjs(el).toDate())],
['unique', ...chart.uniques],
]
+
+ if (views) {
+ columns.push(['total', ...chart.visits])
+ }
+
+ if (bounce) {
+ const bounceArray = _map(chart.uniques, (el, i) => {
+ return _round((el * 100) / chart.visits[i], 1)
+ })
+ columns.push(['bounce', ...bounceArray])
+ }
+
+ return columns
}
// setting the default values for the time period dropdown
const noRegionPeriods = ['custom', 'yesterday']
// function to get the settings and data for the chart(main diagram)
-const getSettings = (chart, timeBucket, showTotal, applyRegions) => {
+const getSettings = (chart, timeBucket, activeChartMetrics, applyRegions) => {
const xAxisSize = _size(chart.x)
let regions
@@ -128,17 +140,26 @@ const getSettings = (chart, timeBucket, showTotal, applyRegions) => {
},
},
],
+ bounce: [
+ {
+ start: regionStart,
+ style: {
+ dasharray: '6 2',
+ },
+ },
+ ],
}
}
return {
data: {
x: 'x',
- columns: getColumns(chart, showTotal),
+ columns: getColumns(chart, activeChartMetrics),
type: area(),
colors: {
unique: '#2563EB',
total: '#D97706',
+ bounce: '#2AC4B3',
},
regions,
},
@@ -233,5 +254,5 @@ const getFormatDate = (date) => {
}
export {
- iconClassName, getFormatDate, panelIconMapping, typeNameMapping, validFilters, validPeriods, validTimeBacket, paidPeriods, noRegionPeriods, getSettings, getExportFilename, getColumns, onCSVExportClick,
+ iconClassName, getFormatDate, panelIconMapping, typeNameMapping, validFilters, validPeriods, validTimeBacket, paidPeriods, noRegionPeriods, getSettings, getExportFilename, getColumns, onCSVExportClick, CHART_METRICS_MAPPING,
}
diff --git a/src/pages/Project/View/ViewProject.jsx b/src/pages/Project/View/ViewProject.jsx
index a4e5d5134..4d536337b 100644
--- a/src/pages/Project/View/ViewProject.jsx
+++ b/src/pages/Project/View/ViewProject.jsx
@@ -1,6 +1,6 @@
/* eslint-disable react/forbid-prop-types, react/no-unstable-nested-components, react/display-name */
import React, {
- useState, useEffect, useMemo, memo, useRef, Fragment,
+ useState, useEffect, useMemo, memo, useRef, Fragment, useCallback,
} from 'react'
import { useHistory, useParams, Link } from 'react-router-dom'
import domToImage from 'dom-to-image'
@@ -22,6 +22,7 @@ import _find from 'lodash/find'
import _filter from 'lodash/filter'
import _startsWith from 'lodash/startsWith'
import _isEqual from 'lodash/isEqual'
+import _debounce from 'lodash/debounce'
import PropTypes from 'prop-types'
import * as SwetrixSDK from '@swetrix/sdk'
@@ -47,7 +48,7 @@ import {
} from './Panels'
import {
onCSVExportClick, getFormatDate, panelIconMapping, typeNameMapping, validFilters, validPeriods,
- validTimeBacket, paidPeriods, noRegionPeriods, getSettings, getColumns,
+ validTimeBacket, paidPeriods, noRegionPeriods, getSettings, getColumns, CHART_METRICS_MAPPING,
} from './ViewProject.helpers'
import CCRow from './components/CCRow'
import RefRow from './components/RefRow'
@@ -83,10 +84,13 @@ const ViewProject = ({
const [period, setPeriod] = useState(projectViewPrefs[id]?.period || periodPairs[3].period)
const [timeBucket, setTimebucket] = useState(projectViewPrefs[id]?.timeBucket || periodPairs[3].tbs[1])
const activePeriod = useMemo(() => _find(periodPairs, p => p.period === period), [period, periodPairs])
- const [showTotal, setShowTotal] = useState(false)
const [chartData, setChartData] = useState({})
const [mainChart, setMainChart] = useState(null)
const [dataLoading, setDataLoading] = useState(false)
+ const [activeChartMetrics, setActiveChartMetrics] = useState({
+ [CHART_METRICS_MAPPING.views]: false,
+ [CHART_METRICS_MAPPING.bounce]: false,
+ })
const [filters, setFilters] = useState([])
// That is needed when using 'Export as image' feature
// Because headless browser cannot do a request to the DDG API due to absense of The Same Origin Policy header
@@ -101,6 +105,26 @@ const ViewProject = ({
const sharedRoles = useMemo(() => _find(user.sharedProjects, p => p.project.id === id)?.role || {}, [user, id])
+ const chartMetrics = useMemo(() => {
+ return [
+ {
+ id: CHART_METRICS_MAPPING.views,
+ label: t('project.showAll'),
+ active: activeChartMetrics[CHART_METRICS_MAPPING.views],
+ },
+ {
+ id: CHART_METRICS_MAPPING.bounce,
+ label: t('dashboard.bounceRate'),
+ active: activeChartMetrics[CHART_METRICS_MAPPING.bounce],
+ },
+ ]
+ }, [t, activeChartMetrics])
+
+ // eslint-disable-next-line react-hooks/exhaustive-deps
+ const switchActiveChartMetric = useCallback(_debounce((pairID) => {
+ setActiveChartMetrics(prev => ({ ...prev, [pairID]: !prev[pairID] }))
+ }, 0), [])
+
const onErrorLoading = () => {
showError(t('project.noExist'))
history.push(routes.dashboard)
@@ -176,7 +200,7 @@ const ViewProject = ({
setIsPanelsDataEmpty(true)
} else {
const applyRegions = !_includes(noRegionPeriods, activePeriod.period)
- const bbSettings = getSettings(chart, timeBucket, showTotal, applyRegions)
+ const bbSettings = getSettings(chart, timeBucket, activeChartMetrics, applyRegions)
setChartData(chart)
setPanelsData({
@@ -298,18 +322,27 @@ const ViewProject = ({
useEffect(() => {
if (!isLoading && !_isEmpty(chartData) && !_isEmpty(mainChart)) {
- if (showTotal) {
+ mainChart.data.names({ unique: t('project.unique'), total: t('project.total'), bounce: `${t('dashboard.bounceRate')} (%)` })
+
+ if (activeChartMetrics.views || activeChartMetrics.bounce) {
mainChart.load({
- columns: getColumns(chartData, true),
+ columns: getColumns(chartData, activeChartMetrics),
})
- mainChart.data.names({ unique: t('project.unique'), total: t('project.total') })
- } else {
+ }
+
+ if (!activeChartMetrics.views) {
mainChart.unload({
ids: 'total',
})
}
+
+ if (!activeChartMetrics.bounce) {
+ mainChart.unload({
+ ids: 'bounce',
+ })
+ }
}
- }, [isLoading, showTotal, chartData, mainChart, t])
+ }, [isLoading, activeChartMetrics, chartData, mainChart, t])
// Initialising Swetrix SDK instance
useEffect(() => {
@@ -788,12 +821,25 @@ const ViewProject = ({
</div>
</div>
<div className='flex flex-row flex-wrap items-center justify-center md:justify-end h-10 mt-16 md:mt-5 mb-4'>
- <Checkbox
- className={cx({ hidden: isPanelsDataEmpty || analyticsLoading })}
- label={t('project.showAll')}
- id='views'
- checked={showTotal}
- onChange={(e) => setShowTotal(e.target.checked)}
+ <Dropdown
+ items={chartMetrics}
+ title={t('project.metricVis')}
+ labelExtractor={(pair) => {
+ const { label, id: pairID, active } = pair
+
+ return (
+ <Checkbox
+ className={cx({ hidden: isPanelsDataEmpty || analyticsLoading })}
+ label={label}
+ id={pairID}
+ checked={active}
+ />
+ )
+ }}
+ keyExtractor={(pair) => pair.id}
+ onSelect={({ id: pairID }) => {
+ switchActiveChartMetric(pairID)
+ }}
/>
<Dropdown
items={[...exportTypes, ...customExportTypes]}