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(feature-flags): offer an email when a flag goes live for everyone (#120555)

Daniel Marchuk5f68c034ac8c
diff --git a/frontend/src/scenes/feature-flags/featureFlagLogic.test.ts b/frontend/src/scenes/feature-flags/featureFlagLogic.test.ts
index 41a8df5696e2..4ee3c5085201 100644
--- a/frontend/src/scenes/feature-flags/featureFlagLogic.test.ts
+++ b/frontend/src/scenes/feature-flags/featureFlagLogic.test.ts
@@ -70,6 +70,7 @@ import {
dependencyActionLabel,
dependencyDisabledReason,
featureFlagLogic,
+ flagWentLive,
hasDirectFlagDependency,
hasMultipleVariantsActive,
hasStaticCohortDependency,
@@ -1271,6 +1272,120 @@ describe('featureFlagLogic', () => {
const READY = { ...COHORT, is_calculating: false, errors_calculating: 0 }
const FAILED = { ...COHORT, is_calculating: false, errors_calculating: 1 }
+ const everyone = (rollout: number | null, active = true): any => ({
+ id: 1,
+ active,
+ filters: { groups: [{ properties: [], rollout_percentage: rollout }] },
+ })
+ const groupSet = { properties: [], rollout_percentage: 100, aggregation_group_type_index: 0 }
+
+ it.each([
+ { scenario: 'a raise to 100%', before: everyone(50), after: everyone(100), live: true },
+ {
+ scenario: 'enabling a flag already at 100%',
+ before: everyone(100, false),
+ after: everyone(100),
+ live: true,
+ },
+ {
+ scenario: 'a null rollout, which means everyone',
+ before: everyone(0),
+ after: everyone(null),
+ live: true,
+ },
+ { scenario: 'a second save at 100%', before: everyone(100), after: everyone(100), live: false },
+ {
+ scenario: 'the first save of a new flag at 100%',
+ before: { ...everyone(100), id: null },
+ after: everyone(100),
+ live: true,
+ },
+ {
+ scenario: 'a flag gated by early access enrollment',
+ before: everyone(0),
+ after: { ...everyone(100), filters: { ...everyone(100).filters, feature_enrollment: true } },
+ live: false,
+ },
+ {
+ scenario: 'a 100% set that aggregates by group in a person flag',
+ before: everyone(0),
+ after: { ...everyone(0), filters: { groups: [groupSet] } },
+ live: false,
+ },
+ {
+ scenario: 'a raise to 100% from a 100% set that aggregates by group',
+ before: { ...everyone(0), filters: { groups: [groupSet] } },
+ after: everyone(100),
+ live: true,
+ },
+ {
+ scenario: 'a 100% set that aggregates by group and stops evaluation early',
+ before: everyone(0),
+ after: { ...everyone(0), filters: { early_exit: true, groups: [groupSet] } },
+ live: false,
+ },
+ { scenario: 'a partial rollout', before: everyone(0), after: everyone(99), live: false },
+ {
+ scenario: '100% of a targeted group only',
+ before: everyone(0),
+ after: {
+ active: true,
+ filters: {
+ groups: [
+ {
+ properties: [{ key: 'email', type: 'person', value: 'x', operator: 'icontains' }],
+ rollout_percentage: 100,
+ },
+ ],
+ },
+ },
+ live: false,
+ },
+ {
+ scenario: 'a 100% set behind a 0% set',
+ before: everyone(0),
+ after: {
+ active: true,
+ filters: {
+ groups: [
+ { properties: [], rollout_percentage: 0 },
+ { properties: [], rollout_percentage: 100 },
+ ],
+ },
+ },
+ live: true,
+ },
+ {
+ scenario: 'a 100% set behind a 0% set that stops evaluation early',
+ before: everyone(0),
+ after: {
+ active: true,
+ filters: {
+ early_exit: true,
+ groups: [
+ { properties: [], rollout_percentage: 0 },
+ { properties: [], rollout_percentage: 100 },
+ ],
+ },
+ },
+ live: false,
+ },
+ {
+ scenario: 'a multivariate flag',
+ before: everyone(0),
+ after: { ...everyone(100), filters: { ...everyone(100).filters, multivariate: { variants: [] } } },
+ live: false,
+ },
+ {
+ scenario: 'a group-targeted flag',
+ before: everyone(0),
+ after: { ...everyone(100), filters: { ...everyone(100).filters, aggregation_group_type_index: 0 } },
+ live: false,
+ },
+ ])('offers the launch broadcast after $scenario: $live', ({ before, after, live }) => {
+ expect(flagWentLive(before, after)).toBe(live)
+ })
+
it.each([
{
scenario: 'opens the broadcast once the cohort fills on the third check',
diff --git a/frontend/src/scenes/feature-flags/featureFlagLogic.ts b/frontend/src/scenes/feature-flags/featureFlagLogic.ts
index c1857ecee363..356ca27a5c2c 100644
--- a/frontend/src/scenes/feature-flags/featureFlagLogic.ts
+++ b/frontend/src/scenes/feature-flags/featureFlagLogic.ts
@@ -34,7 +34,7 @@ import { FEATURE_FLAGS } from 'lib/constants'
import { describeCron } from 'lib/cron'
import { Dayjs, dayjs } from 'lib/dayjs'
import { scrollToFormError } from 'lib/forms/scrollToFormError'
-import { lemonToast } from 'lib/lemon-ui/LemonToast/LemonToast'
+import { ToastButton, lemonToast } from 'lib/lemon-ui/LemonToast/LemonToast'
import { featureFlagLogic as enabledFeaturesLogic } from 'lib/logic/featureFlagLogic'
import { trackedActionToUrl } from 'lib/logic/scenes/trackedActionToUrl'
import { deleteWithUndo } from 'lib/utils/deleteWithUndo'
@@ -118,6 +118,7 @@ import type {
import {
captureMessageAudienceClicked,
cohortAudienceProperties,
+ messageAudienceAccessDisabledReason,
messageAudienceUrl,
} from 'products/workflows/frontend/MessageAudience/messageAudience'
@@ -175,6 +176,8 @@ const REUSABLE_FLAG_COHORT_MAX_AGE_MS = 60 * 60 * 1000
const FLAG_COHORT_MAX_FILL_MS = 4 * 60 * 60 * 1000
// pinned: sent as the broadcast's entry_source and the click event's source, so renaming it splits the history
const FLAG_BROADCAST_SOURCE = 'feature_flag'
+// pinned: the toast offered when a save turns a flag on for everyone
+const FLAG_LIVE_BROADCAST_SOURCE = 'feature_flag_live'
export const FINDING_FLAG_PEOPLE_MESSAGE = 'Finding the people who have this flag…'
function flagCohortNamePrefix(flagKey: string): string {
@@ -243,6 +246,82 @@ export function broadcastNoOneReason(
return undefined
}
+function isOnForEveryone(flag: Pick<FeatureFlagType, 'active' | 'filters'> | null | undefined): boolean {
+ if (!flag?.active || !isV1FeatureFlagConfig(flag.filters) || flag.filters.multivariate) {
+ return false
+ }
+ // The matcher checks enrollment before release conditions, so a person who opted out never reaches them.
+ if (flag.filters.feature_enrollment || flag.filters.super_groups?.length) {
+ return false
+ }
+ const flagAggregation = flag.filters.aggregation_group_type_index ?? null
+ // A set that aggregates by group matches only requests that carry that group, so it can't cover everyone.
+ const reachesEveryPerson = (group: FeatureFlagGroupType): boolean =>
+ !group.properties?.length &&
+ (group.aggregation_group_type_index === undefined ? flagAggregation : group.aggregation_group_type_index) ===
+ null
+ const groups = flag.filters.groups ?? []
+ if (!flag.filters.early_exit) {
+ return groups.some((group) => reachesEveryPerson(group) && (group.rollout_percentage ?? 100) === 100)
+ }
+ // With early exit, a person who matches a set's properties but falls outside its rollout stops there
+ // with no match, so a partial set ahead of the untargeted one leaves some people without the flag.
+ for (const group of groups) {
+ if ((group.rollout_percentage ?? 100) !== 100) {
+ return false
+ }
+ if (reachesEveryPerson(group)) {
+ return true
+ }
+ }
+ return false
+}
+
+/** True when this save is the one that turned the flag on for everyone, so the launch is worth announcing. */
+export function flagWentLive(
+ before: Pick<FeatureFlagType, 'id' | 'active' | 'filters'> | null | undefined,
+ after: Pick<FeatureFlagType, 'deleted' | 'archived' | 'active' | 'filters'>
+): boolean {
+ // A never-saved baseline holds the form's starting config, which no one has received yet.
+ const savedBefore = before?.id != null ? before : null
+ return isOnForEveryone(after) && !broadcastNoOneReason(after) && !isOnForEveryone(savedBefore)
+}
+
+function liveFlagToastOptions(
+ before: FeatureFlagType | null,
+ after: FeatureFlagType,
+ cohortCreationEnabled: boolean
+): { button: ToastButton; toastId: string } | undefined {
+ const flagId = after.id
+ if (
+ flagId === null ||
+ !cohortCreationEnabled ||
+ messageAudienceAccessDisabledReason() ||
+ !flagWentLive(before, after)
+ ) {
+ return undefined
+ }
+ const button: ToastButton = {
+ label: "Tell people it's live",
+ dataAttr: 'feature-flag-live-broadcast',
+ action: () => {
+ // The toast outlives the logic that showed it: a create redirects from the 'new' key to the saved ID,
+ // and leaving the page unmounts the logic. Bind to the saved ID and hold it mounted for the whole search.
+ const logic = featureFlagLogic({ id: flagId })
+ const unmount = logic.mount()
+ // A search started from the flag's menu is still running. A second one could pass the same reuse
+ // check and fill a second cohort.
+ if (logic.values.broadcastCohortLoading) {
+ unmount()
+ return
+ }
+ void logic.asyncActions.findOrCreateBroadcastCohort(FLAG_LIVE_BROADCAST_SOURCE).finally(unmount)
+ },
+ }
+ // Toasts with the same message share an id and a visible one swallows the next, which would drop this button.
+ return { button, toastId: `feature-flag-live-${flagId}` }
+}
+
function reportFailedToCreateFeatureFlagWithCohort(code: string, detail: string): void {
posthog.capture('failed to create feature flag with cohort', { detail, code })
}
@@ -716,7 +795,12 @@ async function waitForFlagCohort(projectId: string, cohortId: number): Promise<C
return status
}
-function showFlagCohortOutcome(cohort: CohortApi, flagId: FeatureFlagLogicProps['id'], flagKey: string): void {
+function showFlagCohortOutcome(
+ cohort: CohortApi,
+ flagId: FeatureFlagLogicProps['id'],
+ flagKey: string,
+ source: string
+): void {
if (cohort.errors_calculating > 0) {
lemonToast.error("Couldn't find the people who have this flag. Try again in a few minutes.")
return
@@ -735,7 +819,7 @@ function showFlagCohortOutcome(cohort: CohortApi, flagId: FeatureFlagLogicProps[
}
const broadcastUrl = messageAudienceUrl(
{
- source: FLAG_BROADCAST_SOURCE,
+ source,
properties: cohortAudienceProperties(cohort),
broadcastName: `${flagKey} announcement`,
},
@@ -1513,7 +1597,7 @@ export interface featureFlagLogicActions {
editing: boolean
expandAdvanced: boolean
}
- findOrCreateBroadcastCohort: (_: void) => void
+ findOrCreateBroadcastCohort: (source?: string) => string
findOrCreateBroadcastCohortFailure: (
error: string,
errorObject?: any
@@ -1523,10 +1607,10 @@ export interface featureFlagLogicActions {
}
findOrCreateBroadcastCohortSuccess: (
broadcastCohort: CohortApi | null,
- payload?: void
+ payload?: string
) => {
broadcastCohort: CohortApi | null
- payload?: void
+ payload?: string
}
loadCopyDependencyRequirements: () => {
value: true
@@ -3826,7 +3910,7 @@ export const featureFlagLogic = kea<featureFlagLogicType>([
broadcastCohort: [
null as CohortApi | null,
{
- findOrCreateBroadcastCohort: async (_: void, breakpoint) => {
+ findOrCreateBroadcastCohort: async (source: string = FLAG_BROADCAST_SOURCE, breakpoint) => {
if (!props.id || props.id === 'new' || props.id === 'link' || !values.currentProjectId) {
return null
}
@@ -3874,7 +3958,7 @@ export const featureFlagLogic = kea<featureFlagLogicType>([
// The cohort fills in the background. Opening the broadcast earlier would let it send to no one.
const status = await waitForFlagCohort(projectId, cohortId)
dismissBroadcastProgress(cache)
- showFlagCohortOutcome(status, props.id, flag.key)
+ showFlagCohortOutcome(status, props.id, flag.key, source)
return status
},
},
@@ -4357,7 +4441,14 @@ export const featureFlagLogic = kea<featureFlagLogicType>([
// For non-approval errors, let the global error handler show the toast to avoid duplicates
},
saveFeatureFlagSuccess: ({ featureFlag }) => {
- lemonToast.success('Feature flag saved')
+ lemonToast.success(
+ 'Feature flag saved',
+ liveFlagToastOptions(
+ values.originalFeatureFlag,
+ featureFlag,
+ !!values.enabledFeatures[FEATURE_FLAGS.FEATURE_FLAG_COHORT_CREATION]
+ )
+ )
dismissAgentNotices(props.id, cache)
actions.setFeatureFlag(featureFlag)
// Whole flag just persisted — the baseline is now the saved state, so the form reads clean.
@@ -4497,7 +4588,14 @@ export const featureFlagLogic = kea<featureFlagLogicType>([
},
updateFeatureFlagActiveSuccess: ({ featureFlagActiveUpdate }) => {
if (featureFlagActiveUpdate) {
- lemonToast.success(`Feature flag ${featureFlagActiveUpdate.active ? 'enabled' : 'disabled'}`)
+ lemonToast.success(
+ `Feature flag ${featureFlagActiveUpdate.active ? 'enabled' : 'disabled'}`,
+ liveFlagToastOptions(
+ values.originalFeatureFlag,
+ featureFlagActiveUpdate,
+ !!values.enabledFeatures[FEATURE_FLAGS.FEATURE_FLAG_COHORT_CREATION]
+ )
+ )
// Only active/version were persisted, so fold just those onto the working copy and
// the baseline. Taking the whole server flag would drop any in-progress edit and
// re-baseline over it, leaving the guard clean.
@@ -4889,8 +4987,8 @@ export const featureFlagLogic = kea<featureFlagLogicType>([
actions.loadCopyDependencyRequirements()
}
},
- findOrCreateBroadcastCohort: () => {
- captureMessageAudienceClicked(FLAG_BROADCAST_SOURCE, 'broadcast')
+ findOrCreateBroadcastCohort: (source = FLAG_BROADCAST_SOURCE) => {
+ captureMessageAudienceClicked(source, 'broadcast')
dismissBroadcastProgress(cache)
cache.broadcastToastId = lemonToast.loading(FINDING_FLAG_PEOPLE_MESSAGE, {
// A fresh id per click: a dismissed id stays cancelled, which would hide the next click's toast.