Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions components/PostHogProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,35 @@
"use client";

import posthog from "posthog-js";
import type { CaptureResult } from "posthog-js";
import { PostHogProvider as PHProvider, usePostHog } from "posthog-js/react";
import { usePathname, useSearchParams } from "next/navigation";
import { useEffect, Suspense } from "react";

// Third-party embeds (for example HubSpot) throw their own uncaught errors.
// Exception autocapture reports these, but they are not app bugs. Drop any
// exception whose every stack frame comes from one of these hosts.
const THIRD_PARTY_ERROR_HOSTS = ["hscollectedforms.net", "hs-scripts.com"];

function isThirdPartyException(event: CaptureResult): boolean {
const exceptions = event.properties?.$exception_list;
if (!Array.isArray(exceptions)) {
return false;
}

const frames = exceptions.flatMap(
(exception) => exception?.stacktrace?.frames ?? [],
);
if (frames.length === 0) {
return false;
}

return frames.every((frame) => {
const filename: string = frame?.filename ?? "";
return THIRD_PARTY_ERROR_HOSTS.some((host) => filename.includes(host));
});
}

export function PostHogProvider({ children }: { children: React.ReactNode }) {
useEffect(() => {
if (process.env.NEXT_PUBLIC_POSTHOG_KEY) {
Expand All @@ -14,6 +39,12 @@ export function PostHogProvider({ children }: { children: React.ReactNode }) {
person_profiles: "always",
capture_pageview: false,
capture_pageleave: true,
before_send: (event) => {
if (event?.event === "$exception" && isThirdPartyException(event)) {
return null;
}
return event;
},
});
}
}, []);
Expand Down