All files / app/features/analytics journeyHooks.ts

94.18% Statements 81/86
78.57% Branches 11/14
93.75% Functions 15/16
96.25% Lines 77/80

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277                                                  1x   1x                                       7x         7x 7x   7x 5x   5x           5x 5x 5x                                                           4x       4x 4x   4x 4x 4x   4x 3x 3x 3x 3x             4x 4x 4x   3x 3x 3x 3x 3x 3x 2x       3x               3x 3x   3x 3x 3x 3x       4x                                   7x       7x 7x   7x   7x 4x     7x 5x 4x 4x 4x     7x                                                     10x 10x 10x 10x 10x   10x   10x 7x 7x   10x 10x         10x   8x 7x 7x 7x   7x     7x                 10x   5x 4x   4x                     4x 4x         10x    
/**
 * Analytics Journey Hooks
 *
 * Specialized hooks for tracking detailed user interactions and behavior patterns.
 * These help measure how users navigate through your app and interact with content.
 *
 * WHAT YOU CAN TRACK:
 * - Funnel steps (signup flows, checkout, etc.) with time spent per step
 * - Scroll depth to see how far users read content
 * - Hover dwell time to measure interest in elements
 * - Form interactions to understand where users struggle
 *
 * HOW TO USE:
 * Import the hook you need and add it to your component. Each hook automatically
 * logs events at the right time (e.g., on mount, unmount, scroll, etc.).
 *
 * @example
 * // Track a signup step
 * useFunnelStep("signup", "email-verification");
 *
 * // Track how far users scroll
 * const scrollRef = useScrollDepth("article.scroll", { article_id: "123" });
 * <div ref={scrollRef}>...</div>
 */
 
import { useCallback, useEffect, useRef } from "react";
 
import { logEvent } from "./eventCollector";
 
/**
 * Track user progress through a multi-step flow (signup, checkout, onboarding, etc.).
 *
 * Automatically logs when the user enters a step and when they leave, including
 * how long they spent on that step. This helps identify where users drop off or
 * get stuck in your flows.
 *
 * @param funnelName - Name of the overall flow (e.g., "signup", "checkout")
 * @param stepName - Name of this specific step (e.g., "email", "payment")
 * @param properties - Optional additional data to track with this step
 *
 * @example
 * // In your step component
 * useFunnelStep("checkout", "payment-info");
 *
 * // With extra properties
 * useFunnelStep("signup", "profile-setup", { user_type: "host" });
 */
export function useFunnelStep(
  funnelName: string,
  stepName: string,
  properties: Record<string, unknown> = {},
) {
  const propsRef = useRef(properties);
  propsRef.current = properties;
 
  useEffect(() => {
    const startTime = performance.now();
 
    logEvent("funnel.step_entered", {
      funnel: funnelName,
      step: stepName,
      ...propsRef.current,
    });
 
    return () => {
      const durationS = (performance.now() - startTime) / 1000;
      logEvent(
        "funnel.step_exited",
        {
          funnel: funnelName,
          step: stepName,
          ...propsRef.current,
        },
        durationS,
      );
    };
    // Only re-run if funnel/step identity changes
  }, [funnelName, stepName]);
}
 
/**
 * Track how far users scroll through content (articles, long pages, etc.).
 *
 * Returns a ref to attach to your scrollable element. Automatically tracks the
 * maximum scroll depth reached and logs it when the user leaves the page or
 * closes the tab. This helps you understand if users are reading your content
 * or dropping off early.
 *
 * @param eventType - Name for the event (e.g., "article.scrolled")
 * @param properties - Additional data about the content being scrolled
 * @returns A ref to attach to your scrollable element
 *
 * @example
 * const scrollRef = useScrollDepth("article.read", { article_id: post.id });
 * return <article ref={scrollRef}>...</article>;
 */
export function useScrollDepth<T extends HTMLElement = HTMLElement>(
  eventType: string,
  properties: Record<string, unknown> = {},
) {
  const propsRef = useRef(properties);
  propsRef.current = properties;
 
  const maxDepthRef = useRef(0);
  const hasLoggedRef = useRef(false);
  const containerRef = useRef<T | null>(null);
 
  const logDepth = useCallback(() => {
    Iif (hasLoggedRef.current) return;
    hasLoggedRef.current = true;
    const depth = maxDepthRef.current;
    logEvent(
      eventType,
      { ...propsRef.current, max_depth: Math.round(depth * 100) },
      depth,
    );
  }, [eventType]);
 
  useEffect(() => {
    const container = containerRef.current;
    if (!container) return;
 
    const handleScroll = () => {
      const { scrollTop, scrollHeight, clientHeight } = container;
      const maxScrollable = scrollHeight - clientHeight;
      Iif (maxScrollable <= 0) return;
      const depth = Math.min(scrollTop / maxScrollable, 1);
      if (depth > maxDepthRef.current) {
        maxDepthRef.current = depth;
      }
    };
 
    const handleVisibilityChange = () => {
      Iif (document.visibilityState === "hidden") {
        logDepth();
        // Reset so unmount doesn't double-log
        hasLoggedRef.current = true;
      }
    };
 
    container.addEventListener("scroll", handleScroll, { passive: true });
    document.addEventListener("visibilitychange", handleVisibilityChange);
 
    return () => {
      container.removeEventListener("scroll", handleScroll);
      document.removeEventListener("visibilitychange", handleVisibilityChange);
      logDepth();
    };
  }, [logDepth]);
 
  return containerRef;
}
 
