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!

Don't store heartbeats for undefined sessions

Blue Mouse0eaee92ff27d
diff --git a/backend/apps/production/src/analytics/analytics.controller.ts b/backend/apps/production/src/analytics/analytics.controller.ts
index b09988d48..933d84634 100644
--- a/backend/apps/production/src/analytics/analytics.controller.ts
+++ b/backend/apps/production/src/analytics/analytics.controller.ts
@@ -56,6 +56,7 @@ import {
REDIS_USERS_COUNT_KEY,
REDIS_PROJECTS_COUNT_KEY,
REDIS_EVENTS_COUNT_KEY,
+ UNIQUE_SESSION_LIFE_TIME,
} from '../common/constants'
import { clickhouse } from '../common/integrations/clickhouse'
import {
@@ -1193,7 +1194,7 @@ export class AnalyticsController {
await this.analyticsService.validate(eventsDTO, origin, ip)
if (eventsDTO.unique) {
- const [unique] = await this.analyticsService.isUnique(
+ const [unique] = await this.analyticsService.generateAndStoreSessionId(
`${eventsDTO.pid}-${eventsDTO.ev}`,
userAgent,
ip,
@@ -1217,7 +1218,7 @@ export class AnalyticsController {
const os = ua.os.name
const osv = this.analyticsService.extractMajorMinorVersion(ua.os.version)
- const [, psid] = await this.analyticsService.isUnique(
+ const [, psid] = await this.analyticsService.generateAndStoreSessionId(
eventsDTO.pid,
userAgent,
ip,
@@ -1278,7 +1279,16 @@ export class AnalyticsController {
await this.analyticsService.validateHeartbeat(logDTO, origin, ip)
- const [, psid] = await this.analyticsService.isUnique(pid, userAgent, ip)
+ const { exists, psid, sessionHash } =
+ await this.analyticsService.getSessionId(pid, userAgent, ip)
+
+ if (!exists) {
+ throw new ForbiddenException(
+ 'The heartbeat was not saved because there is no session for this request. Please, send a pageview or custom event request first to initialise the session.',
+ )
+ }
+
+ await redis.set(sessionHash, psid, 'EX', UNIQUE_SESSION_LIFE_TIME)
await this.analyticsService.processInteractionSD(psid, pid)
}
@@ -1295,11 +1305,12 @@ export class AnalyticsController {
await this.analyticsService.validate(logDTO, origin, ip)
- const [unique, psid] = await this.analyticsService.isUnique(
- logDTO.pid,
- userAgent,
- ip,
- )
+ const [unique, psid] =
+ await this.analyticsService.generateAndStoreSessionId(
+ logDTO.pid,
+ userAgent,
+ ip,
+ )
await this.analyticsService.processInteractionSD(psid, logDTO.pid)
@@ -1432,7 +1443,7 @@ export class AnalyticsController {
await this.analyticsService.validate(logDTO, origin, ip)
- const [, psid] = await this.analyticsService.isUnique(
+ const [, psid] = await this.analyticsService.generateAndStoreSessionId(
logDTO.pid,
userAgent,
ip,
diff --git a/backend/apps/production/src/analytics/analytics.service.ts b/backend/apps/production/src/analytics/analytics.service.ts
index a3a097dd2..344e5ba81 100644
--- a/backend/apps/production/src/analytics/analytics.service.ts
+++ b/backend/apps/production/src/analytics/analytics.service.ts
@@ -425,6 +425,15 @@ export class AnalyticsService {
}
}
+ checkIfAccountSuspended(project: Project) {
+ if (project.admin?.isAccountBillingSuspended) {
+ throw new HttpException(
+ 'The account that owns this site is currently suspended, this is because of a billing issue. This, and all other events, are NOT being tracked and saved on our side. Please log in to your account on Swetrix or contact our support to resolve the issue.',
+ HttpStatus.PAYMENT_REQUIRED,
+ )
+ }
+ }
+
async validate(
logDTO: PageviewsDto | EventsDto | ErrorDto,
origin: string,
@@ -461,12 +470,7 @@ export class AnalyticsService {
)
}
- if (project.admin?.isAccountBillingSuspended) {
- throw new HttpException(
- 'The account that owns this site is currently suspended, this is because of a billing issue. This, and all other events, are NOT being tracked and saved on our side. Please log in to your account on Swetrix or contact our support to resolve the issue.',
- HttpStatus.PAYMENT_REQUIRED,
- )
- }
+ this.checkIfAccountSuspended(project)
this.checkOrigin(project, origin)
@@ -490,12 +494,7 @@ export class AnalyticsService {
)
}
- if (project.admin?.isAccountBillingSuspended) {
- throw new HttpException(
- 'The account that owns this site is currently suspended, this is because of a billing issue. This, and all other events, are NOT being tracked and saved on our side. Please log in to your account on Swetrix or contact our support to resolve the issue.',
- HttpStatus.PAYMENT_REQUIRED,
- )
- }
+ this.checkIfAccountSuspended(project)
this.checkOrigin(project, origin)
@@ -1076,16 +1075,11 @@ export class AnalyticsService {
return crypto.randomBytes(8).readBigUInt64BE(0).toString()
}
- /**
- * Checks if the session is unique and returns the session psid (or creates a new one)
- * @param sessionHash
- * @returns [isUnique, psid]
- */
- async isUnique(
+ async getSessionId(
pid: string,
userAgent: string,
ip: string,
- ): Promise<[boolean, string]> {
+ ): Promise<{ exists: boolean; psid: string; sessionHash: string }> {
const sessionHash = await this.getSessionHash(pid, userAgent, ip)
let psid = await redis.get(sessionHash)
@@ -1095,6 +1089,23 @@ export class AnalyticsService {
psid = this.generateUInt64()
}
+ return { exists, psid, sessionHash }
+ }
+
+ /**
+ * Checks if the session is unique and returns the session psid (or creates a new one)
+ */
+ async generateAndStoreSessionId(
+ pid: string,
+ userAgent: string,
+ ip: string,
+ ): Promise<[boolean, string]> {
+ const { exists, psid, sessionHash } = await this.getSessionId(
+ pid,
+ userAgent,
+ ip,
+ )
+
await redis.set(sessionHash, psid, 'EX', UNIQUE_SESSION_LIFE_TIME)
return [!exists, psid]
}
diff --git a/backend/apps/selfhosted/src/analytics/analytics.controller.ts b/backend/apps/selfhosted/src/analytics/analytics.controller.ts
index 59f5c59b7..ee1048f45 100644
--- a/backend/apps/selfhosted/src/analytics/analytics.controller.ts
+++ b/backend/apps/selfhosted/src/analytics/analytics.controller.ts
@@ -45,7 +45,7 @@ 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 } from '../common/constants'
+import { redis, UNIQUE_SESSION_LIFE_TIME } from '../common/constants'
import { clickhouse } from '../common/integrations/clickhouse'
import {
checkRateLimit,
@@ -911,7 +911,7 @@ export class AnalyticsController {
await this.analyticsService.validate(eventsDTO, origin, ip)
if (eventsDTO.unique) {
- const [unique] = await this.analyticsService.isUnique(
+ const [unique] = await this.analyticsService.generateAndStoreSessionId(
`${eventsDTO.pid}-${eventsDTO.ev}`,
userAgent,
ip,
@@ -935,7 +935,7 @@ export class AnalyticsController {
const { city, region, country } = getGeoDetails(ip, eventsDTO.tz)
- const [, psid] = await this.analyticsService.isUnique(
+ const [, psid] = await this.analyticsService.generateAndStoreSessionId(
eventsDTO.pid,
userAgent,
ip,
@@ -996,7 +996,16 @@ export class AnalyticsController {
await this.analyticsService.validateHeartbeat(logDTO, origin, ip)
- const [, psid] = await this.analyticsService.isUnique(pid, userAgent, ip)
+ const { exists, psid, sessionHash } =
+ await this.analyticsService.getSessionId(pid, userAgent, ip)
+
+ if (!exists) {
+ throw new ForbiddenException(
+ 'The heartbeat was not saved because there is no session for this request. Please, send a pageview or custom event request first to initialise the session.',
+ )
+ }
+
+ await redis.set(sessionHash, psid, 'EX', UNIQUE_SESSION_LIFE_TIME)
await this.analyticsService.processInteractionSD(psid, pid)
}
@@ -1012,11 +1021,12 @@ export class AnalyticsController {
await this.analyticsService.validate(logDTO, origin, ip)
- const [unique, psid] = await this.analyticsService.isUnique(
- logDTO.pid,
- userAgent,
- ip,
- )
+ const [unique, psid] =
+ await this.analyticsService.generateAndStoreSessionId(
+ logDTO.pid,
+ userAgent,
+ ip,
+ )
await this.analyticsService.processInteractionSD(psid, logDTO.pid)
@@ -1149,7 +1159,7 @@ export class AnalyticsController {
await this.analyticsService.validate(logDTO, origin, ip)
- const [, psid] = await this.analyticsService.isUnique(
+ const [, psid] = await this.analyticsService.generateAndStoreSessionId(
logDTO.pid,
userAgent,
ip,
diff --git a/backend/apps/selfhosted/src/analytics/analytics.service.ts b/backend/apps/selfhosted/src/analytics/analytics.service.ts
index b8a259f44..c94ccfc3e 100644
--- a/backend/apps/selfhosted/src/analytics/analytics.service.ts
+++ b/backend/apps/selfhosted/src/analytics/analytics.service.ts
@@ -1038,16 +1038,11 @@ export class AnalyticsService {
return crypto.randomBytes(8).readBigUInt64BE(0).toString()
}
- /**
- * Checks if the session is unique and returns the session psid (or creates a new one)
- * @param sessionHash
- * @returns [isUnique, psid]
- */
- async isUnique(
+ async getSessionId(
pid: string,
userAgent: string,
ip: string,
- ): Promise<[boolean, string]> {
+ ): Promise<{ exists: boolean; psid: string; sessionHash: string }> {
const sessionHash = await this.getSessionHash(pid, userAgent, ip)
let psid = await redis.get(sessionHash)
@@ -1057,6 +1052,23 @@ export class AnalyticsService {
psid = this.generateUInt64()
}
+ return { exists, psid, sessionHash }
+ }
+
+ /**
+ * Checks if the session is unique and returns the session psid (or creates a new one)
+ */
+ async generateAndStoreSessionId(
+ pid: string,
+ userAgent: string,
+ ip: string,
+ ): Promise<[boolean, string]> {
+ const { exists, psid, sessionHash } = await this.getSessionId(
+ pid,
+ userAgent,
+ ip,
+ )
+
await redis.set(sessionHash, psid, 'EX', UNIQUE_SESSION_LIFE_TIME)
return [!exists, psid]
}