All files / src/lib/analytics funnels.ts

82.67% Statements 105/127
100% Branches 0/0
0% Functions 0/4
82.67% Lines 105/127

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 1281x 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 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 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x       1x 1x 1x 1x         1x 1x 1x 1x       1x 1x 1x 1x                          
/**
 * Conversion Funnel Definitions
 *
 * Defines the standard conversion funnels for tracking user journeys.
 */
 
export interface FunnelStep {
  readonly id: string;
  readonly name: string;
  readonly description?: string;
}
 
export interface FunnelDefinition {
  readonly name: string;
  readonly description: string;
  readonly steps: readonly FunnelStep[];
}
 
/**
 * Predefined conversion funnels
 */
export const FUNNELS = {
  checkout: {
    name: "E-commerce Checkout",
    description: "Track users from product view to purchase completion",
    steps: [
      { id: "view_item", name: "View Product", description: "User views a product page" },
      { id: "add_to_cart", name: "Add to Cart", description: "User adds item to cart" },
      { id: "view_cart", name: "View Cart", description: "User views their cart" },
      { id: "begin_checkout", name: "Begin Checkout", description: "User starts checkout process" },
      {
        id: "add_shipping_info",
        name: "Add Shipping Info",
        description: "User enters shipping address"},
      {
        id: "add_payment_info",
        name: "Add Payment Info",
        description: "User enters payment details"},
      { id: "purchase", name: "Purchase", description: "Order completed successfully" },
    ]},
 
  signup: {
    name: "User Registration",
    description: "Track new user registration flow",
    steps: [
      {
        id: "signup_started",
        name: "Started Signup",
        description: "User initiates registration"},
      {
        id: "email_entered",
        name: "Email Entered",
        description: "User enters email address"},
      {
        id: "password_entered",
        name: "Password Created",
        description: "User creates password"},
      {
        id: "signup_completed",
        name: "Signup Complete",
        description: "Account created successfully"},
      {
        id: "email_verified",
        name: "Email Verified",
        description: "User verifies email address"},
    ]},
 
  productDiscovery: {
    name: "Product Discovery",
    description: "Track how users discover and explore products",
    steps: [
      { id: "homepage_view", name: "Homepage View", description: "User lands on homepage" },
      { id: "category_view", name: "Category View", description: "User browses a category" },
      { id: "search", name: "Search", description: "User performs a search" },
      { id: "view_item_list", name: "View Product List", description: "User views search/filter results" },
      { id: "view_item", name: "View Product", description: "User views product details" },
    ]},
 
  engagement: {
    name: "User Engagement",
    description: "Track user engagement actions",
    steps: [
      { id: "login_completed", name: "Login", description: "User logs in" },
      { id: "add_to_wishlist", name: "Add to Wishlist", description: "User saves item to wishlist" },
      { id: "review_submitted", name: "Review Submitted", description: "User submits a review" },
      { id: "share", name: "Share", description: "User shares product/content" },
    ]}} as const;
 
export type FunnelId = keyof typeof FUNNELS;
 
/**
 * Get funnel definition by ID
 */
export function getFunnelDefinition(funnelId: FunnelId): FunnelDefinition {
  return FUNNELS[funnelId];
}
 
/**
 * Get step index within a funnel
 */
export function getFunnelStepIndex(funnelId: FunnelId, stepId: string): number {
  const funnel = FUNNELS[funnelId];
  return funnel.steps.findIndex((step) => step.id === stepId);
}
 
/**
 * Get all funnel IDs
 */
export function getAllFunnelIds(): FunnelId[] {
  return Object.keys(FUNNELS) as FunnelId[];
}
 
/**
 * Check if an event name corresponds to any funnel step
 */
export function findFunnelsForEvent(eventName: string): Array<{ funnelId: FunnelId; stepIndex: number }> {
  const results: Array<{ funnelId: FunnelId; stepIndex: number }> = [];

  for (const [funnelId, funnel] of Object.entries(FUNNELS)) {
    const stepIndex = funnel.steps.findIndex((step) => step.id === eventName);
    if (stepIndex !== -1) {
      results.push({ funnelId: funnelId as FunnelId, stepIndex });
    }
  }

  return results;
}