2020-07-29 09:16:02 +02:00
|
|
|
import React, { useState, useEffect } from 'react';
|
2020-08-02 06:20:52 +02:00
|
|
|
import classNames from 'classnames';
|
2020-07-29 04:04:45 +02:00
|
|
|
import MetricCard from './MetricCard';
|
2020-08-02 06:20:52 +02:00
|
|
|
import { get } from 'lib/web';
|
2020-07-30 06:40:26 +02:00
|
|
|
import { formatShortTime } from 'lib/format';
|
2020-07-31 05:11:43 +02:00
|
|
|
import styles from './MetricsBar.module.css';
|
2020-07-29 04:04:45 +02:00
|
|
|
|
2020-08-02 06:20:52 +02:00
|
|
|
export default function MetricsBar({ websiteId, startDate, endDate, className }) {
|
2020-07-29 09:16:02 +02:00
|
|
|
const [data, setData] = useState({});
|
2020-07-30 06:40:26 +02:00
|
|
|
const { pageviews, uniques, bounces, totaltime } = data;
|
2020-07-29 09:16:02 +02:00
|
|
|
|
|
|
|
async function loadData() {
|
|
|
|
setData(
|
2020-07-31 05:11:43 +02:00
|
|
|
await get(`/api/website/${websiteId}/metrics`, {
|
2020-07-29 09:16:02 +02:00
|
|
|
start_at: +startDate,
|
|
|
|
end_at: +endDate,
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
loadData();
|
2020-08-02 06:20:52 +02:00
|
|
|
}, [websiteId, startDate, endDate]);
|
2020-07-29 04:04:45 +02:00
|
|
|
|
|
|
|
return (
|
2020-08-02 06:20:52 +02:00
|
|
|
<div className={classNames(styles.container, className)}>
|
2020-07-29 09:16:02 +02:00
|
|
|
<MetricCard label="Views" value={pageviews} />
|
|
|
|
<MetricCard label="Visitors" value={uniques} />
|
2020-07-30 05:09:41 +02:00
|
|
|
<MetricCard
|
|
|
|
label="Bounce rate"
|
|
|
|
value={uniques ? (bounces / uniques) * 100 : 0}
|
|
|
|
format={n => Number(n).toFixed(0) + '%'}
|
|
|
|
/>
|
2020-07-30 06:40:26 +02:00
|
|
|
<MetricCard
|
|
|
|
label="Average visit time"
|
|
|
|
value={totaltime && pageviews ? totaltime / (pageviews - bounces) : 0}
|
|
|
|
format={n => formatShortTime(n, ['m', 's'], ' ')}
|
|
|
|
/>
|
2020-07-29 04:04:45 +02:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|