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 summary overcounting pageviews for custom date ranges in non-UTC timezones

Blue Mouseb0c62c8a4d9d
diff --git a/backend/apps/cloud/src/analytics/analytics.service.ts b/backend/apps/cloud/src/analytics/analytics.service.ts
index 40908ce8d..bff6eb3d2 100644
--- a/backend/apps/cloud/src/analytics/analytics.service.ts
+++ b/backend/apps/cloud/src/analytics/analytics.service.ts
@@ -138,6 +138,8 @@ const isValidLocale = (lc: string): boolean => {
}
// 2 minutes
+const hasTimeComponent = (date: string): boolean => /\d{2}:\d{2}/.test(date)
+
const LIVE_SESSION_THRESHOLD_SECONDS = 120
// The concurrency sweep materializes one row per minute of the requested window,
// so cap how far back it can be reconstructed to keep the query bounded
@@ -1090,8 +1092,16 @@ export class AnalyticsService {
} else {
groupFrom = dayjs.utc(from)
groupTo = dayjs.utc(to)
- groupFromUTC = groupFrom.utc().startOf(timeBucket).format(formatFrom)
- groupToUTC = groupTo.utc().endOf(timeBucket).format(formatTo)
+ // Only date-only inputs are expanded to whole buckets; exact
+ // timestamps keep their bounds β€” rounding them to UTC buckets would
+ // widen a single local day picked in a non-UTC timezone into up to
+ // 48 hours of data
+ groupFromUTC = hasTimeComponent(from)
+ ? groupFrom.format(formatFrom)
+ : groupFrom.startOf(timeBucket).format(formatFrom)
+ groupToUTC = hasTimeComponent(to)
+ ? groupTo.format(formatTo)
+ : groupTo.endOf(timeBucket).format(formatTo)
}
} else if (!_isEmpty(period)) {
if (period === 'today') {
diff --git a/backend/apps/cloud/src/analytics/v2/__tests__/group-from-to.spec.ts b/backend/apps/cloud/src/analytics/v2/__tests__/group-from-to.spec.ts
new file mode 100644
index 000000000..a5d7ec4dd
--- /dev/null
+++ b/backend/apps/cloud/src/analytics/v2/__tests__/group-from-to.spec.ts
@@ -0,0 +1,73 @@
+import { AnalyticsService } from '../../analytics.service'
+import { TimeBucketType } from '../../dto/getData.dto'
+
+// getGroupFromTo does not touch `this`, so it can be exercised without
+// instantiating the service and its DI graph
+const getGroupFromTo = AnalyticsService.prototype.getGroupFromTo.bind(
+ null as unknown as AnalyticsService,
+)
+
+describe('getGroupFromTo (custom from/to ranges)', () => {
+ it('keeps exact bounds for full timestamps β€” a single local day picked in a non-UTC timezone must not widen to whole UTC days', () => {
+ // Europe/Rome user picks July 15 as a single day; the dashboard sends
+ // the local day boundaries as UTC ISO timestamps
+ const result = getGroupFromTo(
+ '2026-07-14T22:00:00.000Z',
+ '2026-07-15T21:59:59.999Z',
+ TimeBucketType.DAY,
+ undefined as unknown as string,
+ 'Europe/Rome',
+ undefined,
+ false,
+ )
+
+ expect(result.groupFromUTC).toBe('2026-07-14 22:00:00')
+ expect(result.groupToUTC).toBe('2026-07-15 21:59:59')
+ })
+
+ it('keeps exact bounds for full timestamps with an hour bucket', () => {
+ const result = getGroupFromTo(
+ '2026-07-14T22:00:00.000Z',
+ '2026-07-15T21:59:59.999Z',
+ TimeBucketType.HOUR,
+ undefined as unknown as string,
+ 'Europe/Rome',
+ undefined,
+ false,
+ )
+
+ expect(result.groupFromUTC).toBe('2026-07-14 22:00:00')
+ expect(result.groupToUTC).toBe('2026-07-15 21:59:59')
+ })
+
+ it('expands date-only ranges to whole buckets', () => {
+ const result = getGroupFromTo(
+ '2026-07-10',
+ '2026-07-12',
+ TimeBucketType.DAY,
+ undefined as unknown as string,
+ 'Europe/Rome',
+ undefined,
+ false,
+ )
+
+ expect(result.groupFromUTC).toBe('2026-07-10 00:00:00')
+ expect(result.groupToUTC).toBe('2026-07-12 23:59:59')
+ })
+
+ it('expands an identical date-only from/to pair to a full day in the given timezone', () => {
+ const result = getGroupFromTo(
+ '2026-07-12',
+ '2026-07-12',
+ TimeBucketType.HOUR,
+ undefined as unknown as string,
+ 'Europe/Rome',
+ undefined,
+ false,
+ )
+
+ // Rome is UTC+2 in July
+ expect(result.groupFromUTC).toBe('2026-07-11 22:00:00')
+ expect(result.groupToUTC).toBe('2026-07-12 21:59:59')
+ })
+})
diff --git a/backend/apps/community/src/analytics/analytics.service.ts b/backend/apps/community/src/analytics/analytics.service.ts
index c5540b7f6..45f2a3b02 100644
--- a/backend/apps/community/src/analytics/analytics.service.ts
+++ b/backend/apps/community/src/analytics/analytics.service.ts
@@ -121,6 +121,8 @@ const isValidLocale = (lc: string): boolean => {
}
// 2 minutes
+const hasTimeComponent = (date: string): boolean => /\d{2}:\d{2}/.test(date)
+
const LIVE_SESSION_THRESHOLD_SECONDS = 120
// The concurrency sweep materializes one row per minute of the requested window,
// so cap how far back it can be reconstructed to keep the query bounded
@@ -893,8 +895,16 @@ export class AnalyticsService {
} else {
groupFrom = dayjs.utc(from)
groupTo = dayjs.utc(to)
- groupFromUTC = groupFrom.utc().startOf(timeBucket).format(formatFrom)
- groupToUTC = groupTo.utc().endOf(timeBucket).format(formatTo)
+ // Only date-only inputs are expanded to whole buckets; exact
+ // timestamps keep their bounds β€” rounding them to UTC buckets would
+ // widen a single local day picked in a non-UTC timezone into up to
+ // 48 hours of data
+ groupFromUTC = hasTimeComponent(from)
+ ? groupFrom.format(formatFrom)
+ : groupFrom.startOf(timeBucket).format(formatFrom)
+ groupToUTC = hasTimeComponent(to)
+ ? groupTo.format(formatTo)
+ : groupTo.endOf(timeBucket).format(formatTo)
}
} else if (!_isEmpty(period)) {
if (period === 'today') {
diff --git a/backend/apps/community/src/analytics/v2/__tests__/group-from-to.spec.ts b/backend/apps/community/src/analytics/v2/__tests__/group-from-to.spec.ts
new file mode 100644
index 000000000..a5d7ec4dd
--- /dev/null
+++ b/backend/apps/community/src/analytics/v2/__tests__/group-from-to.spec.ts
@@ -0,0 +1,73 @@
+import { AnalyticsService } from '../../analytics.service'
+import { TimeBucketType } from '../../dto/getData.dto'
+
+// getGroupFromTo does not touch `this`, so it can be exercised without
+// instantiating the service and its DI graph
+const getGroupFromTo = AnalyticsService.prototype.getGroupFromTo.bind(
+ null as unknown as AnalyticsService,
+)
+
+describe('getGroupFromTo (custom from/to ranges)', () => {
+ it('keeps exact bounds for full timestamps β€” a single local day picked in a non-UTC timezone must not widen to whole UTC days', () => {
+ // Europe/Rome user picks July 15 as a single day; the dashboard sends
+ // the local day boundaries as UTC ISO timestamps
+ const result = getGroupFromTo(
+ '2026-07-14T22:00:00.000Z',
+ '2026-07-15T21:59:59.999Z',
+ TimeBucketType.DAY,
+ undefined as unknown as string,
+ 'Europe/Rome',
+ undefined,
+ false,
+ )
+
+ expect(result.groupFromUTC).toBe('2026-07-14 22:00:00')
+ expect(result.groupToUTC).toBe('2026-07-15 21:59:59')
+ })
+
+ it('keeps exact bounds for full timestamps with an hour bucket', () => {
+ const result = getGroupFromTo(
+ '2026-07-14T22:00:00.000Z',
+ '2026-07-15T21:59:59.999Z',
+ TimeBucketType.HOUR,
+ undefined as unknown as string,
+ 'Europe/Rome',
+ undefined,
+ false,
+ )
+
+ expect(result.groupFromUTC).toBe('2026-07-14 22:00:00')
+ expect(result.groupToUTC).toBe('2026-07-15 21:59:59')
+ })
+
+ it('expands date-only ranges to whole buckets', () => {
+ const result = getGroupFromTo(
+ '2026-07-10',
+ '2026-07-12',
+ TimeBucketType.DAY,
+ undefined as unknown as string,
+ 'Europe/Rome',
+ undefined,
+ false,
+ )
+
+ expect(result.groupFromUTC).toBe('2026-07-10 00:00:00')
+ expect(result.groupToUTC).toBe('2026-07-12 23:59:59')
+ })
+
+ it('expands an identical date-only from/to pair to a full day in the given timezone', () => {
+ const result = getGroupFromTo(
+ '2026-07-12',
+ '2026-07-12',
+ TimeBucketType.HOUR,
+ undefined as unknown as string,
+ 'Europe/Rome',
+ undefined,
+ false,
+ )
+
+ // Rome is UTC+2 in July
+ expect(result.groupFromUTC).toBe('2026-07-11 22:00:00')
+ expect(result.groupToUTC).toBe('2026-07-12 21:59:59')
+ })
+})