54 lines
1.8 KiB
TypeScript
54 lines
1.8 KiB
TypeScript
/**
|
|
* Marketing-Funnel Phase 3 — open tracking pixel (Phase 05 #80).
|
|
*
|
|
* Serves a transparent 1x1 GIF and records the first openedAt for the
|
|
* given EmailSequenceStepDelivery row. Idempotent: subsequent fetches
|
|
* never overwrite the first-touch timestamp.
|
|
*
|
|
* Mailbox-provider image-blockers (Apple Mail Privacy Protection in
|
|
* particular) make this an approximation — the metric is documented
|
|
* as such in the admin stats UI. No PII is exposed: the URL contains
|
|
* only the delivery primary-key, not the recipient email.
|
|
*/
|
|
|
|
import type { LoaderFunctionArgs } from "react-router";
|
|
import { db } from "~/utils/db.server";
|
|
import { createScopedLogger } from "~/utils/logger.server";
|
|
|
|
const logger = createScopedLogger("marketing.tracking.open");
|
|
|
|
// 1x1 transparent GIF (43 bytes).
|
|
const PIXEL = Buffer.from(
|
|
"47494638396101000100800000FFFFFF00000021F90401000001002C00000000010001000002024401003B",
|
|
"hex"
|
|
);
|
|
|
|
export async function loader({ params }: LoaderFunctionArgs): Promise<Response> {
|
|
const deliveryId = params.deliveryId ?? "";
|
|
if (deliveryId.length > 0) {
|
|
try {
|
|
// Use updateMany so a missing row is a soft no-op, not a P2025
|
|
// exception; and only flip openedAt the FIRST time.
|
|
await db.emailSequenceStepDelivery.updateMany({
|
|
where: { id: deliveryId, openedAt: null },
|
|
data: { openedAt: new Date() },
|
|
});
|
|
} catch (err) {
|
|
logger.warn(
|
|
{ op: "marketing.tracking.open.error", deliveryId, errorName: (err as Error)?.name },
|
|
"open tracker update failed"
|
|
);
|
|
}
|
|
}
|
|
return new Response(PIXEL, {
|
|
status: 200,
|
|
headers: {
|
|
"Content-Type": "image/gif",
|
|
"Content-Length": String(PIXEL.length),
|
|
"Cache-Control": "no-store, max-age=0",
|
|
// Mail-clients pre-fetch — prevent CDN edges from caching.
|
|
Pragma: "no-cache",
|
|
},
|
|
});
|
|
}
|