2026-01-16 13:28:50 +00:00
|
|
|
/**
|
|
|
|
|
* Monitor 3: User Analytics Service
|
|
|
|
|
* Tracks feature usage and user interactions
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
import { apiClient } from './api/client';
|
|
|
|
|
import { logger } from '@/utils/logger';
|
|
|
|
|
|
2026-01-07 09:33:52 +00:00
|
|
|
export const analyticsService = {
|
2026-01-16 13:28:50 +00:00
|
|
|
/**
|
|
|
|
|
* Monitor 3: Record a user analytics event
|
|
|
|
|
* Tracks feature usage and user interactions
|
|
|
|
|
* @param eventName - Name of the event (e.g., 'feature_used', 'button_clicked')
|
|
|
|
|
* @param payload - Optional event data (e.g., { feature: 'search', query: '...' })
|
|
|
|
|
*/
|
|
|
|
|
recordEvent: async (eventName: string, payload?: Record<string, unknown>) => {
|
|
|
|
|
try {
|
|
|
|
|
// Send event to backend analytics endpoint
|
|
|
|
|
await apiClient.post('/analytics/events', {
|
|
|
|
|
event_name: eventName,
|
|
|
|
|
payload: payload || {},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Log in development for debugging
|
|
|
|
|
if (import.meta.env.DEV) {
|
|
|
|
|
logger.debug('[Analytics] Event recorded', {
|
|
|
|
|
event_name: eventName,
|
|
|
|
|
payload,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
} catch (error) {
|
|
|
|
|
// Don't throw errors for analytics - fail silently to avoid disrupting user experience
|
|
|
|
|
if (import.meta.env.DEV) {
|
|
|
|
|
logger.warn('[Analytics] Failed to record event', {
|
|
|
|
|
event_name: eventName,
|
|
|
|
|
error: error instanceof Error ? error.message : String(error),
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-01-13 18:47:57 +00:00
|
|
|
},
|
2026-01-07 09:33:52 +00:00
|
|
|
|
2026-01-26 13:12:17 +00:00
|
|
|
getGlobalStats: async (range: string = '30d') => {
|
|
|
|
|
try {
|
2026-02-15 15:21:20 +00:00
|
|
|
const response = await apiClient.get<{ data?: Record<string, unknown> }>('/analytics', {
|
2026-01-26 13:12:17 +00:00
|
|
|
params: { days: range.replace('d', '') }
|
|
|
|
|
});
|
|
|
|
|
|
chore: consolidate CI, E2E, backend and frontend updates
- CI: workflows updates (cd, ci), remove playwright.yml
- E2E: global-setup, auth/playlists/profile specs
- Remove playwright-report and test-results artifacts from tracking
- Backend: auth, handlers, services, workers, migrations
- Frontend: components, features, vite config
- Add e2e-results.json to gitignore
- Docs: REMEDIATION_PROGRESS, audit archive
- Rust: chat-server, stream-server updates
2026-02-17 15:43:21 +00:00
|
|
|
const data = (response.data?.data ?? response.data) as Record<string, unknown> | undefined;
|
2026-02-15 15:21:20 +00:00
|
|
|
if (!data || typeof data !== 'object') {
|
|
|
|
|
return {};
|
2026-01-26 13:12:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return {
|
chore: consolidate CI, E2E, backend and frontend updates
- CI: workflows updates (cd, ci), remove playwright.yml
- E2E: global-setup, auth/playlists/profile specs
- Remove playwright-report and test-results artifacts from tracking
- Backend: auth, handlers, services, workers, migrations
- Frontend: components, features, vite config
- Add e2e-results.json to gitignore
- Docs: REMEDIATION_PROGRESS, audit archive
- Rust: chat-server, stream-server updates
2026-02-17 15:43:21 +00:00
|
|
|
total_tracks: (data.total_tracks as number | undefined) ?? 0,
|
|
|
|
|
total_plays: (data.total_plays as number | undefined) ?? 0,
|
|
|
|
|
total_revenue: (data.total_revenue as number | undefined) ?? 0,
|
|
|
|
|
followers: (data.followers as number | undefined) ?? 0,
|
|
|
|
|
profile_views: (data.profile_views as number | undefined) ?? 0,
|
|
|
|
|
trends: (data.trends as Record<string, number> | undefined) ?? { plays: 0, revenue: 0, followers: 0, views: 0 },
|
|
|
|
|
sparklines: (data.sparklines as Record<string, number[]> | undefined) ?? { plays: [0], revenue: [0], followers: [0], views: [0] },
|
2026-01-26 13:12:17 +00:00
|
|
|
};
|
|
|
|
|
} catch (error) {
|
|
|
|
|
logger.error('[Analytics] Failed to fetch global stats', { error });
|
2026-02-15 15:21:20 +00:00
|
|
|
throw error;
|
2026-01-26 13:12:17 +00:00
|
|
|
}
|
2026-01-13 18:47:57 +00:00
|
|
|
},
|
2026-01-07 09:33:52 +00:00
|
|
|
|
2026-01-26 13:12:17 +00:00
|
|
|
getTopTracks: async (range: string = '30d') => {
|
|
|
|
|
try {
|
2026-02-15 15:21:20 +00:00
|
|
|
const response = await apiClient.get<{ data?: { tracks?: { top_tracks?: unknown[] } } }>('/analytics', {
|
2026-01-26 13:12:17 +00:00
|
|
|
params: { days: range.replace('d', '') }
|
|
|
|
|
});
|
|
|
|
|
|
2026-02-15 15:21:20 +00:00
|
|
|
const data = response.data?.data ?? response.data;
|
|
|
|
|
const topTracks = (data as Record<string, unknown>)?.tracks as { top_tracks?: Array<{ id?: string; title?: string; plays?: number; play_count?: number; change?: number; revenue?: number }> } | undefined;
|
|
|
|
|
const tracks = topTracks?.top_tracks ?? [];
|
2026-01-26 13:12:17 +00:00
|
|
|
|
2026-02-15 15:21:20 +00:00
|
|
|
return tracks.map((t) => ({
|
|
|
|
|
id: t.id ?? '',
|
|
|
|
|
title: t.title ?? '',
|
|
|
|
|
plays: t.plays ?? t.play_count ?? 0,
|
|
|
|
|
change: t.change ?? 0,
|
|
|
|
|
revenue: t.revenue ?? 0,
|
|
|
|
|
}));
|
2026-01-26 13:12:17 +00:00
|
|
|
} catch (error) {
|
|
|
|
|
logger.error('[Analytics] Failed to fetch top tracks', { error });
|
2026-02-15 15:21:20 +00:00
|
|
|
throw error;
|
2026-01-26 13:12:17 +00:00
|
|
|
}
|
2026-01-13 18:47:57 +00:00
|
|
|
},
|
2026-01-07 09:33:52 +00:00
|
|
|
|
2026-01-13 18:47:57 +00:00
|
|
|
getTrafficSources: async () => {
|
chore: consolidate CI, E2E, backend and frontend updates
- CI: workflows updates (cd, ci), remove playwright.yml
- E2E: global-setup, auth/playlists/profile specs
- Remove playwright-report and test-results artifacts from tracking
- Backend: auth, handlers, services, workers, migrations
- Frontend: components, features, vite config
- Add e2e-results.json to gitignore
- Docs: REMEDIATION_PROGRESS, audit archive
- Rust: chat-server, stream-server updates
2026-02-17 15:43:21 +00:00
|
|
|
try {
|
|
|
|
|
const response = await apiClient.get<{ sources?: unknown[] }>('/analytics/traffic-sources');
|
|
|
|
|
return (response.data?.sources ?? []) as unknown[];
|
|
|
|
|
} catch (error) {
|
|
|
|
|
logger.warn('[Analytics] Failed to fetch traffic sources', { error });
|
|
|
|
|
return [];
|
|
|
|
|
}
|
2026-01-13 18:47:57 +00:00
|
|
|
},
|
2026-01-07 09:33:52 +00:00
|
|
|
|
2026-02-20 15:59:25 +00:00
|
|
|
getCreatorCharts: async (params?: { days?: number }) => {
|
|
|
|
|
try {
|
|
|
|
|
const searchParams = new URLSearchParams();
|
|
|
|
|
if (params?.days != null) searchParams.set('days', String(params.days));
|
|
|
|
|
const qs = searchParams.toString();
|
|
|
|
|
const url = `/analytics/creator/charts${qs ? `?${qs}` : ''}`;
|
|
|
|
|
const response = await apiClient.get<{
|
|
|
|
|
data?: {
|
|
|
|
|
plays_per_day?: Array<{ date: string; count: number }>;
|
|
|
|
|
top_tracks?: Array<{ track_id: string; title: string; plays: number }>;
|
|
|
|
|
period?: { start_date?: string; end_date?: string; days?: number };
|
|
|
|
|
};
|
|
|
|
|
}>(url);
|
|
|
|
|
const data = (response.data?.data ?? response.data) as Record<string, unknown> | undefined;
|
|
|
|
|
return data ?? {};
|
|
|
|
|
} catch (error) {
|
|
|
|
|
logger.error('[Analytics] Failed to fetch creator charts', { error });
|
|
|
|
|
throw error;
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
2026-02-20 15:57:58 +00:00
|
|
|
getCreatorStats: async (params?: { start_date?: string; end_date?: string; days?: number }) => {
|
|
|
|
|
try {
|
|
|
|
|
const searchParams = new URLSearchParams();
|
|
|
|
|
if (params?.days != null) searchParams.set('days', String(params.days));
|
|
|
|
|
if (params?.start_date) searchParams.set('start_date', params.start_date);
|
|
|
|
|
if (params?.end_date) searchParams.set('end_date', params.end_date);
|
|
|
|
|
const qs = searchParams.toString();
|
|
|
|
|
const url = `/analytics/creator/stats${qs ? `?${qs}` : ''}`;
|
|
|
|
|
const response = await apiClient.get<{
|
|
|
|
|
data?: {
|
|
|
|
|
total_plays?: number;
|
|
|
|
|
unique_listeners?: number;
|
|
|
|
|
average_completion_rate?: number;
|
|
|
|
|
plays_by_day?: number[];
|
|
|
|
|
period?: { start_date?: string; end_date?: string; days?: number };
|
|
|
|
|
};
|
|
|
|
|
}>(url);
|
|
|
|
|
const data = (response.data?.data ?? response.data) as Record<string, unknown> | undefined;
|
|
|
|
|
return data ?? {};
|
|
|
|
|
} catch (error) {
|
|
|
|
|
logger.error('[Analytics] Failed to fetch creator stats', { error });
|
|
|
|
|
throw error;
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
2026-01-13 18:47:57 +00:00
|
|
|
getDeviceBreakdown: async () => {
|
chore: consolidate CI, E2E, backend and frontend updates
- CI: workflows updates (cd, ci), remove playwright.yml
- E2E: global-setup, auth/playlists/profile specs
- Remove playwright-report and test-results artifacts from tracking
- Backend: auth, handlers, services, workers, migrations
- Frontend: components, features, vite config
- Add e2e-results.json to gitignore
- Docs: REMEDIATION_PROGRESS, audit archive
- Rust: chat-server, stream-server updates
2026-02-17 15:43:21 +00:00
|
|
|
try {
|
|
|
|
|
const response = await apiClient.get<{ mobile?: number; desktop?: number }>('/analytics/device-breakdown');
|
|
|
|
|
return {
|
|
|
|
|
mobile: response.data?.mobile ?? 0,
|
|
|
|
|
desktop: response.data?.desktop ?? 0,
|
|
|
|
|
};
|
|
|
|
|
} catch (error) {
|
|
|
|
|
logger.warn('[Analytics] Failed to fetch device breakdown', { error });
|
|
|
|
|
return { mobile: 0, desktop: 0 };
|
|
|
|
|
}
|
2026-01-13 18:47:57 +00:00
|
|
|
},
|
2026-01-07 09:33:52 +00:00
|
|
|
};
|