Track HiveGrid Cloud planning reference

This commit is contained in:
david committed 2026-06-26 20:48:01 +02:00
1 parent b3d5ea6764
commit e341c5938a
5784 files changed
+1104929 -3

No files matched your search

@@ -0,0 +1,52 @@
/**
* Marketing-Funnel Phase 3 — click-through tracking + redirect (Phase 05 #80).
*
* GET /email/track-click/:deliveryId?u=<encoded-target-url>
*
* Records the first clickedAt for the delivery row and 302-redirects
* the browser to the original href. Only http(s) URLs are accepted
* to prevent open-redirect abuse (e.g. javascript: / data: schemes).
*
* Idempotent: subsequent clicks never overwrite the first-touch
* timestamp. Stats jobs aggregate raw counts per delivery.
*/
import type { LoaderFunctionArgs } from "react-router";
import { redirect } from "react-router";
import { db } from "~/utils/db.server";
import { createScopedLogger } from "~/utils/logger.server";
const logger = createScopedLogger("marketing.tracking.click");
function isSafeRedirect(url: string): boolean {
try {
const parsed = new URL(url);
return parsed.protocol === "https:" || parsed.protocol === "http:";
} catch {
return false;
}
}
export async function loader({ params, request }: LoaderFunctionArgs): Promise<Response> {
const deliveryId = params.deliveryId ?? "";
const url = new URL(request.url);
const target = url.searchParams.get("u") ?? "";
if (!isSafeRedirect(target)) {
logger.warn({ op: "marketing.tracking.click.unsafe_url", deliveryId }, "click target rejected");
return new Response("Invalid redirect target", { status: 400 });
}
if (deliveryId.length > 0) {
try {
await db.emailSequenceStepDelivery.updateMany({
where: { id: deliveryId, clickedAt: null },
data: { clickedAt: new Date() },
});
} catch (err) {
logger.warn(
{ op: "marketing.tracking.click.error", deliveryId, errorName: (err as Error)?.name },
"click tracker update failed"
);
}
}
return redirect(target);
}