π 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!
TypeScript chartsclickhousedockergdprgoogle-analyticsmetricsmonitoringmysqlnestjsprivacyredisself-hostedselfhostedstatisticstailwindcssweb-analyticswebanalyticswebsite
feat: Live visitors visualisation over time
Blue Mouseecdbf81d3603
diff --git a/backend/apps/cloud/src/analytics/analytics.service.ts b/backend/apps/cloud/src/analytics/analytics.service.tsindex 64d386e00..78112af9a 100644--- a/backend/apps/cloud/src/analytics/analytics.service.ts+++ b/backend/apps/cloud/src/analytics/analytics.service.ts@@ -5646,6 +5646,84 @@ export class AnalyticsService {}}+ // Reconstructs the number of concurrent (live) visitors for each display bucket+ // from the sessions table intervals [firstSeen, lastSeen] using a sweep-line:+ // +1 at each session's start minute, -1 right after its end minute, a zero-delta+ // "spine" row for every minute in the requested window, then a running sum gives+ // exact per-minute concurrency. Buckets larger than a minute report the peak+ // concurrency within the bucket.+ generateConcurrencyAggregationQuery(timeBucket: TimeBucketType): string {+ const timeBucketFunc = timeBucketConversion[timeBucket]+ const [selector, groupBy] = this.getGroupSubquery(timeBucket)++ return `+ WITH+ parseDateTimeBestEffort({groupFrom:String}) AS fromTs,+ parseDateTimeBestEffort({groupTo:String}) AS toTs,+ toStartOfMinute(fromTs) AS fromMinute,+ toStartOfMinute(toTs) AS toMinute,+ session_intervals AS (+ SELECT+ greatest(min(firstSeen), fromTs) AS sessionStart,+ least(max(lastSeen), toTs) AS sessionEnd+ FROM sessions+ WHERE+ pid = {pid:FixedString(12)}+ AND firstSeen <= toTs+ AND lastSeen >= fromTs+ GROUP BY psid+ )+ SELECT+ ${selector},+ toInt32(max(concurrency)) AS concurrency+ FROM (+ SELECT+ ${timeBucketFunc}(toTimeZone(m, {timezone:String})) AS tz_created,+ concurrency+ FROM (+ SELECT+ m,+ sum(minute_delta) OVER (ORDER BY m ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS concurrency+ FROM (+ SELECT m, sum(delta) AS minute_delta+ FROM (+ SELECT toStartOfMinute(sessionStart) AS m, toInt32(1) AS delta+ FROM session_intervals+ UNION ALL+ SELECT toStartOfMinute(sessionEnd) + INTERVAL 1 MINUTE AS m, toInt32(-1) AS delta+ FROM session_intervals+ UNION ALL+ SELECT fromMinute + toIntervalMinute(number) AS m, toInt32(0) AS delta+ FROM numbers(toUInt64(dateDiff('minute', fromMinute, toMinute)) + 1)+ )+ GROUP BY m+ )+ )+ WHERE m BETWEEN fromMinute AND toMinute+ )+ GROUP BY ${groupBy}+ ORDER BY ${groupBy}+ `+ }++ async getConcurrencyChartData(+ timeBucket: TimeBucketType,+ paramsData: any,+ safeTimezone: string,+ xShifted: string[],+ ): Promise<number[]> {+ const query = this.generateConcurrencyAggregationQuery(timeBucket)++ const { data } = await clickhouse+ .query({+ query,+ query_params: { ...paramsData.params, timezone: safeTimezone },+ })+ .then((resultSet) => resultSet.json<any>())++ return this.extractCountsForSessionChart(data, xShifted, 'concurrency')+ }+async groupChartByTimeBucket(timeBucket: TimeBucketType,from: string,@@ -5665,14 +5743,28 @@ export class AnalyticsService {customEVFilterApplied,)- const { data } = await clickhouse- .query({- query,- query_params: { ...paramsData.params, timezone: safeTimezone },- })- .then((resultSet) =>- resultSet.json<TrafficCHResponse | TrafficCEFilterCHResponse>(),- )+ // The sessions table has no dimension columns, so concurrency cannot respect+ // dashboard filters β return zeros instead of misleading unfiltered data+ const shouldComputeConcurrency = !customEVFilterApplied && !filtersQuery++ const [{ data }, concurrency] = await Promise.all([+ clickhouse+ .query({+ query,+ query_params: { ...paramsData.params, timezone: safeTimezone },+ })+ .then((resultSet) =>+ resultSet.json<TrafficCHResponse | TrafficCEFilterCHResponse>(),+ ),+ shouldComputeConcurrency+ ? this.getConcurrencyChartData(+ timeBucket,+ paramsData,+ safeTimezone,+ xShifted,+ )+ : Promise.resolve(Array(xShifted.length).fill(0)),+ ])const { visits, uniques, sdur, bounces } =this.extractAnalyticsChartDataForScope(@@ -5689,6 +5781,7 @@ export class AnalyticsService {uniques,sdur,bounces,+ concurrency,},})}diff --git a/backend/apps/community/src/analytics/analytics.service.ts b/backend/apps/community/src/analytics/analytics.service.tsindex 6d4b2ddaa..c7e708d97 100644--- a/backend/apps/community/src/analytics/analytics.service.ts+++ b/backend/apps/community/src/analytics/analytics.service.ts@@ -4194,6 +4194,84 @@ export class AnalyticsService {}}+ // Reconstructs the number of concurrent (live) visitors for each display bucket+ // from the sessions table intervals [firstSeen, lastSeen] using a sweep-line:+ // +1 at each session's start minute, -1 right after its end minute, a zero-delta+ // "spine" row for every minute in the requested window, then a running sum gives+ // exact per-minute concurrency. Buckets larger than a minute report the peak+ // concurrency within the bucket.+ generateConcurrencyAggregationQuery(timeBucket: TimeBucketType): string {+ const timeBucketFunc = timeBucketConversion[timeBucket]+ const [selector, groupBy] = this.getGroupSubquery(timeBucket)++ return `+ WITH+ parseDateTimeBestEffort({groupFrom:String}) AS fromTs,+ parseDateTimeBestEffort({groupTo:String}) AS toTs,+ toStartOfMinute(fromTs) AS fromMinute,+ toStartOfMinute(toTs) AS toMinute,+ session_intervals AS (+ SELECT+ greatest(min(firstSeen), fromTs) AS sessionStart,+ least(max(lastSeen), toTs) AS sessionEnd+ FROM sessions+ WHERE+ pid = {pid:FixedString(12)}+ AND firstSeen <= toTs+ AND lastSeen >= fromTs+ GROUP BY psid+ )+ SELECT+ ${selector},+ toInt32(max(concurrency)) AS concurrency+ FROM (+ SELECT+ ${timeBucketFunc}(toTimeZone(m, {timezone:String})) AS tz_created,+ concurrency+ FROM (+ SELECT+ m,+ sum(minute_delta) OVER (ORDER BY m ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS concurrency+ FROM (+ SELECT m, sum(delta) AS minute_delta+ FROM (+ SELECT toStartOfMinute(sessionStart) AS m, toInt32(1) AS delta+ FROM session_intervals+ UNION ALL+ SELECT toStartOfMinute(sessionEnd) + INTERVAL 1 MINUTE AS m, toInt32(-1) AS delta+ FROM session_intervals+ UNION ALL+ SELECT fromMinute + toIntervalMinute(number) AS m, toInt32(0) AS delta+ FROM numbers(toUInt64(dateDiff('minute', fromMinute, toMinute)) + 1)+ )+ GROUP BY m+ )+ )+ WHERE m BETWEEN fromMinute AND toMinute+ )+ GROUP BY ${groupBy}+ ORDER BY ${groupBy}+ `+ }++ async getConcurrencyChartData(+ timeBucket: TimeBucketType,+ paramsData: any,+ safeTimezone: string,+ xShifted: string[],+ ): Promise<number[]> {+ const query = this.generateConcurrencyAggregationQuery(timeBucket)++ const { data } = await clickhouse+ .query({+ query,+ query_params: { ...paramsData.params, timezone: safeTimezone },+ })+ .then((resultSet) => resultSet.json<any>())++ return this.extractCountsForSessionChart(data, xShifted, 'concurrency')+ }+async groupChartByTimeBucket(timeBucket: TimeBucketType,from: string,@@ -4213,14 +4291,28 @@ export class AnalyticsService {customEVFilterApplied,)- const { data } = await clickhouse- .query({- query,- query_params: { ...paramsData.params, timezone: safeTimezone },- })- .then((resultSet) =>- resultSet.json<TrafficCHResponse | TrafficCEFilterCHResponse>(),- )+ // The sessions table has no dimension columns, so concurrency cannot respect+ // dashboard filters β return zeros instead of misleading unfiltered data+ const shouldComputeConcurrency = !customEVFilterApplied && !filtersQuery++ const [{ data }, concurrency] = await Promise.all([+ clickhouse+ .query({+ query,+ query_params: { ...paramsData.params, timezone: safeTimezone },+ })+ .then((resultSet) =>+ resultSet.json<TrafficCHResponse | TrafficCEFilterCHResponse>(),+ ),+ shouldComputeConcurrency+ ? this.getConcurrencyChartData(+ timeBucket,+ paramsData,+ safeTimezone,+ xShifted,+ )+ : Promise.resolve(Array(xShifted.length).fill(0)),+ ])const { visits, uniques, sdur, bounces } =this.extractAnalyticsChartDataForScope(@@ -4237,6 +4329,7 @@ export class AnalyticsService {uniques,sdur,bounces,+ concurrency,},})}diff --git a/web/app/api/api.server.ts b/web/app/api/api.server.tsindex 32c0bab14..2bf923386 100644--- a/web/app/api/api.server.ts+++ b/web/app/api/api.server.ts@@ -740,6 +740,7 @@ export interface TrafficLogResponse {uniques: number[]sdur: number[]bounces?: number[]+ concurrency?: number[]}customs: Record<string, number>properties: Record<string, number>diff --git a/web/app/pages/Project/View/ViewProject.helpers.tsx b/web/app/pages/Project/View/ViewProject.helpers.tsxindex bd2dea069..a246de679 100644--- a/web/app/pages/Project/View/ViewProject.helpers.tsx+++ b/web/app/pages/Project/View/ViewProject.helpers.tsx@@ -251,6 +251,7 @@ const CHART_METRICS_MAPPING = {viewsPerUnique: 'viewsPerUnique',trendlines: 'trendlines',sessionDuration: 'sessionDuration',+ liveVisitors: 'liveVisitors',customEvents: 'customEvents',cumulativeMode: 'cumulativeMode',revenue: 'revenue',@@ -289,6 +290,7 @@ const getColumns = (unique,trendlines,sessionDuration,+ liveVisitors,occurrences,avgResponseTime,} = activeChartMetrics@@ -363,6 +365,14 @@ const getColumns = (}}+ if (liveVisitors && chart.concurrency) {+ columns.push(['liveVisitors', ...chart.concurrency])++ if (compareChart?.concurrency) {+ columns.push(['liveVisitorsCompare', ...compareChart.concurrency])+ }+ }+// Revenue data (if available) - revenue as bars with refunds stacked on topif (activeChartMetrics.revenue && chart.revenue) {columns.push(['revenue', ...chart.revenue])@@ -665,6 +675,11 @@ const getSettings = (...chart.occurrences.filter((n) => n !== undefined && n !== null),)}+ if (activeChartMetrics.liveVisitors && chart.concurrency) {+ allYValues.push(+ ...chart.concurrency.filter((n) => n !== undefined && n !== null),+ )+ }const optimalTicks =allYValues.length > 0 ? calculateOptimalTicks(allYValues) : undefined@@ -719,6 +734,7 @@ const getSettings = (total: [regionObj],bounce: [regionObj],viewsPerUnique: [regionObj],+ liveVisitors: [regionObj],}}@@ -750,6 +766,8 @@ const getSettings = (sessionDuration: chartType === chartTypes.line ? spline() : bar(),sessionDurationCompare:chartType === chartTypes.line ? spline() : bar(),+ liveVisitors: chartType === chartTypes.line ? spline() : bar(),+ liveVisitorsCompare: chartType === chartTypes.line ? spline() : bar(),// Revenue is always bars (stacked with refunds)revenue: bar(),refundsAmount: bar(),@@ -766,6 +784,8 @@ const getSettings = (trendlineTotal: '#eba14b',sessionDuration: '#c945ed',sessionDurationCompare: 'rgba(201, 69, 237, 0.4)',+ liveVisitors: '#16A34A',+ liveVisitorsCompare: 'rgba(22, 163, 74, 0.4)',revenue: '#ea580c', // orange-600 for net revenuerefundsAmount: 'rgba(234, 88, 12, 0.25)', // light orange fill for refunds overlay...customEventsColors,@@ -854,6 +874,7 @@ const getSettings = (unique: 'uniques',total: 'visits',sessionDuration: 'sdur',+ liveVisitors: 'concurrency',}if (_isEmpty(compareChart)) {@@ -960,7 +981,8 @@ const getSettings = (el.id !== 'uniqueCompare' &&el.id !== 'totalCompare' &&el.id !== 'bounceCompare' &&- el.id !== 'sessionDurationCompare',+ el.id !== 'sessionDurationCompare' &&+ el.id !== 'liveVisitorsCompare',)// Build current period section@@ -1049,6 +1071,7 @@ const getSettings = ('totalCompare','bounceCompare','sessionDurationCompare',+ 'liveVisitorsCompare','refundsAmount',],},diff --git a/web/app/pages/Project/tabs/Traffic/TrafficView.tsx b/web/app/pages/Project/tabs/Traffic/TrafficView.tsxindex 656886671..cfe68d217 100644--- a/web/app/pages/Project/tabs/Traffic/TrafficView.tsx+++ b/web/app/pages/Project/tabs/Traffic/TrafficView.tsx@@ -415,6 +415,7 @@ const TrafficViewInner = ({[CHART_METRICS_MAPPING.unique]: true,[CHART_METRICS_MAPPING.views]: false,[CHART_METRICS_MAPPING.sessionDuration]: false,+ [CHART_METRICS_MAPPING.liveVisitors]: false,[CHART_METRICS_MAPPING.bounce]: false,[CHART_METRICS_MAPPING.viewsPerUnique]: false,[CHART_METRICS_MAPPING.trendlines]: false,@@ -553,6 +554,15 @@ const TrafficViewInner = ({CHART_METRICS_MAPPING.revenue,],},+ {+ id: CHART_METRICS_MAPPING.liveVisitors,+ label: t('dashboard.liveVisitors'),+ active: activeChartMetrics[CHART_METRICS_MAPPING.liveVisitors],+ // Concurrency is reconstructed from session intervals which carry no+ // dimension data, so it cannot respect dashboard filters+ disabled: !_isEmpty(filters),+ disabledTooltip: t('project.liveVisitorsFilterConflict'),+ },{id: CHART_METRICS_MAPPING.viewsPerUnique,label: t('dashboard.viewsPerUnique'),@@ -574,7 +584,7 @@ const TrafficViewInner = ({active: activeChartMetrics[CHART_METRICS_MAPPING.customEvents],},].filter(Boolean),- [t, activeChartMetrics, isRevenueConnected],+ [t, activeChartMetrics, isRevenueConnected, filters],)const chartMetricsCustomEvents = useMemo(() => {@@ -608,6 +618,7 @@ const TrafficViewInner = ({trendlineUnique: t('project.trendlineUnique'),occurrences: t('project.occurrences'),sessionDuration: t('dashboard.sessionDuration'),+ liveVisitors: t('dashboard.liveVisitors'),revenue: t('dashboard.revenue'),refundsAmount: t('dashboard.refunds'),...dataNamesCustomEvents,@@ -670,6 +681,21 @@ const TrafficViewInner = ({[isConflicted, t],)+ // Live visitors cannot be filtered β turn it off if a filter gets applied+ // while it's active (the toggle itself is disabled in that state)+ useEffect(() => {+ if (+ !_isEmpty(filters) &&+ activeChartMetrics[CHART_METRICS_MAPPING.liveVisitors]+ ) {+ setActiveChartMetrics((prev) => ({+ ...prev,+ [CHART_METRICS_MAPPING.liveVisitors]: false,+ }))+ }+ // eslint-disable-next-line react-hooks/exhaustive-deps+ }, [filters])+const switchCustomEventChart = useCallback((eventId: string) => {const newParams = new URLSearchParams(searchParams.toString())@@ -973,19 +999,30 @@ const TrafficViewInner = ({)}- return (+ const checkbox = (<Checkboxclasses={{label: 'p-2',}}label={label}- disabled={conflicted}+ disabled={conflicted || pair.disabled}checked={active}onChange={() => {switchTrafficChartMetric(pairID, conflicts)}}/>)++ if (pair.disabled && pair.disabledTooltip) {+ return (+ <Tooltip+ text={pair.disabledTooltip}+ tooltipNode={checkbox}+ />+ )+ }++ return checkbox}}buttonClassName='!p-1.5 rounded-md border border-transparent hover:border-gray-300 hover:bg-white hover:dark:border-slate-700/80 hover:dark:bg-slate-950 dark:focus:ring-slate-300'selectItemClassName='p-0'@@ -997,6 +1034,10 @@ const TrafficViewInner = ({e?.stopPropagation()e?.preventDefault()+ if (pair.disabled) {+ return+ }+if (pairID !== CHART_METRICS_MAPPING.customEvents) {switchTrafficChartMetric(pairID, conflicts)}diff --git a/web/public/locales/en.json b/web/public/locales/en.jsonindex a1c556c4e..abd55be07 100644--- a/web/public/locales/en.json+++ b/web/public/locales/en.json@@ -2196,6 +2196,7 @@"customEvMetadata": "Custom events metadata","pageviewMetadata": "Pageview metadata","conflictMetric": "The metric you selected conflicts with an already selected one",+ "liveVisitorsFilterConflict": "Live visitors can't be shown while dashboard filters are applied","prev": "Previous","next": "Next","results": "Results",