// src/hooks/usePostHog.js import { useCallback } from 'react'; import { getPostHog, trackEvent, trackPageView, identifyUser, setUserProperties, resetUser, optIn, optOut, hasOptedOut, getFeatureFlag, isFeatureEnabled, } from '../lib/posthog'; /** * Custom hook to access PostHog functionality * Provides convenient methods for tracking events and managing user sessions * * @returns {object} PostHog methods */ export const usePostHog = () => { // Get PostHog instance const posthog = getPostHog(); // Track custom event const track = useCallback((eventName, properties = {}) => { trackEvent(eventName, properties); }, []); // Track page view const trackPage = useCallback((pagePath, properties = {}) => { trackPageView(pagePath, properties); }, []); // Identify user const identify = useCallback((userId, userProperties = {}) => { identifyUser(userId, userProperties); }, []); // Set user properties const setProperties = useCallback((properties) => { setUserProperties(properties); }, []); // Reset user session (logout) const reset = useCallback(() => { resetUser(); }, []); // Opt out of tracking const optOutTracking = useCallback(() => { optOut(); }, []); // Opt in to tracking const optInTracking = useCallback(() => { optIn(); }, []); // Check if user has opted out const isOptedOut = useCallback(() => { return hasOptedOut(); }, []); // Get feature flag value const getFlag = useCallback((flagKey, defaultValue = false) => { return getFeatureFlag(flagKey, defaultValue); }, []); // Check if feature is enabled const isEnabled = useCallback((flagKey) => { return isFeatureEnabled(flagKey); }, []); return { // Core PostHog instance posthog, // Tracking methods track, trackPage, // User management identify, setProperties, reset, // Privacy controls optOut: optOutTracking, optIn: optInTracking, isOptedOut, // Feature flags getFlag, isEnabled, }; }; export default usePostHog;