📈 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
fix: correct feature flag and experiment evaluation (#527)
Andrii Romasiun6b212e740250
diff --git a/backend/apps/cloud/src/analytics/analytics.controller.ts b/backend/apps/cloud/src/analytics/analytics.controller.tsindex 4f3063996..2818c83c2 100644--- a/backend/apps/cloud/src/analytics/analytics.controller.ts+++ b/backend/apps/cloud/src/analytics/analytics.controller.ts@@ -95,6 +95,12 @@ import { GetKeywordsDto } from './dto/get-keywords.dto'import { GetBotStatsDto } from './dto/get-bot-stats.dto'import { GSCService } from '../project/gsc.service'import { GetProfileIdDto, GetSessionIdDto } from './dto/get-id.dto'+import { ExperimentService } from '../experiment/experiment.service'+import {+ ExperimentStatus,+ ExposureTrigger,+} from '../experiment/entity/experiment.entity'+import { getExperimentVariant } from '../feature-flag/evaluation'dayjs.extend(utc)dayjs.extend(dayjsTimezone)@@ -205,6 +211,7 @@ export class AnalyticsController {private readonly analyticsService: AnalyticsService,private readonly logger: AppLoggerService,private readonly gscService: GSCService,+ private readonly experimentService: ExperimentService,) {}@ApiBearerAuth()@@ -1663,6 +1670,13 @@ export class AnalyticsController {values: [transformed],clickhouse_settings: { async_insert: 1 },})++ void this.trackCustomEventExperimentExposures(+ eventsDTO.pid,+ eventsDTO.ev,+ profileId,+ transformed.created,+ )} catch (reason) {this.logger.error(reason)throw new InternalServerErrorException(@@ -1673,6 +1687,83 @@ export class AnalyticsController {return {}}+ private async trackCustomEventExperimentExposures(+ pid: string,+ eventName: string,+ profileId: string,+ created: string,+ ) {+ try {+ const experiments = await this.experimentService.find({+ where: {+ project: { id: pid },+ status: ExperimentStatus.RUNNING,+ exposureTrigger: ExposureTrigger.CUSTOM_EVENT,+ customEventName: eventName,+ },+ relations: ['variants'],+ })++ if (_isEmpty(experiments)) {+ return+ }++ const exposures = []+ for (const experiment of experiments) {+ if (!experiment.variants || experiment.variants.length === 0) {+ continue+ }++ const sortedVariants = [...experiment.variants].sort((a, b) =>+ a.key.localeCompare(b.key),+ )+ const variantKey = getExperimentVariant(+ experiment.id,+ sortedVariants.map((variant) => ({+ key: variant.key,+ rolloutPercentage: variant.rolloutPercentage,+ })),+ profileId,+ )++ if (!variantKey) {+ continue+ }++ exposures.push({+ pid,+ experimentId: experiment.id,+ variantKey,+ profileId,+ created,+ })+ }++ if (_isEmpty(exposures)) {+ return+ }++ clickhouse+ .insert({+ table: 'experiment_exposures',+ values: exposures,+ format: 'JSONEachRow',+ clickhouse_settings: { async_insert: 1 },+ })+ .catch((reason) => {+ this.logger.warn(+ { reason },+ 'Failed to async insert custom event experiment exposures',+ )+ })+ } catch (reason) {+ this.logger.warn(+ { reason },+ 'Failed to track custom event experiment exposures',+ )+ }+ }+@Post('hb')@Auth(true, true)async heartbeat(diff --git a/backend/apps/cloud/src/analytics/analytics.module.ts b/backend/apps/cloud/src/analytics/analytics.module.tsindex 529dda282..3e348ce82 100644--- a/backend/apps/cloud/src/analytics/analytics.module.ts+++ b/backend/apps/cloud/src/analytics/analytics.module.ts@@ -11,6 +11,7 @@ import { UserModule } from '../user/user.module'import { AppLoggerModule } from '../logger/logger.module'import { ProjectModule } from '../project/project.module'import { RevenueModule } from '../revenue/revenue.module'+import { ExperimentModule } from '../experiment/experiment.module'@Module({imports: [@@ -19,6 +20,7 @@ import { RevenueModule } from '../revenue/revenue.module'AppLoggerModule,ProjectModule,forwardRef(() => RevenueModule),+ forwardRef(() => ExperimentModule),],providers: [AnalyticsService,diff --git a/backend/apps/cloud/src/analytics/bot-detection.service.ts b/backend/apps/cloud/src/analytics/bot-detection.service.tsindex 79175a021..cab195c09 100644--- a/backend/apps/cloud/src/analytics/bot-detection.service.ts+++ b/backend/apps/cloud/src/analytics/bot-detection.service.ts@@ -20,6 +20,7 @@ export type BotEndpoint =| 'pageview'| 'custom'| 'error'+ | 'feature_flag'| 'heartbeat'| 'noscript'diff --git a/backend/apps/cloud/src/experiment/experiment.controller.ts b/backend/apps/cloud/src/experiment/experiment.controller.tsindex 869176359..f12e29fe9 100644--- a/backend/apps/cloud/src/experiment/experiment.controller.ts+++ b/backend/apps/cloud/src/experiment/experiment.controller.ts@@ -74,6 +74,17 @@ import { Pagination } from '../common/pagination'const EXPERIMENTS_MAXIMUM = 20 // Maximum experiments per projectconst FEATURE_FLAG_KEY_REGEX = /^[a-zA-Z0-9_-]+$/+const validateUniqueVariantKeys = (variants: Array<{ key: string }>): void => {+ const seen = new Set<string>()++ for (const variant of variants) {+ if (seen.has(variant.key)) {+ throw new BadRequestException('Variant keys must be unique')+ }+ seen.add(variant.key)+ }+}+type GoalEventConditions = {eventType: 'pageview' | 'custom_event'matchColumn: 'pg' | 'event_name'@@ -253,6 +264,8 @@ export class ExperimentController {)}+ validateUniqueVariantKeys(experimentDto.variants)+const controlVariants = experimentDto.variants.filter((v) => v.isControl)if (controlVariants.length !== 1) {throw new BadRequestException(@@ -445,6 +458,8 @@ export class ExperimentController {)}+ validateUniqueVariantKeys(experimentDto.variants)+const controlVariants = experimentDto.variants.filter((v) => v.isControl,)@@ -484,31 +499,47 @@ export class ExperimentController {}let featureFlag = experiment.featureFlag- if (- experimentDto.featureFlagMode !== undefined &&- experimentDto.featureFlagMode !== experiment.featureFlagMode- ) {- if (experimentDto.featureFlagMode === FeatureFlagMode.LINK) {- if (!experimentDto.existingFeatureFlagId) {- throw new BadRequestException(- 'Feature flag ID is required when linking an existing flag',- )- }- const existingFlag = await this.featureFlagService.findOne({- where: {- id: experimentDto.existingFeatureFlagId,- project: { id: experiment.project.id },- },- })- if (!existingFlag) {- throw new NotFoundException('Feature flag not found')- }- if (existingFlag.experimentId && existingFlag.experimentId !== id) {- throw new BadRequestException(- 'This feature flag is already linked to another experiment',+ const targetFeatureFlagMode =+ experimentDto.featureFlagMode ?? experiment.featureFlagMode++ if (targetFeatureFlagMode === FeatureFlagMode.LINK) {+ const targetFlagId =+ experimentDto.existingFeatureFlagId ??+ (experiment.featureFlagMode === FeatureFlagMode.LINK+ ? experiment.featureFlag?.id+ : undefined)++ if (!targetFlagId) {+ throw new BadRequestException(+ 'Feature flag ID is required when linking an existing flag',+ )+ }++ const existingFlag = await this.featureFlagService.findOne({+ where: {+ id: targetFlagId,+ project: { id: experiment.project.id },+ },+ })+ if (!existingFlag) {+ throw new NotFoundException('Feature flag not found')+ }+ if (existingFlag.experimentId && existingFlag.experimentId !== id) {+ throw new BadRequestException(+ 'This feature flag is already linked to another experiment',+ )+ }++ if (+ experiment.featureFlag &&+ experiment.featureFlag.id !== existingFlag.id+ ) {+ if (experiment.featureFlagMode === FeatureFlagMode.CREATE) {+ await this.featureFlagService.delete(+ experiment.featureFlag.id,+ transactionalEntityManager,)- }- if (experiment.featureFlag) {+ } else {await this.featureFlagService.update(experiment.featureFlag.id,{@@ -517,6 +548,9 @@ export class ExperimentController {transactionalEntityManager,)}+ }++ if (existingFlag.experimentId !== id) {await this.featureFlagService.update(existingFlag.id,{@@ -524,8 +558,22 @@ export class ExperimentController {},transactionalEntityManager,)- featureFlag = existingFlag}++ featureFlag = existingFlag+ } else if (+ targetFeatureFlagMode === FeatureFlagMode.CREATE &&+ experiment.featureFlag &&+ experiment.featureFlagMode === FeatureFlagMode.LINK+ ) {+ await this.featureFlagService.update(+ experiment.featureFlag.id,+ {+ experimentId: null,+ },+ transactionalEntityManager,+ )+ featureFlag = null}const updatePayload: Partial<Experiment> = {@@ -919,15 +967,16 @@ export class ExperimentController {diff,)+ const exposureAttributionSubquery =+ this.getExposureAttributionSubquery(experiment)+const exposuresQuery = `- SELECT+ SELECTvariantKey,- uniqExact(profileId) as exposures- FROM experiment_exposures- WHERE- pid = {pid:FixedString(12)}- AND experimentId = {experimentId:String}- AND created BETWEEN {groupFrom:String} AND {groupTo:String}+ count() as exposures+ FROM (+ ${exposureAttributionSubquery}+ )GROUP BY variantKey`@@ -964,14 +1013,14 @@ export class ExperimentController {SELECTe.variantKey,uniqExact(e.profileId) as conversions- FROM experiment_exposures e+ FROM (+ ${exposureAttributionSubquery}+ ) eINNER JOIN events c ON e.pid = c.pid AND e.profileId = assumeNotNull(c.profileId) AND c.type = '${eventType}'WHEREe.pid = {pid:FixedString(12)}- AND e.experimentId = {experimentId:String}- AND e.created BETWEEN {groupFrom:String} AND {groupTo:String}AND c.created BETWEEN {groupFrom:String} AND {groupTo:String}- AND c.created >= e.created+ AND c.created >= e.exposureCreatedAND ${matchCondition}${metaCondition}GROUP BY e.variantKey@@ -1140,6 +1189,34 @@ export class ExperimentController {}}+ private getExposureAttributionSubquery(experiment: Experiment): string {+ const variantSelector =+ experiment.multipleVariantHandling ===+ MultipleVariantHandling.FIRST_EXPOSURE ||+ experiment.multipleVariantHandling === MultipleVariantHandling.EXCLUDE+ ? 'argMin(variantKey, tuple(created, variantKey))'+ : 'any(variantKey)'+ const multiVariantFilter =+ experiment.multipleVariantHandling === MultipleVariantHandling.EXCLUDE+ ? 'HAVING uniqExact(variantKey) = 1'+ : ''++ return `+ SELECT+ pid,+ profileId,+ ${variantSelector} as variantKey,+ min(created) as exposureCreated+ FROM experiment_exposures+ WHERE+ pid = {pid:FixedString(12)}+ AND experimentId = {experimentId:String}+ AND created BETWEEN {groupFrom:String} AND {groupTo:String}+ GROUP BY pid, profileId+ ${multiVariantFilter}+ `+ }+/*** Generate time-series chart data for experiment win probabilities*/@@ -1159,16 +1236,18 @@ export class ExperimentController {safeTimezone,)- // Important: table results use overall uniqExact(profileId) counts per variant.+ // Important: table results use one attributed variant per profile.// For the time-series we must avoid "summing per-bucket uniques", because a profile// can appear in multiple time buckets (re-exposed / repeated events). Instead we:- // - bucket exposures by the first exposure per (variantKey, profileId)- // - bucket conversions by the first conversion per (variantKey, profileId)+ // - bucket exposures by each profile's attributed exposure timestamp+ // - bucket conversions by the first conversion per attributed variant/profile// This guarantees that the last chart point uses the same data as the table.const dateColumnsGroupBy = this.getTimeBucketDateColumnsGroupBy(timeBucket)+ const exposureAttributionSubquery =+ this.getExposureAttributionSubquery(experiment)const exposuresDateColumnsSelect = this.getTimeBucketDateColumnsSelect(timeBucket,- 'firstCreated',+ 'exposureCreated',)const exposuresQuery = `@@ -1177,16 +1256,7 @@ export class ExperimentController {variantKey,count() as exposuresFROM (- SELECT- variantKey,- profileId,- min(created) as firstCreated- FROM experiment_exposures- WHERE- pid = {pid:FixedString(12)}- AND experimentId = {experimentId:String}- AND created BETWEEN {groupFrom:String} AND {groupTo:String}- GROUP BY variantKey, profileId+ ${exposureAttributionSubquery})GROUP BY ${dateColumnsGroupBy}, variantKeyORDER BY ${dateColumnsGroupBy}@@ -1234,14 +1304,14 @@ export class ExperimentController {e.variantKey as variantKey,e.profileId as profileId,min(c.created) as firstConversion- FROM experiment_exposures e+ FROM (+ ${exposureAttributionSubquery}+ ) eINNER JOIN events c ON e.pid = c.pid AND e.profileId = assumeNotNull(c.profileId) AND c.type = '${eventType}'WHEREe.pid = {pid:FixedString(12)}- AND e.experimentId = {experimentId:String}- AND e.created BETWEEN {groupFrom:String} AND {groupTo:String}AND c.created BETWEEN {groupFrom:String} AND {groupTo:String}- AND c.created >= e.created+ AND c.created >= e.exposureCreatedAND ${matchCondition}${metaCondition}GROUP BY e.variantKey, e.profileIddiff --git a/backend/apps/cloud/src/feature-flag/evaluation.ts b/backend/apps/cloud/src/feature-flag/evaluation.tsindex 6665c1dba..e627ac87c 100644--- a/backend/apps/cloud/src/feature-flag/evaluation.ts+++ b/backend/apps/cloud/src/feature-flag/evaluation.ts@@ -87,14 +87,13 @@ function matchesTargetingRules(rules: TargetingRule[],attributes?: Record<string, string>,): boolean {- if (!attributes) {- return true- }-for (const rule of rules) {- const attributeValue = attributes[rule.column]+ const attributeValue = attributes?.[rule.column]if (attributeValue === undefined) {+ if (!rule.isExclusive) {+ return false+ }continue}@@ -183,7 +182,7 @@ export function getExperimentVariant(const hashValue = parseInt(hash.substring(0, 8), 16)- const normalizedValue = (hashValue / 0xffffffff) * 100+ const normalizedValue = (hashValue / 0x100000000) * 100let cumulativePercentage = 0for (const variant of variants) {@@ -193,5 +192,5 @@ export function getExperimentVariant(}}- return variants[variants.length - 1].key+ return null}diff --git a/backend/apps/cloud/src/feature-flag/feature-flag.controller.ts b/backend/apps/cloud/src/feature-flag/feature-flag.controller.tsindex 38f84dd2d..1efe4ba28 100644--- a/backend/apps/cloud/src/feature-flag/feature-flag.controller.ts+++ b/backend/apps/cloud/src/feature-flag/feature-flag.controller.ts@@ -50,9 +50,12 @@ import {} from './dto/feature-flag.dto'import { FeatureFlagService } from './feature-flag.service'import { ExperimentService } from '../experiment/experiment.service'-import { ExperimentStatus } from '../experiment/entity/experiment.entity'+import {+ ExperimentStatus,+ ExposureTrigger,+} from '../experiment/entity/experiment.entity'import { clickhouse } from '../common/integrations/clickhouse'-import { getIPFromHeaders, getIPDetails } from '../common/utils'+import { checkRateLimit, getIPFromHeaders, getIPDetails } from '../common/utils'import { getExperimentVariant } from './evaluation'import { trackCustom } from '../common/analytics'@@ -256,7 +259,7 @@ export class FeatureFlagController {@ApiOperation({summary: 'Evaluate feature flags for a visitor (public endpoint)',description:- 'Evaluates all enabled feature flags for a project based on visitor attributes derived from the request. Does not require authentication.',+ 'Evaluates all feature flags for a project based on visitor attributes derived from the request. Does not require authentication.',})async evaluateFlags(@Body() evaluateDto: EvaluateFeatureFlagsDto,@@ -266,10 +269,39 @@ export class FeatureFlagController {this.logger.log({ pid: evaluateDto.pid }, 'POST /feature-flag/evaluate')const ip = getIPFromHeaders(headers) || reqIP || ''+ const userAgent = headers['user-agent'] || ''+ const origin = headers.origin || ''- const project = await this.projectService.findOne({- where: { id: evaluateDto.pid },- })+ if (ip) {+ await checkRateLimit(ip, 'feature-flag-evaluate-ip', 600, 60)+ }+ await checkRateLimit(+ evaluateDto.pid,+ 'feature-flag-evaluate-project',+ 5000,+ 60,+ )++ const botResult = await this.analyticsService.checkBot(+ evaluateDto.pid,+ userAgent,+ headers,+ ip,+ headers.referer || headers.referrer,+ null,+ 'feature_flag',+ )++ if (botResult.isBot) {+ return { flags: {} }+ }++ let project+ try {+ project = await this.projectService.getRedisProject(evaluateDto.pid)+ } catch {+ project = await this.projectService.getFullProject(evaluateDto.pid)+ }// Return empty flags instead of revealing whether a project exists// This prevents project ID enumeration attacks@@ -277,13 +309,16 @@ export class FeatureFlagController {return { flags: {} }}- const flags = await this.featureFlagService.findEnabledByProject(- evaluateDto.pid,- )+ this.analyticsService.checkIpBlacklist(project, ip)+ this.analyticsService.checkOrigin(project, origin)+ this.analyticsService.checkIfAccountSuspended(project)// Derive attributes from request headers (like analytics does)- const userAgent = headers['user-agent'] || ''const { country, city, region } = getIPDetails(ip)+ this.analyticsService.checkCountryBlacklist(project, country)++ const flags = await this.featureFlagService.findByProject(evaluateDto.pid)+const { deviceType, browserName, osName } =await this.analyticsService.getRequestInformation(headers)@@ -337,6 +372,7 @@ export class FeatureFlagController {where: flagsWithExperiments.map((f) => ({id: f.experimentId,status: ExperimentStatus.RUNNING,+ exposureTrigger: ExposureTrigger.FEATURE_FLAG,})),relations: ['variants'],})diff --git a/backend/apps/cloud/src/feature-flag/feature-flag.module.ts b/backend/apps/cloud/src/feature-flag/feature-flag.module.tsindex ebf6926f9..993b5b4f8 100644--- a/backend/apps/cloud/src/feature-flag/feature-flag.module.ts+++ b/backend/apps/cloud/src/feature-flag/feature-flag.module.ts@@ -16,7 +16,7 @@ import { FeatureFlagController } from './feature-flag.controller'ProjectModule,AppLoggerModule,UserModule,- AnalyticsModule,+ forwardRef(() => AnalyticsModule),forwardRef(() => ExperimentModule),],providers: [FeatureFlagService],diff --git a/backend/apps/cloud/src/feature-flag/feature-flag.service.ts b/backend/apps/cloud/src/feature-flag/feature-flag.service.tsindex cbfeedad5..6ceb4c47f 100644--- a/backend/apps/cloud/src/feature-flag/feature-flag.service.ts+++ b/backend/apps/cloud/src/feature-flag/feature-flag.service.ts@@ -113,8 +113,11 @@ export class FeatureFlagService {return repository.update(id, flagData)}- async delete(id: string): Promise<DeleteResult> {- return this.featureFlagRepository.delete(id)+ async delete(id: string, manager?: EntityManager): Promise<DeleteResult> {+ const repository = manager+ ? manager.getRepository(FeatureFlag)+ : this.featureFlagRepository+ return repository.delete(id)}/**diff --git a/backend/apps/community/src/analytics/bot-detection.service.ts b/backend/apps/community/src/analytics/bot-detection.service.tsindex 69553ad49..8bf54ddc6 100644--- a/backend/apps/community/src/analytics/bot-detection.service.ts+++ b/backend/apps/community/src/analytics/bot-detection.service.ts@@ -20,6 +20,7 @@ export type BotEndpoint =| 'pageview'| 'custom'| 'error'+ | 'feature_flag'| 'heartbeat'| 'noscript'diff --git a/backend/apps/community/src/feature-flag/evaluation.ts b/backend/apps/community/src/feature-flag/evaluation.tsindex eac9c8115..758a54147 100644--- a/backend/apps/community/src/feature-flag/evaluation.ts+++ b/backend/apps/community/src/feature-flag/evaluation.ts@@ -91,18 +91,13 @@ function matchesTargetingRules(rules: TargetingRule[],attributes?: Record<string, string>,): boolean {- if (!attributes) {- // If no attributes provided, we can't match any rules- // Return true to be permissive (flag will be shown)- return true- }-for (const rule of rules) {- const attributeValue = attributes[rule.column]+ const attributeValue = attributes?.[rule.column]- // Check if we have the attributeif (attributeValue === undefined) {- // If attribute not provided, skip this rule (be permissive)+ if (!rule.isExclusive) {+ return false+ }continue}diff --git a/backend/apps/community/src/feature-flag/feature-flag.controller.ts b/backend/apps/community/src/feature-flag/feature-flag.controller.tsindex 0f1245fd5..0e70b1c5c 100644--- a/backend/apps/community/src/feature-flag/feature-flag.controller.ts+++ b/backend/apps/community/src/feature-flag/feature-flag.controller.ts@@ -44,7 +44,7 @@ import {} from './dto/feature-flag.dto'import { FeatureFlagService } from './feature-flag.service'import { clickhouse } from '../common/integrations/clickhouse'-import { getIPFromHeaders, getIPDetails } from '../common/utils'+import { checkRateLimit, getIPFromHeaders, getIPDetails } from '../common/utils'const FEATURE_FLAGS_MAXIMUM = 50 // Maximum feature flags per projectconst FEATURE_FLAGS_PAGINATION_MAX_TAKE = 100@@ -203,7 +203,7 @@ export class FeatureFlagController {@ApiOperation({summary: 'Evaluate feature flags for a visitor (public endpoint)',description:- 'Evaluates all enabled feature flags for a project based on visitor attributes derived from the request. Does not require authentication.',+ 'Evaluates enabled feature flags for a project based on visitor attributes derived from the request. Does not require authentication.',})async evaluateFlags(@Body() evaluateDto: EvaluateFeatureFlagsDto,@@ -213,8 +213,43 @@ export class FeatureFlagController {this.logger.log({ pid: evaluateDto.pid }, 'POST /feature-flag/evaluate')const ip = getIPFromHeaders(headers) || reqIP || ''+ const userAgent = headers['user-agent'] || ''+ const origin = headers.origin || ''- const project = await this.projectService.getRedisProject(evaluateDto.pid)+ if (ip) {+ await checkRateLimit(ip, 'feature-flag-evaluate-ip', 600, 60)+ }+ const botResult = await this.analyticsService.checkBot(+ evaluateDto.pid,+ userAgent,+ headers,+ ip,+ headers.referer || headers.referrer,+ null,+ 'feature_flag',+ )++ if (botResult.isBot) {+ return { flags: {} }+ }++ await checkRateLimit(+ evaluateDto.pid,+ 'feature-flag-evaluate-project',+ 5000,+ 60,+ )++ let project+ try {+ project = await this.projectService.getRedisProject(evaluateDto.pid)+ } catch (reason) {+ this.logger.warn(+ `Failed to get Redis project for pid ${evaluateDto.pid} (feature_flag): ${reason}`,+ 'evaluate',+ )+ return { flags: {} }+ }// Return empty flags instead of revealing whether a project exists// This prevents project ID enumeration attacks@@ -222,13 +257,17 @@ export class FeatureFlagController {return { flags: {} }}+ this.analyticsService.checkIpBlacklist(project, ip)+ this.analyticsService.checkOrigin(project, origin)++ // Derive attributes from request headers (like analytics does)+ const { country, city, region } = getIPDetails(ip)+ this.analyticsService.checkCountryBlacklist(project, country)+const flags = await this.featureFlagService.findEnabledByProject(evaluateDto.pid,)- // Derive attributes from request headers (like analytics does)- const userAgent = headers['user-agent'] || ''- const { country, city, region } = getIPDetails(ip)const { deviceType, browserName, osName } =await this.analyticsService.getRequestInformation(headers)