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): scope web experiment variants to a url (#127678)

Phill2f06ed55e3fa
diff --git a/frontend/src/toolbar/extension/components/experiments/ExtensionExperimentEditorProvider.tsx b/frontend/src/toolbar/extension/components/experiments/ExtensionExperimentEditorProvider.tsx
index 4a7db831e673..57fc18ba4a4c 100644
--- a/frontend/src/toolbar/extension/components/experiments/ExtensionExperimentEditorProvider.tsx
+++ b/frontend/src/toolbar/extension/components/experiments/ExtensionExperimentEditorProvider.tsx
@@ -12,7 +12,8 @@ import { extensionExperimentsLogic } from '../../logics/panel/extensionExperimen
/** Binds the toolbar's web experiment editor components to the panel's copy of the form. The page applies the changes. */
export function ExtensionExperimentEditorProvider({ children }: { children: React.ReactNode }): JSX.Element | null {
const { experimentsState, experimentForm } = useValues(extensionExperimentsLogic)
- const { sendExperimentsCommand, renameVariant, setTransformField } = useActions(extensionExperimentsLogic)
+ const { sendExperimentsCommand, renameVariant, setTransformField, setVariantUrlCondition } =
+ useActions(extensionExperimentsLogic)
const editor = useMemo((): WebExperimentEditor | null => {
if (!experimentsState) {
@@ -43,8 +44,16 @@ export function ExtensionExperimentEditorProvider({ children }: { children: Reac
setTransformationField: setTransformField,
setTransformationSelector: (variant, index, selector) =>
sendExperimentsCommand({ type: 'set-experiment-transform-selector', variant, index, selector }),
+ setVariantUrlCondition,
}
- }, [experimentsState, experimentForm.variants, sendExperimentsCommand, renameVariant, setTransformField])
+ }, [
+ experimentsState,
+ experimentForm.variants,
+ sendExperimentsCommand,
+ renameVariant,
+ setTransformField,
+ setVariantUrlCondition,
+ ])
return editor ? (
<WebExperimentEditorContext.Provider value={editor}>{children}</WebExperimentEditorContext.Provider>
diff --git a/frontend/src/toolbar/extension/logics/panel/extensionExperimentsLogic.ts b/frontend/src/toolbar/extension/logics/panel/extensionExperimentsLogic.ts
index 6a0679718938..ba9d851b0084 100644
--- a/frontend/src/toolbar/extension/logics/panel/extensionExperimentsLogic.ts
+++ b/frontend/src/toolbar/extension/logics/panel/extensionExperimentsLogic.ts
@@ -3,7 +3,8 @@ import { forms } from 'kea-forms'
import type { DeepPartial, DeepPartialMap, FieldName, ValidationErrorType } from 'kea-forms'
import { subscriptions } from 'kea-subscriptions'
-import { canRenameVariant } from '~/toolbar/shared/experiments/webExperimentEditorContext'
+import { canRenameVariant, variantsWithUrlCondition } from '~/toolbar/shared/experiments/webExperimentEditorContext'
+import type { WebExperimentUrlMatchType } from '~/toolbar/shared/types'
import { capturePageToolUsed } from '../../browser/extensionTelemetry'
import { PageFormSync } from '../../messaging/pageFormSync'
@@ -90,6 +91,15 @@ export interface extensionExperimentsLogicActions {
value: string
variant: string
}
+ setVariantUrlCondition: (
+ variant: string,
+ url: string,
+ urlMatchType: WebExperimentUrlMatchType
+ ) => {
+ url: string
+ urlMatchType: WebExperimentUrlMatchType
+ variant: string
+ }
submitExperimentForm: () => {
value: boolean
}
@@ -151,6 +161,11 @@ export const extensionExperimentsLogic = kea<extensionExperimentsLogicType>([
field,
value,
}),
+ setVariantUrlCondition: (variant: string, url: string, urlMatchType: WebExperimentUrlMatchType) => ({
+ variant,
+ url,
+ urlMatchType,
+ }),
setSaveRequested: (saveRequested: boolean) => ({ saveRequested }),
}),
forms(({ actions }) => ({
@@ -231,6 +246,12 @@ export const extensionExperimentsLogic = kea<extensionExperimentsLogicType>([
})
}
},
+ setVariantUrlCondition: ({ variant, url, urlMatchType }) => {
+ const nextVariants = variantsWithUrlCondition(values.experimentForm.variants, variant, url, urlMatchType)
+ if (nextVariants) {
+ actions.setExperimentFormValue('variants', nextVariants)
+ }
+ },
renameVariant: ({ variant, newKey }) => {
const { variants } = values.experimentForm
if (!canRenameVariant(variants, variant, newKey)) {
diff --git a/frontend/src/toolbar/floating/components/experiments/ExperimentsTabEditorProvider.test.tsx b/frontend/src/toolbar/floating/components/experiments/ExperimentsTabEditorProvider.test.tsx
index fbce6b5c66b6..bdd541d5ebec 100644
--- a/frontend/src/toolbar/floating/components/experiments/ExperimentsTabEditorProvider.test.tsx
+++ b/frontend/src/toolbar/floating/components/experiments/ExperimentsTabEditorProvider.test.tsx
@@ -52,4 +52,21 @@ describe('WebExperimentVariant', () => {
expect(secondPanel).toHaveAttribute('aria-expanded', 'true')
expect(within(secondPanel).getByText('Select element')).toBeInTheDocument()
})
+
+ it('scopes the variant to the url you type', async () => {
+ setVariantTransforms(['.headline'])
+
+ // The renders of the tests above stay in the document, so the query takes this container only.
+ const { container } = render(
+ <ExperimentsTabEditorProvider>
+ <WebExperimentVariant variant="test" />
+ </ExperimentsTabEditorProvider>
+ )
+ await userEvent.type(within(container).getByPlaceholderText('https://example.com/pricing'), '/pricing')
+
+ expect(experimentsTabLogic.values.experimentForm.variants?.test.conditions).toEqual({
+ url: '/pricing',
+ urlMatchType: 'icontains',
+ })
+ })
})
diff --git a/frontend/src/toolbar/floating/components/experiments/ExperimentsTabEditorProvider.tsx b/frontend/src/toolbar/floating/components/experiments/ExperimentsTabEditorProvider.tsx
index ad4c1923cfbd..1423964f360d 100644
--- a/frontend/src/toolbar/floating/components/experiments/ExperimentsTabEditorProvider.tsx
+++ b/frontend/src/toolbar/floating/components/experiments/ExperimentsTabEditorProvider.tsx
@@ -7,6 +7,7 @@ import {
WebExperimentEditor,
WebExperimentEditorContext,
canRenameVariant,
+ variantsWithUrlCondition,
} from '~/toolbar/shared/experiments/webExperimentEditorContext'
/** Binds the web experiment editor components to the floating toolbar's experiment form. */
@@ -87,6 +88,12 @@ export function ExperimentsTabEditorProvider({ children }: { children: React.Rea
inspectElementSelected(element as HTMLElement, variant, index, selector)
}
},
+ setVariantUrlCondition: (variant, url, urlMatchType) => {
+ const nextVariants = variantsWithUrlCondition(variants, variant, url, urlMatchType)
+ if (nextVariants) {
+ setExperimentFormValue('variants', nextVariants)
+ }
+ },
}
}, [
experimentForm.variants,
diff --git a/frontend/src/toolbar/shared/experiments/WebExperimentVariant.tsx b/frontend/src/toolbar/shared/experiments/WebExperimentVariant.tsx
index ced3826367fa..d3eb312b7ca1 100644
--- a/frontend/src/toolbar/shared/experiments/WebExperimentVariant.tsx
+++ b/frontend/src/toolbar/shared/experiments/WebExperimentVariant.tsx
@@ -10,6 +10,7 @@ import { LemonInput } from 'lib/lemon-ui/LemonInput/LemonInput'
import { useWebExperimentEditor } from '~/toolbar/shared/experiments/webExperimentEditorContext'
import { WebExperimentTransformField } from '~/toolbar/shared/experiments/WebExperimentTransformField'
import { WebExperimentTransformHeader } from '~/toolbar/shared/experiments/WebExperimentTransformHeader'
+import { WebExperimentVariantUrlCondition } from '~/toolbar/shared/experiments/WebExperimentVariantUrlCondition'
interface WebExperimentVariantProps {
variant: string
@@ -41,6 +42,7 @@ export function WebExperimentVariant({ variant }: WebExperimentVariantProps): JS
/>
</div>
)}
+ <WebExperimentVariantUrlCondition variant={variant} />
<div className="flex items-center justify-between mb-2">
<LemonLabel>Transformations</LemonLabel>
<LemonButton
diff --git a/frontend/src/toolbar/shared/experiments/WebExperimentVariantUrlCondition.tsx b/frontend/src/toolbar/shared/experiments/WebExperimentVariantUrlCondition.tsx
new file mode 100644
index 000000000000..b271753ece39
--- /dev/null
+++ b/frontend/src/toolbar/shared/experiments/WebExperimentVariantUrlCondition.tsx
@@ -0,0 +1,52 @@
+import { LemonLabel } from '@posthog/lemon-ui'
+
+import { LemonInput } from 'lib/lemon-ui/LemonInput/LemonInput'
+import { LemonSelect } from 'lib/lemon-ui/LemonSelect'
+
+import { useWebExperimentEditor } from '~/toolbar/shared/experiments/webExperimentEditorContext'
+import type { WebExperimentUrlMatchType } from '~/toolbar/shared/types'
+
+const URL_MATCH_OPTIONS: { value: WebExperimentUrlMatchType; label: string }[] = [
+ { value: 'icontains', label: 'contains' },
+ { value: 'not_icontains', label: 'does not contain' },
+ { value: 'exact', label: 'is' },
+ { value: 'is_not', label: 'is not' },
+ { value: 'regex', label: 'matches regex' },
+ { value: 'not_regex', label: 'does not match regex' },
+]
+
+interface WebExperimentVariantUrlConditionProps {
+ variant: string
+}
+
+export function WebExperimentVariantUrlCondition({ variant }: WebExperimentVariantUrlConditionProps): JSX.Element {
+ const { variants, setVariantUrlCondition } = useWebExperimentEditor()
+ const conditions = variants[variant]?.conditions
+ const url = conditions?.url ?? ''
+ const urlMatchType = conditions?.urlMatchType ?? 'icontains'
+
+ return (
+ <div>
+ <LemonLabel>Pages to change</LemonLabel>
+ <div className="flex flex-wrap items-center gap-2 my-1">
+ <span>URL</span>
+ <LemonSelect
+ size="small"
+ value={urlMatchType}
+ options={URL_MATCH_OPTIONS}
+ onChange={(newUrlMatchType) => setVariantUrlCondition(variant, url, newUrlMatchType)}
+ data-attr="web-experiment-variant-url-match-type"
+ />
+ <LemonInput
+ size="small"
+ className="flex-1 min-w-40"
+ value={url}
+ onChange={(newUrl) => setVariantUrlCondition(variant, newUrl, urlMatchType)}
+ placeholder="https://example.com/pricing"
+ data-attr="web-experiment-variant-url"
+ />
+ </div>
+ <span className="text-xs text-secondary">Leave empty to change every page the element is on.</span>
+ </div>
+ )
+}
diff --git a/frontend/src/toolbar/shared/experiments/experimentsTabLogic.test.ts b/frontend/src/toolbar/shared/experiments/experimentsTabLogic.test.ts
index c9e6a5106709..f1f579c1230e 100644
--- a/frontend/src/toolbar/shared/experiments/experimentsTabLogic.test.ts
+++ b/frontend/src/toolbar/shared/experiments/experimentsTabLogic.test.ts
@@ -75,6 +75,8 @@ const installFetchMock = (): void => {
)
}
describe('experimentsTabLogic', () => {
+ // A new variant starts scoped to the page it was created on, which is the test page here.
+ const currentPageConditions = { url: 'http://localhost/project/997', urlMatchType: 'icontains' }
let theExperimentsTabLogic: ReturnType<typeof experimentsTabLogic.build>
let theExperimentsLogic: ReturnType<typeof experimentsLogic.build>
let theToolbarLogic: ReturnType<typeof toolbarLogic.build>
@@ -180,6 +182,7 @@ describe('experimentsTabLogic', () => {
test: {
is_new: true,
transforms: [{}],
+ conditions: currentPageConditions,
rollout_percentage: 50,
},
},
@@ -206,7 +209,7 @@ describe('experimentsTabLogic', () => {
transforms: [{}],
rollout_percentage: 50,
is_new: true,
- conditions: null,
+ conditions: currentPageConditions,
},
},
original_html_state: {},
@@ -254,6 +257,7 @@ describe('experimentsTabLogic', () => {
test: {
is_new: true,
transforms: [{}],
+ conditions: currentPageConditions,
rollout_percentage: 50,
},
},
diff --git a/frontend/src/toolbar/shared/experiments/experimentsTabLogic.tsx b/frontend/src/toolbar/shared/experiments/experimentsTabLogic.tsx
index dde7197c2822..8102c06a91e8 100644
--- a/frontend/src/toolbar/shared/experiments/experimentsTabLogic.tsx
+++ b/frontend/src/toolbar/shared/experiments/experimentsTabLogic.tsx
@@ -18,13 +18,22 @@ import {
import { toolbarApi } from '~/toolbar/shared/toolbarApi'
import { toolbarConfigLogic } from '~/toolbar/shared/toolbarConfigLogic'
import { toolbarPosthogJS } from '~/toolbar/shared/toolbarPosthogJS'
-import { WebExperiment, WebExperimentDraftType, WebExperimentForm } from '~/toolbar/shared/types'
+import { WebExperiment, WebExperimentDraftType, WebExperimentForm, WebExperimentVariant } from '~/toolbar/shared/types'
import { urls } from '~/toolbar/shared/urls'
import { elementToQuery, isLaunched, joinWithUiHost, percentageDistribution } from '~/toolbar/shared/utils'
import { Experiment, ExperimentIdType } from '~/types'
import type { ToolbarUserIntent } from '../../../types'
+// The element picker finds a selector that is unique on this page, and a shared component renders the same
+// selector elsewhere, so a new variant starts scoped to the page the user picked on.
+function currentPageCondition(): WebExperimentVariant['conditions'] {
+ return {
+ url: window.location.origin + window.location.pathname,
+ urlMatchType: 'icontains',
+ }
+}
+
function newExperiment(): WebExperimentForm {
return {
name: '',
@@ -36,6 +45,7 @@ function newExperiment(): WebExperimentForm {
test: {
is_new: true,
transforms: [{}],
+ conditions: currentPageCondition(),
rollout_percentage: 50,
},
},
@@ -639,7 +649,7 @@ export const experimentsTabLogic = kea<experimentsTabLogicType>([
values.experimentForm.variants[nextVariantName] = {
is_new: true,
transforms: [{}],
- conditions: null,
+ conditions: currentPageCondition(),
rollout_percentage: 0,
}
diff --git a/frontend/src/toolbar/shared/experiments/webExperimentEditorContext.ts b/frontend/src/toolbar/shared/experiments/webExperimentEditorContext.ts
index 2946754ae4d7..daa239a85684 100644
--- a/frontend/src/toolbar/shared/experiments/webExperimentEditorContext.ts
+++ b/frontend/src/toolbar/shared/experiments/webExperimentEditorContext.ts
@@ -1,6 +1,6 @@
import { createContext, useContext } from 'react'
-import type { WebExperimentVariant } from '~/toolbar/shared/types'
+import type { WebExperimentUrlMatchType, WebExperimentVariant } from '~/toolbar/shared/types'
import type { ExperimentIdType } from '~/types'
import type { ElementSelectorType } from './elementSelectorTypes'
@@ -28,6 +28,8 @@ export interface WebExperimentEditor {
inspectForTransformation: (variant: string, index: number | null) => void
setTransformationField: (variant: string, index: number, field: 'html' | 'css', value: string) => void
setTransformationSelector: (variant: string, index: number, selector: string) => void
+ /** An empty url lets the variant change every page. */
+ setVariantUrlCondition: (variant: string, url: string, urlMatchType: WebExperimentUrlMatchType) => void
}
export function canRenameVariant(
@@ -38,6 +40,26 @@ export function canRenameVariant(
return !!variants[variant] && !!newKey && newKey !== variant && !variants[newKey]
}
+/** Returns null when the variant is gone, so a caller can leave the form alone. */
+export function variantsWithUrlCondition(
+ variants: Record<string, WebExperimentVariant>,
+ variant: string,
+ url: string,
+ urlMatchType: WebExperimentUrlMatchType
+): Record<string, WebExperimentVariant> | null {
+ const webVariant = variants[variant]
+ if (!webVariant) {
+ return null
+ }
+ return {
+ ...variants,
+ [variant]: {
+ ...webVariant,
+ conditions: { ...webVariant.conditions, url: url || undefined, urlMatchType },
+ },
+ }
+}
+
export const WebExperimentEditorContext = createContext<WebExperimentEditor | null>(null)
export function useWebExperimentEditor(): WebExperimentEditor {
diff --git a/frontend/src/toolbar/shared/types.ts b/frontend/src/toolbar/shared/types.ts
index 31a7fd348652..001e6e72ff7d 100644
--- a/frontend/src/toolbar/shared/types.ts
+++ b/frontend/src/toolbar/shared/types.ts
@@ -127,7 +127,7 @@ export interface WebExperimentVariant {
conditions?: {
url?: string
urlMatchType?: WebExperimentUrlMatchType
- utm: {
+ utm?: {
utm_source?: string
utm_medium?: string
utm_campaign?: string