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!

fix(analytics): correct preset period boundaries

Volodymyr Vovchenko0934044cdc3b
diff --git a/backend/apps/cloud/src/analytics/analytics.service.ts b/backend/apps/cloud/src/analytics/analytics.service.ts
index d658ed9c0..8b266b893 100644
--- a/backend/apps/cloud/src/analytics/analytics.service.ts
+++ b/backend/apps/cloud/src/analytics/analytics.service.ts
@@ -553,11 +553,14 @@ export const getLowestPossibleTimeBucket = (
return TimeBucketType.HOUR
}
- if (period === '4w') {
+ // The finest buckets must match the frontend's per-period options
+ // (tbPeriodPairs), otherwise summary cards and the chart cover
+ // different boundaries
+ if (period === '4w' || period === '3M' || period === '12M') {
return TimeBucketType.DAY
}
- if (period === '3M' || period === '12M' || period === '24M') {
+ if (period === '24M') {
return TimeBucketType.MONTH
}
@@ -1202,17 +1205,10 @@ export class AnalyticsService {
groupFrom = djsNow.subtract(diff - 1, 'day').startOf(timeBucket)
groupTo = djsNow
} else {
- if (period === '1d' || period === '1h') {
- groupFrom = djsNow.subtract(
- parseInt(period, 10),
- _last(period) as dayjs.ManipulateType,
- )
- } else {
- groupFrom = djsNow.subtract(
- parseInt(period, 10) - 1,
- _last(period) as dayjs.ManipulateType,
- )
- }
+ groupFrom = djsNow.subtract(
+ parseInt(period, 10),
+ _last(period) as dayjs.ManipulateType,
+ )
groupFrom = groupFrom.startOf(timeBucket)
groupTo = djsNow
diff --git a/backend/apps/cloud/src/analytics/v2/__tests__/summary-time-bucket.spec.ts b/backend/apps/cloud/src/analytics/v2/__tests__/summary-time-bucket.spec.ts
index 87cf4215f..aadafd1ca 100644
--- a/backend/apps/cloud/src/analytics/v2/__tests__/summary-time-bucket.spec.ts
+++ b/backend/apps/cloud/src/analytics/v2/__tests__/summary-time-bucket.spec.ts
@@ -1,4 +1,8 @@
-import { AnalyticsService, getSummaryTimeBucket } from '../../analytics.service'
+import {
+ AnalyticsService,
+ getLowestPossibleTimeBucket,
+ getSummaryTimeBucket,
+} from '../../analytics.service'
import { TimeBucketType } from '../../dto/getData.dto'
// getGroupFromTo does not touch `this`, so it can be exercised without
@@ -74,15 +78,59 @@ describe('traffic summary window (no explicit timeBucket)', () => {
expect(groupToUTC).toBe('2026-08-23 14:37:42')
})
- it('period=7d starts six days ago on an hour boundary', () => {
+ it('period=7d starts seven days ago on an hour boundary', () => {
const { bucket, groupFrom, groupFromUTC } = getSummaryWindow(
'7d',
'Etc/GMT',
)
expect(bucket).toBe(TimeBucketType.HOUR)
- expect(groupFrom).toBe('2026-08-17 14:00:00')
- expect(groupFromUTC).toBe('2026-08-17 14:00:00')
+ expect(groupFrom).toBe('2026-08-16 14:00:00')
+ expect(groupFromUTC).toBe('2026-08-16 14:00:00')
+ })
+
+ it('period=4w starts four weeks ago on a day boundary', () => {
+ const { bucket, groupFrom, groupFromUTC } = getSummaryWindow(
+ '4w',
+ 'Etc/GMT',
+ )
+
+ expect(bucket).toBe(TimeBucketType.DAY)
+ expect(groupFrom).toBe('2026-07-26 00:00:00')
+ expect(groupFromUTC).toBe('2026-07-26 00:00:00')
+ })
+
+ it('period=3M starts three months ago on a day boundary, not a month boundary', () => {
+ const { bucket, groupFrom, groupFromUTC } = getSummaryWindow(
+ '3M',
+ 'Etc/GMT',
+ )
+
+ expect(bucket).toBe(TimeBucketType.DAY)
+ expect(groupFrom).toBe('2026-05-23 00:00:00')
+ expect(groupFromUTC).toBe('2026-05-23 00:00:00')
+ })
+
+ it('period=12M starts twelve months ago on a day boundary', () => {
+ const { bucket, groupFrom, groupFromUTC } = getSummaryWindow(
+ '12M',
+ 'Etc/GMT',
+ )
+
+ expect(bucket).toBe(TimeBucketType.DAY)
+ expect(groupFrom).toBe('2025-08-23 00:00:00')
+ expect(groupFromUTC).toBe('2025-08-23 00:00:00')
+ })
+
+ it('period=24M starts on the month boundary twenty-four months back', () => {
+ const { bucket, groupFrom, groupFromUTC } = getSummaryWindow(
+ '24M',
+ 'Etc/GMT',
+ )
+
+ expect(bucket).toBe(TimeBucketType.MONTH)
+ expect(groupFrom).toBe('2024-08-01 00:00:00')
+ expect(groupFromUTC).toBe('2024-08-01 00:00:00')
})
it('period=today still starts at midnight', () => {
@@ -102,6 +150,15 @@ describe('traffic summary window (no explicit timeBucket)', () => {
)
})
+ it('3M and 12M resolve to a day bucket in both resolvers, keeping cards and chart aligned', () => {
+ for (const period of ['3M', '12M']) {
+ expect(getLowestPossibleTimeBucket(period)).toBe(TimeBucketType.DAY)
+ expect(
+ getSummaryTimeBucket(undefined, period, undefined, undefined),
+ ).toBe(TimeBucketType.DAY)
+ }
+ })
+
it('partial and invalid custom ranges keep the legacy fallback and are left to getGroupFromTo to validate', () => {
// Ranges must not be interpreted by getLowestPossibleTimeBucket: its NaN
// diff would throw a misleading range-length error before getGroupFromTo
diff --git a/backend/apps/community/src/analytics/analytics.service.ts b/backend/apps/community/src/analytics/analytics.service.ts
index df68c4aa5..a2aab7cff 100644
--- a/backend/apps/community/src/analytics/analytics.service.ts
+++ b/backend/apps/community/src/analytics/analytics.service.ts
@@ -308,11 +308,14 @@ export const getLowestPossibleTimeBucket = (
return TimeBucketType.HOUR
}
- if (period === '4w') {
+ // The finest buckets must match the frontend's per-period options
+ // (tbPeriodPairs), otherwise summary cards and the chart cover
+ // different boundaries
+ if (period === '4w' || period === '3M' || period === '12M') {
return TimeBucketType.DAY
}
- if (period === '3M' || period === '12M' || period === '24M') {
+ if (period === '24M') {
return TimeBucketType.MONTH
}
@@ -1009,17 +1012,10 @@ export class AnalyticsService {
groupFrom = djsNow.subtract(diff - 1, 'day').startOf(timeBucket)
groupTo = djsNow
} else {
- if (period === '1d' || period === '1h') {
- groupFrom = djsNow.subtract(
- parseInt(period, 10),
- _last(period) as dayjs.ManipulateType,
- )
- } else {
- groupFrom = djsNow.subtract(
- parseInt(period, 10) - 1,
- _last(period) as dayjs.ManipulateType,
- )
- }
+ groupFrom = djsNow.subtract(
+ parseInt(period, 10),
+ _last(period) as dayjs.ManipulateType,
+ )
groupFrom = groupFrom.startOf(timeBucket)
groupTo = djsNow
diff --git a/backend/apps/community/src/analytics/v2/__tests__/summary-time-bucket.spec.ts b/backend/apps/community/src/analytics/v2/__tests__/summary-time-bucket.spec.ts
index 87cf4215f..aadafd1ca 100644
--- a/backend/apps/community/src/analytics/v2/__tests__/summary-time-bucket.spec.ts
+++ b/backend/apps/community/src/analytics/v2/__tests__/summary-time-bucket.spec.ts
@@ -1,4 +1,8 @@
-import { AnalyticsService, getSummaryTimeBucket } from '../../analytics.service'
+import {
+ AnalyticsService,
+ getLowestPossibleTimeBucket,
+ getSummaryTimeBucket,
+} from '../../analytics.service'
import { TimeBucketType } from '../../dto/getData.dto'
// getGroupFromTo does not touch `this`, so it can be exercised without
@@ -74,15 +78,59 @@ describe('traffic summary window (no explicit timeBucket)', () => {
expect(groupToUTC).toBe('2026-08-23 14:37:42')
})
- it('period=7d starts six days ago on an hour boundary', () => {
+ it('period=7d starts seven days ago on an hour boundary', () => {
const { bucket, groupFrom, groupFromUTC } = getSummaryWindow(
'7d',
'Etc/GMT',
)
expect(bucket).toBe(TimeBucketType.HOUR)
- expect(groupFrom).toBe('2026-08-17 14:00:00')
- expect(groupFromUTC).toBe('2026-08-17 14:00:00')
+ expect(groupFrom).toBe('2026-08-16 14:00:00')
+ expect(groupFromUTC).toBe('2026-08-16 14:00:00')
+ })
+
+ it('period=4w starts four weeks ago on a day boundary', () => {
+ const { bucket, groupFrom, groupFromUTC } = getSummaryWindow(
+ '4w',
+ 'Etc/GMT',
+ )
+
+ expect(bucket).toBe(TimeBucketType.DAY)
+ expect(groupFrom).toBe('2026-07-26 00:00:00')
+ expect(groupFromUTC).toBe('2026-07-26 00:00:00')
+ })
+
+ it('period=3M starts three months ago on a day boundary, not a month boundary', () => {
+ const { bucket, groupFrom, groupFromUTC } = getSummaryWindow(
+ '3M',
+ 'Etc/GMT',
+ )
+
+ expect(bucket).toBe(TimeBucketType.DAY)
+ expect(groupFrom).toBe('2026-05-23 00:00:00')
+ expect(groupFromUTC).toBe('2026-05-23 00:00:00')
+ })
+
+ it('period=12M starts twelve months ago on a day boundary', () => {
+ const { bucket, groupFrom, groupFromUTC } = getSummaryWindow(
+ '12M',
+ 'Etc/GMT',
+ )
+
+ expect(bucket).toBe(TimeBucketType.DAY)
+ expect(groupFrom).toBe('2025-08-23 00:00:00')
+ expect(groupFromUTC).toBe('2025-08-23 00:00:00')
+ })
+
+ it('period=24M starts on the month boundary twenty-four months back', () => {
+ const { bucket, groupFrom, groupFromUTC } = getSummaryWindow(
+ '24M',
+ 'Etc/GMT',
+ )
+
+ expect(bucket).toBe(TimeBucketType.MONTH)
+ expect(groupFrom).toBe('2024-08-01 00:00:00')
+ expect(groupFromUTC).toBe('2024-08-01 00:00:00')
})
it('period=today still starts at midnight', () => {
@@ -102,6 +150,15 @@ describe('traffic summary window (no explicit timeBucket)', () => {
)
})
+ it('3M and 12M resolve to a day bucket in both resolvers, keeping cards and chart aligned', () => {
+ for (const period of ['3M', '12M']) {
+ expect(getLowestPossibleTimeBucket(period)).toBe(TimeBucketType.DAY)
+ expect(
+ getSummaryTimeBucket(undefined, period, undefined, undefined),
+ ).toBe(TimeBucketType.DAY)
+ }
+ })
+
it('partial and invalid custom ranges keep the legacy fallback and are left to getGroupFromTo to validate', () => {
// Ranges must not be interpreted by getLowestPossibleTimeBucket: its NaN
// diff would throw a misleading range-length error before getGroupFromTo