: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, afterMount, kea, key, path, props } from 'kea'
import { loaders } from 'kea-loaders'
import posthog from 'posthog-js'
import api from 'lib/api'
import { HogQLQuery, NodeKind } from '~/queries/schema/schema-general'
import { CUSTOMER_ANALYTICS_DEFAULT_QUERY_TAGS } from 'products/customer_analytics/frontend/constants'
import { GroupRevenueAnalyticsRow } from './groupRevenue'
export type GroupRevenueLogicProps = {
groupKeys: string[]
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface groupRevenueLogicValues {
revenueByGroupKey: Record<string, GroupRevenueAnalyticsRow>
revenueByGroupKeyLoading: boolean
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface groupRevenueLogicActions {
loadRevenue: () => any
loadRevenueFailure: (
error: string,
errorObject?: any
) => {
error: string
errorObject?: any
}
loadRevenueSuccess: (
revenueByGroupKey: Record<string, GroupRevenueAnalyticsRow>,
payload?: any
) => {
revenueByGroupKey: Record<string, GroupRevenueAnalyticsRow>
payload?: any
}
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface groupRevenueLogicMeta {
key: string
}
export type groupRevenueLogicType = MakeLogicType<
groupRevenueLogicValues,
groupRevenueLogicActions,
GroupRevenueLogicProps,
groupRevenueLogicMeta
>
export const groupRevenueLogic = kea<groupRevenueLogicType>([
props({} as GroupRevenueLogicProps),
key((props) => [...props.groupKeys].sort().join(',')),
path((key) => ['products', 'conversations', 'frontend', 'scenes', 'ticket', 'groupRevenueLogic', key]),
loaders(({ props }) => ({
revenueByGroupKey: [
{} as Record<string, GroupRevenueAnalyticsRow>,
{
loadRevenue: async () => {
if (props.groupKeys.length === 0) {
return {}
}
try {
const response = await api.query<HogQLQuery>({
kind: NodeKind.HogQLQuery,
query: `
SELECT
group_key,
toFloat(sum(mrr)),
toFloat(sum(revenue))
FROM groups_revenue_analytics
WHERE group_key IN {groupKeys}
GROUP BY group_key
`,
values: { groupKeys: props.groupKeys },
tags: CUSTOMER_ANALYTICS_DEFAULT_QUERY_TAGS,
})
const revenueByGroupKey: Record<string, GroupRevenueAnalyticsRow> = {}
for (const [groupKey, mrr, lifetimeValue] of response?.results ?? []) {
revenueByGroupKey[groupKey] = { mrr: mrr ?? null, lifetimeValue: lifetimeValue ?? null }
}
return revenueByGroupKey
} catch (error) {
// The panel falls back to the revenue group properties, so the failure is not surfaced.
posthog.captureException(error, { tag: 'ticket_group_revenue_query_failed' })
return {}
}
},
},
],
})),
afterMount(({ actions }) => {
actions.loadRevenue()
}),
])