Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

feat(experiments): duplicate multivariate flag exposures (#76572)

Andersee12467a5606
diff --git a/nodejs/src/ingestion/common/steps/event-processing/create-event-step.test.ts b/nodejs/src/ingestion/common/steps/event-processing/create-event-step.test.ts
index 5058db38e58e..b00fc3c7132a 100644
--- a/nodejs/src/ingestion/common/steps/event-processing/create-event-step.test.ts
+++ b/nodejs/src/ingestion/common/steps/event-processing/create-event-step.test.ts
@@ -288,6 +288,102 @@ describe('create-event-step', () => {
}
})
+ describe('$experiment_exposure duplication', () => {
+ it('duplicates multivariate $feature_flag_called events for allowlisted teams with a deterministic uuid', async () => {
+ const step = createCreateEventStep(EVENTS_OUTPUT, '2')
+ const input = {
+ person: mockPerson,
+ preparedEvent: {
+ ...mockPreparedEvent,
+ teamId: 2,
+ projectId: 2 as ProjectId,
+ event: '$feature_flag_called',
+ properties: {
+ $feature_flag: 'checkout-experiment',
+ $feature_flag_response: 'test',
+ unchanged: 'value',
+ },
+ },
+ processPerson: true,
+ historicalMigration: false,
+ headers: createTestEventHeaders(),
+ message: mockMessage,
+ }
+ const result = await step(input)
+ const rerun = await step(input)
+
+ expect(isOkResult(result)).toBe(true)
+ expect(isOkResult(rerun)).toBe(true)
+ if (isOkResult(result) && isOkResult(rerun)) {
+ expect(result.value.eventsToEmit).toHaveLength(2)
+ const [flagCalled, exposure] = result.value.eventsToEmit
+ expect(exposure.output).toBe(EVENTS_OUTPUT)
+ expect(exposure.event.uuid).not.toBe(flagCalled.event.uuid)
+ expect(exposure.event).toEqual({
+ ...flagCalled.event,
+ event: '$experiment_exposure',
+ uuid: exposure.event.uuid,
+ })
+ // A redelivered message must reproduce the same exposure row for
+ // ClickHouse's ReplacingMergeTree to collapse it.
+ expect(rerun.value.eventsToEmit[1].event.uuid).toBe(exposure.event.uuid)
+ }
+ })
+
+ it.each([
+ ['a team outside the allowlist', 1, '$feature_flag_called', 'test'],
+ ['a boolean response', 2, '$feature_flag_called', true],
+ ['a stringified boolean response', 2, '$feature_flag_called', 'true'],
+ ['an empty response', 2, '$feature_flag_called', ''],
+ ['a missing response', 2, '$feature_flag_called', undefined],
+ ['a different event', 2, '$pageview', 'test'],
+ ])('does not duplicate %s', async (_, teamId, event, response) => {
+ const step = createCreateEventStep(EVENTS_OUTPUT, '2')
+ const result = await step({
+ person: mockPerson,
+ preparedEvent: {
+ ...mockPreparedEvent,
+ teamId,
+ projectId: teamId as ProjectId,
+ event,
+ properties: { $feature_flag_response: response },
+ },
+ processPerson: true,
+ historicalMigration: false,
+ headers: createTestEventHeaders(),
+ message: mockMessage,
+ })
+
+ expect(isOkResult(result)).toBe(true)
+ if (isOkResult(result)) {
+ expect(result.value.eventsToEmit).toHaveLength(1)
+ }
+ })
+
+ it('does not duplicate when no teams are configured', async () => {
+ const step = createCreateEventStep(EVENTS_OUTPUT)
+ const result = await step({
+ person: mockPerson,
+ preparedEvent: {
+ ...mockPreparedEvent,
+ teamId: 2,
+ projectId: 2 as ProjectId,
+ event: '$feature_flag_called',
+ properties: { $feature_flag_response: 'test' },
+ },
+ processPerson: true,
+ historicalMigration: false,
+ headers: createTestEventHeaders(),
+ message: mockMessage,
+ })
+
+ expect(isOkResult(result)).toBe(true)
+ if (isOkResult(result)) {
+ expect(result.value.eventsToEmit).toHaveLength(1)
+ }
+ })
+ })
+
describe('historicalMigration flag', () => {
it('should include historical_migration in event when historicalMigration=true', async () => {
const step = createCreateEventStep(EVENTS_OUTPUT)
diff --git a/nodejs/src/ingestion/common/steps/event-processing/create-event-step.ts b/nodejs/src/ingestion/common/steps/event-processing/create-event-step.ts
index 07c0e912970b..a402c94c2712 100644
--- a/nodejs/src/ingestion/common/steps/event-processing/create-event-step.ts
+++ b/nodejs/src/ingestion/common/steps/event-processing/create-event-step.ts
@@ -1,5 +1,7 @@
import { Message } from 'node-rdkafka'
+import { parse as parseUuid, v5 as uuidv5 } from 'uuid'
+import { parseTeamsList } from '~/common/utils/env-utils'
import { createEvent } from '~/ingestion/common/steps/event-processing/create-event'
import { ok } from '~/ingestion/framework/results'
import { ProcessingStep } from '~/ingestion/framework/steps'
@@ -7,6 +9,28 @@ import { EventHeaders, Person, PreIngestionEvent } from '~/types'
import { EventToEmit } from './emit-event-step'
+const FEATURE_FLAG_CALLED_EVENT = '$feature_flag_called'
+const EXPERIMENT_EXPOSURE_EVENT = '$experiment_exposure'
+
+// UUIDv5 namespace (a randomly generated UUIDv4) for deriving a duplicated exposure
+// event's uuid from its source event's uuid.
+const EXPERIMENT_EXPOSURE_UUIDV5_NAMESPACE = parseUuid('1b7c9119-5953-4668-97b7-ab0ef8a6bb48')
+
+// Flag definitions aren't available in ingestion, so multivariate is inferred from the
+// response shape; a variant literally keyed "true" or "false" is indistinguishable from
+// a boolean flag response and knowingly skipped.
+function isMultivariateFeatureFlagCalledEvent(event: PreIngestionEvent): boolean {
+ const response = event.properties['$feature_flag_response']
+
+ return (
+ event.event === FEATURE_FLAG_CALLED_EVENT &&
+ typeof response === 'string' &&
+ response !== '' &&
+ response !== 'true' &&
+ response !== 'false'
+ )
+}
+
export interface CreateEventStepInput {
person?: Person
preparedEvent: PreIngestionEvent
@@ -24,15 +48,39 @@ export interface CreateEventStepResult<O extends string> {
}
export function createCreateEventStep<O extends string, T extends CreateEventStepInput>(
- output: O
+ output: O,
+ experimentExposureDuplicationTeams: string = ''
): ProcessingStep<T, CreateEventStepResult<O>> {
+ const exposureDuplicationTeams = parseTeamsList(experimentExposureDuplicationTeams)
+
return function createEventStep(input) {
const { person, preparedEvent, processPerson, historicalMigration, headers, message } = input
const capturedAt = headers.now ?? null
const rawEvent = createEvent(preparedEvent, person, processPerson, historicalMigration, capturedAt)
+ const eventsToEmit: EventToEmit<O>[] = [{ event: rawEvent, output }]
+
+ // Duplicated exposures build up $experiment_exposure data for allowlisted teams during
+ // the migration away from $feature_flag_called-based experiment exposures. The duplicate
+ // gets its own uuid because event uuids are assumed unique by lookups and API endpoints,
+ // derived deterministically from the source event's uuid so that a Kafka redelivery
+ // reproduces the same row, which the events table's ReplacingMergeTree can collapse.
+ if (
+ (exposureDuplicationTeams === '*' || exposureDuplicationTeams.includes(preparedEvent.teamId)) &&
+ isMultivariateFeatureFlagCalledEvent(preparedEvent)
+ ) {
+ eventsToEmit.push({
+ event: {
+ ...rawEvent,
+ event: EXPERIMENT_EXPOSURE_EVENT,
+ uuid: uuidv5(rawEvent.uuid, EXPERIMENT_EXPOSURE_UUIDV5_NAMESPACE),
+ },
+ output,
+ })
+ }
+
const result: CreateEventStepResult<O> = {
- eventsToEmit: [{ event: rawEvent, output }],
+ eventsToEmit,
teamId: preparedEvent.teamId,
headers,
message,
diff --git a/nodejs/src/ingestion/common/steps/event-processing/event-pipeline-options.ts b/nodejs/src/ingestion/common/steps/event-processing/event-pipeline-options.ts
index 58ac41601b41..1fb63353a9c9 100644
--- a/nodejs/src/ingestion/common/steps/event-processing/event-pipeline-options.ts
+++ b/nodejs/src/ingestion/common/steps/event-processing/event-pipeline-options.ts
@@ -16,4 +16,6 @@ export interface EventPipelineRunnerOptions {
PERSON_PROPERTIES_UPDATE_ALL: boolean
/** Teams whose $feature_flag_called events default to personless: '*' for all, '' to disable, or comma-separated team IDs */
FLAG_CALLED_PERSONLESS_DEFAULT_TEAMS: string
+ /** Teams whose multivariate $feature_flag_called events are duplicated as $experiment_exposure: '*' for all, '' to disable, or comma-separated team IDs */
+ EXPERIMENT_EXPOSURE_DUPLICATION_TEAMS: string
}
diff --git a/nodejs/src/ingestion/config.ts b/nodejs/src/ingestion/config.ts
index bd9159be7aa0..46ff382e84a7 100644
--- a/nodejs/src/ingestion/config.ts
+++ b/nodejs/src/ingestion/config.ts
@@ -213,6 +213,8 @@ export type IngestionConsumerConfig = {
KAFKA_BATCH_START_LOGGING_ENABLED: boolean
/** Teams whose $feature_flag_called events default to personless: '*' for all, '' to disable, or comma-separated team IDs */
FLAG_CALLED_PERSONLESS_DEFAULT_TEAMS: string
+ /** Teams whose multivariate $feature_flag_called events are duplicated as $experiment_exposure: '*' for all, '' to disable, or comma-separated team IDs */
+ EXPERIMENT_EXPOSURE_DUPLICATION_TEAMS: string
// $feature_flag_called keep-first dedup config
/** 'disabled' | 'shadow' (claim + count, never drop) | 'drop' */
@@ -351,6 +353,7 @@ export function getDefaultIngestionConsumerConfig(): IngestionConsumerConfig {
EVENT_SCHEMA_ENFORCEMENT_ENABLED: true,
KAFKA_BATCH_START_LOGGING_ENABLED: false,
FLAG_CALLED_PERSONLESS_DEFAULT_TEAMS: DEFAULT_FLAG_CALLED_PERSONLESS_DEFAULT_TEAMS,
+ EXPERIMENT_EXPOSURE_DUPLICATION_TEAMS: '',
// $feature_flag_called keep-first dedup config
INGESTION_FEATURE_FLAG_CALLED_DEDUP_MODE: 'disabled',
diff --git a/nodejs/src/ingestion/ingestion-consumer.ts b/nodejs/src/ingestion/ingestion-consumer.ts
index d4037b2b5a3c..f501e8595ab7 100644
--- a/nodejs/src/ingestion/ingestion-consumer.ts
+++ b/nodejs/src/ingestion/ingestion-consumer.ts
@@ -296,6 +296,7 @@ export class IngestionConsumer {
PERSON_JSONB_SIZE_ESTIMATE_ENABLE: this.config.PERSON_JSONB_SIZE_ESTIMATE_ENABLE,
PERSON_PROPERTIES_UPDATE_ALL: this.config.PERSON_PROPERTIES_UPDATE_ALL,
FLAG_CALLED_PERSONLESS_DEFAULT_TEAMS: this.config.FLAG_CALLED_PERSONLESS_DEFAULT_TEAMS,
+ EXPERIMENT_EXPOSURE_DUPLICATION_TEAMS: this.config.EXPERIMENT_EXPOSURE_DUPLICATION_TEAMS,
},
concurrentBatches: this.config.INGESTION_WORKER_CONCURRENT_BATCHES,
}
diff --git a/nodejs/src/ingestion/pipelines/analytics/event-subpipeline.ts b/nodejs/src/ingestion/pipelines/analytics/event-subpipeline.ts
index 8d2ef9800631..126c453d3dc3 100644
--- a/nodejs/src/ingestion/pipelines/analytics/event-subpipeline.ts
+++ b/nodejs/src/ingestion/pipelines/analytics/event-subpipeline.ts
@@ -156,7 +156,7 @@ export function createEventSubpipeline<TInput extends EventSubpipelineInput & Wi
]),
{ retry: { tries: 5, sleepMs: 100, name: 'process_groups' } }
)
- .pipe(createCreateEventStep(EVENTS_OUTPUT))
+ .pipe(createCreateEventStep(EVENTS_OUTPUT, options.EXPERIMENT_EXPOSURE_DUPLICATION_TEAMS))
.pipe(
topHog(
createEmitEventStep({
diff --git a/nodejs/src/servers/ingestion-api-server.ts b/nodejs/src/servers/ingestion-api-server.ts
index c30e39c5cfd3..1e64e94a79c6 100644
--- a/nodejs/src/servers/ingestion-api-server.ts
+++ b/nodejs/src/servers/ingestion-api-server.ts
@@ -402,6 +402,7 @@ export class IngestionApiServer implements NodeServer {
PERSON_JSONB_SIZE_ESTIMATE_ENABLE: this.config.PERSON_JSONB_SIZE_ESTIMATE_ENABLE,
PERSON_PROPERTIES_UPDATE_ALL: this.config.PERSON_PROPERTIES_UPDATE_ALL,
FLAG_CALLED_PERSONLESS_DEFAULT_TEAMS: this.config.FLAG_CALLED_PERSONLESS_DEFAULT_TEAMS,
+ EXPERIMENT_EXPOSURE_DUPLICATION_TEAMS: this.config.EXPERIMENT_EXPOSURE_DUPLICATION_TEAMS,
},
concurrentBatches: this.config.INGESTION_WORKER_CONCURRENT_BATCHES,
}
diff --git a/nodejs/tests/ingestion/common/steps/event-processing/process-persons-step.integration.test.ts b/nodejs/tests/ingestion/common/steps/event-processing/process-persons-step.integration.test.ts
index 3b46a58a9490..f0b80ca7152d 100644
--- a/nodejs/tests/ingestion/common/steps/event-processing/process-persons-step.integration.test.ts
+++ b/nodejs/tests/ingestion/common/steps/event-processing/process-persons-step.integration.test.ts
@@ -59,6 +59,7 @@ describe('createProcessPersonsStep', () => {
PERSON_JSONB_SIZE_ESTIMATE_ENABLE: 0,
PERSON_PROPERTIES_UPDATE_ALL: false,
FLAG_CALLED_PERSONLESS_DEFAULT_TEAMS: '*',
+ EXPERIMENT_EXPOSURE_DUPLICATION_TEAMS: '',
}
beforeEach(async () => {
diff --git a/nodejs/tests/ingestion/pipelines/ai/pipelines/ai-event-subpipeline.integration.test.ts b/nodejs/tests/ingestion/pipelines/ai/pipelines/ai-event-subpipeline.integration.test.ts
index eceb5ff34e34..3d6c0099fac5 100644
--- a/nodejs/tests/ingestion/pipelines/ai/pipelines/ai-event-subpipeline.integration.test.ts
+++ b/nodejs/tests/ingestion/pipelines/ai/pipelines/ai-event-subpipeline.integration.test.ts
@@ -104,6 +104,7 @@ function buildPipeline(configOverrides: Partial<AiEventSubpipelineConfig> = {})
PERSON_JSONB_SIZE_ESTIMATE_ENABLE: 0,
PERSON_PROPERTIES_UPDATE_ALL: false,
FLAG_CALLED_PERSONLESS_DEFAULT_TEAMS: '*',
+ EXPERIMENT_EXPOSURE_DUPLICATION_TEAMS: '',
},
outputs: mockOutputs,
teamManager: {