Analytics & Insights
Relevant source files
The following files were used as context for generating this wiki page:
- Features/AnalyticsFeature/Sources/AnalyticsFeature/Views/Analytics/AnalyticsView.swift
- Features/AnalyticsFeature/Sources/AnalyticsFeature/Views/Analytics/AnalyticsViewModel.swift
- Packages/Services/Tests/ServicesTests/SubscriptionAnalyticsImplTests.swift
- Packages/Mocks/Sources/Mocks/Services/SubscriptionAnalyticsMock.swift
- Features/AnalyticsFeature/Tests/AnalyticsFeatureTests/Views/Analytics/AnalyticsViewModelTests.swift
- Packages/SharedUI/Tests/SharedUITests/MonthlySpendingBarChartViewModelTests.swift
The Analytics & Insights module is responsible for aggregating subscription data and presenting actionable financial metrics to the user. It transforms raw subscription records stored in SwiftData into high-level spending patterns, category distributions, and renewal forecasts.
The system operates primarily through the AnalyticsView, which utilizes a dedicated AnalyticsViewModel to communicate with the SubscriptionAnalytics service. The module provides multi-year support, currency-aware conversions, and visual representations of data via bar charts and pie charts.
Sources: AnalyticsView.swift:10-25, AnalyticsViewModel.swift:15-30
Architecture and Data Flow
The module follows a reactive MVVM architecture. The AnalyticsViewModel is marked with @Observable and handles state management, including loading states, error handling, and data transformation for the UI.
Component Relationship
The following diagram illustrates the relationship between the UI, the logic layer, and the underlying data services.
The diagram shows how the AnalyticsView coordinates with the AnalyticsViewModel, which in turn aggregates data from multiple services.
Sources: AnalyticsView.swift:11-20, AnalyticsViewModel.swift:51-70
Dashboard Loading Sequence
When the analytics screen appears, it initiates a comprehensive data fetch sequence to populate various dashboard components.
The sequence ensures that basic stats, charts, and insight cards are updated atomically.
Sources: AnalyticsViewModel.swift:87-114, AnalyticsView.swift:34-40
Data Models and Structures
The system relies on several core data structures to represent financial insights. These models are typically provided by the SubscriptionAnalytics implementation.
| Data Model | Fields | Purpose |
|---|---|---|
SpendingInsights | monthlyTotal, yearlyTotal, activeCount, averagePerSubscription, mostExpensiveSubscription, leastExpensiveSubscription | General KPI summary for the dashboard header. |
MonthlySpending | month (Date), total (Decimal), byCategory | Monthly data points used for bar chart rendering. |
CategorySpending | category, monthlyTotal, subscriptionCount, percentage | Data for category breakdown lists and pie charts. |
UpcomingRenewal | subscription, renewalDate, daysUntilRenewal | Forecast data for the "Upcoming Renewals" horizontal scroll. |
Sources: SubscriptionAnalyticsMock.swift:11-22, AnalyticsViewModel.swift:30-45
Logic and Computation
Financial Metrics Calculation
The module performs several complex calculations to provide "Insights":
- Average Per Subscription: Derived by dividing the monthly total by the count of active subscriptions.
- Yearly Total: Calculated as the monthly total multiplied by 12, assuming current subscription levels persist.
- Monthly Average: Calculated by filtering non-zero months and averaging the totals.
- Trends:
- Yearly Trend: The percentage change between the selected year's total and the previous year's total.
- Monthly Trend: The percentage change between the current average and the previous year's monthly average.
Sources: AnalyticsViewModel.swift:145-185, SubscriptionAnalyticsImplTests.swift:101-118
Trend Calculation Logic
var yearlyTrend: Double? {
guard previousYearTotal > 0 else { return nil }
let change = selectedYearTotal - previousYearTotal
return Double(truncating: (change / previousYearTotal) as NSDecimalNumber)
}
Sources: AnalyticsViewModel.swift:171-175
Interactive Features
The AnalyticsView supports interactive drilling into specific data points:
- Year Selection: Changing the
selectedYeartriggers a reload ofmonthlySpendingandinsightsfor that specific timeframe. - Chart Interaction: Tapping a bar in the
MonthlySpendingBarChartupdatesselectedChartDate, which triggersloadSubscriptionsForSelectedMonth()to show a list of specific renewals for that month. - Category Detail: Tapping a category opens a
CategorySubscriptionsSheetshowing all active subscriptions within that category.
Sources: AnalyticsView.swift:65-71, AnalyticsViewModel.swift:129-141, AnalyticsView.swift:234-265
Visual Components
Monthly Spending Bar Chart
The chart component displays the monthly spending for the selected year. It includes an "Average" line and supports selection states. The logic in MonthlySpendingBarChartViewModel handles the scaling of bar values and opacity during selection.
- Compact Formatting: Amounts are rounded to the nearest whole number and prepended with currency symbols (e.g., "$42", "€51").
- Animation: Bars animate from a zero-value state to their actual total upon dashboard load.
Sources: AnalyticsView.swift:103-125, MonthlySpendingBarChartViewModelTests.swift:50-85
Insight Cards
Four specific cards highlight outliers and key facts:
- Highest/Lowest Month: Identifies the months with maximum and minimum spending in the current year.
- Most/Least Expensive: Identifies specific subscriptions with the highest and lowest converted amounts in the user's base currency.
Sources: AnalyticsView.swift:170-205, AnalyticsViewModel.swift:151-160
Conclusion
The Analytics & Insights module serves as the primary intelligence layer of the application. By centralizing logic within the SubscriptionAnalytics service and maintaining a thin, reactive ViewModel, the system provides real-time visibility into spending habits. Its support for multi-currency conversion and trend analysis allows users to track their financial commitments with high precision across different time scales.
In conclusion, the system ensures technical accuracy by strictly decoupling data aggregation from presentation while providing an interactive, visual-first experience for managing recurring expenses.