Skip to content

Commit c8eefaf

Browse files
committed
marketing: avoid double cast in copy-button analytics
The clipboard handler now asks for a capture via a DOM event (analytics:capture) that the gated PostHog init listens for, instead of reaching through a window double cast. Fixes the lint rule that banned casting through unknown.
1 parent 164792d commit c8eefaf

2 files changed

Lines changed: 16 additions & 8 deletions

File tree

apps/marketing/src/layouts/Layout.astro

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,17 @@ const canonical = new URL(Astro.url.pathname, Astro.site ?? Astro.url).toString(
6767
capture_pageview: false,
6868
persistence: "localStorage",
6969
});
70-
Object.assign(window, { posthog });
70+
// The copy handler (in index.astro) is a separate bundle, so it asks
71+
// for a capture via a DOM event rather than reaching for a global.
72+
window.addEventListener("analytics:capture", (e) => {
73+
const detail = (
74+
e as CustomEvent<{
75+
event: string;
76+
properties?: Record<string, unknown>;
77+
}>
78+
).detail;
79+
posthog.capture(detail.event, detail.properties);
80+
});
7181
});
7282
}
7383
</script>

apps/marketing/src/pages/index.astro

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -442,13 +442,11 @@ Source (and the place to start if something breaks): https://github.com/RhysSull
442442
btn.dataset.copied = "true";
443443
const event = btn.dataset.event;
444444
if (event) {
445-
(
446-
window as unknown as {
447-
posthog?: {
448-
capture: (e: string, p?: Record<string, unknown>) => void;
449-
};
450-
}
451-
).posthog?.capture(event, { surface: "marketing_hero" });
445+
window.dispatchEvent(
446+
new CustomEvent("analytics:capture", {
447+
detail: { event, properties: { surface: "marketing_hero" } },
448+
}),
449+
);
452450
}
453451
} catch {
454452
btn.dataset.copied = "false";

0 commit comments

Comments
 (0)