From 8e0241bc42cb45a10c6c80fcc87c0475cf6e4e86 Mon Sep 17 00:00:00 2001 From: Salihu Date: Sat, 19 Sep 2026 23:27:59 +0100 Subject: [PATCH] add custom date ranges --- .../public/locales/en-US/translation.json | 2 + .../page-analytics/pages/page-analytics.tsx | 69 ++++++++++++++++--- ...s.ts => 20260917T000000-page-analytics.ts} | 1 + apps/server/src/ee | 2 +- 4 files changed, 64 insertions(+), 10 deletions(-) rename apps/server/src/database/migrations/{20260809T000000-page-analytics.ts => 20260917T000000-page-analytics.ts} (99%) diff --git a/apps/client/public/locales/en-US/translation.json b/apps/client/public/locales/en-US/translation.json index 3dc11c1b3..f433c8bd2 100644 --- a/apps/client/public/locales/en-US/translation.json +++ b/apps/client/public/locales/en-US/translation.json @@ -230,6 +230,8 @@ "Forgot your password?": "Forgot your password?", "A password reset link has been sent to your email. Please check your inbox.": "A password reset link has been sent to your email. Please check your inbox.", "Send reset link": "Send reset link", + "Start date": "Start date", + "End dtae": "End date", "Password reset": "Password reset", "Your new password": "Your new password", "Set password": "Set password", diff --git a/apps/client/src/ee/page-analytics/pages/page-analytics.tsx b/apps/client/src/ee/page-analytics/pages/page-analytics.tsx index 2a1126305..801615164 100644 --- a/apps/client/src/ee/page-analytics/pages/page-analytics.tsx +++ b/apps/client/src/ee/page-analytics/pages/page-analytics.tsx @@ -37,8 +37,10 @@ import { RetentionUnit, toISODate, } from "@/ee/utils"; +import { DatePickerInput } from "@mantine/dates"; +import { parseISO } from "date-fns"; -type RangePreset = "7" | "30" | "90"; +type RangePreset = "7" | "30" | "90" | "custom"; const DAILY_PAGE_SIZE = 10; @@ -46,6 +48,12 @@ export default function PageAnalytics() { const { t } = useTranslation(); const locale = useDateFnsLocale(); const [rangePreset, setRangePreset] = useState("30"); + const [customStartDate, setCustomStartDate] = useState(() => + parseISO(toISODate(30)) + ); + const [customEndDate, setCustomEndDate] = useState( + () => new Date() + ); const [topPagesLimit, setTopPagesLimit] = useState("10"); const [settingsOpen, setSettingsOpen] = useState(false); @@ -89,13 +97,26 @@ export default function PageAnalytics() { resetCursor: resetDailyCursor, } = useCursorPaginate(); - const dateRange = useMemo( - () => ({ + const resetCursors = () => { + resetTopPagesCursor(); + resetDailyCursor(); + } + + const dateRange = useMemo(() => { + const defaultEnd = new Date().toISOString().slice(0, 10); + + if (rangePreset === "custom" && customStartDate) { + return { + startDate: customStartDate?.toISOString().slice(0, 10), + endDate: customEndDate?.toISOString().slice(0, 10) ?? defaultEnd, + }; + } + + return { startDate: toISODate(rangePreset), - endDate: new Date().toISOString().slice(0, 10), - }), - [rangePreset] - ); + endDate: defaultEnd, + }; + }, [rangePreset, customStartDate, customEndDate]); const topPagesParams = useMemo( () => ({ @@ -136,8 +157,7 @@ export default function PageAnalytics() { const handleRangeChange = (value: RangePreset) => { if (value) { setRangePreset(value); - resetTopPagesCursor(); - resetDailyCursor(); + resetCursors(); } }; @@ -166,11 +186,42 @@ export default function PageAnalytics() { { value: "7", label: t("Last 7 days") }, { value: "30", label: t("Last 30 days") }, { value: "90", label: t("Last 90 days") }, + { value: "custom", label: t("Custom range") }, ]} w={160} size="sm" allowDeselect={false} /> + {rangePreset === "custom" && ( + + { + setCustomStartDate(value ? parseISO(value) : null); + resetCursors(); + }} + valueFormat="MMM DD, YYYY" + placeholder={t("Start date")} + maxDate={customEndDate} + size="sm" + /> + + { + setCustomEndDate(value ? parseISO(value) : null); + resetCursors(); + }} + valueFormat="MMM DD, YYYY" + placeholder={t("End date")} + minDate={customStartDate} + maxDate={new Date()} + size="sm" + /> + + )} ): Promise { ON page_analytics ( workspace_id, page_id, + view_date, COALESCE(user_id::text, visitor_id) ) `.execute(db); diff --git a/apps/server/src/ee b/apps/server/src/ee index cf0601837..bae88ad48 160000 --- a/apps/server/src/ee +++ b/apps/server/src/ee @@ -1 +1 @@ -Subproject commit cf0601837337e3c30642ad40d83b04f8fa07a44a +Subproject commit bae88ad486c6fee97f326484edae7a8412c1e610