Added extra analytics

This commit is contained in:
Dries Augustyns
2025-12-02 11:10:30 +01:00
parent 2ef463ce5e
commit 2745c1bf5d
4 changed files with 783 additions and 262 deletions
+50 -2
View File
@@ -21,7 +21,7 @@ export class Analytics {
@Get('timeseries')
@Middleware([requireAuth])
@CatchAsync
public async getTimeSeries(req: Request, res: Response, next: NextFunction) {
public async getTimeSeries(req: Request, res: Response, _next: NextFunction) {
const auth = res.locals.auth as AuthResponse;
const startDate = req.query.startDate ? new Date(req.query.startDate as string) : undefined;
const endDate = req.query.endDate ? new Date(req.query.endDate as string) : undefined;
@@ -43,7 +43,7 @@ export class Analytics {
@Get('top-campaigns')
@Middleware([requireAuth])
@CatchAsync
public async getTopCampaigns(req: Request, res: Response, next: NextFunction) {
public async getTopCampaigns(req: Request, res: Response, _next: NextFunction) {
const auth = res.locals.auth as AuthResponse;
const limit = Math.min(parseInt(req.query.limit as string) || 10, 50);
const startDate = req.query.startDate ? new Date(req.query.startDate as string) : undefined;
@@ -53,4 +53,52 @@ export class Analytics {
return res.status(200).json(topCampaigns);
}
/**
* GET /analytics/campaign-stats
* Get campaign overview statistics
*
* Query params:
* - startDate: ISO date string (defaults to 30 days ago)
* - endDate: ISO date string (defaults to now)
*
* Returns aggregate stats: total campaigns, active, completed, average rates
*/
@Get('campaign-stats')
@Middleware([requireAuth])
@CatchAsync
public async getCampaignStats(req: Request, res: Response, _next: NextFunction) {
const auth = res.locals.auth as AuthResponse;
const startDate = req.query.startDate ? new Date(req.query.startDate as string) : undefined;
const endDate = req.query.endDate ? new Date(req.query.endDate as string) : undefined;
const campaignStats = await AnalyticsService.getCampaignStats(auth.projectId, startDate, endDate);
return res.status(200).json(campaignStats);
}
/**
* GET /analytics/top-events
* Get most frequently triggered events
*
* Query params:
* - limit: number (default 5, max 20)
* - startDate: ISO date string (defaults to 30 days ago)
* - endDate: ISO date string (defaults to now)
*
* Returns events sorted by frequency with trend data
*/
@Get('top-events')
@Middleware([requireAuth])
@CatchAsync
public async getTopEvents(req: Request, res: Response, _next: NextFunction) {
const auth = res.locals.auth as AuthResponse;
const limit = Math.min(parseInt(req.query.limit as string) || 5, 20);
const startDate = req.query.startDate ? new Date(req.query.startDate as string) : undefined;
const endDate = req.query.endDate ? new Date(req.query.endDate as string) : undefined;
const topEvents = await AnalyticsService.getTopEvents(auth.projectId, limit, startDate, endDate);
return res.status(200).json(topEvents);
}
}