"use client";

import { useEffect } from "react";
import { analytics } from "@/lib/analytics";

export function useAnalytics() {
  useEffect(() => {
    if (typeof window === "undefined") return;

    // Track page view
    analytics.pageView(window.location.pathname);

    // Track scroll depth
    let maxScroll = 0;
    const handleScroll = () => {
      const scrollPercent = Math.round(
        ((window.scrollY + window.innerHeight) / document.documentElement.scrollHeight) * 100
      );
      if (scrollPercent > maxScroll) {
        maxScroll = scrollPercent;
        analytics.scroll(maxScroll);
      }
    };

    window.addEventListener("scroll", handleScroll, { passive: true });

    return () => {
      window.removeEventListener("scroll", handleScroll);
    };
  }, []);

  return analytics;
}

