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.

5.2 KB
Raw Blame
import { MakeLogicType, connect, kea, key, listeners, path, props } from 'kea'
import posthog from 'posthog-js'

import { SaveAsMenuItem } from 'scenes/data-warehouse/editor/editorSceneLogic'
import { sqlEditorLogic } from 'scenes/data-warehouse/editor/sqlEditorLogic'
import { SQLEditorMode } from 'scenes/data-warehouse/editor/sqlEditorModes'
import { teamLogic } from 'scenes/teamLogic'

import { ProductIntentContext, ProductKey } from '~/queries/schema/schema-general'

import type { ProductIntentProperties } from '../../../../frontend/src/lib/utils/product-intents'

export interface MetricsSqlEditorTrackingLogicProps {
    sqlEditorTabId: string
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface metricsSqlEditorTrackingLogicActions {
    sqlEditorRunQuery: (
        queryOverride?: string | undefined,
        switchTab?: boolean | undefined,
        reuseResults?: boolean | undefined
    ) => {
        queryOverride: string | undefined
        reuseResults: boolean
        switchTab: boolean | undefined
    } // sqlEditorLogic
    sqlEditorSaveAsEndpointSubmit: (
        name: string,
        description?: string | undefined,
        queryOverride?: string | undefined
    ) => {
        description: string | undefined
        name: string
        queryOverride: string | undefined
    } // sqlEditorLogic
    sqlEditorSaveAsInsightSubmit: (
        name: string,
        queryOverride?: string | undefined
    ) => {
        name: string
        queryOverride: string | undefined
    } // sqlEditorLogic
    sqlEditorSaveAsViewSubmit: (
        name: string,
        materializeAfterSave?: any,
        fromDraft?: string | undefined,
        folderId?: string | null | undefined,
        isTest?: any,
        queryOverride?: string | undefined,
        incremental?: import('~/types').DataWarehouseSavedQueryIncremental | undefined,
        description?: string | undefined
    ) => {
        description: string | undefined
        folderId: string | null | undefined
        fromDraft: string | undefined
        incremental: import('~/types').DataWarehouseSavedQueryIncremental | undefined
        isTest: any
        materializeAfterSave: any
        name: string
        queryOverride: string | undefined
    } // sqlEditorLogic
    addProductIntent: (properties: ProductIntentProperties) => ProductIntentProperties // teamLogic
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface metricsSqlEditorTrackingLogicMeta {
    key: string
}

export type metricsSqlEditorTrackingLogicType = MakeLogicType<
    {},
    metricsSqlEditorTrackingLogicActions,
    MetricsSqlEditorTrackingLogicProps,
    metricsSqlEditorTrackingLogicMeta
>

export const metricsSqlEditorTrackingLogic = kea<metricsSqlEditorTrackingLogicType>([
    path(['products', 'metrics', 'frontend', 'components', 'metricsSqlEditorTrackingLogic']),
    props({} as MetricsSqlEditorTrackingLogicProps),
    key((props) => props.sqlEditorTabId),
    connect((props: MetricsSqlEditorTrackingLogicProps) => ({
        actions: [
            sqlEditorLogic({ tabId: props.sqlEditorTabId, mode: SQLEditorMode.Embedded }),
            [
                'runQuery as sqlEditorRunQuery',
                'saveAsViewSubmit as sqlEditorSaveAsViewSubmit',
                'saveAsInsightSubmit as sqlEditorSaveAsInsightSubmit',
                'saveAsEndpointSubmit as sqlEditorSaveAsEndpointSubmit',
            ],
            teamLogic,
            ['addProductIntent'],
        ],
    })),
    listeners(({ actions, cache }) => {
        // Fires on submit, not on save success: intents measure interest, not achievement
        // (a user who names and submits a save has shown the interest we're after, even if
        // the request then fails), and sqlEditorLogic exposes no per-target success action
        // to gate on. Activation criteria, defined later, count real saved artifacts.
        const trackSaved = (target: SaveAsMenuItem['action']): void => {
            posthog.capture('metrics sql query saved', { target })
            actions.addProductIntent({
                product_type: ProductKey.METRICS,
                intent_context: ProductIntentContext.METRICS_QUERY_SAVED,
                metadata: { target },
            })
        }
        return {
            sqlEditorRunQuery: () => {
                // Skip the auto-init runQuery dispatched by MetricsSqlEditor's first mount.
                // Trade-off: on revisit (queryInput already set, no auto-init), the user's first
                // manual run is also skipped. Acceptable under-count for an alpha metric.
                if (!cache.firstRunSeen) {
                    cache.firstRunSeen = true
                    return
                }
                posthog.capture('metrics sql query run')
                actions.addProductIntent({
                    product_type: ProductKey.METRICS,
                    intent_context: ProductIntentContext.METRICS_SQL_QUERY_RUN,
                })
            },
            sqlEditorSaveAsViewSubmit: () => trackSaved('view'),
            sqlEditorSaveAsInsightSubmit: () => trackSaved('insight'),
            sqlEditorSaveAsEndpointSubmit: () => trackSaved('endpoint'),
        }
    }),
])