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 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x | "use client";
/**
* Funnel Tracking Hook
*
* Hook for tracking user progress through conversion funnels.
*/
import { useEffect, useRef, useCallback } from "react";
import { useAnalytics } from "@/components/providers/AnalyticsProvider";
import { FUNNELS, type FunnelId } from "@/lib/analytics/funnels";
/**
* Hook for tracking funnel step views
*
* @param funnelId - The ID of the funnel
* @param stepId - The ID of the current step
* @param properties - Additional properties to track
*
* @example
* ```tsx
* // In checkout page
* useFunnelTracking("checkout", "begin_checkout", { cart_value: 99.99 });
* ```
*/
export function useFunnelTracking(
funnelId: FunnelId,
stepId: string,
properties?: Record<string, unknown>
) {
const { trackEvent, isReady } = useAnalytics();
const trackedRef = useRef(false);
useEffect(() => {
if (!isReady || trackedRef.current) return;
const funnel = FUNNELS[funnelId];
if (!funnel) return;
const stepIndex = funnel.steps.findIndex((s) => s.id === stepId);
if (stepIndex === -1) return;
const step = funnel.steps[stepIndex];
trackEvent("funnel", "funnel_step", {
funnel_id: funnelId,
funnel_name: funnel.name,
step_id: stepId,
step_name: step.name,
step_index: stepIndex,
total_steps: funnel.steps.length,
...properties});
trackedRef.current = true;
}, [funnelId, stepId, isReady, trackEvent, properties]);
}
/**
* Hook for manually tracking funnel steps
*
* @returns Object with trackStep function
*
* @example
* ```tsx
* const { trackStep, trackFunnelComplete } = useFunnelSteps();
*
* const handleEmailSubmit = () => {
* trackStep("signup", "email_entered", { email_domain: "gmail.com" });
* // ... submit email
* };
* ```
*/
export function useFunnelSteps() {
const { trackEvent, isReady } = useAnalytics();
const trackStep = useCallback(
(funnelId: FunnelId, stepId: string, properties?: Record<string, unknown>) => {
if (!isReady) return;
const funnel = FUNNELS[funnelId];
if (!funnel) return;
const stepIndex = funnel.steps.findIndex((s) => s.id === stepId);
if (stepIndex === -1) return;
const step = funnel.steps[stepIndex];
trackEvent("funnel", "funnel_step", {
funnel_id: funnelId,
funnel_name: funnel.name,
step_id: stepId,
step_name: step.name,
step_index: stepIndex,
total_steps: funnel.steps.length,
...properties});
},
[isReady, trackEvent]
);
const trackFunnelComplete = useCallback(
(funnelId: FunnelId, properties?: Record<string, unknown>) => {
if (!isReady) return;
const funnel = FUNNELS[funnelId];
if (!funnel) return;
trackEvent("funnel", "funnel_complete", {
funnel_id: funnelId,
funnel_name: funnel.name,
total_steps: funnel.steps.length,
...properties});
},
[isReady, trackEvent]
);
const trackFunnelAbandoned = useCallback(
(funnelId: FunnelId, lastStepId: string, properties?: Record<string, unknown>) => {
if (!isReady) return;
const funnel = FUNNELS[funnelId];
if (!funnel) return;
const stepIndex = funnel.steps.findIndex((s) => s.id === lastStepId);
trackEvent("funnel", "funnel_abandoned", {
funnel_id: funnelId,
funnel_name: funnel.name,
last_step_id: lastStepId,
last_step_index: stepIndex,
total_steps: funnel.steps.length,
...properties});
},
[isReady, trackEvent]
);
return {
trackStep,
trackFunnelComplete,
trackFunnelAbandoned};
}
|