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!

improved the logic to handle heartbeat events

Blue Mouse5f5913ea32df
diff --git a/src/analytics/analytics.controller.ts b/src/analytics/analytics.controller.ts
index 1839cd214..933950e3d 100644
--- a/src/analytics/analytics.controller.ts
+++ b/src/analytics/analytics.controller.ts
@@ -40,6 +40,7 @@ import {
isValidTimezone,
isValidDate,
checkIfTBAllowed,
+ getHeartbeatKey,
} from './analytics.service'
import { TaskManagerService } from '../task-manager/task-manager.service'
import { CurrentUserId } from '../auth/decorators/current-user-id.decorator'
@@ -702,13 +703,13 @@ export class AnalyticsController {
const result = {}
const keyCountPromises = _map(pidsArray, async currentPID => {
- // @ts-ignore
- const keysAmout = await redis.countKeysByPattern(`hb:${currentPID}:*`)
- result[currentPID] = keysAmout
+ result[currentPID] = await this.analyticsService.getOnlineUserCount(
+ currentPID,
+ )
})
await Promise.all(keyCountPromises).catch(reason => {
- this.logger.error(`[@Get('/hb')] ${reason}`)
+ this.logger.error(`[GET /analytics/hb] ${reason}`)
throw new InternalServerErrorException(
'An error occured while calculating heartbeat statistics',
)
@@ -830,13 +831,18 @@ export class AnalyticsController {
const ip =
headers['cf-connecting-ip'] || headers['x-forwarded-for'] || reqIP || ''
- const sessionID = await this.analyticsService.validateHB(
- logDTO,
+ const sessionID = await this.analyticsService.getSessionHash(
+ pid,
userAgent,
ip,
)
- await redis.set(`hb:${pid}:${sessionID}`, 1, 'EX', HEARTBEAT_SID_LIFE_TIME)
+ await redis.set(
+ getHeartbeatKey(pid, sessionID),
+ 1,
+ 'EX',
+ HEARTBEAT_SID_LIFE_TIME,
+ )
await this.analyticsService.processInteractionSD(sessionID, pid)
}
@@ -861,7 +867,7 @@ export class AnalyticsController {
const sessionHash = getSessionKey(ip, userAgent, logDTO.pid, salt)
const unique = await this.analyticsService.isUnique(sessionHash)
- this.analyticsService.processInteractionSD(sessionHash, logDTO.pid)
+ await this.analyticsService.processInteractionSD(sessionHash, logDTO.pid)
if (unique && logDTO.unique) {
throw new ForbiddenException(
@@ -976,7 +982,7 @@ export class AnalyticsController {
const sessionHash = getSessionKey(ip, userAgent, logDTO.pid, salt)
const unique = await this.analyticsService.isUnique(sessionHash)
- this.analyticsService.processInteractionSD(sessionHash, logDTO.pid)
+ await this.analyticsService.processInteractionSD(sessionHash, logDTO.pid)
const ua = UAParser(userAgent)
const dv = ua.device.type || 'desktop'
diff --git a/src/analytics/analytics.service.ts b/src/analytics/analytics.service.ts
index cc4e3d680..4444ac9b1 100644
--- a/src/analytics/analytics.service.ts
+++ b/src/analytics/analytics.service.ts
@@ -56,6 +56,9 @@ dayjs.extend(dayjsTimezone)
export const getSessionKey = (ip: string, ua: string, pid: string, salt = '') =>
`ses_${hash(`${ua}${ip}${pid}${salt}`).toString('hex')}`
+export const getHeartbeatKey = (pid: string, sessionID: string) =>
+ `hb:${pid}:${sessionID}`
+
export const getSessionDurationKey = (sessionHash: string, pid: string) =>
`sd:${sessionHash}:${pid}`
@@ -359,6 +362,18 @@ export class AnalyticsService {
return sessionHash
}
+ async getSessionHash(
+ pid: string,
+ userAgent: string,
+ ip: string,
+ ): Promise<string | null> {
+ this.validatePID(pid)
+
+ const salt = await redis.get(REDIS_SESSION_SALT_KEY)
+
+ return getSessionKey(ip, userAgent, pid, salt)
+ }
+
async isSessionDurationOpen(sdKey: string): Promise<Array<string | boolean>> {
const sd = await redis.get(sdKey)
return [sd, Boolean(sd)]