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!

use profileId instead of visitorId

Blue Mouse61aa5aebbb77
diff --git a/backend/apps/cloud/src/feature-flag/dto/feature-flag.dto.ts b/backend/apps/cloud/src/feature-flag/dto/feature-flag.dto.ts
index e8ec2d83e..5103e5019 100644
--- a/backend/apps/cloud/src/feature-flag/dto/feature-flag.dto.ts
+++ b/backend/apps/cloud/src/feature-flag/dto/feature-flag.dto.ts
@@ -155,11 +155,12 @@ export class EvaluateFeatureFlagsDto {
pid: string
@ApiPropertyOptional({
- description: 'Visitor/user ID for consistent rollout',
+ description:
+ 'Optional profile ID for long-term user tracking. If not provided, an anonymous profile ID will be generated based on IP and user agent.',
})
@IsOptional()
@IsString()
- visitorId?: string
+ profileId?: string
}
export class FeatureFlagDto {
diff --git a/backend/apps/cloud/src/feature-flag/feature-flag.controller.ts b/backend/apps/cloud/src/feature-flag/feature-flag.controller.ts
index fdabca882..a9719dcdf 100644
--- a/backend/apps/cloud/src/feature-flag/feature-flag.controller.ts
+++ b/backend/apps/cloud/src/feature-flag/feature-flag.controller.ts
@@ -52,7 +52,7 @@ import { FeatureFlagService } from './feature-flag.service'
import { clickhouse } from '../common/integrations/clickhouse'
import { getIPFromHeaders, getGeoDetails } from '../common/utils'
-const FEATURE_FLAGS_MAXIMUM = 100 // Maximum feature flags per project
+const FEATURE_FLAGS_MAXIMUM = 50 // Maximum feature flags per project
const timeBucketConversion: Record<string, string> = {
minute: 'toStartOfMinute',
@@ -274,10 +274,21 @@ export class FeatureFlagController {
// Derive attributes from request headers (like analytics does)
const ip = getIPFromHeaders(headers, true) || reqIP || ''
+ const userAgent = headers['user-agent'] || ''
const { country, city, region } = getGeoDetails(ip)
const { deviceType, browserName, osName } =
await this.analyticsService.getRequestInformation(headers)
+ // Generate profileId using the same method as analytics:
+ // - If user provides profileId, use it (with usr_ prefix)
+ // - Otherwise, generate anonymous profileId from monthly salt + ip + useragent (with anon_ prefix)
+ const profileId = await this.analyticsService.generateProfileId(
+ evaluateDto.pid,
+ userAgent,
+ ip,
+ evaluateDto.profileId,
+ )
+
// Build attributes from derived values
const derivedAttributes: Record<string, string> = {}
@@ -302,7 +313,7 @@ export class FeatureFlagController {
const evaluatedFlags = this.featureFlagService.evaluateFlags(
flags,
- evaluateDto.visitorId,
+ profileId,
derivedAttributes,
)
@@ -311,7 +322,7 @@ export class FeatureFlagController {
evaluateDto.pid,
flags,
evaluatedFlags,
- evaluateDto.visitorId,
+ profileId,
).catch(err => {
this.logger.error({ err }, 'Failed to track flag evaluations')
})
@@ -323,19 +334,19 @@ export class FeatureFlagController {
pid: string,
flags: FeatureFlag[],
evaluatedFlags: Record<string, boolean>,
- visitorId?: string,
+ profileId: string,
) {
if (flags.length === 0) return
const now = new Date().toISOString().slice(0, 19).replace('T', ' ')
- const vid = visitorId || 'anonymous'
+ // Note: The ClickHouse column is named 'visitorId' but we store the profileId value in it
const values = flags.map(flag => ({
pid,
flagId: flag.id,
flagKey: flag.key,
result: evaluatedFlags[flag.key] ? 1 : 0,
- visitorId: vid,
+ visitorId: profileId,
created: now,
}))
diff --git a/backend/apps/cloud/src/feature-flag/feature-flag.service.ts b/backend/apps/cloud/src/feature-flag/feature-flag.service.ts
index 1f4dc7331..0b82ee05e 100644
--- a/backend/apps/cloud/src/feature-flag/feature-flag.service.ts
+++ b/backend/apps/cloud/src/feature-flag/feature-flag.service.ts
@@ -87,13 +87,13 @@ export class FeatureFlagService {
*/
evaluateFlags(
flags: FeatureFlag[],
- visitorId?: string,
+ profileId: string,
attributes?: Record<string, string>,
): Record<string, boolean> {
const result: Record<string, boolean> = {}
for (const flag of flags) {
- result[flag.key] = this.evaluateFlag(flag, visitorId, attributes)
+ result[flag.key] = this.evaluateFlag(flag, profileId, attributes)
}
return result
@@ -104,7 +104,7 @@ export class FeatureFlagService {
*/
evaluateFlag(
flag: FeatureFlag,
- visitorId?: string,
+ profileId: string,
attributes?: Record<string, string>,
): boolean {
// If flag is disabled, always return false
@@ -133,7 +133,7 @@ export class FeatureFlagService {
return this.isInRolloutPercentage(
flag.key,
flag.rolloutPercentage,
- visitorId,
+ profileId,
)
}
@@ -194,12 +194,12 @@ export class FeatureFlagService {
/**
* Determines if a visitor is within the rollout percentage
- * Uses consistent hashing based on flag key and visitor ID
+ * Uses consistent hashing based on flag key and profile ID
*/
private isInRolloutPercentage(
flagKey: string,
percentage: number,
- visitorId?: string,
+ profileId: string,
): boolean {
if (percentage >= 100) {
return true
@@ -208,13 +208,10 @@ export class FeatureFlagService {
return false
}
- // Use visitor ID if provided, otherwise generate random
- const identifier = visitorId || crypto.randomUUID()
-
- // Create a consistent hash based on flag key and visitor ID
+ // Create a consistent hash based on flag key and profile ID
const hash = crypto
.createHash('md5')
- .update(`${flagKey}:${identifier}`)
+ .update(`${flagKey}:${profileId}`)
.digest('hex')
// Convert first 8 hex characters to a number (0 to 2^32-1)