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): preserve local bucket boundary in UTC

Volodymyr Vovchenkoce61ff79132e
diff --git a/backend/apps/cloud/src/analytics/analytics.service.ts b/backend/apps/cloud/src/analytics/analytics.service.ts
index 1325d6bd5..d658ed9c0 100644
--- a/backend/apps/cloud/src/analytics/analytics.service.ts
+++ b/backend/apps/cloud/src/analytics/analytics.service.ts
@@ -1226,7 +1226,10 @@ export class AnalyticsService {
)
}
- groupFromUTC = groupFrom.utc().startOf(timeBucket).format(formatFrom)
+ // groupFrom is already rounded to the bucket in the project's timezone;
+ // rounding again after the UTC conversion would shift the boundary for
+ // timezones with a non-whole-hour offset (e.g. Asia/Kathmandu)
+ groupFromUTC = groupFrom.utc().format(formatFrom)
groupToUTC = groupTo.utc().format(formatFrom)
} else {
throw new BadRequestException(
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 a0bff6c5b..87cf4215f 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
@@ -60,6 +60,20 @@ describe('traffic summary window (no explicit timeBucket)', () => {
expect(groupFromUTC).toBe('2026-08-22 14:00:00')
})
+ it('period=1d keeps the local hour boundary when converted to UTC for non-whole-hour offsets', () => {
+ // Asia/Kathmandu is UTC+05:45: the local 20:00 boundary is 14:15 UTC and
+ // must not be rounded again to 14:00 after the conversion
+ const { bucket, groupFrom, groupFromUTC, groupToUTC } = getSummaryWindow(
+ '1d',
+ 'Asia/Kathmandu',
+ )
+
+ expect(bucket).toBe(TimeBucketType.HOUR)
+ expect(groupFrom).toBe('2026-08-22 20:00:00')
+ expect(groupFromUTC).toBe('2026-08-22 14:15:00')
+ expect(groupToUTC).toBe('2026-08-23 14:37:42')
+ })
+
it('period=7d starts six days ago on an hour boundary', () => {
const { bucket, groupFrom, groupFromUTC } = getSummaryWindow(
'7d',
diff --git a/backend/apps/community/src/analytics/analytics.service.ts b/backend/apps/community/src/analytics/analytics.service.ts
index 2bdda0d70..df68c4aa5 100644
--- a/backend/apps/community/src/analytics/analytics.service.ts
+++ b/backend/apps/community/src/analytics/analytics.service.ts
@@ -1033,7 +1033,10 @@ export class AnalyticsService {
)
}
- groupFromUTC = groupFrom.utc().startOf(timeBucket).format(formatFrom)
+ // groupFrom is already rounded to the bucket in the project's timezone;
+ // rounding again after the UTC conversion would shift the boundary for
+ // timezones with a non-whole-hour offset (e.g. Asia/Kathmandu)
+ groupFromUTC = groupFrom.utc().format(formatFrom)
groupToUTC = groupTo.utc().format(formatFrom)
} else {
throw new BadRequestException(
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 a0bff6c5b..87cf4215f 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
@@ -60,6 +60,20 @@ describe('traffic summary window (no explicit timeBucket)', () => {
expect(groupFromUTC).toBe('2026-08-22 14:00:00')
})
+ it('period=1d keeps the local hour boundary when converted to UTC for non-whole-hour offsets', () => {
+ // Asia/Kathmandu is UTC+05:45: the local 20:00 boundary is 14:15 UTC and
+ // must not be rounded again to 14:00 after the conversion
+ const { bucket, groupFrom, groupFromUTC, groupToUTC } = getSummaryWindow(
+ '1d',
+ 'Asia/Kathmandu',
+ )
+
+ expect(bucket).toBe(TimeBucketType.HOUR)
+ expect(groupFrom).toBe('2026-08-22 20:00:00')
+ expect(groupFromUTC).toBe('2026-08-22 14:15:00')
+ expect(groupToUTC).toBe('2026-08-23 14:37:42')
+ })
+
it('period=7d starts six days ago on an hour boundary', () => {
const { bucket, groupFrom, groupFromUTC } = getSummaryWindow(
'7d',