Skip to content

📈 Open source, privacy-first web analytics — a cookie-less Google Analytics alternative with built-in error tracking and performance monitoring. Self-host it or use our cloud. Get started for free!

Native 'Go back' support for time bucket

Blue Mouse4cec4420f597
diff --git a/web/app/lib/constants/index.ts b/web/app/lib/constants/index.ts
index 107577671..8efb7ed05 100644
--- a/web/app/lib/constants/index.ts
+++ b/web/app/lib/constants/index.ts
@@ -37,7 +37,7 @@ export const ALL_PERIODS = ['minute', 'hour', 'day', 'month', 'year']
export interface TBPeriodPairsProps {
label: string
period: Period
- tbs: string[]
+ tbs: TimeBucket[]
countDays?: number
dropdownLabel?: string
isCustomDate?: boolean
@@ -60,11 +60,15 @@ export const VALID_PERIODS = [
'compare',
] as const
+export const VALID_TIME_BUCKETS = ['minute', 'hour', 'day', 'month', 'year'] as const
+
export type Period = (typeof VALID_PERIODS)[number]
+export type TimeBucket = (typeof VALID_TIME_BUCKETS)[number]
+
export const tbPeriodPairs = (
t: typeof i18nextT,
- tbs?: string[] | null,
+ tbs?: TimeBucket[] | null,
dates?: Date[],
language?: string,
): TBPeriodPairsProps[] => [
@@ -128,18 +132,18 @@ export const tbPeriodPairs = (
dropdownLabel: t('project.custom'),
isCustomDate: true,
period: 'custom',
- tbs: tbs || ['custom'],
+ tbs: tbs || ['month'],
},
{
label: t('project.compare'),
period: 'compare',
- tbs: tbs || ['custom'],
+ tbs: tbs || ['month'],
},
]
export const captchaTbPeriodPairs = (
t: typeof i18nextT,
- tbs?: string[] | null,
+ tbs?: TimeBucket[] | null,
dates?: Date[],
language?: string,
): TBPeriodPairsProps[] => [
@@ -203,7 +207,7 @@ export const captchaTbPeriodPairs = (
dropdownLabel: t('project.custom'),
isCustomDate: true,
period: 'custom',
- tbs: tbs || ['custom'],
+ tbs: tbs || ['month'],
},
]
@@ -256,7 +260,7 @@ export const MAX_MONTHS_IN_PAST = 24
export const timeBucketToDays: {
lt: number
- tb: string[]
+ tb: TimeBucket[]
}[] = [
// { lt: 0, tb: ['minute'] }, // 1 hour
{ lt: 1, tb: ['hour'] }, // 1 days
diff --git a/web/app/pages/Project/View/ViewProject.tsx b/web/app/pages/Project/View/ViewProject.tsx
index 45f0c669a..6796b4687 100644
--- a/web/app/pages/Project/View/ViewProject.tsx
+++ b/web/app/pages/Project/View/ViewProject.tsx
@@ -109,8 +109,10 @@ import {
ERROR_PANELS_ORDER,
ERROR_PERIOD_PAIRS,
FUNNELS_PERIOD_PAIRS,
- Period,
+ type Period,
+ type TimeBucket,
VALID_PERIODS,
+ VALID_TIME_BUCKETS,
} from '~/lib/constants'
import { CountryEntry } from '~/lib/models/Entry'
import { Funnel, AnalyticsFunnel, OverallObject, OverallPerformanceObject } from '~/lib/models/Project'
@@ -178,8 +180,6 @@ import { Panel, Metadata } from './Panels'
import {
handleNavigationParams,
updateFilterState,
- validTimeBacket,
- validPeriods,
parseFiltersFromUrl,
isFilterValid,
FILTER_CHART_METRICS_MAPPING_FOR_COMPARE,
@@ -227,7 +227,7 @@ interface ViewProjectContextType {
// Functions
setDateRange: Dispatch<SetStateAction<Date[] | null>>
updatePeriod: (newPeriod: { period: Period; label?: string }) => void
- updateTimebucket: (newTimebucket: string) => void
+ updateTimebucket: (newTimebucket: TimeBucket) => void
setPeriodPairs: Dispatch<SetStateAction<TBPeriodPairsProps[]>>
// Refs
@@ -296,7 +296,6 @@ const ViewProject = () => {
const [isNewFunnelOpened, setIsNewFunnelOpened] = useState(false)
const [isAddAViewOpened, setIsAddAViewOpened] = useState(false)
const [analyticsLoading, setAnalyticsLoading] = useState(true)
- const [timeBucket, setTimebucket] = useState(preferences.timeBucket || periodPairs[4].tbs[1])
const [chartData, setChartData] = useState<any>({})
const [mainChart, setMainChart] = useState<any>(null)
const [dataLoading, setDataLoading] = useState(false)
@@ -344,6 +343,7 @@ const ViewProject = () => {
return PROJECT_TABS.traffic
}, [searchParams])
+
const period = useMemo<Period>(() => {
const urlPeriod = searchParams.get('period') as Period
@@ -354,6 +354,20 @@ const ViewProject = () => {
return preferences.period || periodPairs[4].period
}, [searchParams, preferences.period, periodPairs])
+ const timeBucket = useMemo<TimeBucket>(() => {
+ const urlTimeBucket = searchParams.get('timeBucket') as TimeBucket
+
+ if (VALID_TIME_BUCKETS.includes(urlTimeBucket)) {
+ const newPeriodFull = _find(periodPairs, (el) => el.period === period)
+
+ if (newPeriodFull?.tbs?.includes(urlTimeBucket)) {
+ return urlTimeBucket
+ }
+ }
+
+ return preferences.timeBucket || periodPairs[4].tbs[1]
+ }, [searchParams, preferences.timeBucket, periodPairs, period])
+
const activePeriod = useMemo(() => _find(periodPairs, (p) => p.period === period), [period, periodPairs])
const [isHotkeysHelpOpened, setIsHotkeysHelpOpened] = useState(false)
@@ -1238,7 +1252,10 @@ const ViewProject = () => {
})
return newPeriodPairs
})
- setTimebucket(newTimebucket)
+
+ const newSearchParams = new URLSearchParams(searchParams.toString())
+ newSearchParams.set('timeBucket', newTimebucket)
+ setSearchParams(newSearchParams)
}
// @ts-expect-error
@@ -1704,7 +1721,9 @@ const ViewProject = () => {
})
return newPeriodPairs
})
- setTimebucket(newTimebucket)
+ const newSearchParams = new URLSearchParams(searchParams.toString())
+ newSearchParams.set('timeBucket', newTimebucket)
+ setSearchParams(newSearchParams)
}
if (!_isEmpty(dataCompare)) {
@@ -2024,7 +2043,7 @@ const ViewProject = () => {
try {
const initialPeriod = (searchParams.get('period') as Period) || preferences.period || '7d'
- if (!_includes(validPeriods, initialPeriod)) {
+ if (!_includes(VALID_PERIODS, initialPeriod)) {
return
}
@@ -2052,19 +2071,6 @@ const ViewProject = () => {
parsePeriodFilters()
- try {
- const initialTimeBucket = searchParams.get('timeBucket')
-
- if (_includes(validTimeBacket, initialTimeBucket)) {
- const newPeriodFull = _find(periodPairs, (el) => el.period === period)
- if (_includes(newPeriodFull?.tbs, initialTimeBucket)) {
- setTimebucket(initialTimeBucket || periodPairs[3].tbs[1])
- }
- }
- } catch {
- //
- }
-
setAreFiltersParsed(true)
}, [activeTab])
@@ -2409,7 +2415,6 @@ const ViewProject = () => {
if (!onRender && !_includes(timeBucketToDays[index].tb, timeBucket)) {
eventEmitTimeBucket = timeBucketToDays[index].tb[0]
newSearchParams.set('timeBucket', eventEmitTimeBucket)
- setTimebucket(eventEmitTimeBucket)
}
newSearchParams.set('from', dates[0].toISOString())
@@ -2487,7 +2492,6 @@ const ViewProject = () => {
const newSearchParamsTB = new URLSearchParams(searchParams.toString())
newSearchParamsTB.set('timeBucket', tb)
setSearchParams(newSearchParamsTB)
- setTimebucket(tb)
}
const newSearchParams = new URLSearchParams(searchParams.toString())
@@ -2516,14 +2520,15 @@ const ViewProject = () => {
})
}
- const updateTimebucket = (newTimebucket: string) => {
+ const updateTimebucket = (newTimebucket: TimeBucket) => {
if (dataLoading) {
return
}
- searchParams.set('timeBucket', newTimebucket)
- setSearchParams(searchParams)
- setTimebucket(newTimebucket)
+ const newSearchParams = new URLSearchParams(searchParams.toString())
+ newSearchParams.set('timeBucket', newTimebucket)
+ setSearchParams(newSearchParams)
+
updatePreferences({
timeBucket: newTimebucket,
})
diff --git a/web/app/providers/CurrentProjectProvider.tsx b/web/app/providers/CurrentProjectProvider.tsx
index 6cb6e07a4..7dff73b06 100644
--- a/web/app/providers/CurrentProjectProvider.tsx
+++ b/web/app/providers/CurrentProjectProvider.tsx
@@ -5,7 +5,7 @@ import { useNavigate, useSearchParams } from 'react-router'
import { toast } from 'sonner'
import { checkPassword, getInstalledExtensions, getProject } from '~/api'
-import { isSelfhosted, LS_PROJECTS_PROTECTED_KEY, Period } from '~/lib/constants'
+import { isSelfhosted, LS_PROJECTS_PROTECTED_KEY, Period, TimeBucket } from '~/lib/constants'
import { Extension, type Project } from '~/lib/models/Project'
import { getItemJSON, removeItem } from '~/utils/localstorage'
import routes from '~/utils/routes'
@@ -134,7 +134,7 @@ const useProject = (id: string) => {
export type ProjectPreferences = {
period?: Period
- timeBucket?: string
+ timeBucket?: TimeBucket
rangeDate?: Date[]
customEvents?: any
metricsVisualisation?: Record<keyof typeof CHART_METRICS_MAPPING, boolean>