From 130942bde8355f0a6df61a92d5c987ab428de17e Mon Sep 17 00:00:00 2001 From: jose-donato <43375532+jose-donato@users.noreply.github.com> Date: Tue, 25 Apr 2023 19:53:51 +0100 Subject: [PATCH] fix: no window thing (#4900) --- website/src/theme/Root.tsx | 50 ++++++++++++++++++++------------------ 1 file changed, 26 insertions(+), 24 deletions(-) diff --git a/website/src/theme/Root.tsx b/website/src/theme/Root.tsx index bc8b5a579c7..87626fb3e2e 100644 --- a/website/src/theme/Root.tsx +++ b/website/src/theme/Root.tsx @@ -8,30 +8,9 @@ export const iFrameContext = createContext({ export const useIFrameContext = () => useContext(iFrameContext); -posthog.init("phc_EqU3YjnV8OYmBlKanwWq222B8OHQksfmQBUtcVeteHR", { - api_host: "https://app.posthog.com", - autocapture: { - css_selector_allowlist: [".ph-capture"], - }, - loaded: () => { - posthog.onFeatureFlags(function () { - if (!posthog.isFeatureEnabled("record-web", { send_event: false })) { - posthog.stopSessionRecording(); - console.log("Stopped session recording"); - } - if (!posthog.isFeatureEnabled("collect-logs-web", { send_event: false })) { - posthog.opt_out_capturing(); - console.log("Opted out of capturing"); - } else if (posthog.has_opted_out_capturing()) { - posthog.opt_in_capturing(); - console.log("Opted in to capturing"); - } - }); - }, -}); - export default function Root({ children }) { const [isIFrame, setIsIFrame] = useState(false); + const [posthogLoaded, setPosthogLoaded] = useState(false); useEffect(() => { setIsIFrame(window.self !== window.top); if (window.self !== window.top) { @@ -42,13 +21,36 @@ export default function Root({ children }) { } }); } + posthog.init("phc_EqU3YjnV8OYmBlKanwWq222B8OHQksfmQBUtcVeteHR", { + api_host: "https://app.posthog.com", + autocapture: { + css_selector_allowlist: [".ph-capture"], + }, + loaded: () => { + setPosthogLoaded(true); + posthog.onFeatureFlags(function () { + if (!posthog.isFeatureEnabled("record-web", { send_event: false })) { + posthog.stopSessionRecording(); + console.log("Stopped session recording"); + } + if (!posthog.isFeatureEnabled("collect-logs-web", { send_event: false })) { + posthog.opt_out_capturing(); + console.log("Opted out of capturing"); + } else if (posthog.has_opted_out_capturing()) { + posthog.opt_in_capturing(); + console.log("Opted in to capturing"); + } + }); + }, + }); }, []); const location = useLocation(); useEffect(() => { - posthog.capture("$pageview"); - }, [location]); + if (posthogLoaded) + posthog.capture("$pageview"); + }, [location.pathname, posthogLoaded]); return (