Files
gridpilot.gg/apps/website/lib/services/analytics/AnalyticsService.ts
2025-12-18 01:20:23 +01:00

45 lines
1.4 KiB
TypeScript

import { AnalyticsApiClient } from '../../api/analytics/AnalyticsApiClient';
import { RecordPageViewOutputDTO, RecordEngagementOutputDTO } from '../../types/generated';
import { RecordPageViewInputViewModel } from '../../view-models/RecordPageViewInputViewModel';
import { RecordEngagementInputViewModel } from '../../view-models/RecordEngagementInputViewModel';
/**
* Analytics Service
*
* Orchestrates analytics operations by coordinating API calls.
* All dependencies are injected via constructor.
*/
export class AnalyticsService {
constructor(
private readonly apiClient: AnalyticsApiClient
) {}
/**
* Record a page view
*/
async recordPageView(input: RecordPageViewInputViewModel): Promise<RecordPageViewOutputDTO> {
const apiInput: { path: string; userId?: string } = {
path: input.path,
};
if (input.userId) {
apiInput.userId = input.userId;
}
return await this.apiClient.recordPageView(apiInput);
}
/**
* Record an engagement event
*/
async recordEngagement(input: RecordEngagementInputViewModel): Promise<RecordEngagementOutputDTO> {
const apiInput: { eventType: string; userId?: string; metadata?: Record<string, unknown> } = {
eventType: input.eventType,
};
if (input.userId) {
apiInput.userId = input.userId;
}
if (input.metadata) {
apiInput.metadata = input.metadata;
}
return await this.apiClient.recordEngagement(apiInput);
}
}