: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
feat(replay): keep trigger groups on v2 when last group deleted (#61738)
Kim Svatos Dugan2efffac33f06
diff --git a/frontend/src/lib/components/IngestionControls/triggers/triggerGroups/TriggerGroupsEditor.tsx b/frontend/src/lib/components/IngestionControls/triggers/triggerGroups/TriggerGroupsEditor.tsxindex 12c066247031..b0559eb494a1 100644--- a/frontend/src/lib/components/IngestionControls/triggers/triggerGroups/TriggerGroupsEditor.tsx+++ b/frontend/src/lib/components/IngestionControls/triggers/triggerGroups/TriggerGroupsEditor.tsx@@ -10,7 +10,6 @@ import {LemonModal,LemonSelect,LemonSnack,- LemonTag,} from '@posthog/lemon-ui'import { FlagSelector } from 'lib/components/FlagSelector'@@ -83,6 +82,7 @@ export function TriggerGroupsEditor(): JSX.Element {previewLegacyGroups,_savingStateLoading,shouldShowMigrationBanner,+ hasLegacyTriggers,} = useValues(replayTriggersV2Logic)const {addTriggerGroup,@@ -135,7 +135,7 @@ export function TriggerGroupsEditor(): JSX.Element {</div>{!isAddingGroup && !editingGroupId && (<div className="flex gap-2">- {triggerGroups.length === 0 && (+ {hasLegacyTriggers && (<LemonButtontype="secondary"size="small"@@ -537,80 +537,19 @@ interface DeleteLastGroupModalProps {}function DeleteLastGroupModal({ isOpen, onClose, onConfirm, groupName }: DeleteLastGroupModalProps): JSX.Element {- const { legacyTriggersPreview } = useValues(replayTriggersV2Logic)-- const { sampleRate, minDurationMs, matchType, urls, events, flag, hasConditions } = legacyTriggersPreview-return (<LemonModal isOpen={isOpen} onClose={onClose} title="Delete last trigger group?"><div className="space-y-4"><p>- Deleting "{groupName}" will remove all trigger groups and your project will revert to using{' '}- <strong>legacy recording triggers</strong>.+ "{groupName}" is your only trigger group. Deleting it will replace it with a default group that{' '}+ <strong>records all sessions</strong>, so your project keeps using trigger groups.</p><p className="text-sm text-muted">- Your project will immediately use the following legacy trigger configuration:+ To record a subset of sessions instead, edit the group's conditions or sample rate rather than+ deleting it. To stop recording entirely, disable session recording in General settings.</p>- <div className="border rounded p-3 bg-surface-primary">- {hasConditions ? (- <>- <div className="mb-2">- <span className="text-sm">- Match <b>sessions</b> against{' '}- <LemonTag type="success" className="uppercase">- {matchType}- </LemonTag>{' '}- criteria- </span>- </div>-- {/* Conditions */}- <div className="space-y-2 text-sm mb-3">- {urls.length > 0 && (- <div className="flex items-center gap-2 flex-wrap">- <span className="text-muted">User has visited URL matching pattern</span>- {urls.map((u) => (- <LemonSnack key={u.url}>{u.url}</LemonSnack>- ))}- </div>- )}- {events.length > 0 && (- <div className="flex items-center gap-2 flex-wrap">- <span className="text-muted">Event</span>- {events.map((event) => (- <LemonSnack key={event}>{event}</LemonSnack>- ))}- <span className="text-muted">occurred</span>- </div>- )}- {flag && (- <div className="flex items-center gap-2 flex-wrap">- <span className="text-muted">Feature flag</span>- <LemonSnack>{typeof flag === 'string' ? flag : flag.key}</LemonSnack>- <span className="text-muted">is enabled</span>- </div>- )}- </div>- </>- ) : null}-- {/* Sample rate - always shown */}- <div className="flex items-center gap-2 text-sm">- <span className="text-muted">Sample</span>- <span className="font-semibold">{Math.round(sampleRate * 100)}%</span>- <span className="text-muted">of all sessions</span>- </div>-- {/* Minimum duration */}- {minDurationMs !== undefined && minDurationMs !== null && minDurationMs > 0 && (- <div className="text-sm text-muted mt-3">- Minimum duration: <b>{minDurationMs / 1000}</b> seconds- </div>- )}- </div>-<div className="flex justify-end gap-2 pt-2 border-t"><LemonButton type="secondary" onClick={onClose}>Cancel@@ -621,7 +560,7 @@ function DeleteLastGroupModal({ isOpen, onClose, onConfirm, groupName }: DeleteLonClick={onConfirm}data-attr="trigger-group-delete-last-confirm">- Delete and use legacy trigger settings+ Delete and record all sessions</LemonButton></div></div>diff --git a/frontend/src/lib/components/IngestionControls/triggers/triggerGroups/replayTriggersV2Logic.test.ts b/frontend/src/lib/components/IngestionControls/triggers/triggerGroups/replayTriggersV2Logic.test.tsindex 7ca871961b6e..62550c581a99 100644--- a/frontend/src/lib/components/IngestionControls/triggers/triggerGroups/replayTriggersV2Logic.test.ts+++ b/frontend/src/lib/components/IngestionControls/triggers/triggerGroups/replayTriggersV2Logic.test.ts@@ -2,6 +2,7 @@ import { expectLogic } from 'kea-test-utils'import { teamLogic } from 'scenes/teamLogic'+import { useMocks } from '~/mocks/jest'import { initKeaTests } from '~/test/init'import { replayTriggersV2Logic } from './replayTriggersV2Logic'@@ -10,6 +11,11 @@ describe('replayTriggersV2Logic', () => {let logic: ReturnType<typeof replayTriggersV2Logic.build>beforeEach(() => {+ useMocks({+ patch: {+ 'api/environments/:id': (req) => [200, { id: 1, ...(req.body as Record<string, any>) }],+ },+ })initKeaTests()logic = replayTriggersV2Logic()logic.mount()@@ -135,4 +141,66 @@ describe('replayTriggersV2Logic', () => {})})})++ describe('deleteTriggerGroup stays on V2', () => {+ it('deleting the only group replaces it with a record-everything group', async () => {+ logic.actions.setTriggerGroupsConfig({+ version: 2,+ groups: [+ {+ id: 'group-1',+ name: 'Checkout',+ sampleRate: 0.5,+ conditions: { matchType: 'all', urls: [{ url: '^/checkout$', matching: 'regex' }] },+ },+ ],+ })++ await expectLogic(logic, () => {+ logic.actions.deleteTriggerGroup('group-1')+ }).toMatchValues({+ triggerGroupsConfig: {+ version: 2,+ groups: [+ {+ id: expect.any(String),+ name: 'Record all sessions',+ sampleRate: 1,+ conditions: { matchType: 'all' },+ },+ ],+ },+ })+ })++ it('deleting one of several groups keeps the rest', async () => {+ const group2 = {+ id: 'group-2',+ name: 'Signup',+ sampleRate: 1,+ conditions: { matchType: 'all' as const, events: [{ name: 'signed_up' }] },+ }+ logic.actions.setTriggerGroupsConfig({+ version: 2,+ groups: [+ {+ id: 'group-1',+ name: 'Checkout',+ sampleRate: 0.5,+ conditions: { matchType: 'all', urls: [{ url: '^/checkout$', matching: 'regex' }] },+ },+ group2,+ ],+ })++ await expectLogic(logic, () => {+ logic.actions.deleteTriggerGroup('group-1')+ }).toMatchValues({+ triggerGroupsConfig: {+ version: 2,+ groups: [group2],+ },+ })+ })+ })})diff --git a/frontend/src/lib/components/IngestionControls/triggers/triggerGroups/replayTriggersV2Logic.ts b/frontend/src/lib/components/IngestionControls/triggers/triggerGroups/replayTriggersV2Logic.tsindex 44e9c9385eae..34b03d725b66 100644--- a/frontend/src/lib/components/IngestionControls/triggers/triggerGroups/replayTriggersV2Logic.ts+++ b/frontend/src/lib/components/IngestionControls/triggers/triggerGroups/replayTriggersV2Logic.ts@@ -6,14 +6,26 @@ import { teamLogic } from 'scenes/teamLogic'import {EventTriggerConfig,- LinkedFeatureFlag,SessionRecordingTriggerGroup,SessionRecordingTriggerGroupsConfig,- UrlTriggerConfig,} from '~/lib/components/IngestionControls/types'import type { replayTriggersV2LogicType } from './replayTriggersV2LogicType'+/**+ * A conditionless group at 100% sample rate matches every session, so it's the explicit "record+ * everything" state in V2. We keep teams on this rather than dropping to zero groups (which would+ * fall back to legacy V1 config) so V2 stays the source of truth.+ */+export function buildRecordEverythingGroup(): SessionRecordingTriggerGroup {+ return {+ id: uuid(),+ name: 'Record all sessions',+ sampleRate: 1,+ conditions: { matchType: 'all' },+ }+}+export const replayTriggersV2Logic = kea<replayTriggersV2LogicType>([path(['lib', 'components', 'IngestionControls', 'triggers', 'triggerGroups', 'replayTriggersV2Logic']),connect({@@ -91,9 +103,12 @@ export const replayTriggersV2Logic = kea<replayTriggersV2LogicType>([if (!state) {return state}+ const groups = state.groups.filter((g) => g.id !== id)+ // Stay on V2 when the last group is removed: a record-everything group keeps the+ // team on trigger groups (record all) instead of falling back to legacy V1 config.return {...state,- groups: state.groups.filter((g) => g.id !== id),+ groups: groups.length === 0 ? [buildRecordEverythingGroup()] : groups,}},updateTriggerGroup: (state, { id, updates }) => {@@ -255,34 +270,6 @@ export const replayTriggersV2Logic = kea<replayTriggersV2LogicType>([]},],- legacyTriggersPreview: [- (s) => [s.currentTeam],- (- team- ): {- sampleRate: number- minDurationMs: number | undefined- matchType: 'any' | 'all'- urls: UrlTriggerConfig[]- events: string[]- flag: string | LinkedFeatureFlag | null- hasConditions: boolean- } => {- const sampleRate = team?.session_recording_sample_rate- ? parseFloat(team.session_recording_sample_rate)- : 1- const minDurationMs = team?.session_recording_minimum_duration_milliseconds ?? undefined- const matchType = team?.session_recording_trigger_match_type_config || 'all'- const urls = team?.session_recording_url_trigger_config || []- const events: string[] = (team?.session_recording_event_trigger_config || []).filter(- (e): e is string => typeof e === 'string' && e.length > 0- )- const flag = team?.session_recording_linked_flag || null- const hasConditions = urls.length > 0 || events.length > 0 || !!flag-- return { sampleRate, minDurationMs, matchType, urls, events, flag, hasConditions }- },- ],}),listeners(({ asyncActions }) => ({addTriggerGroup: async () => {diff --git a/frontend/src/scenes/settings/environment/ReplayTriggers.tsx b/frontend/src/scenes/settings/environment/ReplayTriggers.tsxindex 5b1bad4653ab..f71a6db0e62c 100644--- a/frontend/src/scenes/settings/environment/ReplayTriggers.tsx+++ b/frontend/src/scenes/settings/environment/ReplayTriggers.tsx@@ -399,12 +399,8 @@ export function ReplayTriggers(): JSX.Element {<strong>JavaScript SDK version compatibility</strong><ul className="list-disc ml-4 mt-2 space-y-1"><li>- Older SDK versions (< v{TRIGGER_GROUPS_MIN_SDK_VERSION}) will use the legacy- recording conditions below- </li>- <li>- Newer SDK versions (>= v{TRIGGER_GROUPS_MIN_SDK_VERSION}) will use trigger- groups if configured, otherwise will fallback to the legacy recording conditions+ SDK versions >= v{TRIGGER_GROUPS_MIN_SDK_VERSION} use trigger groups if+ configured, otherwise fall back to the legacy recording conditions</li><li>Both configurations are sent to ensure backward compatibility with all