97 lines
2.6 KiB
TypeScript
97 lines
2.6 KiB
TypeScript
import assert from "node:assert/strict";
|
|
import test from "node:test";
|
|
|
|
import {
|
|
buildRybbitEventsUrl,
|
|
summarizeCampaignRybbitEvents,
|
|
summarizeAuditRybbitEvents,
|
|
type RybbitEvent,
|
|
} from "../lib/rybbit-analytics";
|
|
|
|
test("buildRybbitEventsUrl targets the documented events endpoint", () => {
|
|
const url = buildRybbitEventsUrl({
|
|
apiUrl: "https://analytics.example.com/",
|
|
siteId: "site_123",
|
|
startDate: "2026-06-01T00:00:00.000Z",
|
|
endDate: "2026-06-05T00:00:00.000Z",
|
|
});
|
|
|
|
assert.equal(
|
|
url.toString(),
|
|
"https://analytics.example.com/api/sites/site_123/events?start_date=2026-06-01T00%3A00%3A00.000Z&end_date=2026-06-05T00%3A00%3A00.000Z",
|
|
);
|
|
});
|
|
|
|
test("summarizeAuditRybbitEvents extracts opens, clicks, last view, and devices", () => {
|
|
const events: RybbitEvent[] = [
|
|
{
|
|
type: "pageview",
|
|
timestamp: "2026-06-05T10:00:00.000Z",
|
|
pathname: "/audit/demo",
|
|
properties: { device: "desktop" },
|
|
},
|
|
{
|
|
type: "custom_event",
|
|
timestamp: "2026-06-05T10:05:00.000Z",
|
|
event_name: "audit_cta_click",
|
|
pathname: "/audit/demo",
|
|
properties: { target: "cta", deviceType: "mobile" },
|
|
},
|
|
{
|
|
type: "outbound_link",
|
|
timestamp: "2026-06-05T10:06:00.000Z",
|
|
pathname: "/audit/demo",
|
|
properties: { href: "https://example.com", device: "mobile" },
|
|
},
|
|
{
|
|
type: "pageview",
|
|
timestamp: "2026-06-05T11:00:00.000Z",
|
|
pathname: "/pricing",
|
|
properties: { device: "desktop" },
|
|
},
|
|
];
|
|
|
|
assert.deepEqual(summarizeAuditRybbitEvents(events, "/audit/demo"), {
|
|
opened: true,
|
|
viewCount: 1,
|
|
lastView: "2026-06-05T10:00:00.000Z",
|
|
ctaClicks: 1,
|
|
websiteLinkClicks: 1,
|
|
deviceTypes: ["desktop", "mobile"],
|
|
});
|
|
});
|
|
|
|
test("summarizeAuditRybbitEvents returns graceful empty metrics", () => {
|
|
assert.deepEqual(summarizeAuditRybbitEvents([], "/audit/demo"), {
|
|
opened: false,
|
|
viewCount: 0,
|
|
lastView: null,
|
|
ctaClicks: 0,
|
|
websiteLinkClicks: 0,
|
|
deviceTypes: [],
|
|
});
|
|
});
|
|
|
|
test("summarizeCampaignRybbitEvents aggregates public audit activity", () => {
|
|
assert.deepEqual(
|
|
summarizeCampaignRybbitEvents([
|
|
{ type: "pageview", pathname: "/audit/a" },
|
|
{ type: "pageview", pathname: "/dashboard" },
|
|
{ type: "custom_event", event_name: "audit_cta_click", pathname: "/audit/a" },
|
|
{ type: "outbound_link", pathname: "/audit/a" },
|
|
]),
|
|
{
|
|
auditOpens: 1,
|
|
ctaClicks: 1,
|
|
outboundClicks: 1,
|
|
byPath: {
|
|
"/audit/a": {
|
|
auditOpens: 1,
|
|
ctaClicks: 1,
|
|
outboundClicks: 1,
|
|
},
|
|
},
|
|
},
|
|
);
|
|
});
|