Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 35 additions & 16 deletions components/auto-events.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
// search {via} site search submitted/typed (query text is never sent)
// file_selected {ext, via} a file picked or dropped into a tool
// <data-event> {data-event-*} any element with data-event="name" (CTAs, book_call…)
// engaged {via} first real human input (trusted wheel/touch/key or pointer moves after 3s visible), once per tab session
// engaged {via} first real human input (trusted wheel/touch/key or 3rd pointer move), sent once the page has been visible 1s; once per tab session
//
// Guardrails: max 2 props per event (Vercel limit on some plans), no user
// text/PII, identical events de-duplicated within 1s, at most 40 events per
Expand Down Expand Up @@ -60,8 +60,10 @@ export function AutoEvents() {

// engaged {via}: once per tab session, the first sign of a real person.
// Headless crawlers that run JS rarely produce trusted input, so
// "visitors with engaged" is the trusted-traffic count.
// "visitors with engaged" is the trusted-traffic count. Input during the
// first second is kept and sent once the page has been visible for 1s.
const ENGAGED_KEY = "ae_engaged";
const MIN_VISIBLE_MS = 1000;
let engagedDone = false;
try {
engagedDone = sessionStorage.getItem(ENGAGED_KEY) === "1";
Expand All @@ -70,31 +72,47 @@ export function AutoEvents() {
}
let visibleMs = 0;
let visibleSince = document.visibilityState === "visible" ? performance.now() : 0;
const onVisibility = () => {
if (document.visibilityState === "visible") {
if (!visibleSince) visibleSince = performance.now();
} else if (visibleSince) {
visibleMs += performance.now() - visibleSince;
visibleSince = 0;
}
};
const visibleFor = () => visibleMs + (visibleSince ? performance.now() - visibleSince : 0);
const HUMAN_EVENTS = ["pointermove", "wheel", "touchstart", "keydown"] as const;
let moves = 0;
const onHuman = (e: Event) => {
if (engagedDone || !e.isTrusted || navigator.webdriver) return;
if (e.type === "pointermove" && ++moves < 3) return;
if (visibleFor() < 3000) return;
let pendingVia: string | null = null;
let pendingTimer: ReturnType<typeof setTimeout> | undefined;
const tryEngaged = () => {
clearTimeout(pendingTimer);
if (engagedDone || !pendingVia) return;
const wait = MIN_VISIBLE_MS - visibleFor();
if (wait > 0) {
// Hidden tabs wait for onVisibility to call us again.
if (document.visibilityState === "visible") pendingTimer = setTimeout(tryEngaged, wait + 10);
return;
}
engagedDone = true;
try {
sessionStorage.setItem(ENGAGED_KEY, "1");
} catch {
/* ignore */
}
const via = e.type === "keydown" ? "key" : e.type === "touchstart" ? "touch" : e.type === "wheel" ? "wheel" : "pointer";
send("engaged", { via });
send("engaged", { via: pendingVia });
for (const t of HUMAN_EVENTS) window.removeEventListener(t, onHuman, { capture: true });
};
const onHuman = (e: Event) => {
if (engagedDone || pendingVia || !e.isTrusted || navigator.webdriver) return;
if (e.type === "pointermove" && ++moves < 3) return;
pendingVia = e.type === "keydown" ? "key" : e.type === "touchstart" ? "touch" : e.type === "wheel" ? "wheel" : "pointer";
tryEngaged();
};
const onVisibility = () => {
if (document.visibilityState === "visible") {
if (!visibleSince) visibleSince = performance.now();
tryEngaged();
} else {
if (visibleSince) {
visibleMs += performance.now() - visibleSince;
visibleSince = 0;
}
clearTimeout(pendingTimer);
}
};
if (!engagedDone) {
for (const t of HUMAN_EVENTS) window.addEventListener(t, onHuman, { capture: true, passive: true });
document.addEventListener("visibilitychange", onVisibility);
Expand Down Expand Up @@ -186,6 +204,7 @@ export function AutoEvents() {
window.addEventListener("drop", onDrop, { capture: true });
return () => {
clearTimeout(timer);
clearTimeout(pendingTimer);
for (const t of HUMAN_EVENTS) window.removeEventListener(t, onHuman, { capture: true });
document.removeEventListener("visibilitychange", onVisibility);
document.removeEventListener("click", onClick, { capture: true });
Expand Down