History

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.

Rhys Sullivan committed 4 months ago

Commit c8eefaf1 parent 164792d2 files · +16 −8Browse files

Unified · Split

2 files
  1. apps/marketing/src/layouts/Layout.astro +11 −1
  2. apps/marketing/src/pages/index.astro +5 −7
apps/marketing/src/layouts/Layout.astro+11−1
@@ -67,7 +67,17 @@ const canonical = new URL(Astro.url.pathname, Astro.site ?? Astro.url).toString( · 66 unmodified lines
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+5−7
@@ -442,13 +442,11 @@ Source (and the place to start if something breaks): https://github.com/RhysSull · 441 unmodified lines
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";

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t