Add click heatmaps report
Extracts click events from rrweb session-replay buffers at write time into a new heatmap_event table (Prisma + ClickHouse). New /heatmaps report shows a page picker plus an overlay of click density on the dominant viewport, with an optional iframe of the live page behind.
This commit is contained in:
1 parent
9da332c489
commit
a6b13b71db
18 files changed
+831
No files matched your search
@@ -0,0 +1,80 @@
|
||||
.pageList {
|
||||
overflow-y: auto;
|
||||
padding-right: 8px;
|
||||
max-height: 100%;
|
||||
}
|
||||
|
||||
.pageButton {
|
||||
width: 100%;
|
||||
text-align: left;
|
||||
border: 0;
|
||||
background: transparent;
|
||||
border-radius: 6px;
|
||||
padding: 8px 10px;
|
||||
cursor: pointer;
|
||||
font: inherit;
|
||||
color: inherit;
|
||||
}
|
||||
|
||||
.pageButton:hover {
|
||||
background: var(--interactive-bg-hover);
|
||||
}
|
||||
|
||||
.pageButtonSelected,
|
||||
.pageButtonSelected:hover {
|
||||
background: var(--surface-inverted);
|
||||
color: var(--surface-base);
|
||||
}
|
||||
|
||||
.toggleButton {
|
||||
border: 1px solid var(--border-base);
|
||||
background: var(--surface-base);
|
||||
border-radius: 6px;
|
||||
padding: 4px 10px;
|
||||
cursor: pointer;
|
||||
font: inherit;
|
||||
color: inherit;
|
||||
}
|
||||
|
||||
.toggleButton:hover {
|
||||
background: var(--interactive-bg-hover);
|
||||
}
|
||||
|
||||
.canvas {
|
||||
position: relative;
|
||||
overflow: hidden;
|
||||
border: 1px solid var(--border-base);
|
||||
border-radius: 8px;
|
||||
background: var(--surface-sunken);
|
||||
}
|
||||
|
||||
.iframe {
|
||||
position: absolute;
|
||||
top: 0;
|
||||
left: 0;
|
||||
border: 0;
|
||||
transform-origin: top left;
|
||||
pointer-events: none;
|
||||
}
|
||||
|
||||
.overlay {
|
||||
position: absolute;
|
||||
top: 0;
|
||||
left: 0;
|
||||
right: 0;
|
||||
bottom: 0;
|
||||
pointer-events: none;
|
||||
}
|
||||
|
||||
.dot {
|
||||
position: absolute;
|
||||
border-radius: 50%;
|
||||
background: radial-gradient(
|
||||
circle,
|
||||
rgba(255, 60, 0, 0.9) 0%,
|
||||
rgba(255, 200, 0, 0.6) 45%,
|
||||
rgba(255, 200, 0, 0) 80%
|
||||
);
|
||||
pointer-events: auto;
|
||||
mix-blend-mode: multiply;
|
||||
}
|
||||
@@ -0,0 +1,194 @@
|
||||
'use client';
|
||||
import { Column, Grid, Heading, Row, Text } from '@umami/react-zen';
|
||||
import { useMemo, useState } from 'react';
|
||||
import { LoadingPanel } from '@/components/common/LoadingPanel';
|
||||
import { useResultQuery, useWebsite } from '@/components/hooks';
|
||||
import { formatLongNumber } from '@/lib/format';
|
||||
import type { HeatmapPoint, HeatmapResult } from '@/queries/sql';
|
||||
import styles from './Heatmap.module.css';
|
||||
|
||||
const RENDER_WIDTH = 1024;
|
||||
|
||||
interface ViewportBucket {
|
||||
width: number;
|
||||
height: number;
|
||||
count: number;
|
||||
}
|
||||
|
||||
interface HeatmapProps {
|
||||
websiteId: string;
|
||||
urlPath: string;
|
||||
onUrlPathChange: (urlPath: string) => void;
|
||||
}
|
||||
|
||||
export function Heatmap({ websiteId, urlPath, onUrlPathChange }: HeatmapProps) {
|
||||
const website = useWebsite();
|
||||
const { data, error, isLoading } = useResultQuery<HeatmapResult>('heatmap', {
|
||||
websiteId,
|
||||
urlPath: urlPath || undefined,
|
||||
});
|
||||
|
||||
const pages = data?.pages ?? [];
|
||||
const points = data?.points ?? [];
|
||||
|
||||
return (
|
||||
<LoadingPanel data={data} isLoading={isLoading} error={error} height="100%">
|
||||
<Grid columns="320px 1fr" gap height="100%">
|
||||
<PageList pages={pages} selected={urlPath} onSelect={onUrlPathChange} />
|
||||
<Column gap>
|
||||
{urlPath ? (
|
||||
<HeatmapView domain={website?.domain ?? null} urlPath={urlPath} points={points} />
|
||||
) : (
|
||||
<EmptyState />
|
||||
)}
|
||||
</Column>
|
||||
</Grid>
|
||||
</LoadingPanel>
|
||||
);
|
||||
}
|
||||
|
||||
function PageList({
|
||||
pages,
|
||||
selected,
|
||||
onSelect,
|
||||
}: {
|
||||
pages: HeatmapResult['pages'];
|
||||
selected: string;
|
||||
onSelect: (urlPath: string) => void;
|
||||
}) {
|
||||
return (
|
||||
<Column className={styles.pageList} gap="2">
|
||||
<Heading size="lg">Pages</Heading>
|
||||
{pages.length === 0 && <Text color="muted">No data yet</Text>}
|
||||
{pages.map(p => (
|
||||
<button
|
||||
key={p.urlPath}
|
||||
type="button"
|
||||
onClick={() => onSelect(p.urlPath)}
|
||||
className={`${styles.pageButton} ${selected === p.urlPath ? styles.pageButtonSelected : ''}`}
|
||||
>
|
||||
<Row alignItems="center" justifyContent="space-between" gap="2">
|
||||
<Text truncate>{p.urlPath}</Text>
|
||||
<Text color="muted">{formatLongNumber(p.count)}</Text>
|
||||
</Row>
|
||||
</button>
|
||||
))}
|
||||
</Column>
|
||||
);
|
||||
}
|
||||
|
||||
function pickViewport(points: HeatmapPoint[]): ViewportBucket | null {
|
||||
if (!points.length) return null;
|
||||
const buckets = new Map<string, ViewportBucket>();
|
||||
for (const p of points) {
|
||||
const key = `${p.viewportW}x${p.viewportH}`;
|
||||
const existing = buckets.get(key);
|
||||
if (existing) {
|
||||
existing.count += p.count;
|
||||
} else {
|
||||
buckets.set(key, { width: p.viewportW, height: p.viewportH, count: p.count });
|
||||
}
|
||||
}
|
||||
let best: ViewportBucket | null = null;
|
||||
for (const b of buckets.values()) {
|
||||
if (!best || b.count > best.count) best = b;
|
||||
}
|
||||
return best;
|
||||
}
|
||||
|
||||
function HeatmapView({
|
||||
domain,
|
||||
urlPath,
|
||||
points,
|
||||
}: {
|
||||
domain: string | null;
|
||||
urlPath: string;
|
||||
points: HeatmapPoint[];
|
||||
}) {
|
||||
const [showPage, setShowPage] = useState(true);
|
||||
|
||||
const viewport = useMemo(() => pickViewport(points), [points]);
|
||||
|
||||
const visible = useMemo(() => {
|
||||
if (!viewport) return [];
|
||||
return points.filter(p => p.viewportW === viewport.width && p.viewportH === viewport.height);
|
||||
}, [points, viewport]);
|
||||
|
||||
const maxCount = useMemo(
|
||||
() => visible.reduce((m, p) => (p.count > m ? p.count : m), 1),
|
||||
[visible],
|
||||
);
|
||||
|
||||
if (!viewport || visible.length === 0) {
|
||||
return <EmptyState />;
|
||||
}
|
||||
|
||||
const scale = RENDER_WIDTH / viewport.width;
|
||||
const renderHeight = Math.round(viewport.height * scale);
|
||||
const iframeSrc = domain ? `https://${domain}${urlPath}` : null;
|
||||
|
||||
return (
|
||||
<Column gap>
|
||||
<Row alignItems="center" justifyContent="space-between" gap>
|
||||
<Text color="muted">
|
||||
{visible.length} positions · {formatLongNumber(visible.reduce((s, p) => s + p.count, 0))}{' '}
|
||||
clicks · viewport {viewport.width}×{viewport.height}
|
||||
</Text>
|
||||
{iframeSrc && (
|
||||
<button
|
||||
type="button"
|
||||
className={styles.toggleButton}
|
||||
onClick={() => setShowPage(v => !v)}
|
||||
>
|
||||
{showPage ? 'Hide page' : 'Show page'}
|
||||
</button>
|
||||
)}
|
||||
</Row>
|
||||
<div
|
||||
className={styles.canvas}
|
||||
style={{ width: RENDER_WIDTH, height: renderHeight }}
|
||||
>
|
||||
{showPage && iframeSrc && (
|
||||
<iframe
|
||||
className={styles.iframe}
|
||||
src={iframeSrc}
|
||||
width={viewport.width}
|
||||
height={viewport.height}
|
||||
style={{ transform: `scale(${scale})` }}
|
||||
sandbox="allow-same-origin"
|
||||
referrerPolicy="no-referrer"
|
||||
/>
|
||||
)}
|
||||
<div className={styles.overlay}>
|
||||
{visible.map((p, i) => {
|
||||
const intensity = Math.min(1, p.count / maxCount);
|
||||
const size = 24 + intensity * 36;
|
||||
return (
|
||||
<div
|
||||
key={`${p.x}-${p.y}-${i}`}
|
||||
className={styles.dot}
|
||||
style={{
|
||||
left: p.x * scale - size / 2,
|
||||
top: p.y * scale - size / 2,
|
||||
width: size,
|
||||
height: size,
|
||||
opacity: 0.25 + intensity * 0.55,
|
||||
}}
|
||||
title={`${p.count} click${p.count === 1 ? '' : 's'}`}
|
||||
/>
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
</div>
|
||||
</Column>
|
||||
);
|
||||
}
|
||||
|
||||
function EmptyState() {
|
||||
return (
|
||||
<Column alignItems="center" justifyContent="center" height="100%" gap>
|
||||
<Heading size="lg">Select a page</Heading>
|
||||
<Text color="muted">Choose a page from the list to view its click heatmap.</Text>
|
||||
</Column>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
'use client';
|
||||
import { Column } from '@umami/react-zen';
|
||||
import { useState } from 'react';
|
||||
import { WebsiteControls } from '@/app/(main)/websites/[websiteId]/WebsiteControls';
|
||||
import { Panel } from '@/components/common/Panel';
|
||||
import { Heatmap } from './Heatmap';
|
||||
|
||||
export function HeatmapsPage({ websiteId }: { websiteId: string }) {
|
||||
const [urlPath, setUrlPath] = useState<string>('');
|
||||
|
||||
return (
|
||||
<Column gap>
|
||||
<WebsiteControls websiteId={websiteId} />
|
||||
<Panel height="900px" allowFullscreen>
|
||||
<Heatmap websiteId={websiteId} urlPath={urlPath} onUrlPathChange={setUrlPath} />
|
||||
</Panel>
|
||||
</Column>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
import type { Metadata } from 'next';
|
||||
import { HeatmapsPage } from './HeatmapsPage';
|
||||
|
||||
export default async function ({ params }: { params: Promise<{ websiteId: string }> }) {
|
||||
const { websiteId } = await params;
|
||||
|
||||
return <HeatmapsPage websiteId={websiteId} />;
|
||||
}
|
||||
|
||||
export const metadata: Metadata = {
|
||||
title: 'Heatmaps',
|
||||
};
|
||||
@@ -9,6 +9,8 @@ import { parseRequest } from '@/lib/request';
|
||||
import { badRequest, forbidden, json, serverError } from '@/lib/response';
|
||||
import { getWebsite } from '@/queries/prisma';
|
||||
import { saveRecording } from '@/queries/sql';
|
||||
import { extractHeatmapEvents } from '@/queries/sql/heatmap/extractHeatmapEvents';
|
||||
import { saveHeatmapEvents } from '@/queries/sql/heatmap/saveHeatmapEvents';
|
||||
|
||||
interface Cache {
|
||||
sessionId: string;
|
||||
@@ -113,6 +115,22 @@ export async function POST(request: Request) {
|
||||
endedAt,
|
||||
});
|
||||
|
||||
try {
|
||||
const heatmapRows = extractHeatmapEvents(events).map(e => ({
|
||||
websiteId,
|
||||
sessionId,
|
||||
visitId,
|
||||
...e,
|
||||
}));
|
||||
|
||||
if (heatmapRows.length) {
|
||||
await saveHeatmapEvents(heatmapRows);
|
||||
}
|
||||
} catch (e) {
|
||||
// eslint-disable-next-line no-console
|
||||
console.log('heatmap extraction failed', serializeError(e));
|
||||
}
|
||||
|
||||
return json({ ok: true });
|
||||
} catch (e) {
|
||||
const error = serializeError(e);
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
import { parseRequest, setWebsiteDate } from '@/lib/request';
|
||||
import { json, unauthorized } from '@/lib/response';
|
||||
import { reportResultSchema } from '@/lib/schema';
|
||||
import { canViewWebsite } from '@/permissions';
|
||||
import { getHeatmap, type HeatmapParameters } from '@/queries/sql';
|
||||
|
||||
export async function POST(request: Request) {
|
||||
const { auth, body, error } = await parseRequest(request, reportResultSchema);
|
||||
|
||||
if (error) {
|
||||
return error();
|
||||
}
|
||||
|
||||
const { websiteId } = body;
|
||||
|
||||
if (!(await canViewWebsite(auth, websiteId))) {
|
||||
return unauthorized();
|
||||
}
|
||||
|
||||
const parameters = (await setWebsiteDate(websiteId, body.parameters)) as HeatmapParameters;
|
||||
|
||||
const data = await getHeatmap(websiteId, parameters);
|
||||
|
||||
return json(data);
|
||||
}
|
||||
Reference in new issue
Block a user