Skip to content

#1 PDF Application on GitHub that lets you edit PDFs on any device anywhere

4.1 KB
Raw Blame
import { useEffect } from "react";
import { useAppConfig } from "@editor/contexts/AppConfigContext";
import { setActivePosthog } from "@editor/services/analytics";

// posthog-js is ~230 KB and only needed when analytics is on. This hook owns the
// load and publishes the client to analytics.ts, so capture never loads it.
type Posthog = typeof import("posthog-js").default;

let posthogPromise: Promise<Posthog> | null = null;
let activePosthog: Posthog | null = null;

function loadPosthog(): Promise<Posthog> {
  posthogPromise ??= import("posthog-js").then((mod) => mod.default);
  return posthogPromise;
}

function applyPosthogConsent(posthog: Posthog): void {
  if (typeof window === "undefined" || !posthog.__loaded) {
    return;
  }

  const optedIn =
    window.CookieConsent?.acceptedService?.("posthog", "analytics") || false;

  if (optedIn) {
    posthog.set_config({ persistence: "localStorage+cookie" });
    posthog.opt_in_capturing();
    return;
  }

  posthog.opt_out_capturing();
  posthog.set_config({ persistence: "memory" });
}

function posthogConfig(): { key: string; host: string } | null {
  const key = import.meta.env.VITE_PUBLIC_POSTHOG_KEY;
  const host = import.meta.env.VITE_PUBLIC_POSTHOG_HOST;
  if (!key || !host) return null;
  return { key, host };
}

function initializePosthog(posthog: Posthog, key: string, host: string): void {
  if (posthog.__loaded) return;
  posthog.init(key, {
    api_host: host,
    defaults: "2025-05-24",
    capture_exceptions: true,
    debug: false,
    opt_out_capturing_by_default: true,
    persistence: "memory",
    cross_subdomain_cookie: false,
    // Supabase auth redirects carry the session tokens in the hash.
    disable_capture_url_hashes: true,
    session_recording: { blockSelector: ".ph-sensitive" },
    // Autocapture records attributes even on ph-sensitive elements, and these
    // are the ones that carry file names, link targets and usernames. A link's
    // href is still sent when the link is outside ph-sensitive.
    autocapture: {
      element_attribute_ignorelist: [
        "aria-label",
        "title",
        "alt",
        "href",
        "value",
        "data-testid",
        "data-slot-filename",
      ],
    },
  });
}

export function usePosthogTracking(): void {
  const { config } = useAppConfig();

  useEffect(() => {
    const analyticsEnabled = config?.enableAnalytics === true;
    const posthogEnabled = analyticsEnabled && config?.enablePosthog !== false;

    if (!posthogEnabled) {
      // Nothing loaded means nothing to opt out of; loading the module to do
      // so would defeat the deferral. An already-loaded client is opted out so
      // its own autocapture stops too, not just our capture() calls.
      if (activePosthog) {
        activePosthog.opt_out_capturing();
        activePosthog.set_config({ persistence: "memory" });
      }
      setActivePosthog(null);
      return;
    }

    const credentials = posthogConfig();
    if (!credentials) {
      return;
    }

    let cancelled = false;
    let handleConsentChange: (() => void) | null = null;

    void (async () => {
      const load = loadPosthog();
      const posthog = await load.catch(() => {
        // A rejected import would otherwise be replayed to every later effect;
        // clearing the cache lets the next enabled effect retry.
        if (posthogPromise === load) posthogPromise = null;
        return null;
      });
      if (!posthog || cancelled) return;
      activePosthog = posthog;
      setActivePosthog(posthog);

      initializePosthog(posthog, credentials.key, credentials.host);
      applyPosthogConsent(posthog);

      handleConsentChange = () => {
        applyPosthogConsent(posthog);
      };
      window.addEventListener("cc:onConsent", handleConsentChange);
      window.addEventListener("cc:onChange", handleConsentChange);
    })();

    return () => {
      cancelled = true;
      if (handleConsentChange) {
        window.removeEventListener("cc:onConsent", handleConsentChange);
        window.removeEventListener("cc:onChange", handleConsentChange);
      }
    };
  }, [config?.enableAnalytics, config?.enablePosthog]);
}