Skip to content

Commit c41bc68

Browse files
authored
Safer posthog access (#354)
1 parent 05feff6 commit c41bc68

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

src/components/PostHogProvider/index.tsx

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,23 @@ export function PostHogProvider({ children }: { children: React.ReactNode }) {
4141
const enablePostHog = () => {
4242
if (typeof window !== 'undefined' && window.posthog) {
4343
// Re-initialize PostHog if it was previously disabled
44-
window.posthog.opt_in_capturing();
45-
window.posthog.startSessionRecording();
44+
if (typeof window.posthog.opt_in_capturing === 'function') {
45+
window.posthog.opt_in_capturing();
46+
}
47+
if (typeof window.posthog.startSessionRecording === 'function') {
48+
window.posthog.startSessionRecording();
49+
}
4650
}
4751
};
4852

4953
const disablePostHog = () => {
5054
if (typeof window !== 'undefined' && window.posthog) {
51-
window.posthog.opt_out_capturing();
52-
window.posthog.stopSessionRecording();
55+
if (typeof window.posthog.opt_out_capturing === 'function') {
56+
window.posthog.opt_out_capturing();
57+
}
58+
if (typeof window.posthog.stopSessionRecording === 'function') {
59+
window.posthog.stopSessionRecording();
60+
}
5361
}
5462
};
5563

0 commit comments

Comments
 (0)