Added hooks for website stats and pageviews.

This commit is contained in:
Mike Cao 2024-02-15 14:45:07 -08:00
parent f50067e44f
commit 7ab580c709
5 changed files with 74 additions and 61 deletions

View File

@ -1,39 +1,13 @@
import { useMemo } from 'react'; import { useMemo } from 'react';
import PageviewsChart from 'components/metrics/PageviewsChart'; import PageviewsChart from 'components/metrics/PageviewsChart';
import { useApi, useDateRange, useTimezone, useNavigation } from 'components/hooks';
import { getDateArray } from 'lib/date'; import { getDateArray } from 'lib/date';
import useWebsitePageviews from 'components/hooks/queries/useWebsitePageviews';
import { useDateRange } from 'components/hooks';
export function WebsiteChart({ websiteId }: { websiteId: string }) { export function WebsiteChart({ websiteId }: { websiteId: string }) {
const [dateRange] = useDateRange(websiteId); const [dateRange] = useDateRange(websiteId);
const { startDate, endDate, unit } = dateRange; const { startDate, endDate, unit } = dateRange;
const [timezone] = useTimezone(); const { data, isLoading } = useWebsitePageviews(websiteId);
const {
query: { url, referrer, os, browser, device, country, region, city, title },
} = useNavigation();
const { get, useQuery } = useApi();
const { data, isLoading } = useQuery({
queryKey: [
'websites:pageviews',
{ websiteId, url, referrer, os, browser, device, country, region, city, title },
],
queryFn: () =>
get(`/websites/${websiteId}/pageviews`, {
startAt: +startDate,
endAt: +endDate,
unit,
timezone,
url,
referrer,
os,
browser,
device,
country,
region,
city,
title,
}),
});
const chartData = useMemo(() => { const chartData = useMemo(() => {
if (data) { if (data) {

View File

@ -1,11 +1,12 @@
import classNames from 'classnames'; import classNames from 'classnames';
import { useApi, useDateRange, useMessages, useNavigation, useSticky } from 'components/hooks'; import { useMessages, useSticky } from 'components/hooks';
import WebsiteDateFilter from 'components/input/WebsiteDateFilter'; import WebsiteDateFilter from 'components/input/WebsiteDateFilter';
import MetricCard from 'components/metrics/MetricCard'; import MetricCard from 'components/metrics/MetricCard';
import MetricsBar from 'components/metrics/MetricsBar'; import MetricsBar from 'components/metrics/MetricsBar';
import { formatShortTime } from 'lib/format'; import { formatShortTime } from 'lib/format';
import WebsiteFilterButton from './WebsiteFilterButton'; import WebsiteFilterButton from './WebsiteFilterButton';
import styles from './WebsiteMetricsBar.module.css'; import styles from './WebsiteMetricsBar.module.css';
import useWebsiteStats from 'components/hooks/queries/useWebsiteStats';
export function WebsiteMetricsBar({ export function WebsiteMetricsBar({
websiteId, websiteId,
@ -17,34 +18,8 @@ export function WebsiteMetricsBar({
sticky?: boolean; sticky?: boolean;
}) { }) {
const { formatMessage, labels } = useMessages(); const { formatMessage, labels } = useMessages();
const { get, useQuery } = useApi();
const [dateRange] = useDateRange(websiteId);
const { startDate, endDate } = dateRange;
const { ref, isSticky } = useSticky({ enabled: sticky }); const { ref, isSticky } = useSticky({ enabled: sticky });
const { const { data, isLoading, isFetched, error } = useWebsiteStats(websiteId);
query: { url, referrer, title, os, browser, device, country, region, city },
} = useNavigation();
const { data, error, isLoading, isFetched } = useQuery({
queryKey: [
'websites:stats',
{ websiteId, url, referrer, title, os, browser, device, country, region, city },
],
queryFn: () =>
get(`/websites/${websiteId}/stats`, {
startAt: +startDate,
endAt: +endDate,
url,
referrer,
title,
os,
browser,
device,
country,
region,
city,
}),
});
const { pageviews, uniques, bounces, totaltime } = data || {}; const { pageviews, uniques, bounces, totaltime } = data || {};
const num = Math.min(data && uniques.value, data && bounces.value); const num = Math.min(data && uniques.value, data && bounces.value);

View File

@ -0,0 +1,35 @@
import { useApi, useDateRange, useNavigation, useTimezone } from 'components/hooks';
export function useWebsitePageviews(websiteId: string, options?: { [key: string]: string }) {
const { get, useQuery } = useApi();
const [dateRange] = useDateRange(websiteId);
const { startDate, endDate, unit } = dateRange;
const [timezone] = useTimezone();
const {
query: { url, referrer, os, browser, device, country, region, city, title },
} = useNavigation();
const params = {
startAt: +startDate,
endAt: +endDate,
unit,
timezone,
url,
referrer,
os,
browser,
device,
country,
region,
city,
title,
};
return useQuery({
queryKey: ['websites:pageviews', params],
queryFn: () => get(`/websites/${websiteId}/pageviews`, params),
...options,
});
}
export default useWebsitePageviews;

View File

@ -0,0 +1,32 @@
import { useApi, useDateRange, useNavigation } from 'components/hooks';
export function useWebsiteStats(websiteId: string, options?: { [key: string]: string }) {
const { get, useQuery } = useApi();
const [dateRange] = useDateRange(websiteId);
const { startDate, endDate } = dateRange;
const {
query: { url, referrer, title, os, browser, device, country, region, city },
} = useNavigation();
const params = {
startAt: +startDate,
endAt: +endDate,
url,
referrer,
title,
os,
browser,
device,
country,
region,
city,
};
return useQuery({
queryKey: ['websites:stats', params],
queryFn: () => get(`/websites/${websiteId}/stats`, params),
...options,
});
}
export default useWebsiteStats;

View File

@ -136,10 +136,7 @@ export function BarChart({
const updateChart = () => { const updateChart = () => {
setTooltipPopup(null); setTooltipPopup(null);
datasets.forEach((dataset, index) => { chart.current.data.datasets = datasets;
chart.current.data.datasets[index].data = dataset.data;
chart.current.data.datasets[index].label = dataset.label;
});
chart.current.options = getOptions(); chart.current.options = getOptions();