Files
HiveGrid-Wireframes/03 Quellen und Referenzen/hivegrid-cloud/apps/web/app/routes/e.o.$outboundEmailId.tsx
T
2026-07-08 16:15:37 +02:00

53 lines
1.9 KiB
TypeScript

/**
* AK-P14a · P14 Email — open-tracking pixel.
*
* GET /e/o/:outboundEmailId
*
* Returns a 1x1 transparent GIF and records an OutboundEmailOpen (the first open
* of an email is flagged). Unauthenticated — it is fetched by the recipient's
* mail client. The client IP is stored ONLY as a truncated sha256 hash. An
* unknown / malformed id still returns the pixel (mail clients must not see an
* error), the open is simply not recorded.
*
* The marketing send path (AK-P14b) embeds this URL; this route is the tracking
* sink only.
*/
import type { LoaderFunctionArgs } from "react-router";
import { anonymizeTrackingIp, recordEmailOpen } from "@hivegrid/db/email-tracking.server";
import { db } from "~/utils/db.server";
import { getClientIPAddress } from "~/utils/server/IpUtils";
import { createScopedLogger } from "~/utils/logger.server";
const log = createScopedLogger("email.tracking.open");
// 1x1 transparent GIF.
const TRACKING_PIXEL = Buffer.from("R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7", "base64");
function pixelResponse(): Response {
return new Response(TRACKING_PIXEL, {
status: 200,
headers: {
"Content-Type": "image/gif",
"Content-Length": String(TRACKING_PIXEL.byteLength),
"Cache-Control": "no-store, no-cache, must-revalidate, private",
Pragma: "no-cache",
},
});
}
export async function loader({ params, request }: LoaderFunctionArgs) {
const outboundEmailId = params.outboundEmailId;
if (outboundEmailId) {
try {
const ipHash = anonymizeTrackingIp(getClientIPAddress(request));
await recordEmailOpen({ outboundEmailId, ipHash }, db as never);
} catch (error) {
// Tracking must never break pixel delivery.
log.error({ outboundEmailId, err: error instanceof Error ? error.message : String(error) }, "email open tracking failed");
}
}
return pixelResponse();
}