Added summary stats query.

This commit is contained in:
Mike Cao 2020-07-29 00:16:02 -07:00
parent 18de85a06d
commit f9a6f5f637
5 changed files with 61 additions and 13 deletions

View file

@ -33,7 +33,7 @@ export default function WebsiteStats({ websiteId, startDate, endDate, unit }) {
return (
<div>
<WebsiteSummary data={{ pageviews, uniques }} />
<WebsiteSummary websiteId={websiteId} startDate={startDate} endDate={endDate} />
<PageviewsChart data={{ pageviews, uniques }} unit={unit} />
</div>
);

View file

@ -1,16 +1,30 @@
import React from 'react';
import React, { useState, useEffect } from 'react';
import MetricCard from './MetricCard';
import { get } from '../lib/web';
import styles from './WebsiteSummary.module.css';
function getTotal(data) {
return data.reduce((n, v) => n + v.y, 0);
}
export default function WebsiteSummary({ websiteId, startDate, endDate }) {
const [data, setData] = useState({});
const { pageviews, uniques, bounces } = data;
async function loadData() {
setData(
await get(`/api/website/${websiteId}/summary`, {
start_at: +startDate,
end_at: +endDate,
}),
);
}
useEffect(() => {
loadData();
}, [startDate, endDate]);
export default function WebsiteSummary({ data }) {
return (
<div className={styles.container}>
<MetricCard label="Views" value={getTotal(data.pageviews)} />
<MetricCard label="Visitors" value={getTotal(data.uniques)} />
<MetricCard label="Views" value={pageviews} />
<MetricCard label="Visitors" value={uniques} />
<MetricCard label="Bounce rate" value={`${~~((bounces / uniques) * 100)}%`} />
</div>
);
}