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.

3.8 KB
Raw Blame
import type { ReactElement } from 'react'

import { DataTable, type DataTableColumn } from '@posthog/mcp-ui'
import { Card, CardContent, Progress } from '@posthog/quill'

export interface SurveyStatEntry {
    name: string
    total_count: number
    unique_persons: number
}

export interface SurveyStatsData {
    survey_id?: string
    stats?: Record<string, { total_count: number; unique_persons: number }>
    // Rates come from the API already scaled to a percentage (0-100), e.g. 21.7 means 21.7%.
    rates?: { response_rate?: number; dismissal_rate?: number }
    _posthogUrl?: string
}

export interface SurveyStatsViewProps {
    data: SurveyStatsData
}

export function SurveyStatsView({ data }: SurveyStatsViewProps): ReactElement {
    const entries: SurveyStatEntry[] = data.stats
        ? Object.entries(data.stats).map(([name, val]) => ({
              name,
              total_count: val.total_count,
              unique_persons: val.unique_persons,
          }))
        : []

    const columns: DataTableColumn<SurveyStatEntry>[] = [
        { key: 'name', header: 'Event', sortable: true },
        { key: 'total_count', header: 'Total', align: 'right', sortable: true },
        { key: 'unique_persons', header: 'Unique persons', align: 'right', sortable: true },
    ]

    return (
        <div className="p-4">
            <div className="flex flex-col gap-3">
                <span className="text-lg font-semibold">Survey stats</span>

                {data.rates && (
                    <Card>
                        <CardContent>
                            <div className="flex flex-col gap-2">
                                {data.rates.response_rate != null && (
                                    <div>
                                        <div className="flex justify-between text-sm mb-1">
                                            <span className="text-muted-foreground">Response rate</span>
                                            <span className="tabular-nums">{data.rates.response_rate.toFixed(1)}%</span>
                                        </div>
                                        <Progress value={data.rates.response_rate} variant="success" />
                                    </div>
                                )}
                                {data.rates.dismissal_rate != null && (
                                    <div>
                                        <div className="flex justify-between text-sm mb-1">
                                            <span className="text-muted-foreground">Dismissal rate</span>
                                            <span className="tabular-nums">
                                                {data.rates.dismissal_rate.toFixed(1)}%
                                            </span>
                                        </div>
                                        <Progress value={data.rates.dismissal_rate} variant="warning" />
                                    </div>
                                )}
                            </div>
                        </CardContent>
                    </Card>
                )}

                {entries.length > 0 && (
                    <Card>
                        <CardContent>
                            <div className="flex flex-col gap-2">
                                <span className="text-sm font-semibold">Events</span>
                                <DataTable<SurveyStatEntry>
                                    columns={columns}
                                    data={entries}
                                    emptyMessage="No stats available"
                                />
                            </div>
                        </CardContent>
                    </Card>
                )}
            </div>
        </div>
    )
}