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!

CE - profileId support

Blue Mouseb02675adab9f
diff --git a/backend/apps/community/src/analytics/analytics.controller.ts b/backend/apps/community/src/analytics/analytics.controller.ts
index a5954afbe..ba34d0ecc 100644
--- a/backend/apps/community/src/analytics/analytics.controller.ts
+++ b/backend/apps/community/src/analytics/analytics.controller.ts
@@ -916,10 +916,23 @@ export class AnalyticsController {
ip,
)
- await this.analyticsService.recordSession(psid, eventsDTO.pid, false)
+ const profileId = await this.analyticsService.generateProfileId(
+ eventsDTO.pid,
+ userAgent,
+ ip,
+ eventsDTO.profileId,
+ )
+
+ await this.analyticsService.recordSession(
+ psid,
+ eventsDTO.pid,
+ profileId,
+ false,
+ )
const transformed = customEventTransformer(
psid,
+ profileId,
eventsDTO.pid,
this.analyticsService.getHostFromOrigin(headers.origin),
eventsDTO.ev,
@@ -991,8 +1004,15 @@ export class AnalyticsController {
)
}
+ const profileId = await this.analyticsService.generateProfileId(
+ pid,
+ userAgent,
+ ip,
+ logDTO.profileId,
+ )
+
await this.analyticsService.extendSessionTTL(psid)
- await this.analyticsService.updateSessionActivity(psid, pid)
+ await this.analyticsService.updateSessionActivity(psid, pid, profileId)
return {}
}
@@ -1019,7 +1039,14 @@ export class AnalyticsController {
ip,
)
- await this.analyticsService.recordSession(psid, logDTO.pid, true)
+ const profileId = await this.analyticsService.generateProfileId(
+ logDTO.pid,
+ userAgent,
+ ip,
+ logDTO.profileId,
+ )
+
+ await this.analyticsService.recordSession(psid, logDTO.pid, profileId, true)
if (unique && logDTO.unique) {
throw new ForbiddenException(
@@ -1036,6 +1063,7 @@ export class AnalyticsController {
const transformed = trafficTransformer(
psid,
+ profileId,
logDTO.pid,
this.analyticsService.getHostFromOrigin(headers.origin),
logDTO.pg,
@@ -1152,7 +1180,15 @@ export class AnalyticsController {
ip,
)
- await this.analyticsService.recordSession(psid, logDTO.pid, true)
+ // For noscript requests, we generate an anonymous profile ID since
+ // user-supplied profileId is not available without JavaScript
+ const profileId = await this.analyticsService.generateProfileId(
+ logDTO.pid,
+ userAgent,
+ ip,
+ )
+
+ await this.analyticsService.recordSession(psid, logDTO.pid, profileId, true)
const { city, region, regionCode, country } = getGeoDetails(ip, null)
@@ -1163,6 +1199,7 @@ export class AnalyticsController {
const transformed = trafficTransformer(
psid,
+ profileId,
logDTO.pid,
this.analyticsService.getHostFromOrigin(headers.origin),
null,
@@ -1447,7 +1484,19 @@ export class AnalyticsController {
ip,
)
- await this.analyticsService.recordSession(psid, errorDTO.pid, false)
+ const profileId = await this.analyticsService.generateProfileId(
+ errorDTO.pid,
+ userAgent,
+ ip,
+ errorDTO.profileId,
+ )
+
+ await this.analyticsService.recordSession(
+ psid,
+ errorDTO.pid,
+ profileId,
+ false,
+ )
const { city, region, regionCode, country } = getGeoDetails(ip, errorDTO.tz)
@@ -1461,6 +1510,7 @@ export class AnalyticsController {
const transformed = errorEventTransformer(
psid,
+ profileId,
this.analyticsService.getErrorID(errorDTO),
errorDTO.pid,
this.analyticsService.getHostFromOrigin(headers.origin),
diff --git a/backend/apps/community/src/analytics/analytics.service.ts b/backend/apps/community/src/analytics/analytics.service.ts
index a55b60ccd..d74baa1cf 100644
--- a/backend/apps/community/src/analytics/analytics.service.ts
+++ b/backend/apps/community/src/analytics/analytics.service.ts
@@ -33,6 +33,7 @@ import {
InternalServerErrorException,
UnprocessableEntityException,
PreconditionFailedException,
+ Logger,
} from '@nestjs/common'
import { isbot } from 'isbot'
@@ -327,6 +328,11 @@ const isValidOrigin = (origins: string[], origin: string) => {
@Injectable()
export class AnalyticsService {
+ private readonly logger = new Logger(AnalyticsService.name)
+
+ static readonly PROFILE_PREFIX_ANON = 'anon_'
+ static readonly PROFILE_PREFIX_USER = 'usr_'
+
constructor(
private readonly projectService: ProjectService,
private readonly saltService: SaltService,
@@ -1191,9 +1197,44 @@ export class AnalyticsService {
await redis.set(sessionKey, '1', 'EX', UNIQUE_SESSION_LIFE_TIME)
}
+ private hashToNumericString(value: string): string {
+ return crypto
+ .createHash('sha256')
+ .update(value)
+ .digest()
+ .readBigUInt64BE(0)
+ .toString()
+ }
+
+ async generateProfileId(
+ pid: string,
+ userAgent: string,
+ ip: string,
+ userSupplied?: string,
+ ): Promise<string> {
+ if (userSupplied) {
+ const cleanId = userSupplied
+ .replace(AnalyticsService.PROFILE_PREFIX_ANON, '')
+ .replace(AnalyticsService.PROFILE_PREFIX_USER, '')
+ const hash = this.hashToNumericString(`${cleanId}${pid}`)
+ return `${AnalyticsService.PROFILE_PREFIX_USER}${hash}`
+ }
+
+ const salt = await this.saltService.getSaltForProfile()
+ const combined = `${userAgent}${ip}${pid}${salt}`
+ const hash = this.hashToNumericString(combined)
+
+ return `${AnalyticsService.PROFILE_PREFIX_ANON}${hash}`
+ }
+
+ isUserSuppliedProfile(profileId: string): boolean {
+ return profileId.startsWith(AnalyticsService.PROFILE_PREFIX_USER)
+ }
+
async recordSession(
psid: string,
pid: string,
+ profileId: string,
isPageview: boolean = true,
): Promise<void> {
const now = dayjs.utc().format('YYYY-MM-DD HH:mm:ss')
@@ -1206,7 +1247,7 @@ export class AnalyticsService {
{
psid,
pid,
- profileId: '',
+ profileId,
firstSeen: now,
lastSeen: now,
pageviews: isPageview ? 1 : 0,
@@ -1216,11 +1257,16 @@ export class AnalyticsService {
clickhouse_settings: { async_insert: 1 },
})
} catch (error) {
- console.error('Failed to record session:', error)
+ this.logger.error('Failed to record session:', error)
+ throw error
}
}
- async updateSessionActivity(psid: string, pid: string): Promise<void> {
+ async updateSessionActivity(
+ psid: string,
+ pid: string,
+ profileId: string,
+ ): Promise<void> {
const now = dayjs.utc().format('YYYY-MM-DD HH:mm:ss')
try {
@@ -1231,7 +1277,7 @@ export class AnalyticsService {
{
psid,
pid,
- profileId: '',
+ profileId,
firstSeen: now,
lastSeen: now,
pageviews: 0,
@@ -1241,7 +1287,8 @@ export class AnalyticsService {
clickhouse_settings: { async_insert: 1 },
})
} catch (error) {
- console.error('Failed to update session activity:', error)
+ this.logger.error('Failed to update session activity:', error)
+ throw error
}
}
diff --git a/backend/apps/community/src/analytics/dto/error.dto.ts b/backend/apps/community/src/analytics/dto/error.dto.ts
index 5c992b131..73b0f3493 100644
--- a/backend/apps/community/src/analytics/dto/error.dto.ts
+++ b/backend/apps/community/src/analytics/dto/error.dto.ts
@@ -32,6 +32,15 @@ export class ErrorDto {
})
pid: string
+ @ApiProperty({
+ example: 'user_12345',
+ description:
+ 'Optional profile ID for long-term user tracking. If not provided, one will be auto-generated.',
+ })
+ @IsOptional()
+ @IsString()
+ profileId?: string
+
// Tracking metrics
@ApiProperty({
example: 'Europe/Kiev',
diff --git a/backend/apps/community/src/analytics/dto/events.dto.ts b/backend/apps/community/src/analytics/dto/events.dto.ts
index cc1656dbf..3d897b4c6 100644
--- a/backend/apps/community/src/analytics/dto/events.dto.ts
+++ b/backend/apps/community/src/analytics/dto/events.dto.ts
@@ -87,6 +87,14 @@ export class EventsDto {
@Matches(PID_REGEX, { message: 'The provided Project ID (pid) is incorrect' })
pid: string
+ @ApiProperty({
+ example: 'user_12345',
+ description:
+ 'Optional profile ID for long-term user tracking. If not provided, one will be auto-generated.',
+ })
+ @IsOptional()
+ profileId?: string
+
@ApiProperty({
example: 'user-subscribed',
description: 'Event name',
diff --git a/backend/apps/community/src/analytics/dto/pageviews.dto.ts b/backend/apps/community/src/analytics/dto/pageviews.dto.ts
index d29c114c5..cd3b44fb2 100644
--- a/backend/apps/community/src/analytics/dto/pageviews.dto.ts
+++ b/backend/apps/community/src/analytics/dto/pageviews.dto.ts
@@ -28,6 +28,14 @@ export class PageviewsDto {
@Matches(PID_REGEX, { message: 'The provided Project ID (pid) is incorrect' })
pid: string
+ @ApiProperty({
+ example: 'user_12345',
+ description:
+ 'Optional profile ID for long-term user tracking. If not provided, one will be auto-generated.',
+ })
+ @IsOptional()
+ profileId?: string
+
// Tracking metrics
@ApiProperty({
example: 'Europe/Kiev',
diff --git a/backend/apps/community/src/analytics/utils/transformers.ts b/backend/apps/community/src/analytics/utils/transformers.ts
index 22793b9c1..68c1a8cc0 100644
--- a/backend/apps/community/src/analytics/utils/transformers.ts
+++ b/backend/apps/community/src/analytics/utils/transformers.ts
@@ -25,6 +25,7 @@ const processMetaKV = (
export const trafficTransformer = (
psid: string,
+ profileId: string,
pid: string,
host: string | null,
pg: string | null,
@@ -48,6 +49,7 @@ export const trafficTransformer = (
) => {
return {
psid,
+ profileId,
pid,
host: host || null,
pg: pg || null,
@@ -74,6 +76,7 @@ export const trafficTransformer = (
export const customEventTransformer = (
psid: string,
+ profileId: string,
pid: string,
host: string | null,
ev: string,
@@ -98,6 +101,7 @@ export const customEventTransformer = (
) => {
return {
psid,
+ profileId,
pid,
host: host || null,
ev,
@@ -125,6 +129,7 @@ export const customEventTransformer = (
export const errorEventTransformer = (
psid: string,
+ profileId: string,
eid: string,
pid: string,
host: string | null,
@@ -149,6 +154,7 @@ export const errorEventTransformer = (
) => {
return {
psid,
+ profileId,
eid,
pid,
host: host || null,