: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
feat(marketing-analytics): add metric and notice cards (#105136)
Javier Bahamondes8db2fda1cc16
diff --git a/frontend/snapshots.yml b/frontend/snapshots.ymlindex 40423b2dbec7..ce5217dde746 100644--- a/frontend/snapshots.yml+++ b/frontend/snapshots.yml@@ -5476,6 +5476,22 @@ snapshots:hash: v1.k794b7964.fda088b86665fa1e7f42ac158d9c5acf12b2afc030a90ad385d3d0fe16170d46.WqmQ9-wSSHQj0J-kUbJKkFI8DhDljOP6queW3e6jxDQmarketing-analytics-customer-acquisition-cards--unconfigured--light:hash: v1.k794b7964.40240d162f715f3da0d8363e08797fb8a9c131852a5d6356c892424e25dfa35c.yeHaH7ecKHVh7IOwmerxuVPzSGclQrUDBwWnoPV0rmw+ marketing-analytics-dashboard-metric-cards--interactive--dark:+ hash: v1.k794b7964.4c13e857435c2bee42eea33c6f86ab2d46f0d3c1c9d997f32bcdc57cf1e7dafc.J9JahhAU6VQmtGCK8icH17WJvkELlNa7DkbabJ2wtRU+ marketing-analytics-dashboard-metric-cards--interactive--light:+ hash: v1.k794b7964.ae261dc53797e4fa4d00cd299f8d48d6e2c1281786d5048a36582ed229bec412.jGf_7LuJ55QW0GvghJfa-eid3j8lLV2NLhJGymTVmXU+ marketing-analytics-dashboard-metric-cards--loading--dark:+ hash: v1.k794b7964.51ad00cd8818358b38d57bdd5d694ea98446dbdef7727db1e6f89320dacb1ff9.XLLDF_4xHonffqOBzexuguHLaGw8rSq4xMKSN5cSZmU+ marketing-analytics-dashboard-metric-cards--loading--light:+ hash: v1.k794b7964.cc0559f266eccc72b9b5f443568c3b7c056073ef51c798f024afce930115d993.dtMKcpd7lEz3_eTuPButOD5iWcsQPvNuRxc4G7Oh_fY+ marketing-analytics-dashboard-metric-cards--missing-spec--dark:+ hash: v1.k794b7964.9b4b7284ba622834380b56575fb34d933ee33e05dd1ebd0f059b4feb03489589.HV9E-HZGn9I2YYV7iG1eqbcCSA3kInX_R6eYwVJ8DvQ+ marketing-analytics-dashboard-metric-cards--missing-spec--light:+ hash: v1.k794b7964.cf0d47efba21726ecb3560993a92a79696e7753e2fdfdca9dc78ef7a33ee877c.OI9y74Oqh6D9DgIAraEihIQIBksB6hA2DNLxVJ4KFnA+ marketing-analytics-dashboard-metric-cards--narrow--dark:+ hash: v1.k794b7964.892b971ab555f6d1ca2e0fa85f9a9866e3e5b8fd2843db239963ee69d0557810.JMoDrmwxxF3X_fUQL4s76kPCprGp3yU0oyOPqLf8Smw+ marketing-analytics-dashboard-metric-cards--narrow--light:+ hash: v1.k794b7964.19970bb1bed82447960aefd0360d530aacf7953ad0aed733f9e3cbb2136dd4c8.qWkLBIG4xUN4Qp39pJnDkjC8z0-DunhMLPxYC-o5KZAmarketing-analytics-retention-return-table--all-users--dark:hash: v1.k794b7964.96cadfa4ec3fa4c9077bc65d4c9075ca80691ec89862bc5b76d83609bb4eb9b2.RVLlGI1QG26GFC55_Dv5KdAWGGbRVI1ttz4lL-UxV7Mmarketing-analytics-retention-return-table--all-users--light:diff --git a/products/marketing_analytics/frontend/dashboard/cards/CardsPreview.tsx b/products/marketing_analytics/frontend/dashboard/cards/CardsPreview.tsxnew file mode 100644index 000000000000..1ad81da710cb--- /dev/null+++ b/products/marketing_analytics/frontend/dashboard/cards/CardsPreview.tsx@@ -0,0 +1,83 @@+import { LemonButton } from '@posthog/lemon-ui'++import { MarketingMetricCard } from './MarketingMetricCard'+import { MarketingMetricCardGrid } from './MarketingMetricCardGrid'+import type { MetricCardSpec } from './metricCardSpec'++const LABELS: Record<string, string> = {+ sessions: 'Sessions',+ revenue: 'Revenue',+ bounce_rate: 'Bounce rate',+}++interface CardsPreviewProps {+ loading: boolean+ setupSelected: boolean+ missingSpec: boolean+ onLoadingChange: (loading: boolean) => void+ onSetupSelected: () => void+}++export function CardsPreview({+ loading,+ setupSelected,+ missingSpec,+ onLoadingChange,+ onSetupSelected,+}: CardsPreviewProps): JSX.Element {+ const specs: MetricCardSpec[] = [+ {+ kind: 'metric',+ item: { key: 'sessions', kind: 'unit', value: 1240, previous: 1000, changeFromPreviousPct: 24 },+ },+ {+ kind: 'notice',+ key: 'conversion_value',+ title: 'Conversion value',+ value: 'N/A',+ message: 'Choose a conversion goal with a value to see this metric.',+ action: {+ label: 'Choose a goal',+ onClick: onSetupSelected,+ dataAttr: 'marketing-story-configure-goal',+ },+ },+ {+ kind: 'metric',+ item: { key: 'revenue', kind: 'currency', value: 0, previous: 100, changeFromPreviousPct: -100 },+ },+ {+ kind: 'metric',+ item: {+ key: 'bounce_rate',+ kind: 'percentage',+ value: 30,+ previous: 40,+ changeFromPreviousPct: -25,+ isIncreaseBad: true,+ },+ },+ ]++ // A 960 px scene prevents component snapshots from shrinking the container-query grid to its controls.+ return (+ <div className="flex w-[60rem] max-w-full flex-col gap-4">+ <MarketingMetricCardGrid>+ {specs.map((spec) => (+ <MarketingMetricCard+ key={spec.kind === 'metric' ? spec.item.key : spec.key}+ spec={missingSpec && spec.kind === 'metric' && spec.item.key === 'sessions' ? undefined : spec}+ loading={loading}+ labelFromKey={(key) => LABELS[key] ?? key}+ />+ ))}+ </MarketingMetricCardGrid>+ <div className="flex flex-wrap items-center gap-2">+ <LemonButton data-attr="marketing-story-toggle-loading" onClick={() => onLoadingChange(!loading)}>+ {loading ? 'Show metrics' : 'Show loading'}+ </LemonButton>+ {setupSelected && <span>Goal setup selected</span>}+ </div>+ </div>+ )+}diff --git a/products/marketing_analytics/frontend/dashboard/cards/MarketingMetricCard.stories.tsx b/products/marketing_analytics/frontend/dashboard/cards/MarketingMetricCard.stories.tsxnew file mode 100644index 000000000000..ea7c7f45c213--- /dev/null+++ b/products/marketing_analytics/frontend/dashboard/cards/MarketingMetricCard.stories.tsx@@ -0,0 +1,40 @@+import { Meta, StoryObj } from '@storybook/react'++import { useArgs } from 'storybook/preview-api'++import { CardsPreview } from './CardsPreview'++const meta: Meta<typeof CardsPreview> = {+ title: 'Marketing Analytics/Dashboard/Metric cards',+ component: CardsPreview,+ args: { loading: false, setupSelected: false, missingSpec: false },+ render: function Render(args) {+ const [, updateArgs] = useArgs()+ return (+ <CardsPreview+ {...args}+ onLoadingChange={(loading) => updateArgs({ loading })}+ onSetupSelected={() => updateArgs({ setupSelected: true })}+ />+ )+ },+}+export default meta+type Story = StoryObj<typeof meta>++export const Interactive: Story = {}+export const Loading: Story = {+ args: { loading: true },+ parameters: { testOptions: { waitForLoadersToDisappear: false } },+}+export const MissingSpec: Story = { args: { missingSpec: true } }+export const Narrow: Story = {+ decorators: [+ (Story) => (+ <div className="w-[32.5rem] max-w-full">+ {/* A 520 px scene models the space beside an open side panel. */}+ <Story />+ </div>+ ),+ ],+}diff --git a/products/marketing_analytics/frontend/dashboard/cards/MarketingMetricCard.test.tsx b/products/marketing_analytics/frontend/dashboard/cards/MarketingMetricCard.test.tsxnew file mode 100644index 000000000000..d3e0b4431873--- /dev/null+++ b/products/marketing_analytics/frontend/dashboard/cards/MarketingMetricCard.test.tsx@@ -0,0 +1,14 @@+import { renderToStaticMarkup } from 'react-dom/server'++import { MarketingMetricCard } from './MarketingMetricCard'++describe('MarketingMetricCard', () => {+ it.each([+ { loading: true, expected: /Loading…/ },+ { loading: false, expected: /^$/ },+ ])('renders a missing specification with loading=$loading', ({ loading, expected }) => {+ const markup = renderToStaticMarkup(<MarketingMetricCard loading={loading} labelFromKey={(key) => key} />)++ expect(markup).toMatch(expected)+ })+})diff --git a/products/marketing_analytics/frontend/dashboard/cards/MarketingMetricCard.tsx b/products/marketing_analytics/frontend/dashboard/cards/MarketingMetricCard.tsxnew file mode 100644index 000000000000..c753b15d9315--- /dev/null+++ b/products/marketing_analytics/frontend/dashboard/cards/MarketingMetricCard.tsx@@ -0,0 +1,35 @@+import { OverviewMetricCardGrid } from '~/queries/nodes/OverviewGrid/OverviewMetricCardGrid'++import type { MetricCardSpec } from './metricCardSpec'+import { MetricNoticeCard } from './MetricNoticeCard'++export interface MarketingMetricCardProps {+ spec?: MetricCardSpec+ loading: boolean+ labelFromKey: (key: string) => React.ReactNode+}++/** One card per grid instance, so a notice keeps its place in the configured order instead of+ * being pushed past every metric. */+export function MarketingMetricCard({ spec, loading, labelFromKey }: MarketingMetricCardProps): JSX.Element | null {+ if (loading) {+ return (+ <OverviewMetricCardGrid layout="contents" items={[]} loading numSkeletons={1} labelFromKey={labelFromKey} />+ )+ }+ if (!spec) {+ return null+ }+ if (spec.kind === 'notice') {+ return <MetricNoticeCard title={spec.title} message={spec.message} value={spec.value} action={spec.action} />+ }+ return (+ <OverviewMetricCardGrid+ layout="contents"+ items={[spec.item]}+ loading={false}+ numSkeletons={1}+ labelFromKey={labelFromKey}+ />+ )+}diff --git a/products/marketing_analytics/frontend/dashboard/cards/MarketingMetricCardGrid.tsx b/products/marketing_analytics/frontend/dashboard/cards/MarketingMetricCardGrid.tsxnew file mode 100644index 000000000000..5dc5fc26e54b--- /dev/null+++ b/products/marketing_analytics/frontend/dashboard/cards/MarketingMetricCardGrid.tsx@@ -0,0 +1,9 @@+/** Breaks on its own width, because the scene loses ~520px to the nav and an open side panel.+ * auto-fit rather than a fixed column count so a row of cards always spans the full width. */+export function MarketingMetricCardGrid({ children }: { children: React.ReactNode }): JSX.Element {+ return (+ <div className="@container">+ <div className="grid gap-2 grid-cols-[repeat(auto-fit,minmax(min(100%,11rem),1fr))]">{children}</div>+ </div>+ )+}diff --git a/products/marketing_analytics/frontend/dashboard/cards/MetricNoticeCard.tsx b/products/marketing_analytics/frontend/dashboard/cards/MetricNoticeCard.tsxnew file mode 100644index 000000000000..90f23730c08f--- /dev/null+++ b/products/marketing_analytics/frontend/dashboard/cards/MetricNoticeCard.tsx@@ -0,0 +1,24 @@+import { LemonButton, LemonCard } from '@posthog/lemon-ui'++import type { MetricNotice } from './metricCardSpec'++export type MetricNoticeCardProps = Omit<MetricNotice, 'kind' | 'key'>++/** Stands in for a metric card whenever there is no number to show, so the grid never renders an+ * empty bordered box. Matches the metric card's own shell. */+export function MetricNoticeCard({ title, message, action, value }: MetricNoticeCardProps): JSX.Element {+ return (+ <LemonCard hoverEffect={false} className="flex h-full flex-col gap-1 p-3">+ <span className="text-xs font-medium text-secondary truncate">{title}</span>+ {value && <span className="text-2xl font-semibold text-muted tabular-nums">{value}</span>}+ <span className="text-secondary text-xs">{message}</span>+ {action && (+ <div className="mt-auto pt-1">+ <LemonButton size="small" type="secondary" onClick={action.onClick} data-attr={action.dataAttr}>+ {action.label}+ </LemonButton>+ </div>+ )}+ </LemonCard>+ )+}diff --git a/tsconfig.json b/tsconfig.jsonindex 50ecd884980c..007b07166bd1 100644--- a/tsconfig.json+++ b/tsconfig.json@@ -27,6 +27,8 @@// These packages are installed under frontend/node_modules only, which product// files (products/*/frontend) can't reach by walking up from their own directory."@storybook/react": ["./frontend/node_modules/@storybook/react/dist/index.d.ts"],+ "storybook/preview-api": ["./frontend/node_modules/storybook/dist/preview-api/index.d.ts"],+ "react-dom/server": ["./frontend/node_modules/@types/react-dom/server.d.ts"],"@tiptap/*": ["./frontend/node_modules/@tiptap/*"],// Needed to access the subpath since torph didn't set up types properly"torph/react": ["./frontend/node_modules/torph/dist/react/index"],