: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
fix(replay): save a typed URL pattern with the trigger group (#113565)
HaynesPostHog68a4d3062e26
diff --git a/frontend/src/lib/components/IngestionControls/triggers/triggerGroups/TriggerGroupsEditor.tsx b/frontend/src/lib/components/IngestionControls/triggers/triggerGroups/TriggerGroupsEditor.tsxindex 3f580f1bcc89..34a1f979cd42 100644--- a/frontend/src/lib/components/IngestionControls/triggers/triggerGroups/TriggerGroupsEditor.tsx+++ b/frontend/src/lib/components/IngestionControls/triggers/triggerGroups/TriggerGroupsEditor.tsx@@ -455,7 +455,7 @@ function GroupForm({ group, onSave, onCancel }: GroupFormProps): JSX.Element {Cancel</LemonButton><LemonButton type="primary" onClick={() => addUrl(newUrl)}>- Save+ Add</LemonButton></div>{triggerGroup.urls.length > 0 && (diff --git a/frontend/src/lib/components/IngestionControls/triggers/triggerGroups/triggerGroupFormLogic.test.ts b/frontend/src/lib/components/IngestionControls/triggers/triggerGroups/triggerGroupFormLogic.test.tsnew file mode 100644index 000000000000..8233248bc4f0--- /dev/null+++ b/frontend/src/lib/components/IngestionControls/triggers/triggerGroups/triggerGroupFormLogic.test.ts@@ -0,0 +1,40 @@+import { expectLogic } from 'kea-test-utils'++import { initKeaTests } from '~/test/init'++import { triggerGroupFormLogic } from './triggerGroupFormLogic'++describe('triggerGroupFormLogic', () => {+ it.each([+ ['includes a typed pattern that was never added', '/play/.*', false, ['^/checkout$', '^/play/.*$']],+ ['ignores a typed duplicate', '/checkout', false, ['^/checkout$']],+ ['does not save an invalid typed regex', '/play/(', false, null],+ ['drops a typed pattern after the URL editor is canceled', '/play/.*', true, ['^/checkout$']],+ ['drops an invalid typed regex after the URL editor is canceled', '/play/(', true, ['^/checkout$']],+ ])('submit %s', async (_, typed, canceled, expectedUrls) => {+ initKeaTests()+ const onSave = jest.fn()+ const logic = triggerGroupFormLogic({+ group: {+ id: 'group-1',+ name: 'Checkout',+ sampleRate: 1,+ conditions: { matchType: 'any', urls: [{ url: '^/checkout$', matching: 'regex' }] },+ },+ onSave,+ onCancel: jest.fn(),+ })+ logic.mount()++ logic.actions.setIsAddingUrl(true)+ logic.actions.setNewUrl(typed)+ if (canceled) {+ logic.actions.setIsAddingUrl(false)+ }+ await expectLogic(logic, () => logic.actions.submitTriggerGroup()).toFinishAllListeners()++ expect(onSave.mock.calls[0]?.[0].conditions.urls.map((u: { url: string }) => u.url) ?? null).toEqual(+ expectedUrls+ )+ })+})diff --git a/frontend/src/lib/components/IngestionControls/triggers/triggerGroups/triggerGroupFormLogic.ts b/frontend/src/lib/components/IngestionControls/triggers/triggerGroups/triggerGroupFormLogic.tsindex 866fb94d63f4..14946dd1a85e 100644--- a/frontend/src/lib/components/IngestionControls/triggers/triggerGroups/triggerGroupFormLogic.ts+++ b/frontend/src/lib/components/IngestionControls/triggers/triggerGroups/triggerGroupFormLogic.ts@@ -23,6 +23,25 @@ function isValidRegex(pattern: string): boolean {}}+type UrlPatternResult = { url: string } | { error: 'empty' | 'invalid' | 'duplicate' }++function prepareUrlPattern(raw: string, existing: UrlTriggerConfig[]): UrlPatternResult {+ const trimmedUrl = raw.trim()+ if (!trimmedUrl) {+ return { error: 'empty' }+ }+ if (!isValidRegex(trimmedUrl)) {+ return { error: 'invalid' }+ }+ const anchoredUrl = ensureAnchored(trimmedUrl)+ if (existing.some((u) => u.url === anchoredUrl)) {+ return { error: 'duplicate' }+ }+ return { url: anchoredUrl }+}++const INVALID_REGEX_MESSAGE = 'Invalid regex pattern. Please check your syntax.'+/** Normalize stored events to the object format for the form */function normalizeEvents(events: (string | EventTriggerConfig)[] | undefined): EventTriggerConfig[] {return (events || []).map((e) => (typeof e === 'string' ? { name: e } : e))@@ -277,6 +296,7 @@ export const triggerGroupFormLogic = kea<triggerGroupFormLogicType>(['',{setNewUrl: (_, { url }) => url,+ setIsAddingUrl: (state, { isAdding }) => (isAdding ? state : ''),addUrl: () => '',},],@@ -293,7 +313,7 @@ export const triggerGroupFormLogic = kea<triggerGroupFormLogicType>([},],}),- forms(({ props }) => ({+ forms(({ props, values }) => ({triggerGroup: {defaults: {name: props.group?.name || '',@@ -322,6 +342,17 @@ export const triggerGroupFormLogic = kea<triggerGroupFormLogicType>([name: !name?.trim() ? 'Group name is required' : undefined,}),submit: async (formValues) => {+ // A pattern still in the input counts as added, so one Save is enough+ const pendingUrl = prepareUrlPattern(values.newUrl, formValues.urls)+ if ('error' in pendingUrl && pendingUrl.error === 'invalid') {+ lemonToast.error(INVALID_REGEX_MESSAGE, { hideButton: true })+ return+ }+ const urls =+ 'url' in pendingUrl+ ? [...formValues.urls, { url: pendingUrl.url, matching: 'regex' as const }]+ : formValues.urls+const savedGroup: SessionRecordingTriggerGroup = {id: props.group?.id || uuid(),name: formValues.name.trim(),@@ -330,7 +361,7 @@ export const triggerGroupFormLogic = kea<triggerGroupFormLogicType>([conditions: {matchType: formValues.matchType,events: formValues.events.length > 0 ? formValues.events : undefined,- urls: formValues.urls.length > 0 ? formValues.urls : undefined,+ urls: urls.length > 0 ? urls : undefined,flag: formValues.flag || undefined,properties: formValues.properties.length > 0 ? formValues.properties : undefined,},@@ -341,26 +372,19 @@ export const triggerGroupFormLogic = kea<triggerGroupFormLogicType>([})),listeners(({ actions, values }) => ({addUrl: ({ url }) => {- const trimmedUrl = url.trim()- if (!trimmedUrl) {- return- }-- if (!isValidRegex(trimmedUrl)) {- lemonToast.error('Invalid regex pattern. Please check your syntax.', { hideButton: true })- return- }-- const anchoredUrl = ensureAnchored(trimmedUrl)-- if (values.triggerGroup.urls.find((u) => u.url === anchoredUrl)) {- lemonToast.warning('This URL pattern has already been added')+ const result = prepareUrlPattern(url, values.triggerGroup.urls)+ if ('error' in result) {+ if (result.error === 'invalid') {+ lemonToast.error(INVALID_REGEX_MESSAGE, { hideButton: true })+ } else if (result.error === 'duplicate') {+ lemonToast.warning('This URL pattern has already been added')+ }return}actions.setTriggerGroupValue('urls', [...values.triggerGroup.urls,- { url: anchoredUrl, matching: 'regex' as const },+ { url: result.url, matching: 'regex' as const },])},removeUrl: ({ url }) => {