: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 { MakeLogicType, actions, kea, listeners, path, reducers } from 'kea'
import { loaders } from 'kea-loaders'
import posthog from 'posthog-js'
import api from 'lib/api'
import { lemonToast } from 'lib/lemon-ui/LemonToast/LemonToast'
import { hasTaxonomyPrimaryProperty } from 'lib/utils/events'
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface primaryEventPropertiesModelValues {
loadedEventNames: string[]
primaryProperties: Record<string, string>
primaryPropertiesLoading: boolean
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface primaryEventPropertiesModelActions {
ensureLoadedForEvents: (eventNames: string[]) => {
eventNames: string[]
}
loadPrimaryProperties: ({ names }: { names: string[] }) => {
names: string[]
}
loadPrimaryPropertiesFailure: (
error: string,
errorObject?: any
) => {
error: string
errorObject?: any
}
loadPrimaryPropertiesSuccess: (
primaryProperties: Record<string, string>,
payload?: {
names: string[]
}
) => {
primaryProperties: Record<string, string>
payload?: {
names: string[]
}
}
refreshLoadedPrimaryProperties: () => {
value: true
}
updatePrimaryProperty: ({ eventName, propertyKey }: { eventName: string; propertyKey: string | null }) => {
eventName: string
propertyKey: string | null
}
updatePrimaryPropertyFailure: (
error: string,
errorObject?: any
) => {
error: string
errorObject?: any
}
updatePrimaryPropertySuccess: (
primaryProperties: Record<string, string>,
payload?: {
eventName: string
propertyKey: string | null
}
) => {
primaryProperties: Record<string, string>
payload?: {
eventName: string
propertyKey: string | null
}
}
}
export type primaryEventPropertiesModelType = MakeLogicType<
primaryEventPropertiesModelValues,
primaryEventPropertiesModelActions
>
export const primaryEventPropertiesModel = kea<primaryEventPropertiesModelType>([
path(['models', 'primaryEventPropertiesModel']),
actions({
ensureLoadedForEvents: (eventNames: string[]) => ({ eventNames }),
refreshLoadedPrimaryProperties: true,
}),
loaders(({ values }) => ({
primaryProperties: {
__default: {} as Record<string, string>,
loadPrimaryProperties: async ({ names }: { names: string[] }) => {
if (names.length === 0) {
return values.primaryProperties
}
const response = await api.eventDefinitions.primaryProperties({ names })
const next = { ...values.primaryProperties }
for (const name of names) {
delete next[name]
}
return { ...next, ...response.primary_properties }
},
updatePrimaryProperty: async ({
eventName,
propertyKey,
}: {
eventName: string
propertyKey: string | null
}) => {
let definitionId: string
try {
definitionId = (await api.eventDefinitions.byName({ name: eventName })).id
} catch (error) {
posthog.captureException(error, { action: 'update-primary-property', stage: 'lookup' })
lemonToast.error(`We couldn't find a definition for "${eventName}" yet. Please try again shortly.`)
return values.primaryProperties
}
try {
const updated = await api.eventDefinitions.update({
eventDefinitionId: definitionId,
eventDefinitionData: { primary_property: propertyKey },
})
const next = { ...values.primaryProperties }
if (updated.primary_property) {
next[eventName] = updated.primary_property
} else {
delete next[eventName]
}
return next
} catch (error) {
posthog.captureException(error, { action: 'update-primary-property', stage: 'update' })
lemonToast.error('Could not update the pinned property. Please try again.')
return values.primaryProperties
}
},
},
})),
reducers({
loadedEventNames: [
[] as string[],
{
loadPrimaryPropertiesSuccess: (state, { payload }) =>
Array.from(new Set([...state, ...(payload?.names ?? [])])),
},
],
}),
listeners(({ actions, values }) => ({
ensureLoadedForEvents: ({ eventNames }) => {
const loaded = new Set(values.loadedEventNames)
const namesToLoad = eventNames.filter(
(name) => !!name && !hasTaxonomyPrimaryProperty(name) && !loaded.has(name)
)
if (namesToLoad.length > 0) {
actions.loadPrimaryProperties({ names: namesToLoad })
}
},
refreshLoadedPrimaryProperties: () => {
if (values.loadedEventNames.length > 0) {
actions.loadPrimaryProperties({ names: values.loadedEventNames })
}
},
loadPrimaryPropertiesFailure: ({ errorObject }) => {
posthog.captureException(errorObject, { action: 'load-primary-properties' })
},
})),
])