33 lines
1.3 KiB
TypeScript
33 lines
1.3 KiB
TypeScript
import { beforeEach, describe, expect, it } from "vitest";
|
|||
|
|
import { _resetAnalyticsQueue, track } from "@/lib/analytics";
|
||
|
|
|
||
|
|
describe("analytics layer", () => {
|
||
|
|
beforeEach(() => {
|
||
|
|
window.dataLayer = [];
|
||
|
|
window.sessionStorage.clear();
|
||
|
|
window.localStorage.clear();
|
||
|
|
window.history.replaceState({}, "", "/?utm_source=google&utm_campaign=brand&gclid=G");
|
||
|
|
_resetAnalyticsQueue();
|
||
|
|
});
|
||
|
|
|
||
|
|
it("pushes named events with attribution and device props, without PII", () => {
|
||
|
|
track("campus_selected", { campus: "norwood", selection_source: "manual" });
|
||
|
|
const evt = (window.dataLayer as Record<string, unknown>[]).at(-1)!;
|
||
|
|
expect(evt.event).toBe("campus_selected");
|
||
|
|
expect(evt.campus).toBe("norwood");
|
||
|
|
expect(evt.source).toBe("google");
|
||
|
|
expect(evt.campaign).toBe("brand");
|
||
|
|
expect(evt.platform).toBe("google");
|
||
|
|
expect(evt.variant).toBe("a");
|
||
|
|
expect(["mobile", "tablet", "desktop"]).toContain(evt.device_category);
|
||
|
|
expect(JSON.stringify(evt)).not.toMatch(/@|phone/);
|
||
|
|
});
|
||
|
|
|
||
|
|
it("dispatches a DOM event for custom listeners", () => {
|
||
|
|
const seen: string[] = [];
|
||
|
|
window.addEventListener("wc:analytics", (e) => seen.push((e as CustomEvent).detail.event));
|
||
|
|
track("text_us_clicked", { location: "header" });
|
||
|
|
expect(seen).toEqual(["text_us_clicked"]);
|
||
|
|
});
|
||
|
|
});
|