2020-07-29 09:16:02 +02:00
|
|
|
import React, { useState, useEffect } from 'react';
|
2020-07-29 04:04:45 +02:00
|
|
|
import MetricCard from './MetricCard';
|
2020-07-29 09:16:02 +02:00
|
|
|
import { get } from '../lib/web';
|
2020-07-30 06:40:26 +02:00
|
|
|
import { formatShortTime } from 'lib/format';
|
2020-07-29 04:04:45 +02:00
|
|
|
import styles from './WebsiteSummary.module.css';
|
|
|
|
|
2020-07-29 09:16:02 +02:00
|
|
|
export default function WebsiteSummary({ websiteId, startDate, endDate }) {
|
|
|
|
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(
|
|
|
|
await get(`/api/website/${websiteId}/summary`, {
|
|
|
|
start_at: +startDate,
|
|
|
|
end_at: +endDate,
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
loadData();
|
|
|
|
}, [startDate, endDate]);
|
2020-07-29 04:04:45 +02:00
|
|
|
|
|
|
|
return (
|
|
|
|
<div className={styles.container}>
|
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>
|
|
|
|
);
|
|
|
|
}
|