2023-07-10 13:35:19 +02:00
|
|
|
import { useMemo } from 'react';
|
|
|
|
import PageviewsChart from 'components/metrics/PageviewsChart';
|
|
|
|
import { useApi, useDateRange, useTimezone, usePageQuery } from 'hooks';
|
|
|
|
import { getDateArray, getDateLength } from 'lib/date';
|
|
|
|
|
|
|
|
export function WebsiteChart({ websiteId }) {
|
|
|
|
const [dateRange] = useDateRange(websiteId);
|
|
|
|
const { startDate, endDate, unit, modified } = dateRange;
|
|
|
|
const [timezone] = useTimezone();
|
|
|
|
const {
|
|
|
|
query: { url, referrer, os, browser, device, country, region, city, title },
|
|
|
|
} = usePageQuery();
|
|
|
|
const { get, useQuery } = useApi();
|
|
|
|
|
|
|
|
const { data, isLoading } = useQuery(
|
|
|
|
[
|
|
|
|
'websites:pageviews',
|
|
|
|
{ websiteId, modified, url, referrer, os, browser, device, country, region, city, title },
|
|
|
|
],
|
|
|
|
() =>
|
|
|
|
get(`/websites/${websiteId}/pageviews`, {
|
|
|
|
startAt: +startDate,
|
|
|
|
endAt: +endDate,
|
|
|
|
unit,
|
|
|
|
timezone,
|
|
|
|
url,
|
|
|
|
referrer,
|
|
|
|
os,
|
|
|
|
browser,
|
|
|
|
device,
|
|
|
|
country,
|
|
|
|
region,
|
|
|
|
city,
|
|
|
|
title,
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
|
|
|
|
const chartData = useMemo(() => {
|
|
|
|
if (data) {
|
|
|
|
return {
|
|
|
|
pageviews: getDateArray(data.pageviews, startDate, endDate, unit),
|
|
|
|
sessions: getDateArray(data.sessions, startDate, endDate, unit),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
return { pageviews: [], sessions: [] };
|
|
|
|
}, [data, startDate, endDate, unit, modified]);
|
|
|
|
|
2023-07-26 00:17:50 +02:00
|
|
|
return <PageviewsChart websiteId={websiteId} data={chartData} unit={unit} loading={isLoading} />;
|
2023-07-10 13:35:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export default WebsiteChart;
|