2020-08-31 00:29:31 +02:00
|
|
|
import React, { useState } from 'react';
|
2020-09-06 02:27:01 +02:00
|
|
|
import { FormattedMessage } from 'react-intl';
|
2020-08-02 06:20:52 +02:00
|
|
|
import classNames from 'classnames';
|
2020-08-31 12:53:39 +02:00
|
|
|
import Loading from 'components/common/Loading';
|
2020-10-04 07:36:51 +02:00
|
|
|
import ErrorMessage from 'components/common/ErrorMessage';
|
2020-08-31 00:29:31 +02:00
|
|
|
import useFetch from 'hooks/useFetch';
|
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 useShareToken from 'hooks/useShareToken';
|
|
|
|
import { formatShortTime, formatNumber, formatLongNumber } from 'lib/format';
|
|
|
|
import { TOKEN_HEADER } from 'lib/constants';
|
2020-09-06 02:27:01 +02:00
|
|
|
import MetricCard from './MetricCard';
|
2020-07-31 05:11:43 +02:00
|
|
|
import styles from './MetricsBar.module.css';
|
2020-07-29 04:04:45 +02:00
|
|
|
|
2020-10-11 11:29:55 +02:00
|
|
|
export default function MetricsBar({ websiteId, className }) {
|
|
|
|
const shareToken = useShareToken();
|
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 {
|
|
|
|
query: { url },
|
|
|
|
} = usePageQuery();
|
|
|
|
|
2020-10-04 07:36:51 +02:00
|
|
|
const { data, error, loading } = useFetch(
|
2020-10-09 00:02:48 +02:00
|
|
|
`/api/website/${websiteId}/stats`,
|
2020-08-31 23:11:30 +02:00
|
|
|
{
|
2020-10-09 21:39:03 +02:00
|
|
|
params: {
|
|
|
|
start_at: +startDate,
|
|
|
|
end_at: +endDate,
|
|
|
|
url,
|
|
|
|
},
|
2020-10-11 11:29:55 +02:00
|
|
|
headers: { [TOKEN_HEADER]: shareToken?.token },
|
2020-08-31 23:11:30 +02:00
|
|
|
},
|
2020-10-09 21:39:03 +02:00
|
|
|
[modified],
|
2020-08-31 23:11:30 +02:00
|
|
|
);
|
2020-07-29 09:16:02 +02:00
|
|
|
|
2020-08-19 23:35:38 +02:00
|
|
|
const formatFunc = format ? formatLongNumber : formatNumber;
|
|
|
|
|
|
|
|
function handleSetFormat() {
|
|
|
|
setFormat(state => !state);
|
|
|
|
}
|
|
|
|
|
2020-08-31 12:53:39 +02:00
|
|
|
const { pageviews, uniques, bounces, totaltime } = data || {};
|
2020-07-29 04:04:45 +02:00
|
|
|
|
|
|
|
return (
|
2020-08-19 23:35:38 +02:00
|
|
|
<div className={classNames(styles.bar, className)} onClick={handleSetFormat}>
|
2020-10-04 07:36:51 +02:00
|
|
|
{!data && loading && <Loading />}
|
|
|
|
{error && <ErrorMessage />}
|
|
|
|
{data && !error && (
|
2020-08-31 12:53:39 +02:00
|
|
|
<>
|
|
|
|
<MetricCard
|
2020-09-06 02:27:01 +02:00
|
|
|
label={<FormattedMessage id="metrics.views" defaultMessage="Views" />}
|
|
|
|
value={pageviews}
|
|
|
|
format={formatFunc}
|
|
|
|
/>
|
|
|
|
<MetricCard
|
|
|
|
label={<FormattedMessage id="metrics.visitors" defaultMessage="Visitors" />}
|
|
|
|
value={uniques}
|
|
|
|
format={formatFunc}
|
|
|
|
/>
|
|
|
|
<MetricCard
|
|
|
|
label={<FormattedMessage id="metrics.bounce-rate" defaultMessage="Bounce rate" />}
|
2020-08-31 12:53:39 +02:00
|
|
|
value={pageviews ? (bounces / pageviews) * 100 : 0}
|
|
|
|
format={n => Number(n).toFixed(0) + '%'}
|
|
|
|
/>
|
|
|
|
<MetricCard
|
2020-09-06 02:27:01 +02:00
|
|
|
label={
|
|
|
|
<FormattedMessage
|
|
|
|
id="metrics.average-visit-time"
|
|
|
|
defaultMessage="Average visit time"
|
|
|
|
/>
|
|
|
|
}
|
2020-08-31 12:53:39 +02:00
|
|
|
value={totaltime && pageviews ? totaltime / (pageviews - bounces) : 0}
|
|
|
|
format={n => formatShortTime(n, ['m', 's'], ' ')}
|
|
|
|
/>
|
|
|
|
</>
|
|
|
|
)}
|
2020-07-29 04:04:45 +02:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|