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!

Session duration fixes

Blue Mousedc774a4cd6de
diff --git a/backend/apps/cloud/src/analytics/analytics.service.ts b/backend/apps/cloud/src/analytics/analytics.service.ts
index 389129374..6bd535e3d 100644
--- a/backend/apps/cloud/src/analytics/analytics.service.ts
+++ b/backend/apps/cloud/src/analytics/analytics.service.ts
@@ -197,6 +197,11 @@ interface RedisWithSessionScripts {
const sessionScripts = redis as unknown as RedisWithSessionScripts
const LIVE_SESSION_THRESHOLD_SECONDS = 120
+// The concurrency sweep fans every active minute out into a row per minute of
+// the liveness window, so cap how far back it can be reconstructed to keep the
+// scan bounded β€” period=all on an old project would otherwise read the entire
+// event history
+const MAX_CONCURRENCY_WINDOW_MINUTES = 366 * 24 * 60
const MAX_FILTERS = 100
const MAX_FILTER_VALUES = 100
// Journeys keep only the first {steps} distinct pages of a session, but the
@@ -4150,7 +4155,7 @@ export class AnalyticsService {
SELECT
psidCasted,
pid,
- sum(session_duration) as avg_duration,
+ sum(session_duration) as total_duration,
argMax(sessionProfileId, lastActivity) as profileId
FROM (
SELECT
@@ -4190,7 +4195,7 @@ export class AnalyticsService {
dsf.sessionStart,
dsf.lastActivity,
if(dateDiff('second', dsf.lastActivity, now()) < ${LIVE_SESSION_THRESHOLD_SECONDS}, 1, 0) AS isLive,
- sda.avg_duration AS sdur,
+ sda.total_duration AS sdur,
sda.profileId AS profileId,
if(startsWith(sda.profileId, '${AnalyticsService.PROFILE_PREFIX_USER}'), 1, 0) AS isIdentified,
if(fsp.firstPsid = dsf.psidCasted, 1, 0) AS isFirstSession
@@ -4318,7 +4323,7 @@ export class AnalyticsService {
SELECT
psidCasted,
pid,
- sum(session_duration) as avg_duration,
+ sum(session_duration) as total_duration,
argMax(sessionProfileId, lastActivity) as profileId
FROM (
SELECT
@@ -4358,7 +4363,7 @@ export class AnalyticsService {
dsf.sessionStart,
dsf.lastActivity,
if(dateDiff('second', dsf.lastActivity, now()) < ${LIVE_SESSION_THRESHOLD_SECONDS}, 1, 0) AS isLive,
- sda.avg_duration AS sdur,
+ sda.total_duration AS sdur,
sda.profileId AS profileId,
if(startsWith(sda.profileId, '${AnalyticsService.PROFILE_PREFIX_USER}'), 1, 0) AS isIdentified,
if(fsp.firstPsid = dsf.psidCasted, 1, 0) AS isFirstSession
@@ -4914,7 +4919,7 @@ export class AnalyticsService {
SELECT avgOrNull(duration) as sdur
FROM (
SELECT
- psid,
+ sid,
dateDiff('second', min(firstSeen), max(lastSeen)) as duration
FROM sessions
WHERE pid = {pid:FixedString(12)}
@@ -4926,7 +4931,7 @@ export class AnalyticsService {
AND psid IS NOT NULL
${filtersQuery}
)
- GROUP BY psid
+ GROUP BY sid
)
),
bounce_counts AS (
@@ -5088,7 +5093,7 @@ export class AnalyticsService {
AND created BETWEEN {groupFromUTC:String} AND {groupToUTC:String}
${currentFiltersQuery}
)
- GROUP BY psid
+ GROUP BY sid
)
),
bounce_counts AS (
@@ -5153,7 +5158,7 @@ export class AnalyticsService {
AND created BETWEEN {periodSubtracted:String} AND {groupFromUTC:String}
${previousFiltersQuery}
)
- GROUP BY psid
+ GROUP BY sid
)
),
bounce_counts AS (
@@ -6630,8 +6635,16 @@ export class AnalyticsService {
customEVFilterApplied,
)
+ // Concurrency is only computed when explicitly requested (the live visitors
+ // metric is off by default) and within a bounded window. It is derived from
+ // an unfiltered per-minute sweep over the events table, so it cannot respect
+ // dashboard filters β€” return zeros instead of misleading unfiltered data
const shouldComputeConcurrency =
- includeConcurrency && !customEVFilterApplied && !filtersQuery
+ includeConcurrency &&
+ !customEVFilterApplied &&
+ !filtersQuery &&
+ dayjs.utc(to).diff(dayjs.utc(from), 'minute') <=
+ MAX_CONCURRENCY_WINDOW_MINUTES
const [{ data }, concurrency] = await Promise.all([
clickhouse
@@ -8195,7 +8208,7 @@ export class AnalyticsService {
SELECT
psidCasted,
pid,
- sum(session_duration) as avg_duration,
+ sum(session_duration) as total_duration,
argMax(sessionProfileId, lastActivity) as profileId
FROM (
SELECT
@@ -8251,7 +8264,7 @@ export class AnalyticsService {
toFloat64(COALESCE(rt.refunds, toDecimal64(0, 4))) AS refunds,
dsf.sessionStart AS sessionStart,
dsf.lastActivity AS lastActivity,
- sda.avg_duration AS sdur,
+ sda.total_duration AS sdur,
coalesce(nullIf(sda.profileId, ''), dsf.profileId) AS profileId,
COALESCE(rs.replayCount, 0) AS replayCount,
if(
@@ -8261,7 +8274,7 @@ export class AnalyticsService {
isNull(rs.firstReplayTimestamp)
OR isNull(rs.lastReplayTimestamp)
OR rs.lastReplayTimestamp <= rs.firstReplayTimestamp,
- ifNull(sda.avg_duration, 0),
+ ifNull(sda.total_duration, 0),
intDiv(rs.lastReplayTimestamp - rs.firstReplayTimestamp, 1000)
)
) AS replayDuration,
@@ -8476,7 +8489,7 @@ export class AnalyticsService {
SELECT
psidCasted,
pid,
- sum(session_duration) as avg_duration,
+ sum(session_duration) as total_duration,
argMax(sessionProfileId, lastActivity) as profileId
FROM (
SELECT
@@ -8516,7 +8529,7 @@ export class AnalyticsService {
toFloat64(COALESCE(rt.refunds, toDecimal64(0, 4))) AS refunds,
fs.sessionStart AS sessionStart,
fs.lastActivity AS lastActivity,
- sda.avg_duration AS sdur,
+ sda.total_duration AS sdur,
coalesce(nullIf(sda.profileId, ''), fs.profileId) AS profileId
FROM replay_summary rs
INNER JOIN filtered_sessions fs ON rs.psidCasted = fs.psidCasted AND rs.pid = fs.pid
@@ -9402,7 +9415,7 @@ export class AnalyticsService {
SELECT
psidCasted,
pid,
- sum(session_duration) as avg_duration
+ sum(session_duration) as total_duration
FROM (
SELECT
CAST(psid, 'String') AS psidCasted,
@@ -9427,7 +9440,7 @@ export class AnalyticsService {
ps.sessionStart,
ps.lastActivity,
if(dateDiff('second', ps.lastActivity, now()) < ${LIVE_SESSION_THRESHOLD_SECONDS}, 1, 0) AS isLive,
- sda.avg_duration AS sdur
+ sda.total_duration AS sdur
FROM profile_sessions ps
LEFT JOIN pageview_counts pc ON ps.psidCasted = pc.psidCasted AND ps.pid = pc.pid
LEFT JOIN event_counts ec ON ps.psidCasted = ec.psidCasted AND ps.pid = ec.pid
diff --git a/backend/apps/cloud/src/analytics/v2/registry/metrics.ts b/backend/apps/cloud/src/analytics/v2/registry/metrics.ts
index 964eb8dfe..4f5d81254 100644
--- a/backend/apps/cloud/src/analytics/v2/registry/metrics.ts
+++ b/backend/apps/cloud/src/analytics/v2/registry/metrics.ts
@@ -29,10 +29,7 @@ export const V2_METRICS: V2MetricDef[] = [
{
api: 'sessions',
types: ['traffic'],
- sqlExpr: ({ customEVFilterApplied }) =>
- customEVFilterApplied
- ? 'count(*)'
- : 'count(DISTINCT coalesce(sid, psid))',
+ sqlExpr: () => 'count(DISTINCT coalesce(sid, psid))',
format: 'integer',
description:
'Session count. A session ends after 30 minutes of inactivity, so one visitor can have several in a day',
diff --git a/backend/apps/community/src/analytics/analytics.controller.ts b/backend/apps/community/src/analytics/analytics.controller.ts
index 5ca02528f..aeadcf4d8 100644
--- a/backend/apps/community/src/analytics/analytics.controller.ts
+++ b/backend/apps/community/src/analytics/analytics.controller.ts
@@ -1661,6 +1661,9 @@ export class AnalyticsController {
perfTransformed = eventTransformer({
type: 'performance',
+ psid,
+ sid,
+ profileId,
pid: logDTO.pid,
host: this.analyticsService.getHostFromOrigin(headers.origin),
pg: logDTO.pg,
diff --git a/backend/apps/community/src/analytics/analytics.service.ts b/backend/apps/community/src/analytics/analytics.service.ts
index 7bfd1edbf..b06e1cec4 100644
--- a/backend/apps/community/src/analytics/analytics.service.ts
+++ b/backend/apps/community/src/analytics/analytics.service.ts
@@ -180,6 +180,11 @@ interface RedisWithSessionScripts {
const sessionScripts = redis as unknown as RedisWithSessionScripts
const LIVE_SESSION_THRESHOLD_SECONDS = 120
+// The concurrency sweep fans every active minute out into a row per minute of
+// the liveness window, so cap how far back it can be reconstructed to keep the
+// scan bounded β€” period=all on an old project would otherwise read the entire
+// event history
+const MAX_CONCURRENCY_WINDOW_MINUTES = 366 * 24 * 60
const MAX_FILTERS = 100
const MAX_FILTER_VALUES = 100
// Journeys keep only the first {steps} distinct pages of a session, but the
@@ -2523,7 +2528,7 @@ export class AnalyticsService {
clickhouse_settings: { async_insert: 1 },
})
} catch (error) {
- console.error('Failed to record session:', error)
+ this.logger.error(`Failed to record session: ${error}`)
}
}
@@ -2960,7 +2965,7 @@ export class AnalyticsService {
SELECT
psidCasted,
pid,
- sum(session_duration) as avg_duration,
+ sum(session_duration) as total_duration,
argMax(sessionProfileId, lastActivity) as profileId
FROM (
SELECT
@@ -3000,7 +3005,7 @@ export class AnalyticsService {
dsf.sessionStart,
dsf.lastActivity,
if(dateDiff('second', toTimeZone(dsf.lastActivity, 'UTC'), toTimeZone(now(), 'UTC')) < ${LIVE_SESSION_THRESHOLD_SECONDS}, 1, 0) AS isLive,
- sda.avg_duration AS sdur,
+ sda.total_duration AS sdur,
sda.profileId AS profileId,
if(startsWith(sda.profileId, '${AnalyticsService.PROFILE_PREFIX_USER}'), 1, 0) AS isIdentified,
if(fsp.firstPsid = dsf.psidCasted, 1, 0) AS isFirstSession
@@ -3128,7 +3133,7 @@ export class AnalyticsService {
SELECT
psidCasted,
pid,
- sum(session_duration) as avg_duration,
+ sum(session_duration) as total_duration,
argMax(sessionProfileId, lastActivity) as profileId
FROM (
SELECT
@@ -3168,7 +3173,7 @@ export class AnalyticsService {
dsf.sessionStart,
dsf.lastActivity,
if(dateDiff('second', toTimeZone(dsf.lastActivity, 'UTC'), toTimeZone(now(), 'UTC')) < ${LIVE_SESSION_THRESHOLD_SECONDS}, 1, 0) AS isLive,
- sda.avg_duration AS sdur,
+ sda.total_duration AS sdur,
sda.profileId AS profileId,
if(startsWith(sda.profileId, '${AnalyticsService.PROFILE_PREFIX_USER}'), 1, 0) AS isIdentified,
if(fsp.firstPsid = dsf.psidCasted, 1, 0) AS isFirstSession
@@ -3494,7 +3499,7 @@ export class AnalyticsService {
SELECT avgOrNull(duration) as sdur
FROM (
SELECT
- psid,
+ sid,
dateDiff('second', min(firstSeen), max(lastSeen)) as duration
FROM sessions
WHERE pid = {pid:FixedString(12)}
@@ -3506,7 +3511,7 @@ export class AnalyticsService {
AND psid IS NOT NULL
${filtersQuery}
)
- GROUP BY psid
+ GROUP BY sid
)
),
bounce_counts AS (
@@ -3664,7 +3669,7 @@ export class AnalyticsService {
AND created BETWEEN {groupFromUTC:String} AND {groupToUTC:String}
${currentFiltersQuery}
)
- GROUP BY psid
+ GROUP BY sid
)
),
bounce_counts AS (
@@ -3729,7 +3734,7 @@ export class AnalyticsService {
AND created BETWEEN {periodSubtracted:String} AND {groupFromUTC:String}
${previousFiltersQuery}
)
- GROUP BY psid
+ GROUP BY sid
)
),
bounce_counts AS (
@@ -5167,8 +5172,16 @@ export class AnalyticsService {
customEVFilterApplied,
)
+ // Concurrency is only computed when explicitly requested (the live visitors
+ // metric is off by default) and within a bounded window. It is derived from
+ // an unfiltered per-minute sweep over the events table, so it cannot respect
+ // dashboard filters β€” return zeros instead of misleading unfiltered data
const shouldComputeConcurrency =
- includeConcurrency && !customEVFilterApplied && !filtersQuery
+ includeConcurrency &&
+ !customEVFilterApplied &&
+ !filtersQuery &&
+ dayjs.utc(to).diff(dayjs.utc(from), 'minute') <=
+ MAX_CONCURRENCY_WINDOW_MINUTES
const [{ data }, concurrency] = await Promise.all([
clickhouse
@@ -6598,7 +6611,7 @@ export class AnalyticsService {
SELECT
psidCasted,
pid,
- sum(session_duration) as avg_duration,
+ sum(session_duration) as total_duration,
argMax(sessionProfileId, lastActivity) as profileId
FROM (
SELECT
@@ -6638,7 +6651,7 @@ export class AnalyticsService {
COALESCE(errc.count, 0) AS errors,
dsf.sessionStart AS sessionStart,
dsf.lastActivity AS lastActivity,
- sda.avg_duration AS sdur,
+ sda.total_duration AS sdur,
coalesce(nullIf(sda.profileId, ''), dsf.profileId) AS profileId
FROM distinct_sessions_filtered dsf
LEFT JOIN pageview_counts pc ON dsf.psidCasted = pc.psidCasted AND dsf.pid = pc.pid
@@ -8138,7 +8151,7 @@ export class AnalyticsService {
SELECT
psidCasted,
pid,
- sum(session_duration) as avg_duration
+ sum(session_duration) as total_duration
FROM (
SELECT
CAST(psid, 'String') AS psidCasted,
@@ -8163,7 +8176,7 @@ export class AnalyticsService {
ps.sessionStart,
ps.lastActivity,
if(dateDiff('second', toTimeZone(ps.lastActivity, 'UTC'), toTimeZone(now(), 'UTC')) < ${LIVE_SESSION_THRESHOLD_SECONDS}, 1, 0) AS isLive,
- sda.avg_duration AS sdur
+ sda.total_duration AS sdur
FROM profile_sessions ps
LEFT JOIN pageview_counts pc ON ps.psidCasted = pc.psidCasted AND ps.pid = pc.pid
LEFT JOIN event_counts ec ON ps.psidCasted = ec.psidCasted AND ps.pid = ec.pid
diff --git a/backend/apps/community/src/analytics/utils/transformers.ts b/backend/apps/community/src/analytics/utils/transformers.ts
index 90e570788..e192424b0 100644
--- a/backend/apps/community/src/analytics/utils/transformers.ts
+++ b/backend/apps/community/src/analytics/utils/transformers.ts
@@ -75,18 +75,7 @@ interface ErrorEventOptions extends CommonOptions {
interface PerformanceOptions extends Omit<
CommonOptions,
- | 'psid'
- | 'profileId'
- | 'os'
- | 'osv'
- | 'lc'
- | 'ref'
- | 'so'
- | 'me'
- | 'ca'
- | 'te'
- | 'co'
- | 'meta'
+ 'os' | 'osv' | 'lc' | 'ref' | 'so' | 'me' | 'ca' | 'te' | 'co' | 'meta'
> {
type: 'performance'
dns: number
@@ -190,6 +179,9 @@ export const eventTransformer = (opts: EventTransformerOptions) => {
return {
type: 'performance' as const,
pid: opts.pid,
+ psid: opts.psid ?? null,
+ sid: opts.sid ?? null,
+ profileId: opts.profileId ?? null,
host: opts.host || null,
pg: opts.pg || null,
dv: opts.dv || null,
diff --git a/backend/apps/community/src/analytics/v2/registry/metrics.ts b/backend/apps/community/src/analytics/v2/registry/metrics.ts
index 964eb8dfe..4f5d81254 100644
--- a/backend/apps/community/src/analytics/v2/registry/metrics.ts
+++ b/backend/apps/community/src/analytics/v2/registry/metrics.ts
@@ -29,10 +29,7 @@ export const V2_METRICS: V2MetricDef[] = [
{
api: 'sessions',
types: ['traffic'],
- sqlExpr: ({ customEVFilterApplied }) =>
- customEVFilterApplied
- ? 'count(*)'
- : 'count(DISTINCT coalesce(sid, psid))',
+ sqlExpr: () => 'count(DISTINCT coalesce(sid, psid))',
format: 'integer',
description:
'Session count. A session ends after 30 minutes of inactivity, so one visitor can have several in a day',
diff --git a/backend/migrations/clickhouse/2026_08_01_session_id.js b/backend/migrations/clickhouse/2026_08_01_session_id.js
index 559149f09..52385a288 100644
--- a/backend/migrations/clickhouse/2026_08_01_session_id.js
+++ b/backend/migrations/clickhouse/2026_08_01_session_id.js
@@ -25,6 +25,19 @@ const queries = [
AND lastSeen > toDateTime(0)`,
`RENAME TABLE ${dbName}.sessions TO ${dbName}.sessions_by_visitor_day, ${dbName}.sessions_v2 TO ${dbName}.sessions`,
+
+ // The backfill above runs while the old code is still writing to the old
+ // table, so anything recorded between the INSERT and the RENAME would be
+ // lost. Replay the tail of the old table into the new one β€” ReplacingMergeTree
+ // dedupes on (pid, sid) and keeps the row with the greatest lastSeen. Only the
+ // last day is replayed: sessions expire after 30 minutes of inactivity, so
+ // nothing older than that can have changed during the migration.
+ `INSERT INTO ${dbName}.sessions (sid, psid, pid, profileId, firstSeen, lastSeen)
+ SELECT psid AS sid, psid, pid, profileId, firstSeen, lastSeen
+ FROM ${dbName}.sessions_by_visitor_day
+ WHERE lastSeen >= now() - INTERVAL 1 DAY
+ AND firstSeen <= lastSeen
+ AND lastSeen > toDateTime(0)`,
]
queriesRunner(queries)
diff --git a/docs/content/docs/api/stats-v2.mdx b/docs/content/docs/api/stats-v2.mdx
index b9b18acab..f797cfaf2 100644
--- a/docs/content/docs/api/stats-v2.mdx
+++ b/docs/content/docs/api/stats-v2.mdx
@@ -204,7 +204,7 @@ curl 'https://api.swetrix.com/v2/projects/YOUR_PROJECT_ID/traffic/timeseries?per
}
```
-The `concurrency` metric returns the number of concurrent live visitors over time (the "live visitors" series in the dashboard's traffic chart). A visitor counts as live for 5 minutes after their last pageview or custom event, which is the same rule the real-time live visitor counter uses. It is only computed when explicitly requested, and it cannot be segmented: when `filters` are applied the series is zero-filled. For the `minute` time bucket it is the exact number of visitors active during that minute; for larger buckets it is the peak within the bucket.
+The `concurrency` metric returns the number of concurrent live visitors over time (the "live visitors" series in the dashboard's traffic chart). A visitor counts as live for 5 minutes after their last pageview or custom event, which is the same rule the real-time live visitor counter uses. It is only computed when explicitly requested, and it cannot be segmented: when `filters` are applied, or the requested range exceeds one year, the series is zero-filled. For the `minute` time bucket it is the exact number of visitors active during that minute; for larger buckets it is the peak within the bucket.
### GET /v2/projects/:pid/traffic/breakdown
diff --git a/docs/content/docs/visitor-identification.mdx b/docs/content/docs/visitor-identification.mdx
index 0132d29f6..e14e626e5 100644
--- a/docs/content/docs/visitor-identification.mdx
+++ b/docs/content/docs/visitor-identification.mdx
@@ -49,7 +49,7 @@ unlinkable to the new ones. This design is what allows Swetrix to
operate without persistent identifiers on the visitor's device.
A **session** is a separate thing from either salt: it starts on a
-visitor's first pageview and ends after **30 minutes of inactivity**. Someone who
+visitor's first tracked event and ends after **30 minutes of inactivity**. Someone who
reads a page in the morning and comes back in the evening counts as one
visitor and **two sessions**. Because a session is bounded by inactivity
rather than by the salt, a session that is still running at UTC midnight