: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 { useActions, useMountedLogic, useValues } from 'kea'
import posthog from 'posthog-js'
import { LemonBanner, LemonButton, LemonTabs } from '@posthog/lemon-ui'
import { Button } from '@posthog/quill'
import { useFeatureFlag } from 'lib/hooks/useFeatureFlag'
import { useOnMountEffect } from 'lib/hooks/useOnMountEffect'
import { IconFeedback } from 'lib/lemon-ui/icons'
import { getAccessControlDisabledReason } from 'lib/utils/accessControlUtils'
import { sceneConfigurations } from 'scenes/scenes'
import { Scene, SceneExport } from 'scenes/sceneTypes'
import { FeaturePreviewSceneGate } from '~/layout/scenes/components/FeaturePreviewSceneGate'
import { SceneContent } from '~/layout/scenes/components/SceneContent'
import { SceneTitleSection } from '~/layout/scenes/components/SceneTitleSection'
import { ProductKey } from '~/queries/schema/schema-general'
import { AccessControlLevel, AccessControlResourceType } from '~/types'
import { metricNamePickerLogic } from './components/metricNamePickerLogic'
import { MetricsDashboardActions } from './components/MetricsDashboardActions'
import { MetricsOverview } from './components/MetricsOverview'
import { MetricsSqlEditor } from './components/MetricsSqlEditor'
import { metricsUsageTrackingLogic } from './components/metricsUsageTrackingLogic'
import { MetricsViewer } from './components/MetricsViewer'
import { QuillMetricsDashboardActions } from './components/QuillMetricsDashboardActions'
import { MetricsDashboardImportModal } from './dashboardImport/MetricsDashboardImportModal'
import { metricsEmptyState } from './emptyState/metricsEmptyState'
import { metricsFeaturePreviewGate } from './featurePreviewGate'
import { MetricsSceneActiveTab, metricsSceneLogic } from './metricsSceneLogic'
export const METRICS_LOGIC_KEY = 'metrics'
const METRICS_FEEDBACK_SURVEY_ID = '01a07c35-6be3-0000-16b3-4cd66a6873f3'
const TABS: { key: MetricsSceneActiveTab; label: string; 'data-attr': string }[] = [
{ key: 'overview', label: 'Overview', 'data-attr': 'metrics-scene-tab-overview' },
{ key: 'viewer', label: 'Viewer', 'data-attr': 'metrics-scene-tab-viewer' },
{ key: 'sql', label: 'SQL', 'data-attr': 'metrics-scene-tab-sql' },
]
export const scene: SceneExport = {
component: MetricsScene,
logic: metricsSceneLogic,
productKey: ProductKey.METRICS,
emptyState: metricsEmptyState,
}
export function MetricsScene(): JSX.Element {
return (
<FeaturePreviewSceneGate config={metricsFeaturePreviewGate}>
<SceneContent className="@container/metrics-scene h-[calc(var(--scene-layout-rect-height,_100vh)_-_1rem)]">
<MetricsSceneContent />
</SceneContent>
</FeaturePreviewSceneGate>
)
}
const MetricsSceneContent = (): JSX.Element => {
const { activeTab } = useValues(metricsSceneLogic)
const { setActiveTab } = useActions(metricsSceneLogic)
const metricsViewerDisabledReason = getAccessControlDisabledReason(
AccessControlResourceType.Metrics,
AccessControlLevel.Viewer
)
const metricsSqlDisabledReason = getAccessControlDisabledReason(
AccessControlResourceType.WarehouseObjects,
AccessControlLevel.Viewer
)
const tabDisabledReasons: Record<MetricsSceneActiveTab, string | null> = {
overview: metricsViewerDisabledReason,
viewer: metricsViewerDisabledReason,
sql: metricsSqlDisabledReason,
}
// Scene-level so tab switches in both directions are captured; keeps the viewer
// and samples logics (its connect targets) mounted across tab flips as a side effect.
useMountedLogic(metricsUsageTrackingLogic)
// Prime the metric-name list here rather than inside MetricsViewer, so the fetch
// races the has_metrics check instead of waiting on the setup prompt to resolve.
useMountedLogic(metricNamePickerLogic)
const { primeItems } = useActions(metricNamePickerLogic)
useOnMountEffect(() => {
primeItems()
})
const todayRailNav = useFeatureFlag('TODAY_RAIL_NAV')
const onFeedbackClick = (): void => {
posthog.displaySurvey(METRICS_FEEDBACK_SURVEY_ID)
}
return (
<>
<SceneTitleSection
name={sceneConfigurations[Scene.Metrics].name}
description={sceneConfigurations[Scene.Metrics].description}
resourceType={{
type: sceneConfigurations[Scene.Metrics].iconType || 'default_icon_type',
}}
actions={
todayRailNav ? (
<div className="flex flex-wrap items-center gap-2">
<div className="@min-[48rem]/metrics-scene:hidden">
<QuillMetricsDashboardActions />
</div>
<Button variant="outline" onClick={onFeedbackClick}>
<IconFeedback />
Feedback
</Button>
</div>
) : (
<>
{/* In a narrow scene, the tab bar has no space for the import menu. */}
<div className="@min-[48rem]/metrics-scene:hidden">
<MetricsDashboardActions />
</div>
<LemonButton
size="small"
type="secondary"
icon={<IconFeedback />}
onClick={onFeedbackClick}
>
Feedback
</LemonButton>
</>
)
}
/>
<LemonBanner
type="warning"
dismissKey="metrics-alpha-notice"
action={{
icon: <IconFeedback />,
children: 'Share feedback',
onClick: onFeedbackClick,
}}
>
Metrics is in alpha. Please share feedback on how to improve the product.
</LemonBanner>
<LemonTabs<MetricsSceneActiveTab>
activeKey={activeTab}
onChange={(tab) => {
if (!tabDisabledReasons[tab]) {
setActiveTab(tab)
}
}}
tabs={TABS.map((tab) => ({
...tab,
disabledReason: tabDisabledReasons[tab.key] ?? undefined,
}))}
sceneInset
rightSlot={<MetricsDashboardActions />}
rightSlotClassName="hidden @min-[48rem]/metrics-scene:flex"
/>
<div className="flex flex-col gap-2 py-2 flex-1 min-h-0">
{activeTab === 'overview' && <MetricsOverview />}
{activeTab === 'viewer' && <MetricsViewer />}
{activeTab === 'sql' && <MetricsSqlEditor />}
</div>
<MetricsDashboardImportModal />
</>
)
}