2022-12-29 00:43:22 +01:00
|
|
|
import { useState } from 'react';
|
|
|
|
import { Loading } from 'react-basics';
|
2020-10-04 07:36:51 +02:00
|
|
|
import ErrorMessage from 'components/common/ErrorMessage';
|
2022-12-29 00:43:22 +01:00
|
|
|
import useApi from 'hooks/useApi';
|
2020-09-18 07:52:20 +02:00
|
|
|
import useDateRange from 'hooks/useDateRange';
|
2020-09-26 07:31:18 +02:00
|
|
|
import usePageQuery from 'hooks/usePageQuery';
|
2020-10-11 11:29:55 +02:00
|
|
|
import { formatShortTime, formatNumber, formatLongNumber } from 'lib/format';
|
2020-09-06 02:27:01 +02:00
|
|
|
import MetricCard from './MetricCard';
|
2023-03-22 22:05:55 +01:00
|
|
|
import useMessages from 'hooks/useMessages';
|
2023-04-10 05:22:28 +02:00
|
|
|
import styles from './MetricsBar.module.css';
|
2020-07-29 04:04:45 +02:00
|
|
|
|
2023-03-09 05:23:32 +01:00
|
|
|
export default function MetricsBar({ websiteId }) {
|
2023-03-22 22:05:55 +01:00
|
|
|
const { formatMessage, labels } = useMessages();
|
2022-12-29 00:43:22 +01:00
|
|
|
const { get, useQuery } = useApi();
|
2020-09-19 19:35:05 +02:00
|
|
|
const [dateRange] = useDateRange(websiteId);
|
2020-09-01 00:02:32 +02:00
|
|
|
const { startDate, endDate, modified } = dateRange;
|
2020-09-26 07:31:18 +02:00
|
|
|
const [format, setFormat] = useState(true);
|
|
|
|
const {
|
2023-04-17 09:10:51 +02:00
|
|
|
query: { url, referrer, os, browser, device, country, region, city },
|
2020-09-26 07:31:18 +02:00
|
|
|
} = usePageQuery();
|
|
|
|
|
2023-02-04 17:59:52 +01:00
|
|
|
const { data, error, isLoading, isFetched } = useQuery(
|
2023-04-17 09:10:51 +02:00
|
|
|
[
|
|
|
|
'websites:stats',
|
|
|
|
{ websiteId, modified, url, referrer, os, browser, device, country, region, city },
|
|
|
|
],
|
2022-12-29 00:43:22 +01:00
|
|
|
() =>
|
|
|
|
get(`/websites/${websiteId}/stats`, {
|
2022-12-27 02:36:48 +01:00
|
|
|
startAt: +startDate,
|
|
|
|
endAt: +endDate,
|
2020-10-09 21:39:03 +02:00
|
|
|
url,
|
2022-04-07 11:46:39 +02:00
|
|
|
referrer,
|
2022-04-10 12:51:43 +02:00
|
|
|
os,
|
|
|
|
browser,
|
|
|
|
device,
|
|
|
|
country,
|
2023-04-17 09:10:51 +02:00
|
|
|
region,
|
|
|
|
city,
|
2022-12-29 00:43:22 +01:00
|
|
|
}),
|
2020-08-31 23:11:30 +02:00
|
|
|
);
|
2020-07-29 09:16:02 +02:00
|
|
|
|
2021-08-13 01:01:51 +02:00
|
|
|
const formatFunc = format
|
|
|
|
? n => (n >= 0 ? formatLongNumber(n) : `-${formatLongNumber(Math.abs(n))}`)
|
|
|
|
: formatNumber;
|
2020-08-19 23:35:38 +02:00
|
|
|
|
|
|
|
function handleSetFormat() {
|
|
|
|
setFormat(state => !state);
|
|
|
|
}
|
|
|
|
|
2020-08-31 12:53:39 +02:00
|
|
|
const { pageviews, uniques, bounces, totaltime } = data || {};
|
2021-08-13 01:01:51 +02:00
|
|
|
const num = Math.min(data && uniques.value, data && bounces.value);
|
|
|
|
const diffs = data && {
|
|
|
|
pageviews: pageviews.value - pageviews.change,
|
|
|
|
uniques: uniques.value - uniques.change,
|
|
|
|
bounces: bounces.value - bounces.change,
|
|
|
|
totaltime: totaltime.value - totaltime.change,
|
|
|
|
};
|
2020-07-29 04:04:45 +02:00
|
|
|
|
|
|
|
return (
|
2023-03-09 05:23:32 +01:00
|
|
|
<div className={styles.bar} onClick={handleSetFormat}>
|
2023-02-04 17:59:52 +01:00
|
|
|
{isLoading && !isFetched && <Loading icon="dots" />}
|
2020-10-04 07:36:51 +02:00
|
|
|
{error && <ErrorMessage />}
|
2023-02-04 17:59:52 +01:00
|
|
|
{data && !error && isFetched && (
|
2020-08-31 12:53:39 +02:00
|
|
|
<>
|
|
|
|
<MetricCard
|
2023-04-10 05:22:28 +02:00
|
|
|
className={styles.card}
|
2023-03-09 05:23:32 +01:00
|
|
|
label={formatMessage(labels.views)}
|
2021-08-13 01:01:51 +02:00
|
|
|
value={pageviews.value}
|
|
|
|
change={pageviews.change}
|
2020-09-06 02:27:01 +02:00
|
|
|
format={formatFunc}
|
|
|
|
/>
|
|
|
|
<MetricCard
|
2023-04-10 05:22:28 +02:00
|
|
|
className={styles.card}
|
2023-03-09 05:23:32 +01:00
|
|
|
label={formatMessage(labels.visitors)}
|
2021-08-13 01:01:51 +02:00
|
|
|
value={uniques.value}
|
|
|
|
change={uniques.change}
|
2020-09-06 02:27:01 +02:00
|
|
|
format={formatFunc}
|
|
|
|
/>
|
|
|
|
<MetricCard
|
2023-04-10 05:22:28 +02:00
|
|
|
className={styles.card}
|
2023-03-09 05:23:32 +01:00
|
|
|
label={formatMessage(labels.bounceRate)}
|
2021-08-13 01:01:51 +02:00
|
|
|
value={uniques.value ? (num / uniques.value) * 100 : 0}
|
|
|
|
change={
|
|
|
|
uniques.value && uniques.change
|
|
|
|
? (num / uniques.value) * 100 -
|
|
|
|
(Math.min(diffs.uniques, diffs.bounces) / diffs.uniques) * 100 || 0
|
|
|
|
: 0
|
|
|
|
}
|
2020-08-31 12:53:39 +02:00
|
|
|
format={n => Number(n).toFixed(0) + '%'}
|
2021-08-13 01:01:51 +02:00
|
|
|
reverseColors
|
2020-08-31 12:53:39 +02:00
|
|
|
/>
|
|
|
|
<MetricCard
|
2023-04-10 05:22:28 +02:00
|
|
|
className={styles.card}
|
2023-03-09 05:23:32 +01:00
|
|
|
label={formatMessage(labels.averageVisitTime)}
|
2021-08-13 01:01:51 +02:00
|
|
|
value={
|
|
|
|
totaltime.value && pageviews.value
|
|
|
|
? totaltime.value / (pageviews.value - bounces.value)
|
|
|
|
: 0
|
|
|
|
}
|
|
|
|
change={
|
|
|
|
totaltime.value && pageviews.value
|
|
|
|
? (diffs.totaltime / (diffs.pageviews - diffs.bounces) -
|
|
|
|
totaltime.value / (pageviews.value - bounces.value)) *
|
|
|
|
-1 || 0
|
|
|
|
: 0
|
|
|
|
}
|
|
|
|
format={n => `${n < 0 ? '-' : ''}${formatShortTime(Math.abs(~~n), ['m', 's'], ' ')}`}
|
2020-08-31 12:53:39 +02:00
|
|
|
/>
|
|
|
|
</>
|
|
|
|
)}
|
2020-07-29 04:04:45 +02:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|