All files / src/lib/analytics aggregation.ts

0% Statements 0/225
100% Branches 0/0
0% Functions 0/1
0% Lines 0/225

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                                                                                                                                                                                                                                                                                                                                                                                                                                                                   
/**
 * Analytics Data Aggregation
 *
 * Functions for aggregating raw analytics data into daily summaries.
 * Should be run via cron job at the end of each day.
 */

import { prisma } from "@/lib/prisma";
import { logger } from "@/lib/logging";
import { Decimal } from "@prisma/client/runtime/library";

/**
 * Run daily aggregation for analytics
 * Aggregates page views, sessions, and events into daily summary
 *
 * @param date - The date to aggregate (defaults to yesterday)
 */
export async function runDailyAggregation(date: Date = getYesterday()): Promise<void> {
  const startOfDay = new Date(date);
  startOfDay.setHours(0, 0, 0, 0);

  const endOfDay = new Date(date);
  endOfDay.setHours(23, 59, 59, 999);

  logger.info(`Running daily aggregation for ${startOfDay.toISOString().split("T")[0]}`, { category: "ANALYTICS", date: startOfDay.toISOString().split("T")[0] });

  try {
    const [
      pageViewCount,
      sessionStats,
      uniqueVisitorsList,
      ecommerceEvents,
      bouncedSessionCount,
    ] = await Promise.all([
      // Total page views
      prisma.analyticsPageView.count({
        where: {
          createdAt: { gte: startOfDay, lte: endOfDay }
        }
      }),

      // Session metrics
      prisma.analyticsSession.aggregate({
        where: {
          startedAt: { gte: startOfDay, lte: endOfDay }
        },
        _count: true,
        _avg: { duration: true }
      }),

      // Unique visitors
      prisma.analyticsSession.findMany({
        where: {
          startedAt: { gte: startOfDay, lte: endOfDay }
        },
        select: { visitorId: true },
        distinct: ["visitorId"]
      }),

      // E-commerce events
      prisma.analyticsEvent.groupBy({
        by: ["eventName"],
        where: {
          createdAt: { gte: startOfDay, lte: endOfDay },
          eventName: { in: ["add_to_cart", "begin_checkout", "purchase"] }
        },
        _count: true
      }),

      // Bounced sessions (single page view)
      prisma.analyticsSession.count({
        where: {
          startedAt: { gte: startOfDay, lte: endOfDay },
          pageViews: 1
        }
      }),
    ]);

    // Calculate bounce rate
    const bounceRate = sessionStats._count > 0
      ? (bouncedSessionCount / sessionStats._count) * 100
      : 0;

    // Calculate revenue from purchase events
    const purchaseEvents = await prisma.analyticsEvent.findMany({
      where: {
        createdAt: { gte: startOfDay, lte: endOfDay },
        eventName: "purchase"
      },
      select: { properties: true }
    });

    const revenue = purchaseEvents.reduce((sum, event) => {
      const props = event.properties as { value?: number } | null;
      return sum + (props?.value || 0);
    }, 0);

    // Get event counts by name
    const addToCartCount = ecommerceEvents.find((e) => e.eventName === "add_to_cart")?._count || 0;
    const checkoutCount = ecommerceEvents.find((e) => e.eventName === "begin_checkout")?._count || 0;
    const purchaseCount = ecommerceEvents.find((e) => e.eventName === "purchase")?._count || 0;

    // Upsert daily aggregate
    await prisma.analyticsDaily.upsert({
      where: { date: startOfDay },
      create: {
        date: startOfDay,
        pageViews: pageViewCount,
        uniqueVisitors: uniqueVisitorsList.length,
        sessions: sessionStats._count,
        avgSessionDuration: Math.round(sessionStats._avg.duration || 0),
        bounceRate,
        addToCartCount,
        checkoutCount,
        purchaseCount,
        revenue: new Decimal(revenue)
      },
      update: {
        pageViews: pageViewCount,
        uniqueVisitors: uniqueVisitorsList.length,
        sessions: sessionStats._count,
        avgSessionDuration: Math.round(sessionStats._avg.duration || 0),
        bounceRate,
        addToCartCount,
        checkoutCount,
        purchaseCount,
        revenue: new Decimal(revenue)
      }
    });

    logger.info(`Daily aggregation complete for ${startOfDay.toISOString().split("T")[0]}`, {
      category: "ANALYTICS",
      date: startOfDay.toISOString().split("T")[0],
      pageViews: pageViewCount,
      uniqueVisitors: uniqueVisitorsList.length,
      sessions: sessionStats._count,
      bounceRate: Math.round(bounceRate),
      revenue
    });
  } catch (error) {
    logger.error("Daily aggregation failed", error instanceof Error ? error : new Error(String(error)), { category: "ANALYTICS" });
    throw error;
  }
}

/**
 * Run aggregation for a range of dates
 * Useful for backfilling historical data
 *
 * @param startDate - Start date of the range
 * @param endDate - End date of the range
 */
export async function runRangeAggregation(startDate: Date, endDate: Date): Promise<void> {
  const current = new Date(startDate);
  current.setHours(0, 0, 0, 0);

  const end = new Date(endDate);
  end.setHours(0, 0, 0, 0);

  logger.info(`Running range aggregation from ${current.toISOString()} to ${end.toISOString()}`, { category: "ANALYTICS", startDate: current.toISOString(), endDate: end.toISOString() });

  while (current <= end) {
    await runDailyAggregation(new Date(current));
    current.setDate(current.getDate() + 1);
  }

  logger.info("Range aggregation complete", { category: "ANALYTICS" });
}

/**
 * Clean up old analytics data
 * Deletes raw events older than the specified retention period
 *
 * @param retentionDays - Number of days to retain raw data (default: 90)
 */
export async function cleanupOldData(retentionDays: number = 90): Promise<void> {
  const cutoffDate = new Date();
  cutoffDate.setDate(cutoffDate.getDate() - retentionDays);

  logger.info(`Cleaning up data older than ${cutoffDate.toISOString()}`, { category: "ANALYTICS", retentionDays, cutoffDate: cutoffDate.toISOString() });

  try {
    const [deletedEvents, deletedPageViews, deletedSessions] = await Promise.all([
      // Delete old events
      prisma.analyticsEvent.deleteMany({
        where: {
          createdAt: { lt: cutoffDate }
        }
      }),

      // Delete old page views
      prisma.analyticsPageView.deleteMany({
        where: {
          createdAt: { lt: cutoffDate }
        }
      }),

      // Delete old sessions (will cascade to related records)
      prisma.analyticsSession.deleteMany({
        where: {
          startedAt: { lt: cutoffDate }
        }
      }),
    ]);

    logger.info("Data cleanup complete", {
      category: "ANALYTICS",
      deletedEvents: deletedEvents.count,
      deletedPageViews: deletedPageViews.count,
      deletedSessions: deletedSessions.count
    });
  } catch (error) {
    logger.error("Data cleanup failed", error instanceof Error ? error : new Error(String(error)), { category: "ANALYTICS" });
    throw error;
  }
}

/**
 * Get yesterday's date
 */
function getYesterday(): Date {
  const yesterday = new Date();
  yesterday.setDate(yesterday.getDate() - 1);
  return yesterday;
}