: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
import { AnyPropertyFilter, PropertyFilterType, PropertyOperator } from '~/types'
import {
AUDIENCE_PREFILL_PARAM,
parseBroadcastAudiencePrefill,
urlForNewBroadcastWithAudience,
} from './broadcastAudiencePrefill'
const COHORT_AUDIENCE: AnyPropertyFilter[] = [
{ key: 'id', type: PropertyFilterType.Cohort, value: 7, operator: PropertyOperator.In },
]
describe('broadcastAudiencePrefill', () => {
it('round-trips an audience through the URL', () => {
const url = urlForNewBroadcastWithAudience({ properties: COHORT_AUDIENCE, source: 'cohort' })
const raw = new URLSearchParams(url.split('?')[1]).get(AUDIENCE_PREFILL_PARAM)
expect(parseBroadcastAudiencePrefill(raw)).toEqual(COHORT_AUDIENCE)
})
it.each([
['nothing', undefined],
['a non-JSON string', 'not-json'],
['an empty audience', '[]'],
['an object instead of a list', '{"key":"email"}'],
['an event filter, which a broadcast audience cannot use', '[{"key":"$browser","type":"event","value":"x"}]'],
['a person filter missing its value', '[{"key":"email","type":"person","operator":"exact"}]'],
['a cohort filter without a cohort id', '[{"key":"id","type":"cohort","operator":"in"}]'],
['a person filter missing its operator', '[{"key":"email","type":"person","value":"[email protected]"}]'],
[
'an operator the backend does not know',
'[{"key":"email","type":"person","operator":"nonsense","value":"x"}]',
],
['a cohort filter with a negative id', '[{"key":"id","type":"cohort","value":-3}]'],
[
'one bad filter next to a good one',
'[{"key":"id","type":"cohort","value":7},{"key":"email","type":"person","operator":"exact","value":[]}]',
],
])('returns null for %s', (_label, raw) => {
expect(parseBroadcastAudiencePrefill(raw)).toBeNull()
})
it('keeps only the fields the audience uses, so a hidden field cannot make the backend drop the filter', () => {
const raw = JSON.stringify([
{ key: 'email', type: 'person', operator: 'exact', value: '[email protected]', group_type_index: -1 },
{ key: 'id', type: 'cohort', value: 7, operator: 'in', cohort_name: 'Beta', group_type_index: -1 },
])
expect(parseBroadcastAudiencePrefill(raw)).toEqual([
{
key: 'email',
type: PropertyFilterType.Person,
operator: PropertyOperator.Exact,
value: '[email protected]',
},
{
key: 'id',
type: PropertyFilterType.Cohort,
value: 7,
operator: PropertyOperator.In,
cohort_name: 'Beta',
},
])
})
it('accepts a person filter that needs no value', () => {
const audience = [{ key: 'email', type: PropertyFilterType.Person, operator: PropertyOperator.IsSet }]
expect(parseBroadcastAudiencePrefill(JSON.stringify(audience))).toEqual(audience)
})
})