48 lines
2.0 KiB
TypeScript
48 lines
2.0 KiB
TypeScript
import { getWebsiteApiBaseUrl } from '@/lib/config/apiBaseUrl';
|
|
import { Service } from '@/lib/contracts/services/Service';
|
|
import { AnalyticsApiClient } from '@/lib/gateways/api/analytics/AnalyticsApiClient';
|
|
import { EnhancedErrorReporter } from '@/lib/infrastructure/EnhancedErrorReporter';
|
|
import { ConsoleLogger } from '@/lib/infrastructure/logging/ConsoleLogger';
|
|
import { RecordEngagementOutputViewModel } from '@/lib/view-models/RecordEngagementOutputViewModel';
|
|
import { RecordPageViewOutputViewModel } from '@/lib/view-models/RecordPageViewOutputViewModel';
|
|
import { injectable, unmanaged } from 'inversify';
|
|
|
|
@injectable()
|
|
export class AnalyticsService implements Service {
|
|
private readonly apiClient: AnalyticsApiClient;
|
|
|
|
constructor(@unmanaged() apiClient?: AnalyticsApiClient) {
|
|
if (apiClient) {
|
|
this.apiClient = apiClient;
|
|
} else {
|
|
const baseUrl = getWebsiteApiBaseUrl();
|
|
const logger = new ConsoleLogger();
|
|
const errorReporter = new EnhancedErrorReporter(logger);
|
|
this.apiClient = new AnalyticsApiClient(baseUrl, errorReporter, logger);
|
|
}
|
|
}
|
|
|
|
async recordPageView(input: { path: string; userId?: string }): Promise<RecordPageViewOutputViewModel> {
|
|
const data = await this.apiClient.recordPageView({
|
|
entityType: 'page',
|
|
entityId: input.path,
|
|
visitorType: input.userId ? 'user' : 'guest',
|
|
sessionId: 'temp-session', // Should come from a session service
|
|
...input
|
|
});
|
|
return new RecordPageViewOutputViewModel(data as any);
|
|
}
|
|
|
|
async recordEngagement(input: { eventType: string; userId?: string; metadata?: Record<string, any> }): Promise<RecordEngagementOutputViewModel> {
|
|
const data = await this.apiClient.recordEngagement({
|
|
action: input.eventType,
|
|
entityType: 'ui_element',
|
|
entityId: 'unknown',
|
|
actorType: input.userId ? 'user' : 'guest',
|
|
sessionId: 'temp-session', // Should come from a session service
|
|
...input
|
|
});
|
|
return new RecordEngagementOutputViewModel(data as any);
|
|
}
|
|
}
|