Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

fix(analytics): record email sends from the queued outbox (#9021)

Chaitanya Agarwal944874a29a3d
diff --git a/apps/web/src/lib/analytics/app-events.ts b/apps/web/src/lib/analytics/app-events.ts
index c687600642..93074ae462 100644
--- a/apps/web/src/lib/analytics/app-events.ts
+++ b/apps/web/src/lib/analytics/app-events.ts
@@ -218,7 +218,11 @@ export type AppEvents = {
/** An existing subscriber moved between Premium and Max. */
plan_changed: { plan: 'premium' | 'max'; from?: string };
- email_message_sent: Record<string, unknown>;
+ /**
+ * An email the user sent, counted once when the outbox accepts it. Undo
+ * does not retract it. Older events have no properties.
+ */
+ email_message_sent: { isReply: boolean };
/**
* How the inbox-link OAuth callback ended. `connected` covers re-linking an
* inbox that was already connected; the client cannot tell them apart.
diff --git a/apps/web/src/lib/queries/email/send-queue.test.ts b/apps/web/src/lib/queries/email/send-queue.test.ts
index 85f97a68b4..42ec3985f1 100644
--- a/apps/web/src/lib/queries/email/send-queue.test.ts
+++ b/apps/web/src/lib/queries/email/send-queue.test.ts
@@ -25,7 +25,9 @@ const mocks = vi.hoisted(() => ({
publish: vi.fn(),
status: vi.fn(),
save: vi.fn(),
+ track: vi.fn(),
}));
+vi.mock('@app/lib/analytics', () => ({ analytics: { track: mocks.track } }));
vi.mock('@service-storage/email-send', () => ({
readEmailSendStatus: mocks.status,
}));
@@ -118,6 +120,31 @@ it('persists approval without contacting the network or normalized cache', async
expect(mocks.status).not.toHaveBeenCalled();
expect(result.persistence).toBe('queued');
});
+it('counts each accepted send once for product analytics', async () => {
+ await sendEmailQueued(args());
+ await sendEmailQueued({
+ ...args(),
+ draft: {
+ ...draft,
+ draftId: '00000000-0000-4000-8000-000000000004',
+ replyingToId: '00000000-0000-4000-8000-000000000005',
+ },
+ });
+ expect(mocks.track.mock.calls).toEqual([
+ ['email_message_sent', { isReply: false }],
+ ['email_message_sent', { isReply: true }],
+ ]);
+});
+it('does not count a send the queue refused', async () => {
+ mocks.persist.mockRejectedValueOnce(
+ new DOMException('Full', 'QuotaExceededError')
+ );
+ await expect(sendEmailQueued(args())).rejects.toThrow('Full');
+ await sendEmailQueued(args());
+ mocks.read.mockResolvedValue([mocks.persist.mock.lastCall?.[0]]);
+ await expect(sendEmailQueued(args())).rejects.toThrow('already queued');
+ expect(mocks.track).toHaveBeenCalledOnce();
+});
it('freezes the exact input before any asynchronous storage read', async () => {
const pending = Promise.withResolvers<EmailSendIntent[]>();
mocks.read.mockReturnValueOnce(pending.promise);
diff --git a/apps/web/src/lib/queries/email/send-queue.ts b/apps/web/src/lib/queries/email/send-queue.ts
index 4b49988ffe..722b5b5302 100644
--- a/apps/web/src/lib/queries/email/send-queue.ts
+++ b/apps/web/src/lib/queries/email/send-queue.ts
@@ -21,6 +21,7 @@ import {
emailSendFailureKind,
recordEmailSend,
traceEmailSend,
+ trackEmailMessageSent,
} from './send-telemetry';
export type { EmailSendIntent } from './send-intent';
@@ -132,6 +133,8 @@ export async function sendEmailQueued(args: {
persistLocalEmailSend(intent, true)
);
recordEmailSend({ operation: 'enqueue', outcome: 'queued' }, intent);
+ // Every queued send passes here once, whichever surface started it.
+ trackEmailMessageSent({ isReply: draft.replyingToId != null });
return {
draftId: String(draft.draftId),
threadId: draft.threadDbId,
diff --git a/apps/web/src/lib/queries/email/send-telemetry.ts b/apps/web/src/lib/queries/email/send-telemetry.ts
index 108c07caa5..27d48ff606 100644
--- a/apps/web/src/lib/queries/email/send-telemetry.ts
+++ b/apps/web/src/lib/queries/email/send-telemetry.ts
@@ -1,3 +1,4 @@
+import { analytics } from '@app/lib/analytics';
import { Telemetry } from '@macro-inc/observability';
import type { EmailSendAttemptFieldsFragment } from '@service-storage/graphql/generated/graphql';
import { CombinedError } from '@urql/core';
@@ -89,3 +90,12 @@ export function recordEmailSend(
// Exporter failures cannot change send authority or draft persistence.
}
}
+
+/** Product analytics for one accepted send: flags only, never content or addresses. */
+export function trackEmailMessageSent(properties: { isReply: boolean }): void {
+ try {
+ analytics.track('email_message_sent', properties);
+ } catch (error) {
+ Telemetry.error(error);
+ }
+}