📈 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
Eliminate Redis from deriving session ID
Blue Mousee5e04374f74b
diff --git a/backend/apps/cloud/src/analytics/analytics.controller.ts b/backend/apps/cloud/src/analytics/analytics.controller.tsindex d97b8b86e..5aeb7a373 100644--- a/backend/apps/cloud/src/analytics/analytics.controller.ts+++ b/backend/apps/cloud/src/analytics/analytics.controller.ts@@ -55,7 +55,6 @@ import {REDIS_TRIALS_COUNT_KEY,REDIS_PROJECTS_COUNT_KEY,REDIS_EVENTS_COUNT_KEY,- UNIQUE_SESSION_LIFE_TIME,} from '../common/constants'import { clickhouse } from '../common/integrations/clickhouse'import {@@ -1343,8 +1342,11 @@ export class AnalyticsController {await this.analyticsService.validateHeartbeat(logDTO, origin, ip)- const { exists, psid, sessionHash } =- await this.analyticsService.getSessionId(pid, userAgent, ip)+ const { exists, psid } = await this.analyticsService.getSessionId(+ pid,+ userAgent,+ ip,+ )if (!exists) {throw new ForbiddenException(@@ -1352,7 +1354,7 @@ export class AnalyticsController {)}- await redis.set(sessionHash, psid, 'EX', UNIQUE_SESSION_LIFE_TIME)+ await this.analyticsService.extendSessionTTL(psid)await this.analyticsService.processInteractionSD(psid, pid)diff --git a/backend/apps/cloud/src/analytics/analytics.service.ts b/backend/apps/cloud/src/analytics/analytics.service.tsindex c0c7bbff0..2248c2493 100644--- a/backend/apps/cloud/src/analytics/analytics.service.ts+++ b/backend/apps/cloud/src/analytics/analytics.service.ts@@ -728,16 +728,6 @@ export class AnalyticsService {}}- async getSessionHash(- pid: string,- userAgent: string,- ip: string,- ): Promise<string> {- const salt = await this.saltService.getSaltForProject(pid)-- return `ses_${hash(`${userAgent}${ip}${pid}${salt}`)}`- }-async isSessionDurationOpen(sdKey: string): Promise<[string, boolean]> {const sd = await redis.get(sdKey)return [sd, Boolean(sd)]@@ -1125,45 +1115,60 @@ export class AnalyticsService {]}- generateUInt64(): string {- return crypto.randomBytes(8).readBigUInt64BE(0).toString()+ derivePsidFromInputs(+ pid: string,+ userAgent: string,+ ip: string,+ salt: string,+ ): string {+ const combined = `${userAgent}${ip}${pid}${salt}`+ return crypto+ .createHash('sha256')+ .update(combined)+ .digest()+ .readBigUInt64BE(0)+ .toString()+ }++ getSessionKey(psid: string): string {+ return `ses:${psid}`}async getSessionId(pid: string,userAgent: string,ip: string,- ): Promise<{ exists: boolean; psid: string; sessionHash: string }> {- const sessionHash = await this.getSessionHash(pid, userAgent, ip)-- let psid = await redis.get(sessionHash)- const exists = Boolean(psid)+ ): Promise<{ exists: boolean; psid: string }> {+ const salt = await this.saltService.getSaltForProject(pid)+ const psid = this.derivePsidFromInputs(pid, userAgent, ip, salt)+ const sessionKey = this.getSessionKey(psid)- if (!exists) {- psid = this.generateUInt64()- }+ const exists = Boolean(await redis.exists(sessionKey))- return { exists, psid, sessionHash }+ return { exists, psid }}/**- * Checks if the session is unique and returns the session psid (or creates a new one)+ * Checks if the session is unique and returns the session psid.+ * The psid is derived deterministically from inputs - no storage needed.*/async generateAndStoreSessionId(pid: string,userAgent: string,ip: string,): Promise<[boolean, string]> {- const { exists, psid, sessionHash } = await this.getSessionId(- pid,- userAgent,- ip,- )+ const { exists, psid } = await this.getSessionId(pid, userAgent, ip)+ const sessionKey = this.getSessionKey(psid)- await redis.set(sessionHash, psid, 'EX', UNIQUE_SESSION_LIFE_TIME)+ await redis.set(sessionKey, '1', 'EX', UNIQUE_SESSION_LIFE_TIME)return [!exists, psid]}+ async extendSessionTTL(psid: string): Promise<void> {+ const sessionKey = this.getSessionKey(psid)+ await redis.set(sessionKey, '1', 'EX', UNIQUE_SESSION_LIFE_TIME)+ }+formatFunnel(data: IFunnelCHResponse[], pages: string[]): IFunnel[] {const funnel = _map(data, (row, index) => {const value = pages[index]diff --git a/backend/apps/community/src/analytics/analytics.controller.ts b/backend/apps/community/src/analytics/analytics.controller.tsindex 4d63136c6..de8c9c674 100644--- a/backend/apps/community/src/analytics/analytics.controller.ts+++ b/backend/apps/community/src/analytics/analytics.controller.ts@@ -47,7 +47,6 @@ import { GetPagePropertyMetaDto } from './dto/get-page-property-meta.dto'import { GetUserFlowDto } from './dto/getUserFlow.dto'import { GetFunnelsDto } from './dto/getFunnels.dto'import { AppLoggerService } from '../logger/logger.service'-import { redis, UNIQUE_SESSION_LIFE_TIME } from '../common/constants'import { clickhouse } from '../common/integrations/clickhouse'import {checkRateLimit,@@ -976,8 +975,11 @@ export class AnalyticsController {await this.analyticsService.validateHeartbeat(logDTO, origin, ip)- const { exists, psid, sessionHash } =- await this.analyticsService.getSessionId(pid, userAgent, ip)+ const { exists, psid } = await this.analyticsService.getSessionId(+ pid,+ userAgent,+ ip,+ )if (!exists) {throw new ForbiddenException(@@ -985,7 +987,7 @@ export class AnalyticsController {)}- await redis.set(sessionHash, psid, 'EX', UNIQUE_SESSION_LIFE_TIME)+ await this.analyticsService.extendSessionTTL(psid)await this.analyticsService.processInteractionSD(psid, pid)diff --git a/backend/apps/community/src/analytics/analytics.service.ts b/backend/apps/community/src/analytics/analytics.service.tsindex 6775c5e67..cf0692541 100644--- a/backend/apps/community/src/analytics/analytics.service.ts+++ b/backend/apps/community/src/analytics/analytics.service.ts@@ -735,16 +735,6 @@ export class AnalyticsService {}}- async getSessionHash(- pid: string,- userAgent: string,- ip: string,- ): Promise<string> {- const salt = await this.saltService.getSaltForProject(pid)-- return `ses_${hash(`${userAgent}${ip}${pid}${salt}`)}`- }-async isSessionDurationOpen(sdKey: string): Promise<[string, boolean]> {const sd = await redis.get(sdKey)return [sd, Boolean(sd)]@@ -1134,45 +1124,60 @@ export class AnalyticsService {]}- generateUInt64(): string {- return crypto.randomBytes(8).readBigUInt64BE(0).toString()+ derivePsidFromInputs(+ pid: string,+ userAgent: string,+ ip: string,+ salt: string,+ ): string {+ const combined = `${userAgent}${ip}${pid}${salt}`+ return crypto+ .createHash('sha256')+ .update(combined)+ .digest()+ .readBigUInt64BE(0)+ .toString()+ }++ getSessionKey(psid: string): string {+ return `ses:${psid}`}async getSessionId(pid: string,userAgent: string,ip: string,- ): Promise<{ exists: boolean; psid: string; sessionHash: string }> {- const sessionHash = await this.getSessionHash(pid, userAgent, ip)-- let psid = await redis.get(sessionHash)- const exists = Boolean(psid)+ ): Promise<{ exists: boolean; psid: string }> {+ const salt = await this.saltService.getSaltForProject(pid)+ const psid = this.derivePsidFromInputs(pid, userAgent, ip, salt)+ const sessionKey = this.getSessionKey(psid)- if (!exists) {- psid = this.generateUInt64()- }+ const exists = Boolean(await redis.exists(sessionKey))- return { exists, psid, sessionHash }+ return { exists, psid }}/**- * Checks if the session is unique and returns the session psid (or creates a new one)+ * Checks if the session is unique and returns the session psid.+ * The psid is derived deterministically from inputs - no storage needed.*/async generateAndStoreSessionId(pid: string,userAgent: string,ip: string,): Promise<[boolean, string]> {- const { exists, psid, sessionHash } = await this.getSessionId(- pid,- userAgent,- ip,- )+ const { exists, psid } = await this.getSessionId(pid, userAgent, ip)+ const sessionKey = this.getSessionKey(psid)- await redis.set(sessionHash, psid, 'EX', UNIQUE_SESSION_LIFE_TIME)+ await redis.set(sessionKey, '1', 'EX', UNIQUE_SESSION_LIFE_TIME)return [!exists, psid]}+ async extendSessionTTL(psid: string): Promise<void> {+ const sessionKey = this.getSessionKey(psid)+ await redis.set(sessionKey, '1', 'EX', UNIQUE_SESSION_LIFE_TIME)+ }+formatFunnel(data: IFunnelCHResponse[], pages: string[]): IFunnel[] {const funnel = _map(data, (row, index) => {const value = pages[index]