implement sepearte replay/heatmap enabled routes and collection
This commit is contained in:
1 parent
4b40be6b5a
commit
5bd92ce950
39 files changed
+586
-295
No files matched your search
+116
-38
@@ -5,11 +5,12 @@ import { secret } from '@/lib/crypto';
|
||||
import { getClientInfo, hasBlockedIp } from '@/lib/detect';
|
||||
import { parseToken } from '@/lib/jwt';
|
||||
import { fetchAccount, fetchTeam } from '@/lib/load';
|
||||
import { HEATMAP_EVENT_TYPE } from '@/lib/constants';
|
||||
import { getRecorderConfig } from '@/lib/recorder';
|
||||
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 {
|
||||
@@ -17,14 +18,60 @@ interface Cache {
|
||||
visitId: string;
|
||||
}
|
||||
|
||||
const schema = z.object({
|
||||
type: z.literal('record'),
|
||||
payload: z.object({
|
||||
website: z.uuid(),
|
||||
events: z.array(z.any()).max(200),
|
||||
timestamp: z.coerce.number().int().optional(),
|
||||
const schema = z.discriminatedUnion('type', [
|
||||
z.object({
|
||||
type: z.literal('record'),
|
||||
payload: z.object({
|
||||
website: z.uuid(),
|
||||
events: z.array(z.any()).max(200),
|
||||
timestamp: z.coerce.number().int().optional(),
|
||||
}),
|
||||
}),
|
||||
});
|
||||
z.object({
|
||||
type: z.literal('heatmap'),
|
||||
payload: z.object({
|
||||
website: z.uuid(),
|
||||
events: z
|
||||
.array(
|
||||
z.discriminatedUnion('type', [
|
||||
z.object({
|
||||
type: z.literal('click'),
|
||||
url: z.string(),
|
||||
x: z.coerce.number().optional(),
|
||||
y: z.coerce.number().optional(),
|
||||
pageX: z.coerce.number().optional(),
|
||||
pageY: z.coerce.number().optional(),
|
||||
pageW: z.coerce.number().optional(),
|
||||
pageH: z.coerce.number().optional(),
|
||||
viewportW: z.coerce.number().optional(),
|
||||
viewportH: z.coerce.number().optional(),
|
||||
timestamp: z.coerce.number().int().optional(),
|
||||
}),
|
||||
z.object({
|
||||
type: z.literal('scroll'),
|
||||
url: z.string(),
|
||||
scrollPct: z.coerce.number().optional(),
|
||||
pageW: z.coerce.number().optional(),
|
||||
pageH: z.coerce.number().optional(),
|
||||
viewportW: z.coerce.number().optional(),
|
||||
viewportH: z.coerce.number().optional(),
|
||||
timestamp: z.coerce.number().int().optional(),
|
||||
}),
|
||||
]),
|
||||
)
|
||||
.max(200),
|
||||
timestamp: z.coerce.number().int().optional(),
|
||||
}),
|
||||
}),
|
||||
]);
|
||||
|
||||
function getUrlPath(url: string) {
|
||||
try {
|
||||
return new URL(url).pathname || '/';
|
||||
} catch {
|
||||
return url.startsWith('/') ? url.split(/[?#]/)[0] || '/' : '/';
|
||||
}
|
||||
}
|
||||
|
||||
export async function POST(request: Request) {
|
||||
try {
|
||||
@@ -34,7 +81,9 @@ export async function POST(request: Request) {
|
||||
return error();
|
||||
}
|
||||
|
||||
const { website: websiteId, events, timestamp } = body.payload;
|
||||
const { website: websiteId } = body.payload;
|
||||
const events = body.payload.events;
|
||||
const timestamp = body.payload.timestamp;
|
||||
|
||||
if (!events?.length) {
|
||||
return json({ ok: true });
|
||||
@@ -55,15 +104,19 @@ export async function POST(request: Request) {
|
||||
|
||||
const { sessionId, visitId } = cache;
|
||||
|
||||
// Query directly to avoid stale Redis cache for recordingEnabled
|
||||
// Query directly to avoid stale Redis cache for recorderEnabled
|
||||
const website = await getWebsite(websiteId);
|
||||
|
||||
if (!website) {
|
||||
return badRequest({ message: 'Website not found.' });
|
||||
}
|
||||
|
||||
if (!website.replayEnabled) {
|
||||
return json({ ok: false, reason: 'replay_disabled' });
|
||||
const recorderConfig = getRecorderConfig(website.replayConfig);
|
||||
const replayEnabled = recorderConfig.replayEnabled === true;
|
||||
const heatmapEnabled = recorderConfig.heatmapEnabled === true;
|
||||
|
||||
if (!website.recorderEnabled) {
|
||||
return json({ ok: false, reason: 'recorder_disabled' });
|
||||
}
|
||||
|
||||
if (process.env.CLOUD_MODE) {
|
||||
@@ -89,38 +142,63 @@ export async function POST(request: Request) {
|
||||
return forbidden();
|
||||
}
|
||||
|
||||
// Compute timestamps from events
|
||||
const eventTimestamps = events
|
||||
.map((e: any) => Number(e?.timestamp))
|
||||
.filter((t: number) => Number.isFinite(t) && t > 0);
|
||||
if (body.type === 'record') {
|
||||
if (!replayEnabled) {
|
||||
return json({ ok: false, reason: 'replay_disabled' });
|
||||
}
|
||||
|
||||
const fallbackMs = (timestamp || Math.floor(Date.now() / 1000)) * 1000;
|
||||
const minTimestamp = eventTimestamps.length ? Math.min(...eventTimestamps) : fallbackMs;
|
||||
const maxTimestamp = eventTimestamps.length ? Math.max(...eventTimestamps) : fallbackMs;
|
||||
const eventTimestamps = events
|
||||
.map((e: any) => Number(e?.timestamp))
|
||||
.filter((t: number) => Number.isFinite(t) && t > 0);
|
||||
|
||||
const startedAt = new Date(minTimestamp);
|
||||
const endedAt = new Date(maxTimestamp);
|
||||
const fallbackMs = (timestamp || Math.floor(Date.now() / 1000)) * 1000;
|
||||
const minTimestamp = eventTimestamps.length ? Math.min(...eventTimestamps) : fallbackMs;
|
||||
const maxTimestamp = eventTimestamps.length ? Math.max(...eventTimestamps) : fallbackMs;
|
||||
|
||||
// Use timestamp-based chunk index for ordering
|
||||
const chunkIndex = timestamp || Math.floor(Date.now() / 1000);
|
||||
const startedAt = new Date(minTimestamp);
|
||||
const endedAt = new Date(maxTimestamp);
|
||||
const chunkIndex = timestamp || Math.floor(Date.now() / 1000);
|
||||
|
||||
await saveRecording({
|
||||
websiteId,
|
||||
sessionId,
|
||||
visitId,
|
||||
chunkIndex,
|
||||
events,
|
||||
eventCount: events.length,
|
||||
startedAt,
|
||||
endedAt,
|
||||
});
|
||||
|
||||
try {
|
||||
const heatmapRows = extractHeatmapEvents(events, { chunkIndex }).map(e => ({
|
||||
await saveRecording({
|
||||
websiteId,
|
||||
sessionId,
|
||||
visitId,
|
||||
...e,
|
||||
chunkIndex,
|
||||
events,
|
||||
eventCount: events.length,
|
||||
startedAt,
|
||||
endedAt,
|
||||
});
|
||||
|
||||
return json({ ok: true });
|
||||
}
|
||||
|
||||
if (!heatmapEnabled) {
|
||||
return json({ ok: false, reason: 'heatmap_disabled' });
|
||||
}
|
||||
|
||||
try {
|
||||
const fallbackMs = (timestamp || Math.floor(Date.now() / 1000)) * 1000;
|
||||
const heatmapRows = events.map(event => ({
|
||||
websiteId,
|
||||
sessionId,
|
||||
visitId,
|
||||
eventType: event.type === 'click' ? HEATMAP_EVENT_TYPE.click : HEATMAP_EVENT_TYPE.scroll,
|
||||
nodeId: null,
|
||||
x: event.type === 'click' ? event.x ?? null : null,
|
||||
y: event.type === 'click' ? event.y ?? null : null,
|
||||
pageX: event.type === 'click' ? event.pageX ?? null : null,
|
||||
pageY: event.type === 'click' ? event.pageY ?? null : null,
|
||||
pageW: event.pageW ?? null,
|
||||
viewportW: event.viewportW ?? null,
|
||||
viewportH: event.viewportH ?? null,
|
||||
pageH: event.pageH ?? null,
|
||||
scrollPct: event.type === 'scroll' ? event.scrollPct ?? null : null,
|
||||
urlPath: getUrlPath(event.url),
|
||||
createdAt: new Date(event.timestamp ?? fallbackMs),
|
||||
replayChunkIndex: null,
|
||||
replayEventIndex: null,
|
||||
replayTimeMs: null,
|
||||
}));
|
||||
|
||||
if (heatmapRows.length) {
|
||||
@@ -128,7 +206,7 @@ export async function POST(request: Request) {
|
||||
}
|
||||
} catch (e) {
|
||||
// eslint-disable-next-line no-console
|
||||
console.log('heatmap extraction failed', serializeError(e));
|
||||
console.log('heatmap save failed', serializeError(e));
|
||||
}
|
||||
|
||||
return json({ ok: true });
|
||||
|
||||
@@ -1,14 +1,7 @@
|
||||
import { parseRequest } from '@/lib/request';
|
||||
import { json } from '@/lib/response';
|
||||
import { getRecorderConfig } from '@/lib/recorder';
|
||||
import { getWebsite } from '@/queries/prisma';
|
||||
|
||||
interface ReplayConfig {
|
||||
sampleRate?: number;
|
||||
maskLevel?: string;
|
||||
maxDuration?: number;
|
||||
blockSelector?: string;
|
||||
}
|
||||
|
||||
export async function GET(
|
||||
request: Request,
|
||||
{ params }: { params: Promise<{ websiteId: string }> },
|
||||
@@ -26,16 +19,19 @@ export async function GET(
|
||||
'Cache-Control': 'public, max-age=60, stale-while-revalidate=300',
|
||||
};
|
||||
|
||||
if (!website || !website.replayEnabled) {
|
||||
if (!website || !website.recorderEnabled) {
|
||||
return Response.json({ enabled: false }, { headers });
|
||||
}
|
||||
|
||||
const config = (website.replayConfig as ReplayConfig) || {};
|
||||
const config = getRecorderConfig(website.replayConfig);
|
||||
|
||||
return Response.json(
|
||||
{
|
||||
enabled: true,
|
||||
replayEnabled: config.replayEnabled === true,
|
||||
heatmapEnabled: config.heatmapEnabled === true,
|
||||
sampleRate: config.sampleRate ?? 0.15,
|
||||
heatmapSampleRate: config.heatmapSampleRate ?? 0.15,
|
||||
maskLevel: config.maskLevel ?? 'moderate',
|
||||
maxDuration: config.maxDuration ?? 300000,
|
||||
blockSelector: config.blockSelector ?? '',
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
import { z } from 'zod';
|
||||
import type { Prisma } from '@/generated/prisma/client';
|
||||
import { ENTITY_TYPE } from '@/lib/constants';
|
||||
import { uuid } from '@/lib/crypto';
|
||||
import { getRecorderConfig, getRecorderEnabled } from '@/lib/recorder';
|
||||
import { parseRequest } from '@/lib/request';
|
||||
import { badRequest, json, ok, serverError, unauthorized } from '@/lib/response';
|
||||
import { canDeleteWebsite, canUpdateWebsite, canViewWebsite } from '@/permissions';
|
||||
@@ -42,10 +44,12 @@ export async function POST(
|
||||
name: z.string().optional(),
|
||||
domain: z.string().optional(),
|
||||
shareId: z.string().max(50).nullable().optional(),
|
||||
replayEnabled: z.boolean().optional(),
|
||||
replayConfig: z
|
||||
.object({
|
||||
replayEnabled: z.boolean().optional(),
|
||||
heatmapEnabled: z.boolean().optional(),
|
||||
sampleRate: z.number().min(0).max(1).optional(),
|
||||
heatmapSampleRate: z.number().min(0).max(1).optional(),
|
||||
maskLevel: z.enum(['strict', 'moderate']).optional(),
|
||||
maxDuration: z.number().int().positive().optional(),
|
||||
blockSelector: z.string().optional(),
|
||||
@@ -61,18 +65,35 @@ export async function POST(
|
||||
}
|
||||
|
||||
const { websiteId } = await params;
|
||||
const { name, domain, shareId, replayEnabled, replayConfig } = body;
|
||||
const { name, domain, shareId, replayConfig } = body;
|
||||
|
||||
if (!(await canUpdateWebsite(auth, websiteId))) {
|
||||
return unauthorized();
|
||||
}
|
||||
|
||||
try {
|
||||
const currentWebsite = await getWebsite(websiteId);
|
||||
|
||||
if (!currentWebsite) {
|
||||
return badRequest({ message: 'Website not found.' });
|
||||
}
|
||||
|
||||
const nextReplayConfig = getRecorderConfig(
|
||||
replayConfig === null
|
||||
? {}
|
||||
: {
|
||||
...getRecorderConfig(currentWebsite.replayConfig),
|
||||
...(replayConfig ?? {}),
|
||||
},
|
||||
);
|
||||
|
||||
const website = await updateWebsite(websiteId, {
|
||||
name,
|
||||
domain,
|
||||
...(replayEnabled !== undefined && { replayEnabled }),
|
||||
...(replayConfig !== undefined && { replayConfig }),
|
||||
...(replayConfig !== undefined && {
|
||||
replayConfig: nextReplayConfig as Prisma.InputJsonObject,
|
||||
recorderEnabled: getRecorderEnabled(nextReplayConfig),
|
||||
}),
|
||||
});
|
||||
|
||||
if (shareId === null) {
|
||||
|
||||
Reference in new issue
Block a user