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!

migrated to the new data format

Blue Mouse147a4388f9bc
diff --git a/app/pages/Project/View/Panels.tsx b/app/pages/Project/View/Panels.tsx
index 3450666ed..771828c49 100644
--- a/app/pages/Project/View/Panels.tsx
+++ b/app/pages/Project/View/Panels.tsx
@@ -17,8 +17,7 @@ import _values from 'lodash/values'
import _map from 'lodash/map'
import _isEmpty from 'lodash/isEmpty'
import _isString from 'lodash/isString'
-import _uniqBy from 'lodash/uniqBy'
-import _isFunction from 'lodash/isFunction'
+import _orderBy from 'lodash/orderBy'
import _reduce from 'lodash/reduce'
import _round from 'lodash/round'
import _find from 'lodash/find'
@@ -42,7 +41,7 @@ import Button from 'ui/Button'
import Chart from 'ui/Chart'
import { PROJECT_TABS } from 'redux/constants'
-
+import { IEntry } from 'redux/models/IEntry'
import LiveVisitorsDropdown from './components/LiveVisitorsDropdown'
import InteractiveMap from './components/InteractiveMap'
import UserFlow from './components/UserFlow'
@@ -824,7 +823,7 @@ CustomEvents.defaultProps = {
interface IPanel {
name: string
- data: any
+ data: IEntry[]
rowMapper: any
valueMapper: any
capitalize: boolean
@@ -852,25 +851,25 @@ const Panel = ({
}: IPanel): JSX.Element => {
const [page, setPage] = useState(0)
const currentIndex = page * ENTRIES_PER_PANEL
- const keys = useMemo(() => _keys(data).sort((a, b) => data[b] - data[a]), [data])
- const keysToDisplay = useMemo(() => _slice(keys, currentIndex, currentIndex + ENTRIES_PER_PANEL), [keys, currentIndex])
- const total = useMemo(() => _reduce(keys, (prev, curr) => prev + data[curr], 0), [keys]) // eslint-disable-line
- const totalPages = useMemo(() => _ceil(_size(keys) / ENTRIES_PER_PANEL), [keys])
+ const total = useMemo(() => _reduce(data, (prev, curr) => prev + curr.count, 0), [data])
+ const totalPages = _ceil(total / ENTRIES_PER_PANEL)
+ const entries = useMemo(() => _orderBy(data, 'count', 'desc'), [data])
+ const entriesToDisplay = _slice(entries, currentIndex, currentIndex + ENTRIES_PER_PANEL)
const [activeFragment, setActiveFragment] = useState(0)
const [modal, setModal] = useState(false)
const [isReversedUserFlow, setIsReversedUserFlow] = useState<boolean>(false)
const canGoPrev = () => page > 0
- const canGoNext = () => page < _floor((_size(keys) - 1) / ENTRIES_PER_PANEL)
+ const canGoNext = () => page < _floor((_size(entries) - 1) / ENTRIES_PER_PANEL)
const _onFilter = hideFilters ? () => { } : onFilter
useEffect(() => {
- const sizeKeys = _size(keys)
+ const sizeKeys = _size(entries)
if (currentIndex > sizeKeys) {
setPage(_floor(sizeKeys / ENTRIES_PER_PANEL))
}
- }, [currentIndex, keys])
+ }, [currentIndex, entries])
useEffect(() => {
setPage(0)
@@ -930,6 +929,7 @@ const Panel = ({
)
}
+ // User flow tab for the Page panel
if (id === 'pg' && activeFragment === 1) {
return (
<PanelContainer
@@ -990,21 +990,22 @@ const Panel = ({
)
}
- // Showing chart of stats a data (start if)
+ // Showing chart of stats a data
if ((id === 'os' || id === 'br' || id === 'dv') && activeFragment === 1 && !_isEmpty(data)) {
const tQuantity = t('project.quantity')
const tRatio = t('project.ratio')
- const mappedData = _map(data, valueMapper)
+ const columns = _map(data, (el) => [el.name, valueMapper(el.count)])
+ const values = _map(data, (el) => valueMapper(el.count))
const options = {
data: {
- columns: _map(data, (e, index) => [index, e]),
+ columns,
type: pie(),
},
tooltip: {
contents: {
text: {
- QUANTITY: _values(mappedData),
+ QUANTITY: values,
},
template: `
<ul class='bg-gray-100 dark:text-gray-50 dark:bg-slate-700 rounded-md shadow-md px-3 py-1'>
@@ -1051,7 +1052,6 @@ const Panel = ({
</PanelContainer>
)
}
- // Showing chart of stats a data (end if)
// Showing custom tabs (Extensions Marketplace)
// todo: check activeFragment for being equal to customTabs -> extensionID + panelID
@@ -1084,18 +1084,21 @@ const Panel = ({
<p className='mt-1 text-base text-gray-700 dark:text-gray-300'>
{t('project.noParamData')}
</p>
- ) : _map(keysToDisplay, key => {
- const perc = _round((data[key] / total) * 100, 2)
- const rowData = _isFunction(rowMapper) ? rowMapper(key) : key
- const valueData = _isFunction(valueMapper) ? valueMapper(data[key]) : data[key]
+ ) : _map(entriesToDisplay, entry => {
+ const {
+ count, name: entryName,
+ } = entry
+ const perc = _round((count / total) * 100, 2)
+ const rowData = rowMapper(entryName)
+ const valueData = valueMapper(count)
return (
- <Fragment key={key}>
+ <Fragment key={entryName}>
<div
className={cx('flex justify-between mt-[0.32rem] first:mt-0 dark:text-gray-50 rounded', {
'group hover:bg-gray-100 hover:dark:bg-slate-700 cursor-pointer': !hideFilters,
})}
- onClick={() => _onFilter(id, key)}
+ onClick={() => _onFilter(id, entryName)}
>
{linkContent ? (
<a
@@ -1133,12 +1136,12 @@ const Panel = ({
)
})}
{/* for pagination in tabs */}
- {_size(keys) > ENTRIES_PER_PANEL && (
+ {_size(entries) > ENTRIES_PER_PANEL && (
<div className='absolute bottom-0 w-card-toggle-sm sm:!w-card-toggle'>
<div className='flex justify-between select-none mb-2'>
<div>
<span className='text-gray-500 dark:text-gray-200 font-light lowercase text-xs'>
- {_size(keys)}
+ {_size(entries)}
{' '}
{t('project.results')}
</span>
@@ -1203,8 +1206,8 @@ Panel.propTypes = {
Panel.defaultProps = {
id: null,
- rowMapper: null,
- valueMapper: null,
+ rowMapper: (value: any): any => value,
+ valueMapper: (value: number): number => value,
capitalize: false,
linkContent: false,
onFilter: () => { },
diff --git a/app/pages/Project/View/components/InteractiveMap.tsx b/app/pages/Project/View/components/InteractiveMap.tsx
index f865bea10..1381803fe 100644
--- a/app/pages/Project/View/components/InteractiveMap.tsx
+++ b/app/pages/Project/View/components/InteractiveMap.tsx
@@ -1,6 +1,7 @@
-import React, { memo, useState } from 'react'
+import React, { memo, useState, useMemo } from 'react'
import cx from 'clsx'
import _map from 'lodash/map'
+import _reduce from 'lodash/reduce'
import { useTranslation } from 'react-i18next'
import PropTypes from 'prop-types'
import countries from 'utils/isoCountries'
@@ -8,30 +9,36 @@ import { PROJECT_TABS } from 'redux/constants'
import { StateType } from 'redux/store'
import { getTimeFromSeconds, getStringFromTime, nFormatter } from 'utils/generic'
+import { IEntry } from 'redux/models/IEntry'
import countriesList from 'utils/countries'
import { useSelector } from 'react-redux'
-const InteractiveMap = ({ data, onClickCountry, total }: {
- data: Record<string, number>
+interface IInteractiveMap {
+ data: IEntry[],
onClickCountry: (country: string) => void
total: number
-}) => {
+}
+
+interface ICursorPosition {
+ pageX: number
+ pageY: number
+}
+
+interface IDataHover {
+ countries: string
+ data: number
+}
+
+interface ICountryMap {
+ [key: string]: number
+}
+
+const InteractiveMap = ({ data, onClickCountry, total }: IInteractiveMap) => {
const { t, i18n: { language } } = useTranslation('common')
const [hoverShow, setHoverShow] = useState<boolean>(false)
- const [dataHover, setDataHover] = useState<{
- countries: string
- data: number
- }>({} as {
- countries: string
- data: number
- })
- const [cursorPosition, setCursorPosition] = useState<{
- pageX: number
- pageY: number
- }>({} as {
- pageX: number
- pageY: number
- })
+ const [dataHover, setDataHover] = useState<IDataHover>({} as IDataHover)
+ const [cursorPosition, setCursorPosition] = useState<ICursorPosition>({} as ICursorPosition)
+ const countryMap: ICountryMap = useMemo(() => _reduce(data, (prev, curr) => ({ ...prev, [curr.name]: curr.count }), {}), [data])
const projectTab = useSelector((state: StateType) => state.ui.projects.projectTab)
const isTrafficTab = projectTab === PROJECT_TABS.traffic
@@ -47,14 +54,14 @@ const InteractiveMap = ({ data, onClickCountry, total }: {
<div className='relative'>
<svg id='map' viewBox='0 0 1050 650' className='w-full h-full' onMouseMove={onMouseMove}>
<g>
- {_map(countriesList, (item, index) => {
- const ccData = data[index] || 0
+ {_map(countriesList, (key, value) => {
+ const ccData = countryMap[value] || 0
const perc = ((ccData / total) * 100) || 0
return (
<path
- key={index}
- id={index}
+ key={value}
+ id={value}
className={isTrafficTab
? cx({
'hover:opacity-90': perc > 0,
@@ -76,13 +83,13 @@ const InteractiveMap = ({ data, onClickCountry, total }: {
'fill-[#f74b4b]': ccData >= 10,
'cursor-pointer': Boolean(ccData),
})}
- d={item.d}
- onClick={() => perc !== 0 && onClickCountry(index)}
+ d={key.d}
+ onClick={() => perc !== 0 && onClickCountry(value)}
onMouseEnter={() => {
if (ccData) {
setHoverShow(true)
setDataHover({
- countries: countries.getName(index, language),
+ countries: countries.getName(value, language),
data: ccData,
})
}
@@ -126,13 +133,16 @@ const InteractiveMap = ({ data, onClickCountry, total }: {
InteractiveMap.propTypes = {
onClickCountry: PropTypes.func,
- data: PropTypes.objectOf(PropTypes.number),
+ data: PropTypes.arrayOf(PropTypes.shape({
+ name: PropTypes.string,
+ count: PropTypes.number,
+ })),
total: PropTypes.number,
}
InteractiveMap.defaultProps = {
- data: {},
onClickCountry: () => { },
+ data: [],
total: 0,
}
diff --git a/app/redux/models/IEntry.ts b/app/redux/models/IEntry.ts
new file mode 100644
index 000000000..be1f92ef6
--- /dev/null
+++ b/app/redux/models/IEntry.ts
@@ -0,0 +1,5 @@
+export interface IEntry {
+ name: string
+ count: number
+ cc?: string
+}