Complete Rybbit campaign aggregation

This commit is contained in:
2026-06-05 21:51:39 +02:00
parent f069b74b08
commit 3efbc06e40
6 changed files with 102 additions and 2 deletions

View File

@@ -22,6 +22,11 @@ export type CampaignRybbitSummary = {
auditOpens: number;
ctaClicks: number;
outboundClicks: number;
byPath: Record<string, {
auditOpens: number;
ctaClicks: number;
outboundClicks: number;
}>;
};
type FetchLike = (
@@ -127,6 +132,25 @@ export function summarizeCampaignRybbitEvents(
events: RybbitEvent[],
): CampaignRybbitSummary {
const auditEvents = events.filter((event) => eventPath(event).startsWith("/audit/"));
const byPath: CampaignRybbitSummary["byPath"] = {};
for (const event of auditEvents) {
const path = eventPath(event);
byPath[path] ??= { auditOpens: 0, ctaClicks: 0, outboundClicks: 0 };
if (event.type === "pageview") {
byPath[path].auditOpens += 1;
}
if (event.type === "custom_event" && eventName(event) === "audit_cta_click") {
byPath[path].ctaClicks += 1;
}
if (
event.type === "outbound_link" ||
eventName(event) === "audit_website_link_click"
) {
byPath[path].outboundClicks += 1;
}
}
return {
auditOpens: auditEvents.filter((event) => event.type === "pageview").length,
@@ -137,6 +161,7 @@ export function summarizeCampaignRybbitEvents(
return event.type === "outbound_link" ||
eventName(event) === "audit_website_link_click";
}).length,
byPath,
};
}