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!

feature: custom events to chart

kruzhambus90f1058b5746
diff --git a/src/api/index.ts b/src/api/index.ts
index 7f06d9ba9..5af44b425 100644
--- a/src/api/index.ts
+++ b/src/api/index.ts
@@ -748,3 +748,25 @@ export const confirmSubscriberInvite = (id: string, token: string) =>
? error.response.data
: error.response.data.message
})
+
+export const getProjectDataCustomEvents = (
+ pid: string,
+ tb: string = 'hour',
+ period: string = '3d',
+ filters: string[] = [],
+ from: string = '',
+ to: string = '',
+ timezone: string = '',
+ customEvents: string[] = [],
+) =>
+ api
+ .get(
+ `log/custom-events?pid=${pid}&timeBucket=${tb}&period=${period}&filters=${JSON.stringify(filters)}&from=${from}&to=${to}&timezone=${timezone}`,
+ )
+ .then((response) => response.data)
+ .catch((error) => {
+ debug('%s', error)
+ throw _isEmpty(error.response.data?.message)
+ ? error.response.data
+ : error.response.data.message
+ })
diff --git a/src/pages/Project/View/ViewProject.helpers.tsx b/src/pages/Project/View/ViewProject.helpers.tsx
index 5a1b5e4e5..9cd2e923a 100644
--- a/src/pages/Project/View/ViewProject.helpers.tsx
+++ b/src/pages/Project/View/ViewProject.helpers.tsx
@@ -174,6 +174,7 @@ const CHART_METRICS_MAPPING = {
viewsPerUnique: 'viewsPerUnique',
trendlines: 'trendlines',
sessionDuration: 'sessionDuration',
+ customEvents: 'customEvents',
}
const CHART_METRICS_MAPPING_PERF = {
diff --git a/src/pages/Project/View/ViewProject.tsx b/src/pages/Project/View/ViewProject.tsx
index a713a05f0..ef477a787 100644
--- a/src/pages/Project/View/ViewProject.tsx
+++ b/src/pages/Project/View/ViewProject.tsx
@@ -10,7 +10,7 @@ import domToImage from 'dom-to-image'
import { saveAs } from 'file-saver'
import bb from 'billboard.js'
import {
- ArrowDownTrayIcon, Cog8ToothIcon, ArrowPathIcon, CurrencyDollarIcon, ChartBarIcon, BoltIcon, BellIcon, PresentationChartBarIcon, PresentationChartLineIcon,
+ ArrowDownTrayIcon, Cog8ToothIcon, ArrowPathIcon, CurrencyDollarIcon, ChartBarIcon, BoltIcon, BellIcon, PresentationChartBarIcon, PresentationChartLineIcon, NoSymbolIcon,
} from '@heroicons/react/24/outline'
import cx from 'clsx'
import dayjs from 'dayjs'
@@ -24,6 +24,7 @@ import _replace from 'lodash/replace'
import _values from 'lodash/values'
import _find from 'lodash/find'
import _filter from 'lodash/filter'
+import _findIndex from 'lodash/findIndex'
import _startsWith from 'lodash/startsWith'
import _debounce from 'lodash/debounce'
import _some from 'lodash/some'
@@ -126,6 +127,7 @@ const ViewProject = ({
const [customExportTypes, setCustomExportTypes] = useState<any[]>([])
const [customPanelTabs, setCustomPanelTabs] = useState<any[]>([])
const [sdkInstance, setSdkInstance] = useState<any>(null)
+ const [activeChartMetricsCustomEvents, setActiveChartMetricsCustomEvents] = useState<any[]>([])
const dashboardRef = useRef<HTMLDivElement>(null)
const { id }: {
id: string
@@ -263,6 +265,11 @@ const ViewProject = ({
label: t('dashboard.trendlines'),
active: activeChartMetrics[CHART_METRICS_MAPPING.trendlines],
},
+ {
+ id: CHART_METRICS_MAPPING.customEvents,
+ label: t('dashboard.customEvents'),
+ active: activeChartMetrics[CHART_METRICS_MAPPING.customEvents],
+ },
]
}, [t, activeChartMetrics])
@@ -296,6 +303,17 @@ const ViewProject = ({
]
}, [t, activeChartMetricsPerf])
+ const chartMetricsCustomEvents = useMemo(() => {
+ if (!_isEmpty(panelsData.customs)) {
+ return _map(_keys(panelsData.customs), key => ({
+ id: key,
+ label: key,
+ active: _includes(activeChartMetricsCustomEvents, key),
+ }))
+ }
+ return []
+ }, [panelsData, activeChartMetricsCustomEvents])
+
const dataNames = useMemo(() => {
return {
unique: t('project.unique'),
@@ -1503,9 +1521,51 @@ const ViewProject = ({
const conflicted = isConflicted(conflicts)
+ if (pairID === CHART_METRICS_MAPPING.customEvents) {
+ if (_isEmpty(panelsData.customs)) {
+ return (
+ <span className='px-4 py-2 flex items-center cursor-not-allowed'>
+ <NoSymbolIcon className='w-5 h-5 mr-1' />
+ {label}
+ </span>
+ )
+ }
+
+ return (
+ <Dropdown
+ className=''
+ items={chartMetricsCustomEvents}
+ title={label}
+ labelExtractor={(event) => (
+ <Checkbox
+ className={cx({ hidden: isPanelsDataEmpty || analyticsLoading })}
+ label={event.label}
+ id={event.id}
+ onChange={() => {
+ setActiveChartMetricsCustomEvents((prev) => {
+ const newActiveChartMetricsCustomEvents = [...prev]
+ const index = _findIndex(prev, (item) => item === event.id)
+ if (index === -1) {
+ newActiveChartMetricsCustomEvents.push(event.id)
+ } else {
+ newActiveChartMetricsCustomEvents.splice(index, 1)
+ }
+ return newActiveChartMetricsCustomEvents
+ })
+ }}
+ checked={event.active}
+ />
+ )}
+ buttonClassName='group-hover:bg-gray-50 px-4 py-2 dark:group-hover:bg-gray-600 inline-flex w-full rounded-md shadow-sm bg-white text-sm font-medium text-gray-700 dark:text-gray-50 dark:border-gray-800 dark:bg-gray-700'
+ keyExtractor={(event) => event.id}
+ onSelect={() => {}}
+ />
+ )
+ }
+
return (
<Checkbox
- className={cx({ hidden: isPanelsDataEmpty || analyticsLoading })}
+ className={cx('px-4 py-2', { hidden: isPanelsDataEmpty || analyticsLoading })}
label={label}
disabled={conflicted}
id={pairID}
@@ -1513,12 +1573,18 @@ const ViewProject = ({
/>
)
}}
+ selectItemClassName='group text-gray-700 dark:text-gray-50 dark:border-gray-800 dark:bg-gray-700 block text-sm cursor-pointer hover:bg-gray-200 dark:hover:bg-gray-600'
keyExtractor={(pair) => pair.id}
onSelect={({ id: pairID, conflicts }) => {
if (isConflicted(conflicts)) {
generateAlert(t('project.conflictMetric'), 'error')
return
}
+
+ if (pairID === CHART_METRICS_MAPPING.customEvents) {
+ return
+ }
+
switchActiveChartMetric(pairID)
}}
/>
diff --git a/src/ui/Dropdown.tsx b/src/ui/Dropdown.tsx
index a8e9423e4..3cc2f9685 100644
--- a/src/ui/Dropdown.tsx
+++ b/src/ui/Dropdown.tsx
@@ -7,7 +7,7 @@ import _isEmpty from 'lodash/isEmpty'
import PropTypes from 'prop-types'
const Dropdown = ({
- title, desc, className, items, labelExtractor, keyExtractor, onSelect, aside, buttonClassName, selectItemClassName,
+ title, desc, className, items, labelExtractor, keyExtractor, onSelect, aside, buttonClassName, selectItemClassName, menuItemsClassName,
}: {
title: string | number | React.ReactNode,
desc?: string | number | React.ReactNode,
@@ -15,10 +15,11 @@ const Dropdown = ({
items: any[],
labelExtractor: (item: any) => string | number | React.ReactNode,
keyExtractor: (item: any) => string | number | React.ReactNode,
- onSelect: (item: any) => void,
+ onSelect: (item: any) => void | null,
aside?: boolean,
buttonClassName?: string,
selectItemClassName?: string,
+ menuItemsClassName?: string,
}): JSX.Element => (
<Menu as='div' className={cx('relative inline-block text-left', className)}>
{({ open }) => (
@@ -48,7 +49,7 @@ const Dropdown = ({
leaveFrom='transform opacity-100 scale-100'
leaveTo='transform opacity-0 scale-95'
>
- <Menu.Items static className='z-50 origin-top-right absolute right-0 mt-2 w-40 min-w-max rounded-md shadow-lg bg-white dark:bg-gray-700 ring-1 ring-black ring-opacity-5 focus:outline-none'>
+ <Menu.Items static className={cx('z-50 origin-top-right absolute right-0 mt-2 w-40 min-w-max rounded-md shadow-lg bg-white dark:bg-gray-700 ring-1 ring-black ring-opacity-5 focus:outline-none', menuItemsClassName)}>
<div className='py-1'>
{_map(items, item => (
<Menu.Item key={keyExtractor ? keyExtractor(item) : item}>
@@ -87,6 +88,7 @@ Dropdown.propTypes = {
keyExtractor: PropTypes.func,
aside: PropTypes.bool,
desc: PropTypes.string,
+ menuItemsClassName: PropTypes.string,
}
Dropdown.defaultProps = {
@@ -98,6 +100,7 @@ Dropdown.defaultProps = {
aside: false,
desc: '',
items: [],
+ menuItemsClassName: '',
}
export default memo(Dropdown)