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.

4.5 KB
Raw Blame
import { MakeLogicType, actions, connect, kea, listeners, path, selectors } from 'kea'

import { isEmptyObject } from 'lib/utils/guards'
import { teamLogic } from 'scenes/teamLogic'

import {
    ActionsNode,
    AnyEntityNode,
    CustomerAnalyticsConfig,
    DataWarehouseNode,
    EventsNode,
    GroupNode,
} from '~/queries/schema/schema-general'

import type { TeamType } from '../../../frontend/src/types'

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface customerAnalyticsConfigLogicValues {
    customerAnalyticsConfig: CustomerAnalyticsConfig // teamLogic
    activityEvent: AnyEntityNode
    paymentEvent: AnyEntityNode
    signupEvent: AnyEntityNode
    signupPageviewEvent: AnyEntityNode
    subscriptionEvent: AnyEntityNode
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface customerAnalyticsConfigLogicActions {
    updateCurrentTeam: (payload: Partial<TeamType>) => Partial<TeamType> // teamLogic
    loadConfig: () => {
        value: true
    }
    updateEvents: (events: Record<string, ActionsNode | DataWarehouseNode | EventsNode | GroupNode>) => {
        events: Record<string, ActionsNode | DataWarehouseNode | EventsNode | GroupNode<DataWarehouseNode>>
    }
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface customerAnalyticsConfigLogicMeta {
    __keaTypeGenInternalSelectorTypes: {
        activityEvent: (customerAnalyticsConfig: CustomerAnalyticsConfig) => AnyEntityNode
        signupEvent: (customerAnalyticsConfig: CustomerAnalyticsConfig) => AnyEntityNode
        signupPageviewEvent: (customerAnalyticsConfig: CustomerAnalyticsConfig) => AnyEntityNode
        subscriptionEvent: (customerAnalyticsConfig: CustomerAnalyticsConfig) => AnyEntityNode
        paymentEvent: (customerAnalyticsConfig: CustomerAnalyticsConfig) => AnyEntityNode
    }
}

export type customerAnalyticsConfigLogicType = MakeLogicType<
    customerAnalyticsConfigLogicValues,
    customerAnalyticsConfigLogicActions,
    Record<string, any>,
    customerAnalyticsConfigLogicMeta
>

export const customerAnalyticsConfigLogic = kea<customerAnalyticsConfigLogicType>([
    path(['products', 'customerAnalytics', 'customerAnalyticsConfigModel']),

    actions({
        loadConfig: true,
        updateEvents: (events: Record<string, ActionsNode | EventsNode | DataWarehouseNode | GroupNode>) => ({
            events,
        }),
    }),

    connect(() => ({
        values: [teamLogic, ['customerAnalyticsConfig']],
        actions: [teamLogic, ['updateCurrentTeam']],
    })),
    selectors({
        activityEvent: [
            (s) => [s.customerAnalyticsConfig],
            (config: CustomerAnalyticsConfig): AnyEntityNode =>
                !isEmptyObject(config.activity_event)
                    ? { ...config.activity_event, custom_name: 'Activity' }
                    : ({} as AnyEntityNode),
        ],
        signupEvent: [
            (s) => [s.customerAnalyticsConfig],
            (config: CustomerAnalyticsConfig): AnyEntityNode =>
                !isEmptyObject(config.signup_event)
                    ? { ...config.signup_event, custom_name: 'Signups' }
                    : ({} as AnyEntityNode),
        ],
        signupPageviewEvent: [
            (s) => [s.customerAnalyticsConfig],
            (config: CustomerAnalyticsConfig): AnyEntityNode =>
                !isEmptyObject(config.signup_pageview_event)
                    ? { ...config.signup_pageview_event, custom_name: 'Signup pageviews' }
                    : ({} as AnyEntityNode),
        ],
        subscriptionEvent: [
            (s) => [s.customerAnalyticsConfig],
            (config: CustomerAnalyticsConfig): AnyEntityNode =>
                !isEmptyObject(config.subscription_event)
                    ? { ...config.subscription_event, custom_name: 'Subscriptions' }
                    : ({} as AnyEntityNode),
        ],
        paymentEvent: [
            (s) => [s.customerAnalyticsConfig],
            (config: CustomerAnalyticsConfig): AnyEntityNode =>
                !isEmptyObject(config.payment_event)
                    ? { ...config.payment_event, custom_name: 'Payments' }
                    : ({} as AnyEntityNode),
        ],
    }),

    listeners(({ actions }) => ({
        updateEvents: ({ events }) => {
            const customer_analytics_config = { ...events } as any as CustomerAnalyticsConfig
            actions.updateCurrentTeam({ customer_analytics_config })
        },
    })),
])