/**
 * Track how long users hover over an element (useful for measuring interest).
 *
 * Returns event handlers to attach to your element. Logs the hover duration
 * when the user moves their mouse away. This helps identify which elements
 * catch users' attention.
 *
 * @param eventType - Name for the event (e.g., "cta.hovered")
 * @param properties - Additional data about the element
 * @returns Object with onMouseEnter and onMouseLeave handlers
 *
 * @example
 * const hoverHandlers = useHoverDwell("feature.explored", { feature: "map" });
 * return <div {...hoverHandlers}>...</div>;
 */
export function useHoverDwell(
  eventType: string,
  properties: Record<string, unknown> = {},
) {
  const propsRef = useRef(properties);
  propsRef.current = properties;
 
  const enterTimeRef = useRef<number | null>(null);
 
  const onMouseEnter = useCallback(() => {
    enterTimeRef.current = performance.now();
  }, []);
 
  const onMouseLeave = useCallback(() => {
    if (enterTimeRef.current === null) return;
    const dwellS = (performance.now() - enterTimeRef.current) / 1000;
    enterTimeRef.current = null;
    logEvent(eventType, propsRef.current, dwellS);
  }, [eventType]);
 
  return { onMouseEnter, onMouseLeave };
}
 
/**
 * Track detailed form interactions to understand where users struggle.
 *
 * Returns functions to call on field focus, blur, and form submit. Automatically
 * tracks which fields users spend time on, helping identify confusing or
 * problematic form fields.
 *
 * @param formName - Name of the form (e.g., "signup", "profile-edit")
 * @returns Object with trackFieldFocus, trackFieldBlur, and trackSubmit functions
 *
 * Events logged:
 * - `form.interaction_started` - When user focuses first field
 * - `form.field_blurred` - When user leaves each field (with time spent)
 * - `form.submitted` - When form is submitted (with per-field duration breakdown)
 *
 * @example
 * const { trackFieldFocus, trackFieldBlur, trackSubmit } = useFormInteraction("signup");
 *
 * <input
 *   onFocus={() => trackFieldFocus("email")}
 *   onBlur={() => trackFieldBlur("email")}
 * />
 * <button onClick={() => trackSubmit({ success: true })}>Submit</button>
 */
export function useFormInteraction(formName: string) {
  const startTimeRef = useRef<number | null>(null);
  const fieldStartRef = useRef<number | null>(null);
  const currentFieldRef = useRef<string | null>(null);
  const fieldDurationsRef = useRef<Record<string, number>>({});
 
  const trackFieldFocus = useCallback(
    (fieldName: string) => {
      if (startTimeRef.current === null) {
        startTimeRef.current = performance.now();
        logEvent("form.interaction_started", { form: formName });
      }
      currentFieldRef.current = fieldName;
      fieldStartRef.current = performance.now();
    },
    [formName],
  );
 
  const trackFieldBlur = useCallback(
    (fieldName: string) => {
      if (fieldStartRef.current === null) return;
      const durationS = (performance.now() - fieldStartRef.current) / 1000;
      fieldStartRef.current = null;
      currentFieldRef.current = null;
 
      fieldDurationsRef.current[fieldName] =
        (fieldDurationsRef.current[fieldName] ?? 0) + durationS;
 
      logEvent("form.field_blurred", {
        form: formName,
        field: fieldName,
        duration: durationS,
      });
    },
    [formName],
  );
 
  const trackSubmit = useCallback(
    (extraProperties?: Record<string, unknown>) => {
      if (startTimeRef.current === null) return;
      const totalDurationS = (performance.now() - startTimeRef.current) / 1000;
 
      logEvent(
        "form.submitted",
        {
          form: formName,
          field_durations: { ...fieldDurationsRef.current },
          ...extraProperties,
        },
        totalDurationS,
      );
 
      // Reset state for potential re-use
      startTimeRef.current = null;
      fieldDurationsRef.current = {};
    },
    [formName],
  );
 
  return { trackFieldFocus, trackFieldBlur, trackSubmit };
}