Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 14 additions & 10 deletions apps/api/src/controllers/Analytics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type {NextFunction, Request, Response} from 'express';
import {type FieldError, ValidationError} from '../exceptions/index.js';
import {requireAuth, requireEmailVerified} from '../middleware/auth.js';
import {type AnalyticsQuery, AnalyticsService} from '../services/AnalyticsService.js';
import {EmailService} from '../services/EmailService.js';
import {CatchAsync} from '../utils/asyncHandler.js';

const STREAMS: AnalyticsStream[] = ['ALL', 'TRANSACTIONAL', 'CAMPAIGN', 'WORKFLOW'];
Expand All @@ -20,8 +21,10 @@ const DAY_MS = 24 * 60 * 60 * 1000;
*
* Clients should send stable bounds (day boundaries rather than "now") so repeat loads hit
* the cache; the server never draws buckets past the current time.
*
* The streams sent with open and click tracking come from the project's tracking mode.
*/
function parseQuery(req: Request, res: Response): AnalyticsQuery {
async function parseQuery(req: Request, res: Response): Promise<AnalyticsQuery> {
const errors: FieldError[] = [];

const parseDate = (field: 'from' | 'to'): Date | undefined => {
Expand Down Expand Up @@ -61,7 +64,8 @@ function parseQuery(req: Request, res: Response): AnalyticsQuery {
throw new ValidationError(errors);
}

return {projectId: res.locals.auth.projectId, from, to, tz, stream};
const projectId: string = res.locals.auth.projectId;
return {projectId, from, to, tz, stream, trackedStreams: await EmailService.trackedStreams(projectId)};
}

function isValidTimezone(tz: string): boolean {
Expand All @@ -83,7 +87,7 @@ export class Analytics {
@Middleware([requireAuth, requireEmailVerified])
@CatchAsync
public async getOverview(req: Request, res: Response, _next: NextFunction) {
return res.status(200).json(await AnalyticsService.getOverview(parseQuery(req, res)));
return res.status(200).json(await AnalyticsService.getOverview(await parseQuery(req, res)));
}

/**
Expand All @@ -94,7 +98,7 @@ export class Analytics {
@Middleware([requireAuth, requireEmailVerified])
@CatchAsync
public async getTimeseries(req: Request, res: Response, _next: NextFunction) {
return res.status(200).json(await AnalyticsService.getTimeseries(parseQuery(req, res)));
return res.status(200).json(await AnalyticsService.getTimeseries(await parseQuery(req, res)));
}

/**
Expand All @@ -105,7 +109,7 @@ export class Analytics {
@Middleware([requireAuth, requireEmailVerified])
@CatchAsync
public async getDeliverability(req: Request, res: Response, _next: NextFunction) {
return res.status(200).json(await AnalyticsService.getDeliverability(parseQuery(req, res)));
return res.status(200).json(await AnalyticsService.getDeliverability(await parseQuery(req, res)));
}

/**
Expand All @@ -116,7 +120,7 @@ export class Analytics {
@Middleware([requireAuth, requireEmailVerified])
@CatchAsync
public async getLeaderboard(req: Request, res: Response, _next: NextFunction) {
const query = parseQuery(req, res);
const query = await parseQuery(req, res);
const kind = String(req.query.kind ?? 'campaigns') as AnalyticsLeaderboardKind;

if (!LEADERBOARD_KINDS.includes(kind)) {
Expand All @@ -136,7 +140,7 @@ export class Analytics {
@Middleware([requireAuth, requireEmailVerified])
@CatchAsync
public async getLinks(req: Request, res: Response, _next: NextFunction) {
return res.status(200).json(await AnalyticsService.getLinks(parseQuery(req, res)));
return res.status(200).json(await AnalyticsService.getLinks(await parseQuery(req, res)));
}

/**
Expand All @@ -147,7 +151,7 @@ export class Analytics {
@Middleware([requireAuth, requireEmailVerified])
@CatchAsync
public async getHeatmap(req: Request, res: Response, _next: NextFunction) {
return res.status(200).json(await AnalyticsService.getHeatmap(parseQuery(req, res)));
return res.status(200).json(await AnalyticsService.getHeatmap(await parseQuery(req, res)));
}

/**
Expand All @@ -158,7 +162,7 @@ export class Analytics {
@Middleware([requireAuth, requireEmailVerified])
@CatchAsync
public async getAudience(req: Request, res: Response, _next: NextFunction) {
return res.status(200).json(await AnalyticsService.getAudience(parseQuery(req, res)));
return res.status(200).json(await AnalyticsService.getAudience(await parseQuery(req, res)));
}

/**
Expand All @@ -169,6 +173,6 @@ export class Analytics {
@Middleware([requireAuth, requireEmailVerified])
@CatchAsync
public async getTopEvents(req: Request, res: Response, _next: NextFunction) {
return res.status(200).json(await AnalyticsService.getTopEvents(parseQuery(req, res)));
return res.status(200).json(await AnalyticsService.getTopEvents(await parseQuery(req, res)));
}
}
42 changes: 30 additions & 12 deletions apps/api/src/services/ActivityService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import signale from 'signale';
import {prisma} from '../database/prisma.js';
import {redis} from '../database/redis.js';
import {HttpException} from '../exceptions/index.js';
import {EmailService} from './EmailService.js';
import {Keys} from './keys.js';

/**
Expand Down Expand Up @@ -193,8 +194,17 @@ export class ActivityService {
* Falls back to database aggregation if cache miss
*/
public static async getStats(projectId: string, startDate?: Date, endDate?: Date): Promise<ActivityStats> {
// Opens and clicks are measured against the streams the project tracks now, so they are
// part of the cache key: changing the tracking mode must not serve stats from the old one
const trackedStreams = await EmailService.trackedStreams(projectId);

// Try to get from cache
const cacheKey = Keys.Activity.stats(projectId, startDate?.getTime() || 'all', endDate?.getTime() || 'now');
const cacheKey = Keys.Activity.stats(
projectId,
startDate?.getTime() || 'all',
endDate?.getTime() || 'now',
trackedStreams.join(','),
);

try {
const cached = await redis.get(cacheKey);
Expand All @@ -214,7 +224,7 @@ export class ActivityService {
};

// Compute stats from database (in parallel for performance)
const [totalEvents, emailStats] = await Promise.all([
const [totalEvents, emailGroups] = await Promise.all([
// Count total events
prisma.event.count({
where: {
Expand All @@ -223,16 +233,17 @@ export class ActivityService {
},
}),

// Aggregate email stats
prisma.email.aggregate({
// Email counts per stream, so opens and clicks can be measured against tracked emails.
// Every column read here is in emails_projectId_createdAt_stats_idx, so the counts are
// served from that index alone
prisma.email.groupBy({
by: ['sourceType', 'simulated'],
where: {
projectId,
createdAt: dateFilter,
},
// _all rather than id: COUNT(*) can be served from emails_projectId_createdAt_stats_idx
// alone, COUNT("id") would need every row fetched since id is not in that index
_count: {
_all: true,
sentAt: true,
openedAt: true,
clickedAt: true,
},
Expand All @@ -249,18 +260,25 @@ export class ActivityService {
},
});

const totalEmailsSent = emailStats._count._all;
const totalEmailsOpened = emailStats._count.openedAt || 0;
const totalEmailsClicked = emailStats._count.clickedAt || 0;
// Measured as analytics does: outbound emails that went out, leaving out mail to the SES
// mailbox simulator, which never reaches anyone
const outbound = emailGroups.filter(group => !group.simulated && group.sourceType !== 'INBOUND');
const tracked = outbound.filter(group => trackedStreams.includes(group.sourceType));

const totalEmailsSent = outbound.reduce((sum, group) => sum + group._count.sentAt, 0);
const totalEmailsTracked = tracked.reduce((sum, group) => sum + group._count.sentAt, 0);
const totalEmailsOpened = tracked.reduce((sum, group) => sum + group._count.openedAt, 0);
const totalEmailsClicked = tracked.reduce((sum, group) => sum + group._count.clickedAt, 0);

const stats: ActivityStats = {
totalEvents,
totalEmailsSent,
totalEmailsTracked,
totalEmailsOpened,
totalEmailsClicked,
totalWorkflowsStarted,
openRate: totalEmailsSent > 0 ? (totalEmailsOpened / totalEmailsSent) * 100 : 0,
clickRate: totalEmailsSent > 0 ? (totalEmailsClicked / totalEmailsSent) * 100 : 0,
openRate: totalEmailsTracked > 0 ? (totalEmailsOpened / totalEmailsTracked) * 100 : 0,
clickRate: totalEmailsTracked > 0 ? (totalEmailsClicked / totalEmailsTracked) * 100 : 0,
};

// Cache for 5 minutes
Expand Down
Loading