: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, EventPropertyFilter, PropertyFilterType, PropertyOperator } from '~/types'
/** Cleans properties of entities i.e. event and action nodes. These are a simple list of property filters. */
export const cleanEntityProperties = (
properties: Record<string, any> | Record<string, any>[] | undefined
): AnyPropertyFilter[] | undefined => {
if (
properties == undefined ||
(Array.isArray(properties) && properties.length == 0) ||
Object.keys(properties).length == 0
) {
// empty properties
return undefined
} else if (isOldStyleProperties(properties)) {
// old style properties
return transformOldStyleProperties(properties)
} else if (Array.isArray(properties)) {
// list of property filters
return properties.map(cleanProperty)
} else if (
(properties['type'] === 'AND' || properties['type'] === 'OR') &&
!properties['values'].some((property: any) => property['type'] === 'AND' || property['type'] === 'OR')
) {
// property group filter value
return properties.values.map(cleanProperty)
}
throw new Error('Unexpected format of entity properties.')
}
const cleanProperty = (property: Record<string, any>): AnyPropertyFilter => {
if (Object.keys(property).length === 0) {
return { type: PropertyFilterType.HogQL, key: 'true' }
}
// remove invalid properties without type
if (property['type'] === undefined) {
return { type: PropertyFilterType.HogQL, key: 'true' }
}
// fix type typo
if (property['type'] === 'events') {
property['type'] = 'event'
}
// fix value key typo
if (property['values'] !== undefined && property['value'] === undefined) {
property['value'] = property['values']
delete property['values']
}
// convert precalculated and static cohorts to cohorts
if (['precalculated-cohort', 'static-cohort'].includes(property['type'])) {
property['type'] = 'cohort'
}
// fix invalid property key for cohorts
if (property['type'] === 'cohort' && property['key'] !== 'id') {
property['key'] = 'id'
}
// set a default operator for properties that support it, but don't have an operator set.
// A behavioral filter is exempt: its operator is the count comparison for
// performed_event_multiple, so an absent one means "no count comparison", not "exactly".
if (isPropertyWithOperator(property) && property['type'] !== 'behavioral' && property['operator'] === undefined) {
property['operator'] = 'exact'
}
// remove the operator for properties that don't support it, but have it set
if (!isPropertyWithOperator(property) && property['operator'] !== undefined) {
delete property['operator']
}
// convert `negation` for cohorts
if (property['type'] === 'cohort' && property['negation'] !== undefined) {
if (property['operator'] === PropertyOperator.Exact && property['negation']) {
property['operator'] = PropertyOperator.NotIn
}
delete property['negation']
}
// remove none from values
if (Array.isArray(property['value'])) {
property['value'] = property['value'].filter((x) => x !== null)
}
// remove keys without concrete value
Object.keys(property).forEach((key) => {
if (property[key] === undefined) {
delete property[key]
}
})
return property
}
const isPropertyWithOperator = (property: Record<string, any>): boolean => {
return !['hogql'].includes(property['type'])
}
// old style dict properties e.g. {"utm_medium__icontains": "email"}
const isOldStyleProperties = (properties: Record<string, any> | Record<string, any>[]): boolean => {
return (
!Array.isArray(properties) && Object.keys(properties).length === 1 && !['AND', 'OR'].includes(properties.type)
)
}
const transformOldStyleProperties = (
properties: Record<string, any> | Record<string, any>[]
): EventPropertyFilter[] => {
const key = Object.keys(properties)[0]
const value = Object.values(properties)[0]
const keySplit = key.split('__')
return [
{
key: keySplit[0],
value: value,
operator: keySplit.length > 1 ? (keySplit[1] as PropertyOperator) : PropertyOperator.Exact,
type: PropertyFilterType.Event,
},
]
}