Track HiveGrid Cloud planning reference
This commit is contained in:
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);
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
/**
|
||||
* 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",
|
||||
},
|
||||
});
|
||||
}
|
||||
Reference in new issue
Block a user