Analytics

A product analytics overview: a period switch, four key figures with their change, a trend chart and the top pages.

Open full screen

Flat · Sidebar. <AppShell direction="flat">: one white surface; lines split the navigation, the page header and the content.

npx wideset add block-analytics

Import the default export from @/blocks/wideset/analytics. The files below are relative to your project's source directory.

Installed with this block
  • app-shell
  • avatar
  • block-styles
  • button
  • segmented-control
  • sidebar
  • status-badge
  • table
  • utils
blocks/wideset/analytics/index.tsx
'use client';

import '@/styles/wideset-blocks.css';

import { AppShell } from '@/components/ui/wideset/app-shell/AppShell';
import { cn } from '@/lib/wideset/utils';
import { useState } from 'react';

import { type Metric, type Period, PERIODS, report } from './data';
import { formatCount, formatPercent, formatPoints, formatRange } from './format';
import { KpiCard } from './kpi-card';
import { PageHeader } from './page-header';
import { AppSidebar } from './sidebar';
import { TopPagesTable } from './top-pages-table';
import { TrafficSources } from './traffic-sources';
import { TrendChart } from './trend-chart';

const formatValue = (metric: Metric) =>
  metric.kind === 'rate' ? formatPercent(metric.value, metric.decimals) : formatCount(metric.value);

/** A metric as a key figure: the value, its change and what the change is measured against. */
const toKpi = (metric: Metric) => {
  const delta = metric.value - metric.previous;
  const direction = delta >= 0 ? 'up' : 'down';
  return {
    label: metric.label,
    value: formatValue(metric),
    change:
      metric.kind === 'rate'
        ? formatPoints(delta, metric.decimals)
        : formatPercent(Math.abs(delta / metric.previous)),
    direction,
    good: direction === metric.better,
    comparison: `from ${formatValue({ ...metric, value: metric.previous })}`,
  } as const;
};

/**
 * Product analytics, Flat direction: the sidebar and the page on one white surface, the page's
 * regions split by lines. The period switch in the header recounts every figure.
 */
export default function Analytics() {
  const [period, setPeriod] = useState<Period>('30d');
  const { days } = PERIODS.find((entry) => entry.value === period)!;
  const { current, previous, metrics, pages, sources } = report(period);
  const first = current[0].date;
  const last = current[current.length - 1].date;

  return (
    <AppShell className="ws-block-utility" direction="flat">
      <AppSidebar />
      <AppShell.Main className="flex flex-col">
        <PageHeader period={period} onPeriodChange={setPeriod} range={formatRange(first, last)} />

        <section aria-label="Key figures" className="border-ws-static-subtle px-ws-14 border-b">
          <div className="mx-auto grid w-full max-w-[80rem] grid-cols-4">
            {metrics.map((metric, index) => (
              <KpiCard
                key={metric.label}
                {...toKpi(metric)}
                className={cn(
                  'py-ws-14 pr-ws-14',
                  index > 0 && 'border-ws-static-subtle pl-ws-14 border-l',
                )}
              />
            ))}
          </div>
        </section>

        <div className="border-ws-static-subtle px-ws-14 py-ws-14 border-b">
          <TrendChart
            title="Visitors"
            dates={current.map((day) => day.date)}
            current={current.map((day) => day.visitors)}
            previous={previous.map((day) => day.visitors)}
            currentLabel={`Last ${days} days`}
            previousLabel={`Previous ${days} days`}
            className="mx-auto w-full max-w-[80rem]"
          />
        </div>

        {/* The last band takes the rest of the window: the line between its columns runs to the
            bottom edge instead of stopping in the air. */}
        <div className="px-ws-14 flex-1">
          <div className="mx-auto grid h-full w-full max-w-[80rem] grid-cols-4">
            <TopPagesTable
              pages={pages}
              className="border-ws-static-subtle py-ws-14 pr-ws-14 col-span-3 border-r"
            />
            <TrafficSources sources={sources} className="py-ws-14 pl-ws-14" />
          </div>
        </div>
      </AppShell.Main>
    </AppShell>
  );
}