📈 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
proper session duration calculation
Blue Mouse98db908ddce7
diff --git a/backend/apps/cloud/src/analytics/analytics.service.ts b/backend/apps/cloud/src/analytics/analytics.service.tsindex f90ffcb40..4991ea31d 100644--- a/backend/apps/cloud/src/analytics/analytics.service.ts+++ b/backend/apps/cloud/src/analytics/analytics.service.ts@@ -3692,19 +3692,22 @@ export class AnalyticsService {const query = `SELECT- psidCasted AS psid,- any(cc) AS cc,- any(os) AS os,- any(br) AS br,+ events.psidCasted AS psid,+ any(events.cc) AS cc,+ any(events.os) AS os,+ any(events.br) AS br,count() AS pageviews,- min(created) AS sessionStart,- max(created) AS lastActivity,- if(dateDiff('second', max(created), now()) < ${LIVE_SESSION_THRESHOLD_SECONDS}, 1, 0) AS isLive+ min(events.created) AS sessionStart,+ max(events.created) AS lastActivity,+ if(dateDiff('second', max(events.created), now()) < ${LIVE_SESSION_THRESHOLD_SECONDS}, 1, 0) AS isLive,+ avg(durations.duration) AS sdurFROM(${customEVFilterApplied ? customEVSubquery : analyticsSubquery}- )- GROUP BY psid+ ) AS events+ LEFT JOIN session_durations AS durations+ ON events.psidCasted = CAST(durations.psid, 'String') AND durations.pid = {pid:FixedString(12)}+ GROUP BY events.psidCastedORDER BY sessionStart DESCLIMIT ${take}OFFSET ${skip}diff --git a/backend/apps/community/src/analytics/analytics.service.ts b/backend/apps/community/src/analytics/analytics.service.tsindex 0d6ef92bf..5e1da148e 100644--- a/backend/apps/community/src/analytics/analytics.service.ts+++ b/backend/apps/community/src/analytics/analytics.service.ts@@ -3379,19 +3379,22 @@ export class AnalyticsService {const query = `SELECT- psidCasted AS psid,- any(cc) AS cc,- any(os) AS os,- any(br) AS br,+ events.psidCasted AS psid,+ any(events.cc) AS cc,+ any(events.os) AS os,+ any(events.br) AS br,count() AS pageviews,- min(created) AS sessionStart,- max(created) AS lastActivity,- if(dateDiff('second', max(created), now()) < ${LIVE_SESSION_THRESHOLD_SECONDS}, 1, 0) AS isLive+ min(events.created) AS sessionStart,+ max(events.created) AS lastActivity,+ if(dateDiff('second', max(events.created), now()) < ${LIVE_SESSION_THRESHOLD_SECONDS}, 1, 0) AS isLive,+ avg(durations.duration) AS sdurFROM(${customEVFilterApplied ? customEVSubquery : analyticsSubquery}- )- GROUP BY psid+ ) AS events+ LEFT JOIN session_durations AS durations+ ON events.psidCasted = CAST(durations.psid, 'String') AND durations.pid = {pid:FixedString(12)}+ GROUP BY events.psidCastedORDER BY sessionStart DESCLIMIT ${take}OFFSET ${skip}diff --git a/web/app/pages/Project/View/components/Sessions.tsx b/web/app/pages/Project/View/components/Sessions.tsxindex 83863f18e..1306a7ab0 100644--- a/web/app/pages/Project/View/components/Sessions.tsx+++ b/web/app/pages/Project/View/components/Sessions.tsx@@ -2,7 +2,7 @@ import { ChevronRightIcon } from '@heroicons/react/24/outline'import dayjs from 'dayjs'import duration from 'dayjs/plugin/duration'import _map from 'lodash/map'-import React from 'react'+import React, { useMemo } from 'react'import { useTranslation } from 'react-i18next'import { Link } from 'react-router'import { ClientOnly } from 'remix-utils/client-only'@@ -40,19 +40,24 @@ const Session = ({ session, onClick, timeFormat }: SessionProps) => {i18n: { language },} = useTranslation('common')- const lastActivityTime = dayjs(session.lastActivity)const sessionStartTime = dayjs(session.sessionStart)let sessionDurationString = ''-if (!session.isLive) {- const diffSeconds = lastActivityTime.diff(sessionStartTime, 'seconds')-- sessionDurationString = getStringFromTime(getTimeFromSeconds(diffSeconds))+ if (session.sdur != null && session.sdur > 0) {+ sessionDurationString = getStringFromTime(getTimeFromSeconds(session.sdur))+ } else {+ const diffSeconds = dayjs(session.lastActivity).diff(sessionStartTime, 'seconds')+ // Only set duration string if it's meaningfully positive+ if (diffSeconds > 0) {+ sessionDurationString = getStringFromTime(getTimeFromSeconds(diffSeconds))+ }+ }}- const dateLineString = session.isLive- ? sessionStartTime.toDate().toLocaleDateString(language, {+ const dateLineString = useMemo(() => {+ if (session.isLive) {+ return sessionStartTime.toDate().toLocaleDateString(language, {day: 'numeric',month: 'short',year: 'numeric',@@ -60,18 +65,33 @@ const Session = ({ session, onClick, timeFormat }: SessionProps) => {minute: 'numeric',hourCycle: timeFormat === '12-hour' ? 'h12' : 'h23',})- : `${sessionStartTime.toDate().toLocaleDateString(language, {- day: 'numeric',- month: 'short',- year: 'numeric',- hour: 'numeric',- minute: 'numeric',- hourCycle: timeFormat === '12-hour' ? 'h12' : 'h23',- })} - ${lastActivityTime.toDate().toLocaleTimeString(language, {- hour: 'numeric',- minute: 'numeric',- hourCycle: timeFormat === '12-hour' ? 'h12' : 'h23',- })} (${sessionDurationString})`+ }++ const startDateTimeStr = sessionStartTime.toDate().toLocaleDateString(language, {+ day: 'numeric',+ month: 'short',+ year: 'numeric',+ hour: 'numeric',+ minute: 'numeric',+ hourCycle: timeFormat === '12-hour' ? 'h12' : 'h23',+ })++ let endDisplayTime+ if (session.sdur != null && session.sdur > 0) {+ endDisplayTime = sessionStartTime.add(session.sdur, 'seconds')+ } else {+ endDisplayTime = dayjs(session.lastActivity)+ }++ const endTimeStr = endDisplayTime.toDate().toLocaleTimeString(language, {+ hour: 'numeric',+ minute: 'numeric',+ hourCycle: timeFormat === '12-hour' ? 'h12' : 'h23',+ })++ const durationDisplay = sessionDurationString ? ` (${sessionDurationString})` : ''+ return `${startDateTimeStr} - ${endTimeStr}${durationDisplay}`+ }, [session, language, timeFormat, sessionDurationString, sessionStartTime])const psidUrl = new URL(window.location.href)psidUrl.searchParams.set('psid', session.psid)diff --git a/web/app/pages/Project/View/interfaces/session.ts b/web/app/pages/Project/View/interfaces/session.tsindex 74da2509b..d7d4b72ad 100644--- a/web/app/pages/Project/View/interfaces/session.ts+++ b/web/app/pages/Project/View/interfaces/session.ts@@ -6,6 +6,7 @@ export interface Session {pageviews: numbercreated: stringisLive: 1 | 0+ sdur?: numbersessionStart: stringlastActivity: string