diff --git a/apps/website/e2e/minimax.spec.ts b/apps/website/e2e/minimax.spec.ts index b1f69df0de6..2eae0b3ba65 100644 --- a/apps/website/e2e/minimax.spec.ts +++ b/apps/website/e2e/minimax.spec.ts @@ -17,7 +17,7 @@ const CTA_HEADING = t('minimax.cta.heading', 'en') const CTA_PRIMARY = t('minimax.cta.primaryCta', 'en') const CLOUD_URL = externalLinks.cloud const CLOUD_RUN_URL = minimaxLinks.cloudRun -const FAQS = minimaxPage.faq.items +const FAQS = minimaxPage.faq?.items ?? [] const FAQ_COUNT = FAQS.length const FIRST_FAQ = FAQS[0] const PRICING_HEADING = t('pricing.title', 'en') diff --git a/apps/website/src/templates/model-launch/ModelLaunchGallerySection.vue b/apps/website/src/templates/model-launch/ModelLaunchGallerySection.vue index 9d3f376a577..3cc0ac47f12 100644 --- a/apps/website/src/templates/model-launch/ModelLaunchGallerySection.vue +++ b/apps/website/src/templates/model-launch/ModelLaunchGallerySection.vue @@ -84,7 +84,7 @@ const { stop } = useIntersectionObserver( : t('modelLaunch.tagPremium', locale) }} - + {{ card.note[locale] }} @@ -96,7 +96,7 @@ const { stop } = useIntersectionObserver( rel="noopener" :aria-label="card.name[locale]" size="sm" - class="bg-primary-warm-gray hover:bg-primary-comfy-yellow rounded-xl text-primary-comfy-ink hover:text-primary-comfy-ink" + class="hover:bg-primary-comfy-yellow rounded-xl bg-primary-warm-gray text-primary-comfy-ink hover:text-primary-comfy-ink" > diff --git a/apps/website/src/templates/model-launch/ModelLaunchHeroSection.vue b/apps/website/src/templates/model-launch/ModelLaunchHeroSection.vue index 01fd4267caf..60ab0de2308 100644 --- a/apps/website/src/templates/model-launch/ModelLaunchHeroSection.vue +++ b/apps/website/src/templates/model-launch/ModelLaunchHeroSection.vue @@ -33,7 +33,7 @@ const { locale = 'en', hero } = defineProps<{ class="bg-transparency-white-t4 pointer-events-none absolute top-6 right-6 flex size-12 items-center justify-center rounded-2xl backdrop-blur-sm lg:top-10 lg:right-10 lg:size-[70px] lg:rounded-3xl" > +

{{ t(hero.footnoteKey, locale) }}

diff --git a/apps/website/src/templates/model-launch/modelLaunchPages.test.ts b/apps/website/src/templates/model-launch/modelLaunchPages.test.ts index f17c1943c42..43fd548cc1b 100644 --- a/apps/website/src/templates/model-launch/modelLaunchPages.test.ts +++ b/apps/website/src/templates/model-launch/modelLaunchPages.test.ts @@ -12,7 +12,7 @@ const pages: { name: string; page: ModelLaunchPage }[] = [ { name: 'flux3', page: flux3Page } ] -describe.each(pages)('$name launch page config', ({ page }) => { +describe.for(pages)('$name launch page config', ({ page }) => { it('gives every gallery card a unique id', () => { const ids = page.gallery?.cards.map((card) => card.id) ?? [] expect(new Set(ids).size).toBe(ids.length) diff --git a/browser_tests/fixtures/data/cloudWorkspace.ts b/browser_tests/fixtures/data/cloudWorkspace.ts index fbd97e53e74..8de04cf54d2 100644 --- a/browser_tests/fixtures/data/cloudWorkspace.ts +++ b/browser_tests/fixtures/data/cloudWorkspace.ts @@ -80,7 +80,9 @@ export const TEAM_BILLING_STATUS = { billing_status: 'paid', has_funds: true, renewal_date: '2099-02-20T00:00:00Z', - team_credit_stop: null + team_credit_stop: null, + max_seats: 30, + occupied_seats: DEFAULT_TEAM_MEMBERS.length } satisfies IngestBillingStatusResponse export const ENDED_STANDARD_BILLING_STATUS = { @@ -92,7 +94,9 @@ export const ENDED_STANDARD_BILLING_STATUS = { subscription_duration: 'MONTHLY', subscription_status: 'ended', subscription_tier: 'STANDARD', - team_credit_stop: null + team_credit_stop: null, + max_seats: 1, + occupied_seats: 1 } satisfies IngestBillingStatusResponse & { billing_rail: 'stripe' } export const TEAM_PRO_PLAN: Plan = { diff --git a/browser_tests/tests/dialogs/pricingTableDeepLink.spec.ts b/browser_tests/tests/dialogs/pricingTableDeepLink.spec.ts index a3c5f68c87a..3fc6b610bf9 100644 --- a/browser_tests/tests/dialogs/pricingTableDeepLink.spec.ts +++ b/browser_tests/tests/dialogs/pricingTableDeepLink.spec.ts @@ -99,7 +99,9 @@ const ACTIVE_TEAM_STATUS = { id: 'team_700', credits_monthly: 147_700, stop_usd: 700 - } + }, + max_seats: 5, + occupied_seats: 1 } satisfies BillingStatusResponse const ACTIVE_STANDARD_STATUS = { @@ -111,7 +113,9 @@ const ACTIVE_STANDARD_STATUS = { billing_status: 'paid', has_funds: true, renewal_date: '2099-02-20T00:00:00Z', - team_credit_stop: null + team_credit_stop: null, + max_seats: 1, + occupied_seats: 1 } satisfies BillingStatusResponse const ACTIVE_CREATOR_STATUS = { diff --git a/src/platform/telemetry/initHostTelemetry.test.ts b/src/platform/telemetry/initHostTelemetry.test.ts index 3e61f69278d..e7b52ff19c9 100644 --- a/src/platform/telemetry/initHostTelemetry.test.ts +++ b/src/platform/telemetry/initHostTelemetry.test.ts @@ -51,4 +51,30 @@ describe('initHostTelemetry', () => { undefined ) }) + + it('dispatches canonical billing events through the host registry', () => { + const capture = vi.fn() + window.__comfyDesktop2 = { isRemote: () => false, Telemetry: { capture } } + remoteConfig.value = { enable_telemetry: true } + + initHostTelemetry() + useTelemetry()?.trackBillingEvent({ + operation: 'subscription_checkout', + stage: 'succeeded', + outcome: 'success', + billing_op_id: 'op-1', + tier: 'pro' + }) + + expect(capture).toHaveBeenCalledWith( + TelemetryEvents.BILLING_SUBSCRIPTION_CHECKOUT_SUCCEEDED, + { + operation: 'subscription_checkout', + stage: 'succeeded', + outcome: 'success', + billing_op_id: 'op-1', + tier: 'pro' + } + ) + }) }) diff --git a/src/platform/telemetry/providers/cloud/DatadogRumTelemetryProvider.test.ts b/src/platform/telemetry/providers/cloud/DatadogRumTelemetryProvider.test.ts index b6a09ff1a32..5902204c96f 100644 --- a/src/platform/telemetry/providers/cloud/DatadogRumTelemetryProvider.test.ts +++ b/src/platform/telemetry/providers/cloud/DatadogRumTelemetryProvider.test.ts @@ -54,7 +54,8 @@ describe('DatadogRumTelemetryProvider', () => { cycle: 'monthly', checkout_type: 'new', payment_intent_source: 'subscribe_to_run', - failure_category: 'provider_decline' + failure_category: 'provider_decline', + duration_ms: 4200 } new DatadogRumTelemetryProvider().trackBillingEvent(event) diff --git a/src/platform/telemetry/providers/cloud/PostHogTelemetryProvider.test.ts b/src/platform/telemetry/providers/cloud/PostHogTelemetryProvider.test.ts index 14defc3f87c..013fd947fc1 100644 --- a/src/platform/telemetry/providers/cloud/PostHogTelemetryProvider.test.ts +++ b/src/platform/telemetry/providers/cloud/PostHogTelemetryProvider.test.ts @@ -560,7 +560,8 @@ describe('PostHogTelemetryProvider', () => { stage: 'succeeded', outcome: 'success', billing_op_id: 'op-cancel', - operation_type: 'cancel' + operation_type: 'cancel', + duration_ms: 4200 }, TelemetryEvents.BILLING_OPERATION_SUCCEEDED ], diff --git a/src/platform/telemetry/providers/host/HostTelemetrySink.test.ts b/src/platform/telemetry/providers/host/HostTelemetrySink.test.ts index bdf19b6002c..778f11762f8 100644 --- a/src/platform/telemetry/providers/host/HostTelemetrySink.test.ts +++ b/src/platform/telemetry/providers/host/HostTelemetrySink.test.ts @@ -156,6 +156,54 @@ describe('HostTelemetrySink', () => { ) }) + it('forwards canonical billing events using the derived name and payload', () => { + new HostTelemetrySink().trackBillingEvent({ + operation: 'operation', + stage: 'succeeded', + outcome: 'success', + billing_op_id: 'op-1', + operation_type: 'subscription', + tier: 'pro', + cycle: 'monthly', + checkout_type: 'new' + }) + + expect(state.capture).toHaveBeenCalledExactlyOnceWith( + TelemetryEvents.BILLING_OPERATION_SUCCEEDED, + { + operation: 'operation', + stage: 'succeeded', + outcome: 'success', + billing_op_id: 'op-1', + operation_type: 'subscription', + tier: 'pro', + cycle: 'monthly', + checkout_type: 'new' + } + ) + }) + + it('forwards billing failures with their failure category', () => { + new HostTelemetrySink().trackBillingEvent({ + operation: 'topup', + stage: 'failed', + outcome: 'failure', + billing_op_id: 'op-2', + failure_category: 'provider_decline' + }) + + expect(state.capture).toHaveBeenCalledExactlyOnceWith( + TelemetryEvents.BILLING_TOPUP_FAILED, + { + operation: 'topup', + stage: 'failed', + outcome: 'failure', + billing_op_id: 'op-2', + failure_category: 'provider_decline' + } + ) + }) + it('does nothing when the host bridge is absent', () => { delete window.__comfyDesktop2 diff --git a/src/platform/telemetry/providers/host/HostTelemetrySink.ts b/src/platform/telemetry/providers/host/HostTelemetrySink.ts index 23e72eeb5f5..a6d7b8d4f8d 100644 --- a/src/platform/telemetry/providers/host/HostTelemetrySink.ts +++ b/src/platform/telemetry/providers/host/HostTelemetrySink.ts @@ -13,6 +13,7 @@ import type { AddCreditsClickMetadata, AuthMetadata, BeginCheckoutMetadata, + BillingTelemetryEvent, DefaultViewSetMetadata, EnterLinearMetadata, ExecutionErrorMetadata, @@ -50,7 +51,12 @@ import type { WorkflowImportMetadata, WorkflowSavedMetadata } from '../../types' -import { CANCELLATION_STAGE_EVENTS, TelemetryEvents } from '../../types' +import { + CANCELLATION_STAGE_EVENTS, + TelemetryEvents, + getBillingTelemetryEventName, + getBillingTelemetryEventPayload +} from '../../types' import { normalizeSurveyResponses } from '../../utils/surveyNormalization' type HostTelemetryProperties = Parameters< @@ -121,6 +127,13 @@ export class HostTelemetrySink implements TelemetryProvider { this.capture(TelemetryEvents.BEGIN_CHECKOUT, metadata) } + trackBillingEvent(event: BillingTelemetryEvent): void { + this.capture( + getBillingTelemetryEventName(event), + getBillingTelemetryEventPayload(event) + ) + } + trackMonthlySubscriptionSucceeded( metadata?: SubscriptionSuccessMetadata ): void { diff --git a/src/platform/telemetry/types.ts b/src/platform/telemetry/types.ts index e2c4d1be824..7209260cc25 100644 --- a/src/platform/telemetry/types.ts +++ b/src/platform/telemetry/types.ts @@ -763,7 +763,12 @@ type SubscriptionCheckoutBillingEvent = { cycle?: BillingCycle checkout_type?: SubscriptionCheckoutType payment_intent_source?: PaymentIntentSource -} & (BillingSucceeded | BillingFailed) + /** + * Client-observed end-to-end wall time from this attempt's canonical + * `started` event through to this terminal event. + */ + duration_ms?: number +} & (BillingStarted | BillingSucceeded | BillingFailed) type BillingOperationBillingEvent = { operation: 'operation' @@ -774,7 +779,15 @@ type BillingOperationBillingEvent = { cycle?: BillingCycle checkout_type?: SubscriptionCheckoutType payment_intent_source?: PaymentIntentSource -} & (BillingSucceeded | BillingFailed | BillingTimedOut) + /** + * Client-observed end-to-end wall time from this attempt's canonical + * `started` event through to this terminal event, including the + * initiating API call's latency (not just the poll-observation window). + * On `timeout` this is how long the client watched, not the operation's + * true duration. + */ + duration_ms?: number +} & (BillingStarted | BillingSucceeded | BillingFailed | BillingTimedOut) type ResubscribeBillingEvent = { operation: 'resubscribe' @@ -785,13 +798,23 @@ type ResubscribeBillingEvent = { type TopupBillingEvent = { operation: 'topup' billing_op_id?: string -} & (BillingSucceeded | BillingFailed) + /** + * Client-observed end-to-end wall time from this attempt's canonical + * `started` event through to this terminal event. + */ + duration_ms?: number +} & (BillingStarted | BillingSucceeded | BillingFailed) type DowngradeToPersonalBillingEvent = { operation: 'downgrade_to_personal' member_removal_count: number member_removal_failures: number target_tier?: TierKey + /** + * Client-observed end-to-end wall time from this attempt's canonical + * `started` event through to this terminal event. + */ + duration_ms?: number } & (BillingStarted | BillingSucceeded | BillingFailed) export type BillingTelemetryEvent = @@ -849,7 +872,9 @@ export function getBillingTelemetryEventPayload(event: BillingTelemetryEvent) { member_removal_failures: event.member_removal_failures }), ...('target_tier' in event && - event.target_tier !== undefined && { target_tier: event.target_tier }) + event.target_tier !== undefined && { target_tier: event.target_tier }), + ...('duration_ms' in event && + event.duration_ms !== undefined && { duration_ms: event.duration_ms }) } } @@ -1025,15 +1050,19 @@ export const TelemetryEvents = { BEGIN_CHECKOUT: 'begin_checkout', // Canonical Billing Lifecycle + BILLING_SUBSCRIPTION_CHECKOUT_STARTED: + 'billing.subscription_checkout.started', BILLING_SUBSCRIPTION_CHECKOUT_SUCCEEDED: 'billing.subscription_checkout.succeeded', BILLING_SUBSCRIPTION_CHECKOUT_FAILED: 'billing.subscription_checkout.failed', + BILLING_OPERATION_STARTED: 'billing.operation.started', BILLING_OPERATION_SUCCEEDED: 'billing.operation.succeeded', BILLING_OPERATION_FAILED: 'billing.operation.failed', BILLING_OPERATION_TIMEOUT: 'billing.operation.timeout', BILLING_RESUBSCRIBE_STARTED: 'billing.resubscribe.started', BILLING_RESUBSCRIBE_SUCCEEDED: 'billing.resubscribe.succeeded', BILLING_RESUBSCRIBE_FAILED: 'billing.resubscribe.failed', + BILLING_TOPUP_STARTED: 'billing.topup.started', BILLING_TOPUP_SUCCEEDED: 'billing.topup.succeeded', BILLING_TOPUP_FAILED: 'billing.topup.failed', BILLING_DOWNGRADE_TO_PERSONAL_STARTED: diff --git a/src/platform/workspace/components/TopUpCreditsDialogContentWorkspace.test.ts b/src/platform/workspace/components/TopUpCreditsDialogContentWorkspace.test.ts index 8c85e98ffb3..93fffafae22 100644 --- a/src/platform/workspace/components/TopUpCreditsDialogContentWorkspace.test.ts +++ b/src/platform/workspace/components/TopUpCreditsDialogContentWorkspace.test.ts @@ -11,7 +11,8 @@ import TopUpCreditsDialogContentWorkspace from './TopUpCreditsDialogContentWorks const mockFetchBalance = vi.fn() const mockFetchStatus = vi.fn() -const mockTopup = vi.fn<(amountCents: number) => Promise>() +const mockTopup = + vi.fn<(amountCents: number) => Promise>() const mockStartOperation = vi.fn() const mockShowSettings = vi.fn() const mockToastAdd = vi.fn() @@ -218,6 +219,46 @@ describe('TopUpCreditsDialogContentWorkspace', () => { }) }) + it('fires a started event before the purchase resolves', async () => { + mockTopup.mockResolvedValue(topupResponse('pending')) + + renderDialog() + await clickAddCredits() + await userEvent.click(screen.getByRole('button', { name: 'Pay $50.00' })) + + await waitFor(() => + expect(mockTrackBillingEvent).toHaveBeenCalledWith({ + operation: 'topup', + stage: 'started', + outcome: 'pending' + }) + ) + expect(mockTrackBillingEvent).toHaveBeenCalledWith({ + operation: 'operation', + stage: 'started', + outcome: 'pending', + operation_type: 'topup' + }) + }) + + it('reports failure telemetry when topup resolves with no response', async () => { + mockTopup.mockResolvedValue(undefined) + + renderDialog() + await clickAddCredits() + await userEvent.click(screen.getByRole('button', { name: 'Pay $50.00' })) + + await waitFor(() => + expect(mockTrackBillingEvent).toHaveBeenCalledWith({ + operation: 'topup', + stage: 'failed', + outcome: 'failure', + failure_category: 'unknown', + duration_ms: expect.any(Number) + }) + ) + }) + it('allows a top-up while an unrelated billing operation is pending', () => { renderDialog() @@ -315,7 +356,9 @@ describe('TopUpCreditsDialogContentWorkspace', () => { expect(screen.getByRole('button', { name: 'Back' })).toBeDisabled() expect(payButton).toBeDisabled() - expect(mockStartOperation).toHaveBeenCalledWith('op-1', 'topup') + expect(mockStartOperation).toHaveBeenCalledWith('op-1', 'topup', { + attemptStartedAt: expect.any(Number) + }) payButton.dispatchEvent(new MouseEvent('click', { bubbles: true })) expect(mockTopup).toHaveBeenCalledOnce() @@ -344,7 +387,8 @@ describe('TopUpCreditsDialogContentWorkspace', () => { stage: 'failed', outcome: 'failure', billing_op_id: 'op-1', - failure_category: 'unknown' + failure_category: 'unknown', + duration_ms: expect.any(Number) }) expect(consoleError).toHaveBeenCalledWith('Purchase failed') consoleError.mockRestore() @@ -364,7 +408,16 @@ describe('TopUpCreditsDialogContentWorkspace', () => { operation: 'topup', stage: 'succeeded', outcome: 'success', - billing_op_id: 'op-1' + billing_op_id: 'op-1', + duration_ms: expect.any(Number) + }) + expect(mockTrackBillingEvent).toHaveBeenCalledWith({ + operation: 'operation', + stage: 'succeeded', + outcome: 'success', + operation_type: 'topup', + billing_op_id: 'op-1', + duration_ms: expect.any(Number) }) }) @@ -377,12 +430,13 @@ describe('TopUpCreditsDialogContentWorkspace', () => { await clickAddCredits() await userEvent.click(screen.getByRole('button', { name: 'Pay $50.00' })) - expect(mockTrackBillingEvent).toHaveBeenCalledTimes(1) + expect(mockTrackBillingEvent).toHaveBeenCalledTimes(4) expect(mockTrackBillingEvent).toHaveBeenCalledWith({ operation: 'topup', stage: 'succeeded', outcome: 'success', - billing_op_id: 'op-1' + billing_op_id: 'op-1', + duration_ms: expect.any(Number) }) expect(mockShowSettings).toHaveBeenCalledWith('workspace') }) @@ -394,10 +448,14 @@ describe('TopUpCreditsDialogContentWorkspace', () => { await clickAddCredits() await userEvent.click(screen.getByRole('button', { name: 'Pay $50.00' })) - expect(mockStartOperation).toHaveBeenCalledWith('op-1', 'topup') + expect(mockStartOperation).toHaveBeenCalledWith('op-1', 'topup', { + attemptStartedAt: expect.any(Number) + }) expect(mockFetchBalance).not.toHaveBeenCalled() expect(mockFetchStatus).not.toHaveBeenCalled() - expect(mockTrackBillingEvent).not.toHaveBeenCalled() + expect(mockTrackBillingEvent).not.toHaveBeenCalledWith( + expect.objectContaining({ stage: 'succeeded' }) + ) }) it('does not refresh balance or status for a failed top-up', async () => { @@ -414,7 +472,17 @@ describe('TopUpCreditsDialogContentWorkspace', () => { stage: 'failed', outcome: 'failure', billing_op_id: 'op-1', - failure_category: 'provider_decline' + failure_category: 'provider_decline', + duration_ms: expect.any(Number) + }) + expect(mockTrackBillingEvent).toHaveBeenCalledWith({ + operation: 'operation', + stage: 'failed', + outcome: 'failure', + operation_type: 'topup', + billing_op_id: 'op-1', + failure_category: 'provider_decline', + duration_ms: expect.any(Number) }) }) @@ -431,7 +499,8 @@ describe('TopUpCreditsDialogContentWorkspace', () => { operation: 'topup', stage: 'failed', outcome: 'failure', - failure_category: 'api_rejected' + failure_category: 'api_rejected', + duration_ms: expect.any(Number) }) ) }) diff --git a/src/platform/workspace/components/TopUpCreditsDialogContentWorkspace.vue b/src/platform/workspace/components/TopUpCreditsDialogContentWorkspace.vue index bf705a44f3a..1181f099fcb 100644 --- a/src/platform/workspace/components/TopUpCreditsDialogContentWorkspace.vue +++ b/src/platform/workspace/components/TopUpCreditsDialogContentWorkspace.vue @@ -408,13 +408,40 @@ async function handleBuy() { loading.value = true paymentSubmitted.value = true + const attemptStartedAt = Date.now() try { telemetry?.trackApiCreditTopupButtonPurchaseClicked(payAmount.value) + telemetry?.trackBillingEvent({ + operation: 'topup', + stage: 'started', + outcome: 'pending' + }) + telemetry?.trackBillingEvent({ + operation: 'operation', + stage: 'started', + outcome: 'pending', + operation_type: 'topup' + }) const amountCents = payAmount.value * 100 const response = await topup(amountCents) if (!response) { paymentSubmitted.value = false + telemetry?.trackBillingEvent({ + operation: 'topup', + stage: 'failed', + outcome: 'failure', + failure_category: 'unknown', + duration_ms: Date.now() - attemptStartedAt + }) + telemetry?.trackBillingEvent({ + operation: 'operation', + stage: 'failed', + outcome: 'failure', + operation_type: 'topup', + failure_category: 'unknown', + duration_ms: Date.now() - attemptStartedAt + }) return } @@ -423,7 +450,16 @@ async function handleBuy() { operation: 'topup', stage: 'succeeded', outcome: 'success', - billing_op_id: response.billing_op_id + billing_op_id: response.billing_op_id, + duration_ms: Date.now() - attemptStartedAt + }) + telemetry?.trackBillingEvent({ + operation: 'operation', + stage: 'succeeded', + outcome: 'success', + operation_type: 'topup', + billing_op_id: response.billing_op_id, + duration_ms: Date.now() - attemptStartedAt }) toast.add({ severity: 'success', @@ -435,12 +471,12 @@ async function handleBuy() { settingsDialog.show('workspace') } else if (response.status === 'pending') { void billingOperationStore - .startOperation(response.billing_op_id, 'topup') + .startOperation(response.billing_op_id, 'topup', { attemptStartedAt }) .then(() => { paymentSubmitted.value = false }) .catch(() => { - reportPurchaseError(response.billing_op_id) + reportPurchaseError(attemptStartedAt, response.billing_op_id) }) } else { // Synchronous 'failed' here means the charge was declined, not rejected pre-attempt. @@ -450,7 +486,17 @@ async function handleBuy() { stage: 'failed', outcome: 'failure', billing_op_id: response.billing_op_id, - failure_category: 'provider_decline' + failure_category: 'provider_decline', + duration_ms: Date.now() - attemptStartedAt + }) + telemetry?.trackBillingEvent({ + operation: 'operation', + stage: 'failed', + outcome: 'failure', + operation_type: 'topup', + billing_op_id: response.billing_op_id, + failure_category: 'provider_decline', + duration_ms: Date.now() - attemptStartedAt }) toast.add({ severity: 'error', @@ -459,13 +505,17 @@ async function handleBuy() { }) } } catch (error) { - reportPurchaseError(undefined, error) + reportPurchaseError(attemptStartedAt, undefined, error) } finally { loading.value = false } } -function reportPurchaseError(billingOpId?: string, error?: unknown) { +function reportPurchaseError( + attemptStartedAt: number, + billingOpId?: string, + error?: unknown +) { paymentSubmitted.value = false console.error('Purchase failed', ...(error === undefined ? [] : [error])) @@ -475,7 +525,18 @@ function reportPurchaseError(billingOpId?: string, error?: unknown) { outcome: 'failure', ...(billingOpId ? { billing_op_id: billingOpId } : {}), failure_category: - error === undefined ? 'unknown' : categorizeBillingApiError(error) + error === undefined ? 'unknown' : categorizeBillingApiError(error), + duration_ms: Date.now() - attemptStartedAt + }) + telemetry?.trackBillingEvent({ + operation: 'operation', + stage: 'failed', + outcome: 'failure', + operation_type: 'topup', + ...(billingOpId ? { billing_op_id: billingOpId } : {}), + failure_category: + error === undefined ? 'unknown' : categorizeBillingApiError(error), + duration_ms: Date.now() - attemptStartedAt }) toast.add({ severity: 'error', diff --git a/src/platform/workspace/composables/useDowngradeToPersonal.test.ts b/src/platform/workspace/composables/useDowngradeToPersonal.test.ts index 283fc371a28..aa7690319e8 100644 --- a/src/platform/workspace/composables/useDowngradeToPersonal.test.ts +++ b/src/platform/workspace/composables/useDowngradeToPersonal.test.ts @@ -1,6 +1,7 @@ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' import { ref } from 'vue' +import { WorkspaceApiError } from '@/platform/workspace/api/workspaceApi' import type { WorkspaceMember } from '@/platform/workspace/stores/teamWorkspaceStore' import { @@ -447,6 +448,15 @@ describe('useDowngradeToPersonal', () => { calls.push('remove') return Promise.resolve() }) + mockTrackBillingEvent.mockImplementation((event) => { + if ( + event.stage === 'started' && + (event.operation === 'subscription_checkout' || + event.operation === 'operation') + ) { + calls.push(`${event.operation}-start`) + } + }) mockSubscribe.mockImplementation(() => { calls.push('subscribe') return Promise.resolve({ billing_op_id: 'op-1', status: 'subscribed' }) @@ -455,7 +465,13 @@ describe('useDowngradeToPersonal', () => { await downgradeToPersonal('founder-monthly') - expect(calls).toEqual(['preview', 'remove', 'subscribe']) + expect(calls).toEqual([ + 'preview', + 'remove', + 'subscription_checkout-start', + 'operation-start', + 'subscribe' + ]) }) it('returns the preview and subscribe response', async () => { @@ -498,7 +514,8 @@ describe('useDowngradeToPersonal', () => { outcome: 'success', member_removal_count: 0, member_removal_failures: 0, - target_tier: 'creator' + target_tier: 'creator', + duration_ms: expect.any(Number) }) }) @@ -539,8 +556,10 @@ describe('useDowngradeToPersonal', () => { downgradeToPersonal: { memberRemovalCount: 1, memberRemovalFailures: 0, - targetTier: undefined - } + targetTier: undefined, + startedAt: expect.any(Number) + }, + attemptStartedAt: expect.any(Number) }) }) @@ -602,8 +621,10 @@ describe('useDowngradeToPersonal', () => { downgradeToPersonal: { memberRemovalCount: 1, memberRemovalFailures: 0, - targetTier: undefined - } + targetTier: undefined, + startedAt: expect.any(Number) + }, + attemptStartedAt: expect.any(Number) }) expect(mockTrackBillingEvent).not.toHaveBeenCalledWith( expect.objectContaining({ stage: 'succeeded' }) @@ -681,7 +702,8 @@ describe('useDowngradeToPersonal', () => { member_removal_failures: 1, target_tier: undefined, failure_category: 'unknown', - error_code: 'member_removal_failed' + error_code: 'member_removal_failed', + duration_ms: expect.any(Number) }) expect(mockTrackBillingEvent).not.toHaveBeenCalledWith( expect.objectContaining({ stage: 'succeeded' }) @@ -702,7 +724,74 @@ describe('useDowngradeToPersonal', () => { member_removal_count: 1, member_removal_failures: 0, target_tier: undefined, - failure_category: 'unknown' + failure_category: 'unknown', + duration_ms: expect.any(Number) + }) + }) + + it('categorizes an uncaught previewSubscribe failure via the shared classifier', async () => { + mockMembers.value = teamWithOwnerAnd('m1') + mockPreviewSubscribe.mockRejectedValue( + new WorkspaceApiError('offline', 502) + ) + const { downgradeToPersonal } = useDowngradeToPersonal() + + await expect(downgradeToPersonal('founder-monthly')).rejects.toThrow( + 'offline' + ) + + expect(mockTrackBillingEvent).toHaveBeenCalledWith({ + operation: 'downgrade_to_personal', + stage: 'failed', + outcome: 'failure', + member_removal_count: 1, + member_removal_failures: 0, + target_tier: undefined, + failure_category: 'api_rejected', + duration_ms: expect.any(Number) + }) + }) + + it('categorizes a status-less previewSubscribe failure as network', async () => { + mockMembers.value = teamWithOwnerAnd('m1') + mockPreviewSubscribe.mockRejectedValue(new WorkspaceApiError('offline')) + const { downgradeToPersonal } = useDowngradeToPersonal() + + await expect(downgradeToPersonal('founder-monthly')).rejects.toThrow( + 'offline' + ) + + expect(mockTrackBillingEvent).toHaveBeenCalledWith({ + operation: 'downgrade_to_personal', + stage: 'failed', + outcome: 'failure', + member_removal_count: 1, + member_removal_failures: 0, + target_tier: undefined, + failure_category: 'network', + duration_ms: expect.any(Number) + }) + }) + + it('categorizes a member-removal failure via the shared classifier', async () => { + mockMembers.value = teamWithOwnerAnd('m1') + mockRemoveMember.mockRejectedValue(new WorkspaceApiError('rejected', 400)) + const { downgradeToPersonal } = useDowngradeToPersonal() + + await expect(downgradeToPersonal('founder-monthly')).rejects.toThrow( + 'm1@example.com' + ) + + expect(mockTrackBillingEvent).toHaveBeenCalledWith({ + operation: 'downgrade_to_personal', + stage: 'failed', + outcome: 'failure', + member_removal_count: 1, + member_removal_failures: 1, + target_tier: undefined, + failure_category: 'api_rejected', + error_code: 'member_removal_failed', + duration_ms: expect.any(Number) }) }) }) diff --git a/src/platform/workspace/composables/useDowngradeToPersonal.ts b/src/platform/workspace/composables/useDowngradeToPersonal.ts index f147159c211..92c820f7ac5 100644 --- a/src/platform/workspace/composables/useDowngradeToPersonal.ts +++ b/src/platform/workspace/composables/useDowngradeToPersonal.ts @@ -10,6 +10,7 @@ import { TIER_TO_KEY } from '@/platform/cloud/subscription/constants/tierPricing import type { BillingCycle } from '@/platform/cloud/subscription/utils/subscriptionTierRank' import { useTelemetry } from '@/platform/telemetry' import type { BillingFailure } from '@/platform/telemetry/types' +import { categorizeBillingApiError } from '@/platform/telemetry/utils/billingFailureCategory' import type { PreviewSubscribeResponse, SubscribeResponse @@ -149,8 +150,10 @@ export function useDowngradeToPersonal() { let memberRemovalFailures = 0 let targetTier: TierKey | undefined let targetCycle: BillingCycle | undefined - let telemetryFailure: BillingFailure = { failure_category: 'unknown' } + let telemetryFailure: BillingFailure | undefined + let checkoutStartedAt: number | undefined + const downgradeStartedAt = Date.now() telemetry?.trackBillingEvent({ operation: 'downgrade_to_personal', stage: 'started', @@ -160,13 +163,35 @@ export function useDowngradeToPersonal() { }) function trackSucceeded() { + const now = Date.now() telemetry?.trackBillingEvent({ operation: 'downgrade_to_personal', stage: 'succeeded', outcome: 'success', member_removal_count: membersToRemove.length, member_removal_failures: memberRemovalFailures, - target_tier: targetTier + target_tier: targetTier, + duration_ms: now - downgradeStartedAt + }) + if (checkoutStartedAt === undefined) return + telemetry?.trackBillingEvent({ + operation: 'subscription_checkout', + stage: 'succeeded', + outcome: 'success', + tier: targetTier, + cycle: targetCycle, + checkout_type: 'change', + duration_ms: now - checkoutStartedAt + }) + telemetry?.trackBillingEvent({ + operation: 'operation', + stage: 'succeeded', + outcome: 'success', + operation_type: 'subscription', + tier: targetTier, + cycle: targetCycle, + checkout_type: 'change', + duration_ms: now - checkoutStartedAt }) } @@ -222,7 +247,7 @@ export function useDowngradeToPersonal() { } catch (error) { memberRemovalFailures += 1 telemetryFailure = { - failure_category: 'unknown', + failure_category: categorizeBillingApiError(error), error_code: 'member_removal_failed' } throw new Error( @@ -235,6 +260,24 @@ export function useDowngradeToPersonal() { } ensureCanDowngrade() + checkoutStartedAt = Date.now() + telemetry?.trackBillingEvent({ + operation: 'subscription_checkout', + stage: 'started', + outcome: 'pending', + tier: targetTier, + cycle: targetCycle, + checkout_type: 'change' + }) + telemetry?.trackBillingEvent({ + operation: 'operation', + stage: 'started', + outcome: 'pending', + operation_type: 'subscription', + tier: targetTier, + cycle: targetCycle, + checkout_type: 'change' + }) let response: SubscribeResponse | void try { response = await subscribe(planSlug, { @@ -294,8 +337,10 @@ export function useDowngradeToPersonal() { downgradeToPersonal: { memberRemovalCount: membersToRemove.length, memberRemovalFailures, - targetTier - } + targetTier, + startedAt: downgradeStartedAt + }, + attemptStartedAt: checkoutStartedAt } ) return null @@ -312,8 +357,10 @@ export function useDowngradeToPersonal() { downgradeToPersonal: { memberRemovalCount: membersToRemove.length, memberRemovalFailures, - targetTier - } + targetTier, + startedAt: downgradeStartedAt + }, + attemptStartedAt: checkoutStartedAt } ) return null @@ -322,6 +369,10 @@ export function useDowngradeToPersonal() { trackSucceeded() return { preview, response } } catch (error) { + const failure = telemetryFailure ?? { + failure_category: categorizeBillingApiError(error) + } + const now = Date.now() telemetry?.trackBillingEvent({ operation: 'downgrade_to_personal', stage: 'failed', @@ -329,8 +380,32 @@ export function useDowngradeToPersonal() { member_removal_count: membersToRemove.length, member_removal_failures: memberRemovalFailures, target_tier: targetTier, - ...telemetryFailure + ...failure, + duration_ms: now - downgradeStartedAt }) + if (checkoutStartedAt !== undefined) { + telemetry?.trackBillingEvent({ + operation: 'subscription_checkout', + stage: 'failed', + outcome: 'failure', + tier: targetTier, + cycle: targetCycle, + checkout_type: 'change', + ...failure, + duration_ms: now - checkoutStartedAt + }) + telemetry?.trackBillingEvent({ + operation: 'operation', + stage: 'failed', + outcome: 'failure', + operation_type: 'subscription', + tier: targetTier, + cycle: targetCycle, + checkout_type: 'change', + ...failure, + duration_ms: now - checkoutStartedAt + }) + } throw error } } diff --git a/src/platform/workspace/composables/useResubscribe.test.ts b/src/platform/workspace/composables/useResubscribe.test.ts index 81b975a252c..a61863e6b65 100644 --- a/src/platform/workspace/composables/useResubscribe.test.ts +++ b/src/platform/workspace/composables/useResubscribe.test.ts @@ -83,6 +83,19 @@ describe('useResubscribe', () => { expect(isResubscribing.value).toBe(false) }) + it('fires a started event before resubscribe resolves', async () => { + const { handleResubscribe } = useResubscribe() + + await handleResubscribe() + + expect(state.trackBillingEvent).toHaveBeenCalledWith({ + operation: 'resubscribe', + stage: 'started', + outcome: 'pending', + source: 'settings_billing_panel' + }) + }) + it('does not report checkout launch as terminal legacy success', async () => { state.shouldUseWorkspaceBilling = false state.canManageSubscriptionLifecycle = false @@ -101,6 +114,9 @@ describe('useResubscribe', () => { outcome: 'pending', source: 'settings_billing_panel' }) + // Exactly one started event on the legacy success rail: the pre-call start, + // with no duplicate post-await started/pending emitted after resubscribe() resolves. + expect(state.trackBillingEvent).toHaveBeenCalledTimes(1) expect(state.toastAdd).toHaveBeenCalledWith( expect.objectContaining({ severity: 'success' }) ) diff --git a/src/platform/workspace/composables/useResubscribe.ts b/src/platform/workspace/composables/useResubscribe.ts index 8b736ec5353..7ed6318c5c7 100644 --- a/src/platform/workspace/composables/useResubscribe.ts +++ b/src/platform/workspace/composables/useResubscribe.ts @@ -32,26 +32,26 @@ export function useResubscribe() { useTelemetry()?.trackResubscribeClicked({ source: 'settings_billing_panel' }) + useTelemetry()?.trackBillingEvent({ + operation: 'resubscribe', + stage: 'started', + outcome: 'pending', + source: 'settings_billing_panel' + }) isResubscribing.value = true try { await resubscribe() - // Workspace's resubscribe() is terminal; legacy only opens Stripe, so it reports - // started/pending and lets useSubscription.ts's pending-checkout recovery own the terminal. - useTelemetry()?.trackBillingEvent( - shouldUseWorkspaceBilling.value - ? { - operation: 'resubscribe', - stage: 'succeeded', - outcome: 'success', - source: 'settings_billing_panel' - } - : { - operation: 'resubscribe', - stage: 'started', - outcome: 'pending', - source: 'settings_billing_panel' - } - ) + // Workspace's resubscribe() is terminal, so it reports succeeded here. Legacy + // only opens Stripe; the started/pending event fired above already covers it, + // and useSubscription.ts's pending-checkout recovery owns the eventual terminal. + if (shouldUseWorkspaceBilling.value) { + useTelemetry()?.trackBillingEvent({ + operation: 'resubscribe', + stage: 'succeeded', + outcome: 'success', + source: 'settings_billing_panel' + }) + } toast.add({ severity: 'success', summary: t('subscription.resubscribeSuccess'), diff --git a/src/platform/workspace/composables/useSubscriptionCheckout.test.ts b/src/platform/workspace/composables/useSubscriptionCheckout.test.ts index 094b48cd027..72e15eef857 100644 --- a/src/platform/workspace/composables/useSubscriptionCheckout.test.ts +++ b/src/platform/workspace/composables/useSubscriptionCheckout.test.ts @@ -739,7 +739,7 @@ describe('useSubscriptionCheckout', () => { ) }) - it('tracks conversion success for an immediate Team downgrade', async () => { + it('does not duplicate telemetry owned by the Team downgrade orchestration', async () => { mockIsTeamPlan.value = true mockShowDowngradeToPersonalDialog.mockResolvedValue({ preview: { is_immediate: true }, @@ -756,16 +756,7 @@ describe('useSubscriptionCheckout', () => { }) expect(checkout.checkoutStep.value).toBe('success') - expect(mockTrackBillingEvent).toHaveBeenCalledWith({ - operation: 'subscription_checkout', - stage: 'succeeded', - outcome: 'success', - tier: 'creator', - cycle: 'monthly', - checkout_type: 'change', - payment_intent_source: undefined, - billing_op_id: 'immediate-downgrade' - }) + expect(mockTrackBillingEvent).not.toHaveBeenCalled() }) }) @@ -1266,6 +1257,45 @@ describe('useSubscriptionCheckout', () => { }) describe('handleTeamSubscribe', () => { + it('fires a started event before subscribing', async () => { + const checkout = await setup() + await checkout.handleSubscribeTeamClick({ + stop: { + id: 'team_700', + usd: 700, + credits: 147_700, + discountedUsd: 665 + }, + billingCycle: 'monthly' + }) + mockSubscribe.mockResolvedValueOnce({ + status: 'subscribed', + billing_op_id: 'op-team-1' + }) + + await checkout.handleTeamSubscribe() + + expect(mockTrackBillingEvent).toHaveBeenCalledWith({ + operation: 'subscription_checkout', + stage: 'started', + outcome: 'pending', + tier: 'team', + cycle: 'monthly', + checkout_type: 'new', + payment_intent_source: undefined + }) + expect(mockTrackBillingEvent).toHaveBeenCalledWith({ + operation: 'operation', + stage: 'started', + outcome: 'pending', + operation_type: 'subscription', + tier: 'team', + cycle: 'monthly', + checkout_type: 'new', + payment_intent_source: undefined + }) + }) + it('subscribes with the team plan slug, stop id and billing cycle', async () => { const checkout = await setup() await checkout.handleSubscribeTeamClick({ @@ -1534,6 +1564,10 @@ describe('useSubscriptionCheckout', () => { expect(mockToastAdd).toHaveBeenCalledWith( expect.objectContaining({ severity: 'error' }) ) + // Regression guard: this reactivation-consent guard is not a checkout + // attempt, so it must not open a funnel entry no terminal event will + // ever close. + expect(mockTrackBillingEvent).not.toHaveBeenCalled() }) it('refuses to bill a team reactivation when a fresh preview no longer matches the confirmed charge', async () => { @@ -1631,13 +1665,7 @@ describe('useSubscriptionCheckout', () => { detail: 'status unavailable' }) ) - expect(mockTrackBillingEvent).toHaveBeenCalledWith( - expect.objectContaining({ - operation: 'subscription_checkout', - stage: 'failed', - outcome: 'failure' - }) - ) + expect(mockTrackBillingEvent).not.toHaveBeenCalled() }) // Regression guard: drift recovery must reuse the same reactivation- @@ -1741,13 +1769,8 @@ describe('useSubscriptionCheckout', () => { detail: 'subscription.preview.reactivation.confirmationRequired' }) ) - expect(mockTrackBillingEvent).toHaveBeenCalledWith( - expect.objectContaining({ - operation: 'subscription_checkout', - stage: 'failed', - outcome: 'failure', - tier: 'team' - }) + expect(mockTrackBillingEvent).not.toHaveBeenCalledWith( + expect.objectContaining({ stage: 'failed' }) ) mockPreviewSubscribe.mockResolvedValueOnce(preview) @@ -1766,6 +1789,19 @@ describe('useSubscriptionCheckout', () => { }) ) expect(checkout.checkoutStep.value).toBe('success') + expect( + mockTrackBillingEvent.mock.calls.filter( + ([event]) => + event.operation === 'subscription_checkout' && + event.stage === 'started' + ) + ).toHaveLength(1) + expect( + mockTrackBillingEvent.mock.calls.filter( + ([event]) => + event.operation === 'operation' && event.stage === 'started' + ) + ).toHaveLength(1) }) it('uses the annual plan slug for the yearly cycle', async () => { @@ -1869,7 +1905,8 @@ describe('useSubscriptionCheckout', () => { cycle: 'yearly', checkout_type: 'new', payment_intent_source: undefined, - failure_category: 'unknown' + failure_category: 'unknown', + duration_ms: expect.any(Number) }) }) @@ -2094,6 +2131,38 @@ describe('useSubscriptionCheckout', () => { }) describe('handleAddCreditCard', () => { + it('fires a started event before subscribing', async () => { + const checkout = await setup() + checkout.selectedTierKey.value = 'standard' + checkout.selectedBillingCycle.value = 'yearly' + mockSubscribe.mockResolvedValueOnce({ + status: 'subscribed', + billing_op_id: 'op-1' + }) + + await checkout.handleAddCreditCard() + + expect(mockTrackBillingEvent).toHaveBeenCalledWith({ + operation: 'subscription_checkout', + stage: 'started', + outcome: 'pending', + tier: 'standard', + cycle: 'yearly', + checkout_type: 'new', + payment_intent_source: undefined + }) + expect(mockTrackBillingEvent).toHaveBeenCalledWith({ + operation: 'operation', + stage: 'started', + outcome: 'pending', + operation_type: 'subscription', + tier: 'standard', + cycle: 'yearly', + checkout_type: 'new', + payment_intent_source: undefined + }) + }) + it('shows existing success immediately without owning post-response reconciliation', async () => { const checkout = await setup() checkout.selectedTierKey.value = 'standard' @@ -2121,7 +2190,8 @@ describe('useSubscriptionCheckout', () => { cycle: 'yearly', checkout_type: 'new', payment_intent_source: undefined, - billing_op_id: 'op-1' + billing_op_id: 'op-1', + duration_ms: expect.any(Number) }) expect(mockTrackMonthlySubscriptionSucceeded).toHaveBeenCalledWith({ tier: 'standard', @@ -2241,7 +2311,8 @@ describe('useSubscriptionCheckout', () => { tier: 'standard', cycle: 'yearly', checkoutType: 'new', - paymentIntentSource: undefined + paymentIntentSource: undefined, + attemptStartedAt: expect.any(Number) } ) expect(checkout.checkoutStep.value).toBe('success') @@ -2272,7 +2343,8 @@ describe('useSubscriptionCheckout', () => { tier: 'standard', cycle: 'yearly', checkoutType: 'new', - paymentIntentSource: undefined + paymentIntentSource: undefined, + attemptStartedAt: expect.any(Number) } ) expect(checkout.checkoutStep.value).toBe('success') @@ -2299,7 +2371,8 @@ describe('useSubscriptionCheckout', () => { tier: 'standard', cycle: 'yearly', checkoutType: 'new', - paymentIntentSource: undefined + paymentIntentSource: undefined, + attemptStartedAt: expect.any(Number) } ) expect(checkout.checkoutStep.value).toBe('preview') @@ -2328,7 +2401,8 @@ describe('useSubscriptionCheckout', () => { cycle: 'yearly', checkout_type: 'new', payment_intent_source: undefined, - failure_category: 'unknown' + failure_category: 'unknown', + duration_ms: expect.any(Number) }) }) @@ -2349,7 +2423,8 @@ describe('useSubscriptionCheckout', () => { checkout_type: 'new', payment_intent_source: undefined, failure_category: 'unknown', - error_code: 'missing_checkout_response' + error_code: 'missing_checkout_response', + duration_ms: expect.any(Number) }) }) @@ -2516,13 +2591,8 @@ describe('useSubscriptionCheckout', () => { detail: 'subscription.preview.reactivation.confirmationRequired' }) ) - expect(mockTrackBillingEvent).toHaveBeenCalledWith( - expect.objectContaining({ - operation: 'subscription_checkout', - stage: 'failed', - outcome: 'failure', - tier: 'standard' - }) + expect(mockTrackBillingEvent).not.toHaveBeenCalledWith( + expect.objectContaining({ stage: 'failed' }) ) }) @@ -2723,6 +2793,10 @@ describe('useSubscriptionCheckout', () => { expect(mockToastAdd).toHaveBeenCalledWith( expect.objectContaining({ severity: 'error' }) ) + // Regression guard: this reactivation-consent guard is not a checkout + // attempt, so it must not open a funnel entry no terminal event will + // ever close. + expect(mockTrackBillingEvent).not.toHaveBeenCalled() }) it('refuses to bill when a fresh preview no longer matches the confirmed charge', async () => { @@ -2901,6 +2975,26 @@ describe('useSubscriptionCheckout', () => { }) describe('handleResubscribe', () => { + it('fires a started event before resubscribe resolves', async () => { + const checkout = await setup('subscribe_to_run') + mockResubscribe.mockResolvedValueOnce({ + billing_op_id: 'op-4', + status: 'active' + }) + mockFetchStatus.mockResolvedValueOnce(undefined) + mockFetchBalance.mockResolvedValueOnce(undefined) + + await checkout.handleResubscribe() + + expect(mockTrackBillingEvent).toHaveBeenCalledWith({ + operation: 'resubscribe', + stage: 'started', + outcome: 'pending', + source: 'pricing_dialog', + payment_intent_source: 'subscribe_to_run' + }) + }) + it('emits close on success', async () => { const checkout = await setup('subscribe_to_run') mockResubscribe.mockResolvedValueOnce({ @@ -2970,6 +3064,9 @@ describe('useSubscriptionCheckout', () => { source: 'pricing_dialog', payment_intent_source: 'subscribe_to_run' }) + // Exactly one started event on the legacy success rail: the pre-call start, + // with no duplicate post-await started/pending emitted after resubscribe() resolves. + expect(mockTrackBillingEvent).toHaveBeenCalledTimes(1) }) it('fires resubscribe failure telemetry on the legacy rail too', async () => { diff --git a/src/platform/workspace/composables/useSubscriptionCheckout.ts b/src/platform/workspace/composables/useSubscriptionCheckout.ts index 9f1f8d2e39e..a725de64274 100644 --- a/src/platform/workspace/composables/useSubscriptionCheckout.ts +++ b/src/platform/workspace/composables/useSubscriptionCheckout.ts @@ -128,6 +128,7 @@ export function useSubscriptionCheckout( const selectedTeamCheckout = ref(null) let teamPreviewRequestId = 0 let refreshStatusOnFocus = false + let activeCheckoutAttemptStartedAt: number | undefined useEventListener(window, 'focus', () => { if (!refreshStatusOnFocus) return refreshStatusOnFocus = false @@ -457,7 +458,7 @@ export function useSubscriptionCheckout( cycle: selectedBillingCycle.value, checkoutType: 'change' }, - result.preview.is_immediate + false ) return true } @@ -640,6 +641,7 @@ export function useSubscriptionCheckout( previewData.value = null selectedTeamCheckout.value = null activeCheckoutOperationId.value = null + activeCheckoutAttemptStartedAt = undefined } function handleBackToPricing() { @@ -677,6 +679,11 @@ export function useSubscriptionCheckout( await refreshPreviewOnReactivationBlock(planSlug) return } + const attemptStartedAt = trackSubscriptionStarted({ + tier: tierKey, + cycle: billingCycle, + checkoutType + }) if ( confirmReactivation && (isCancelled.value || reactivationRequired.value) @@ -704,22 +711,26 @@ export function useSubscriptionCheckout( await handleSubscribeResponse(response, { tier: tierKey, cycle: billingCycle, - checkoutType + checkoutType, + attemptStartedAt }) + activeCheckoutAttemptStartedAt = undefined } catch (error) { + if (hasErrorCode(error, 'REACTIVATION_CONFIRMATION_REQUIRED')) { + await refreshPreviewOnReactivationBlock(planSlug) + return + } trackSubscriptionFailure( { tier: tierKey, cycle: billingCycle, - checkoutType + checkoutType, + attemptStartedAt: activeCheckoutAttemptStartedAt }, error ) + activeCheckoutAttemptStartedAt = undefined if (await recoverOutstandingPayment(error)) return - if (hasErrorCode(error, 'REACTIVATION_CONFIRMATION_REQUIRED')) { - await refreshPreviewOnReactivationBlock(planSlug) - return - } if (await refreshExpiredProrationQuote(error, planSlug)) return showSubscribeError(error) } finally { @@ -742,6 +753,45 @@ export function useSubscriptionCheckout( tier: CheckoutTierKey | 'team' cycle: BillingCycle checkoutType: SubscriptionCheckoutType + /** + * Timestamp captured alongside the canonical `subscription_checkout` + * `started` event, threaded through to the billing-op poller so its + * `duration_ms` spans the full attempt (including the initiating + * subscribe call), not just the poll-observation window. + */ + attemptStartedAt?: number + } + + function trackSubscriptionStarted( + context: SubscriptionOutcomeContext + ): number | undefined { + if (activeCheckoutAttemptStartedAt !== undefined) { + return activeCheckoutAttemptStartedAt + } + if (!shouldUseWorkspaceBilling.value) return undefined + + activeCheckoutAttemptStartedAt = Date.now() + + telemetry?.trackBillingEvent({ + operation: 'subscription_checkout', + stage: 'started', + outcome: 'pending', + tier: context.tier, + cycle: context.cycle, + checkout_type: context.checkoutType, + payment_intent_source: paymentIntentSource + }) + telemetry?.trackBillingEvent({ + operation: 'operation', + stage: 'started', + outcome: 'pending', + operation_type: 'subscription', + tier: context.tier, + cycle: context.cycle, + checkout_type: context.checkoutType, + payment_intent_source: paymentIntentSource + }) + return activeCheckoutAttemptStartedAt } function trackSubscriptionFailure( @@ -749,7 +799,7 @@ export function useSubscriptionCheckout( error?: unknown, errorCode?: 'missing_checkout_response' ) { - if (!shouldUseWorkspaceBilling.value) return + if (context.attemptStartedAt === undefined) return // No checkout_url is a malformed response, not a caught error — 'unknown' is honest here. const failureCategory = errorCode @@ -765,7 +815,21 @@ export function useSubscriptionCheckout( checkout_type: context.checkoutType, payment_intent_source: paymentIntentSource, failure_category: failureCategory, - ...(errorCode && { error_code: errorCode }) + ...(errorCode && { error_code: errorCode }), + duration_ms: Date.now() - context.attemptStartedAt + }) + telemetry?.trackBillingEvent({ + operation: 'operation', + stage: 'failed', + outcome: 'failure', + operation_type: 'subscription', + tier: context.tier, + cycle: context.cycle, + checkout_type: context.checkoutType, + payment_intent_source: paymentIntentSource, + failure_category: failureCategory, + ...(errorCode && { error_code: errorCode }), + duration_ms: Date.now() - context.attemptStartedAt }) } @@ -780,7 +844,11 @@ export function useSubscriptionCheckout( } if (response.status === 'subscribed') { - if (shouldTrackSubscriptionSuccess) { + if ( + shouldTrackSubscriptionSuccess && + context.attemptStartedAt !== undefined + ) { + const durationMs = Date.now() - context.attemptStartedAt telemetry?.trackBillingEvent({ operation: 'subscription_checkout', stage: 'succeeded', @@ -789,8 +857,23 @@ export function useSubscriptionCheckout( cycle: context.cycle, checkout_type: context.checkoutType, payment_intent_source: paymentIntentSource, - billing_op_id: response.billing_op_id + billing_op_id: response.billing_op_id, + duration_ms: durationMs + }) + telemetry?.trackBillingEvent({ + operation: 'operation', + stage: 'succeeded', + outcome: 'success', + operation_type: 'subscription', + tier: context.tier, + cycle: context.cycle, + checkout_type: context.checkoutType, + payment_intent_source: paymentIntentSource, + billing_op_id: response.billing_op_id, + duration_ms: durationMs }) + } + if (shouldTrackSubscriptionSuccess) { // Also fires the legacy event for providers (Mixpanel, GTM) that don't implement trackBillingEvent. telemetry?.trackMonthlySubscriptionSucceeded({ tier: context.tier, @@ -841,7 +924,8 @@ export function useSubscriptionCheckout( tier: context.tier, cycle: context.cycle, checkoutType: context.checkoutType, - paymentIntentSource + paymentIntentSource, + attemptStartedAt: context.attemptStartedAt } ) if ( @@ -889,6 +973,11 @@ export function useSubscriptionCheckout( }) return } + const attemptStartedAt = trackSubscriptionStarted({ + tier: 'team', + cycle: billingCycle, + checkoutType + }) if ( confirmReactivation && (isCancelled.value || reactivationRequired.value) @@ -921,25 +1010,29 @@ export function useSubscriptionCheckout( await handleSubscribeResponse(response, { tier: 'team', cycle: billingCycle, - checkoutType + checkoutType, + attemptStartedAt }) + activeCheckoutAttemptStartedAt = undefined } catch (error) { + if (hasErrorCode(error, 'REACTIVATION_CONFIRMATION_REQUIRED')) { + await refreshPreviewOnReactivationBlock(planSlug, { + teamCreditStopId: stop.id, + billingCycle + }) + return + } trackSubscriptionFailure( { tier: 'team', cycle: billingCycle, - checkoutType + checkoutType, + attemptStartedAt: activeCheckoutAttemptStartedAt }, error ) + activeCheckoutAttemptStartedAt = undefined if (await recoverOutstandingPayment(error)) return - if (hasErrorCode(error, 'REACTIVATION_CONFIRMATION_REQUIRED')) { - await refreshPreviewOnReactivationBlock(planSlug, { - teamCreditStopId: stop.id, - billingCycle - }) - return - } if ( await refreshExpiredProrationQuote(error, planSlug, { teamCreditStopId: stop.id, @@ -960,28 +1053,28 @@ export function useSubscriptionCheckout( source: 'pricing_dialog', payment_intent_source: paymentIntentSource }) + telemetry?.trackBillingEvent({ + operation: 'resubscribe', + stage: 'started', + outcome: 'pending', + source: 'pricing_dialog', + payment_intent_source: paymentIntentSource + }) isResubscribing.value = true try { await resubscribe() - // Workspace's resubscribe() is terminal; legacy only opens Stripe, so it reports - // started/pending and lets useSubscription.ts's pending-checkout recovery own the terminal. - telemetry?.trackBillingEvent( - shouldUseWorkspaceBilling.value - ? { - operation: 'resubscribe', - stage: 'succeeded', - outcome: 'success', - source: 'pricing_dialog', - payment_intent_source: paymentIntentSource - } - : { - operation: 'resubscribe', - stage: 'started', - outcome: 'pending', - source: 'pricing_dialog', - payment_intent_source: paymentIntentSource - } - ) + // Workspace's resubscribe() is terminal, so it reports succeeded here. Legacy + // only opens Stripe; the started/pending event fired above already covers it, + // and useSubscription.ts's pending-checkout recovery owns the eventual terminal. + if (shouldUseWorkspaceBilling.value) { + telemetry?.trackBillingEvent({ + operation: 'resubscribe', + stage: 'succeeded', + outcome: 'success', + source: 'pricing_dialog', + payment_intent_source: paymentIntentSource + }) + } toast.add({ severity: 'success', summary: t('subscription.resubscribeSuccess'), diff --git a/src/platform/workspace/composables/useWorkspaceBilling.test.ts b/src/platform/workspace/composables/useWorkspaceBilling.test.ts index 3f758ece122..a9abb9f7718 100644 --- a/src/platform/workspace/composables/useWorkspaceBilling.test.ts +++ b/src/platform/workspace/composables/useWorkspaceBilling.test.ts @@ -758,7 +758,9 @@ describe('useWorkspaceBilling', () => { const billing = setupBilling() await billing.cancelSubscription() - expect(mockStartOperation).toHaveBeenCalledWith('op-cancel', 'cancel') + expect(mockStartOperation).toHaveBeenCalledWith('op-cancel', 'cancel', { + attemptStartedAt: expect.any(Number) + }) expect(billing.error.value).toBeNull() }) @@ -826,6 +828,24 @@ describe('useWorkspaceBilling', () => { expect(mockStartOperation).not.toHaveBeenCalled() }) + it('fires a started event before the cancel API call resolves', async () => { + mockWorkspaceApi.cancelSubscription.mockResolvedValue({ + billing_op_id: 'op-cancel', + cancel_at: '2026-06-01T00:00:00Z' + }) + mockStartOperation.mockResolvedValue(operation()) + + const billing = setupBilling() + await billing.cancelSubscription() + + expect(mockTrackBillingEvent).toHaveBeenCalledWith({ + operation: 'operation', + stage: 'started', + outcome: 'pending', + operation_type: 'cancel' + }) + }) + it('fires billing telemetry directly when the initiating call fails before a billing_op_id exists', async () => { mockWorkspaceApi.cancelSubscription.mockRejectedValue( new mockWorkspaceApiError('Upstream failure', 502) @@ -841,7 +861,8 @@ describe('useWorkspaceBilling', () => { stage: 'failed', outcome: 'failure', operation_type: 'cancel', - failure_category: 'api_rejected' + failure_category: 'api_rejected', + duration_ms: expect.any(Number) }) }) @@ -872,7 +893,9 @@ describe('useWorkspaceBilling', () => { await expect(billing.cancelSubscription()).rejects.toThrow( 'processor rejected' ) - expect(mockTrackBillingEvent).not.toHaveBeenCalled() + expect(mockTrackBillingEvent).not.toHaveBeenCalledWith( + expect.objectContaining({ stage: 'failed' }) + ) }) it('falls back to a generic error message when cancel rejects with a non-Error', async () => { @@ -908,7 +931,22 @@ describe('useWorkspaceBilling', () => { expect(billing.subscription.value?.tier).toBe('CREATOR') expect(mockStartOperation).not.toHaveBeenCalled() expect(billing.isLoading.value).toBe(false) - expect(mockTrackBillingEvent).not.toHaveBeenCalled() + expect(mockTrackBillingEvent).not.toHaveBeenCalledWith( + expect.objectContaining({ stage: 'failed' }) + ) + expect(mockTrackBillingEvent).toHaveBeenCalledWith({ + operation: 'operation', + stage: 'started', + outcome: 'pending', + operation_type: 'cancel' + }) + expect(mockTrackBillingEvent).toHaveBeenCalledWith({ + operation: 'operation', + stage: 'succeeded', + outcome: 'success', + operation_type: 'cancel', + duration_ms: expect.any(Number) + }) }) it('stays a success when the follow-up status read also fails', async () => { diff --git a/src/platform/workspace/composables/useWorkspaceBilling.ts b/src/platform/workspace/composables/useWorkspaceBilling.ts index fe579215e64..50ec395ad17 100644 --- a/src/platform/workspace/composables/useWorkspaceBilling.ts +++ b/src/platform/workspace/composables/useWorkspaceBilling.ts @@ -319,6 +319,13 @@ export function useWorkspaceBilling(): BillingState & BillingActions { async function cancelSubscription(): Promise { isLoading.value = true error.value = null + const attemptStartedAt = Date.now() + telemetry?.trackBillingEvent({ + operation: 'operation', + stage: 'started', + outcome: 'pending', + operation_type: 'cancel' + }) // Once set, the poller (billingOperationStore) owns failure telemetry; until then, this must report it. let billingOpId: string | undefined try { @@ -326,7 +333,8 @@ export function useWorkspaceBilling(): BillingState & BillingActions { billingOpId = response.billing_op_id const operation = await billingOperationStore.startOperation( billingOpId, - 'cancel' + 'cancel', + { attemptStartedAt } ) if (operation.status !== 'succeeded') { @@ -340,6 +348,13 @@ export function useWorkspaceBilling(): BillingState & BillingActions { // fetchStatus records its own read failure; the cancellation still // holds, so the operation is not in error. error.value = null + telemetry?.trackBillingEvent({ + operation: 'operation', + stage: 'succeeded', + outcome: 'success', + operation_type: 'cancel', + duration_ms: Date.now() - attemptStartedAt + }) return } if (billingOpId === undefined) { @@ -348,7 +363,8 @@ export function useWorkspaceBilling(): BillingState & BillingActions { stage: 'failed', outcome: 'failure', operation_type: 'cancel', - failure_category: categorizeBillingApiError(err) + failure_category: categorizeBillingApiError(err), + duration_ms: Date.now() - attemptStartedAt }) } error.value = diff --git a/src/platform/workspace/stores/billingOperationStore.test.ts b/src/platform/workspace/stores/billingOperationStore.test.ts index 78d45d60176..b3632fa759b 100644 --- a/src/platform/workspace/stores/billingOperationStore.test.ts +++ b/src/platform/workspace/stores/billingOperationStore.test.ts @@ -224,6 +224,92 @@ describe('billingOperationStore', () => { }) describe('polling success', () => { + it('emits only the generic lifecycle for a recovered subscription operation', async () => { + vi.mocked(workspaceApi.getBillingOpStatus).mockResolvedValue({ + id: 'op-recovered', + status: 'succeeded', + started_at: new Date().toISOString() + }) + + const store = useBillingOperationStore() + void store.startOperation('op-recovered', 'subscription') + + await vi.advanceTimersByTimeAsync(0) + + expect(mockTrackBillingEvent.mock.calls).toEqual([ + [ + { + operation: 'operation', + stage: 'started', + outcome: 'pending', + operation_type: 'subscription' + } + ], + [ + { + operation: 'operation', + stage: 'succeeded', + outcome: 'success', + billing_op_id: 'op-recovered', + operation_type: 'subscription', + tier: undefined, + cycle: undefined, + checkout_type: undefined, + payment_intent_source: undefined, + duration_ms: 0 + } + ] + ]) + }) + + it('closes both terminal streams for an initiated subscription operation', async () => { + vi.mocked(workspaceApi.getBillingOpStatus).mockResolvedValue({ + id: 'op-initiated', + status: 'succeeded', + started_at: new Date().toISOString() + }) + + const store = useBillingOperationStore() + void store.startOperation('op-initiated', 'subscription', { + tier: 'creator', + cycle: 'monthly', + checkoutType: 'new', + attemptStartedAt: Date.now() + }) + + await vi.advanceTimersByTimeAsync(0) + + expect(mockTrackBillingEvent.mock.calls).toEqual([ + [ + { + operation: 'operation', + stage: 'succeeded', + outcome: 'success', + billing_op_id: 'op-initiated', + operation_type: 'subscription', + tier: 'creator', + cycle: 'monthly', + checkout_type: 'new', + payment_intent_source: undefined, + duration_ms: 0 + } + ], + [ + { + operation: 'subscription_checkout', + stage: 'succeeded', + outcome: 'success', + tier: 'creator', + cycle: 'monthly', + checkout_type: 'new', + payment_intent_source: undefined, + billing_op_id: 'op-initiated', + duration_ms: 0 + } + ] + ]) + }) + it('updates status and shows toast on success', async () => { vi.mocked(workspaceApi.getBillingOpStatus).mockResolvedValue({ id: 'op-1', @@ -296,7 +382,9 @@ describe('billingOperationStore', () => { }) const store = useBillingOperationStore() - void store.startOperation('op-1', 'subscription') + void store.startOperation('op-1', 'subscription', { + attemptStartedAt: Date.now() + }) await vi.advanceTimersByTimeAsync(0) @@ -308,7 +396,8 @@ describe('billingOperationStore', () => { cycle: undefined, checkout_type: undefined, payment_intent_source: undefined, - billing_op_id: 'op-1' + billing_op_id: 'op-1', + duration_ms: expect.any(Number) }) }) @@ -324,7 +413,8 @@ describe('billingOperationStore', () => { void store.startOperation('op-1', 'subscription', { tier: 'creator', cycle: 'yearly', - checkoutType: 'new' + checkoutType: 'new', + attemptStartedAt: Date.now() }) await vi.advanceTimersByTimeAsync(0) @@ -365,10 +455,12 @@ describe('billingOperationStore', () => { tier: 'creator', cycle: 'monthly', checkoutType: 'change', + attemptStartedAt: Date.now(), downgradeToPersonal: { memberRemovalCount: 2, memberRemovalFailures: 0, - targetTier: 'creator' + targetTier: 'creator', + startedAt: Date.now() } }) @@ -381,7 +473,8 @@ describe('billingOperationStore', () => { outcome: 'success', member_removal_count: 2, member_removal_failures: 0, - target_tier: 'creator' + target_tier: 'creator', + duration_ms: expect.any(Number) }) }) @@ -393,7 +486,9 @@ describe('billingOperationStore', () => { }) const store = useBillingOperationStore() - void store.startOperation('op-1', 'topup') + void store.startOperation('op-1', 'topup', { + attemptStartedAt: Date.now() + }) await vi.advanceTimersByTimeAsync(0) @@ -401,10 +496,48 @@ describe('billingOperationStore', () => { operation: 'topup', stage: 'succeeded', outcome: 'success', - billing_op_id: 'op-1' + billing_op_id: 'op-1', + duration_ms: expect.any(Number) }) }) + // Parity with .failed/.timeout, which fire for all three types: a + // succeeded/(succeeded+failed) ratio reads a permanent 0% for any type + // missing from the numerator. + it.for(['subscription', 'topup', 'cancel'] as const)( + 'fires billing.operation.succeeded for a %s operation', + async (type) => { + vi.mocked(workspaceApi.getBillingOpStatus).mockResolvedValue({ + id: 'op-1', + status: 'succeeded', + started_at: new Date().toISOString() + }) + + const store = useBillingOperationStore() + void store.startOperation('op-1', type, { + tier: 'creator', + cycle: 'monthly', + checkoutType: 'new', + paymentIntentSource: 'subscription_required' + }) + + await vi.advanceTimersByTimeAsync(0) + + expect(mockTrackBillingEvent).toHaveBeenCalledWith({ + operation: 'operation', + stage: 'succeeded', + outcome: 'success', + billing_op_id: 'op-1', + operation_type: type, + tier: 'creator', + cycle: 'monthly', + checkout_type: 'new', + payment_intent_source: 'subscription_required', + duration_ms: expect.any(Number) + }) + } + ) + it('shows topup success message for topup operations', async () => { vi.mocked(workspaceApi.getBillingOpStatus).mockResolvedValue({ id: 'op-1', @@ -424,6 +557,62 @@ describe('billingOperationStore', () => { }) }) + it('reports duration_ms as the time elapsed since the operation started', async () => { + vi.mocked(workspaceApi.getBillingOpStatus) + .mockResolvedValueOnce({ + id: 'op-1', + status: 'pending', + started_at: new Date().toISOString() + }) + .mockResolvedValue({ + id: 'op-1', + status: 'succeeded', + started_at: new Date().toISOString() + }) + + const store = useBillingOperationStore() + void store.startOperation('op-1', 'topup') + + await vi.advanceTimersByTimeAsync(0) + await vi.advanceTimersByTimeAsync(1500) + + const successCall = mockTrackBillingEvent.mock.calls.find( + ([event]) => + event.operation === 'operation' && event.stage === 'succeeded' + ) + expect(successCall?.[0].duration_ms).toBeGreaterThanOrEqual(1500) + }) + + it('computes duration_ms from the caller-supplied attemptStartedAt, not from when startOperation() itself ran', async () => { + vi.mocked(workspaceApi.getBillingOpStatus) + .mockResolvedValueOnce({ + id: 'op-1', + status: 'pending', + started_at: new Date().toISOString() + }) + .mockResolvedValue({ + id: 'op-1', + status: 'succeeded', + started_at: new Date().toISOString() + }) + + const store = useBillingOperationStore() + // Simulate the canonical `started` event having fired 300ms before the + // initiating API call returned and startOperation() itself ran. + const attemptStartedAt = Date.now() - 300 + void store.startOperation('op-1', 'topup', { attemptStartedAt }) + + await vi.advanceTimersByTimeAsync(0) + await vi.advanceTimersByTimeAsync(1500) + + const successCall = mockTrackBillingEvent.mock.calls.find( + ([event]) => event.operation === 'topup' && event.stage === 'succeeded' + ) + // Poll-observed time alone is ~1500ms; duration_ms must also include the + // 300ms initiation latency that preceded startOperation() running. + expect(successCall?.[0].duration_ms).toBeGreaterThanOrEqual(1800) + }) + it('removes the received toast when operation succeeds', async () => { vi.mocked(workspaceApi.getBillingOpStatus).mockResolvedValue({ id: 'op-1', @@ -478,7 +667,8 @@ describe('billingOperationStore', () => { cycle: undefined, checkout_type: undefined, payment_intent_source: undefined, - failure_category: 'provider_decline' + failure_category: 'provider_decline', + duration_ms: expect.any(Number) }) }) @@ -502,48 +692,48 @@ describe('billingOperationStore', () => { ) }) - it('categorizes a cancel poll failure as an api rejection, not a provider decline', async () => { + it('stays silent when a checkout was superseded by a new plan choice', async () => { vi.mocked(workspaceApi.getBillingOpStatus).mockResolvedValue({ id: 'op-1', status: 'failed', + error_message: 'checkout_superseded', started_at: new Date().toISOString() }) const store = useBillingOperationStore() - void store.startOperation('op-1', 'cancel') + void store.startOperation('op-1', 'subscription') await vi.advanceTimersByTimeAsync(0) + expect(store.getOperation('op-1')?.status).toBe('failed') + expect(mockToastAdd).not.toHaveBeenCalledWith( + expect.objectContaining({ severity: 'error' }) + ) expect(mockTrackBillingEvent).toHaveBeenCalledWith( expect.objectContaining({ - operation_type: 'cancel', - failure_category: 'api_rejected' + stage: 'failed', + billing_op_id: 'op-1', + failure_category: 'stale_operation' }) ) }) - it('stays silent when a checkout was superseded by a new plan choice', async () => { + it('categorizes a cancel poll failure as an api rejection, not a provider decline', async () => { vi.mocked(workspaceApi.getBillingOpStatus).mockResolvedValue({ id: 'op-1', status: 'failed', - error_message: 'checkout_superseded', started_at: new Date().toISOString() }) const store = useBillingOperationStore() - void store.startOperation('op-1', 'subscription') + void store.startOperation('op-1', 'cancel') await vi.advanceTimersByTimeAsync(0) - expect(store.getOperation('op-1')?.status).toBe('failed') - expect(mockToastAdd).not.toHaveBeenCalledWith( - expect.objectContaining({ severity: 'error' }) - ) expect(mockTrackBillingEvent).toHaveBeenCalledWith( expect.objectContaining({ - stage: 'failed', - billing_op_id: 'op-1', - failure_category: 'stale_operation' + operation_type: 'cancel', + failure_category: 'api_rejected' }) ) }) @@ -561,7 +751,8 @@ describe('billingOperationStore', () => { downgradeToPersonal: { memberRemovalCount: 2, memberRemovalFailures: 0, - targetTier: 'free' + targetTier: 'free', + startedAt: Date.now() } }) @@ -588,6 +779,38 @@ describe('billingOperationStore', () => { ) }) + it('reports duration_ms as the time elapsed since the operation started', async () => { + vi.mocked(workspaceApi.getBillingOpStatus) + .mockResolvedValueOnce({ + id: 'op-1', + status: 'pending', + started_at: new Date().toISOString() + }) + .mockResolvedValue({ + id: 'op-1', + status: 'failed', + started_at: new Date().toISOString() + }) + + const store = useBillingOperationStore() + void store.startOperation('op-1', 'topup') + + await vi.advanceTimersByTimeAsync(0) + await vi.advanceTimersByTimeAsync(1500) + + expect(mockTrackBillingEvent).toHaveBeenCalledWith( + expect.objectContaining({ + operation: 'operation', + stage: 'failed', + duration_ms: expect.any(Number) + }) + ) + const failureCall = mockTrackBillingEvent.mock.calls.find( + ([event]) => event.operation === 'operation' && event.stage === 'failed' + ) + expect(failureCall?.[0].duration_ms).toBeGreaterThanOrEqual(1500) + }) + it('uses default message when no error_message in response', async () => { vi.mocked(workspaceApi.getBillingOpStatus).mockResolvedValue({ id: 'op-1', @@ -701,6 +924,33 @@ describe('billingOperationStore', () => { }) }) + it('reports duration_ms of at least the timeout threshold', async () => { + vi.mocked(workspaceApi.getBillingOpStatus).mockResolvedValue({ + id: 'op-1', + status: 'pending', + started_at: new Date().toISOString() + }) + + const store = useBillingOperationStore() + void store.startOperation('op-1', 'subscription') + + await vi.advanceTimersByTimeAsync(121_000) + await vi.runAllTimersAsync() + + expect(mockTrackBillingEvent).toHaveBeenCalledWith( + expect.objectContaining({ + operation: 'operation', + stage: 'timeout', + duration_ms: expect.any(Number) + }) + ) + const timeoutCall = mockTrackBillingEvent.mock.calls.find( + ([event]) => + event.operation === 'operation' && event.stage === 'timeout' + ) + expect(timeoutCall?.[0].duration_ms).toBeGreaterThanOrEqual(120_000) + }) + it('keeps a valid action URL pending past discovery and clears it on success', async () => { const actionUrl = 'https://verify.example/sensitive-token' vi.mocked(workspaceApi.getBillingOpStatus) @@ -973,7 +1223,8 @@ describe('billingOperationStore', () => { stage: 'succeeded', outcome: 'success', billing_op_id: 'op-1', - operation_type: 'cancel' + operation_type: 'cancel', + duration_ms: expect.any(Number) }) }) diff --git a/src/platform/workspace/stores/billingOperationStore.ts b/src/platform/workspace/stores/billingOperationStore.ts index 2a878a5c0d2..b826cbe9e28 100644 --- a/src/platform/workspace/stores/billingOperationStore.ts +++ b/src/platform/workspace/stores/billingOperationStore.ts @@ -43,7 +43,17 @@ export interface StartOperationMetadata { memberRemovalCount: number memberRemovalFailures: number targetTier?: TierKey + startedAt: number } + /** + * The timestamp the caller used for its own canonical `started` telemetry + * event (i.e. before the initiating subscribe/top-up/cancel API call), so + * `duration_ms` on the poller's terminal events spans the full emitted + * lifecycle instead of just the poll-observation window. Defaults to + * `Date.now()` (poll-start time) when the caller has no such timestamp, + * e.g. recovering a pending operation on page load. + */ + attemptStartedAt?: number } interface BillingOperation { @@ -52,6 +62,8 @@ interface BillingOperation { status: OperationStatus errorMessage: string | null startedAt: number + operationStartedAt: number + businessAttemptStartedAt?: number actionUrl: string | null authenticationRequiredSeen: boolean workspaceId: string | null @@ -132,12 +144,15 @@ export const useBillingOperationStore = defineStore('billingOperation', () => { if (existing) clearOperation(opId) const actionUrl = validateActionUrl(initialActionUrl) + const now = Date.now() const operation: BillingOperation = { opId, type, status: 'pending', errorMessage: null, - startedAt: Date.now(), + startedAt: now, + operationStartedAt: metadata?.attemptStartedAt ?? now, + businessAttemptStartedAt: metadata?.attemptStartedAt, actionUrl, authenticationRequiredSeen: actionUrl !== null, workspaceId: workspaceStore.activeWorkspaceId, @@ -151,6 +166,15 @@ export const useBillingOperationStore = defineStore('billingOperation', () => { operations.value = new Map(operations.value).set(opId, operation) intervals.set(opId, INITIAL_INTERVAL_MS) + if (metadata?.attemptStartedAt === undefined) { + useTelemetry()?.trackBillingEvent({ + operation: 'operation', + stage: 'started', + outcome: 'pending', + operation_type: type + }) + } + if (type !== 'cancel') { const messageKey = type === 'subscription' @@ -279,7 +303,26 @@ export const useBillingOperationStore = defineStore('billingOperation', () => { cleanup(opId) const telemetry = useTelemetry() - if (operation.type === 'subscription') { + const now = Date.now() + const operationDurationMs = now - operation.operationStartedAt + telemetry?.trackBillingEvent({ + operation: 'operation', + stage: 'succeeded', + outcome: 'success', + billing_op_id: opId, + operation_type: operation.type, + tier: operation.tier, + cycle: operation.cycle, + checkout_type: operation.checkoutType, + payment_intent_source: operation.paymentIntentSource, + duration_ms: operationDurationMs + }) + + if ( + operation.type === 'subscription' && + operation.businessAttemptStartedAt !== undefined + ) { + const durationMs = now - operation.businessAttemptStartedAt telemetry?.trackBillingEvent({ operation: 'subscription_checkout', stage: 'succeeded', @@ -288,7 +331,8 @@ export const useBillingOperationStore = defineStore('billingOperation', () => { cycle: operation.cycle, checkout_type: operation.checkoutType, payment_intent_source: operation.paymentIntentSource, - billing_op_id: opId + billing_op_id: opId, + duration_ms: durationMs }) // Also fires the legacy event for providers (Mixpanel, GTM) that don't implement trackBillingEvent. telemetry?.trackMonthlySubscriptionSucceeded({ @@ -307,23 +351,20 @@ export const useBillingOperationStore = defineStore('billingOperation', () => { operation.downgradeToPersonal.memberRemovalCount, member_removal_failures: operation.downgradeToPersonal.memberRemovalFailures, - target_tier: operation.downgradeToPersonal.targetTier + target_tier: operation.downgradeToPersonal.targetTier, + duration_ms: now - operation.downgradeToPersonal.startedAt }) } - } else if (operation.type === 'topup') { + } else if ( + operation.type === 'topup' && + operation.businessAttemptStartedAt !== undefined + ) { telemetry?.trackBillingEvent({ operation: 'topup', stage: 'succeeded', outcome: 'success', - billing_op_id: opId - }) - } else { - telemetry?.trackBillingEvent({ - operation: 'operation', - stage: 'succeeded', - outcome: 'success', billing_op_id: opId, - operation_type: 'cancel' + duration_ms: now - operation.businessAttemptStartedAt }) } @@ -370,6 +411,9 @@ export const useBillingOperationStore = defineStore('billingOperation', () => { if (!operation) return const superseded = errorMessage === CHECKOUT_SUPERSEDED_REASON + const failureCategory = superseded + ? 'stale_operation' + : categorizePollFailure(operation.type) const defaultMessage = failureMessage(operation.type) const detail = operation.type === 'subscription' @@ -380,9 +424,7 @@ export const useBillingOperationStore = defineStore('billingOperation', () => { cleanup(opId) const telemetry = useTelemetry() - const failureCategory = superseded - ? 'stale_operation' - : categorizePollFailure(operation.type) + const now = Date.now() telemetry?.trackBillingEvent({ operation: 'operation', stage: 'failed', @@ -393,8 +435,38 @@ export const useBillingOperationStore = defineStore('billingOperation', () => { cycle: operation.cycle, checkout_type: operation.checkoutType, payment_intent_source: operation.paymentIntentSource, - failure_category: failureCategory + failure_category: failureCategory, + duration_ms: now - operation.operationStartedAt }) + if ( + operation.type === 'subscription' && + operation.businessAttemptStartedAt !== undefined + ) { + telemetry?.trackBillingEvent({ + operation: 'subscription_checkout', + stage: 'failed', + outcome: 'failure', + tier: operation.tier, + cycle: operation.cycle, + checkout_type: operation.checkoutType, + payment_intent_source: operation.paymentIntentSource, + billing_op_id: opId, + failure_category: failureCategory, + duration_ms: now - operation.businessAttemptStartedAt + }) + } else if ( + operation.type === 'topup' && + operation.businessAttemptStartedAt !== undefined + ) { + telemetry?.trackBillingEvent({ + operation: 'topup', + stage: 'failed', + outcome: 'failure', + billing_op_id: opId, + failure_category: failureCategory, + duration_ms: now - operation.businessAttemptStartedAt + }) + } if (operation.downgradeToPersonal) { telemetry?.trackBillingEvent({ operation: 'downgrade_to_personal', @@ -404,7 +476,8 @@ export const useBillingOperationStore = defineStore('billingOperation', () => { member_removal_failures: operation.downgradeToPersonal.memberRemovalFailures, target_tier: operation.downgradeToPersonal.targetTier, - failure_category: failureCategory + failure_category: failureCategory, + duration_ms: now - operation.downgradeToPersonal.startedAt }) } @@ -429,6 +502,7 @@ export const useBillingOperationStore = defineStore('billingOperation', () => { cleanup(opId) const telemetry = useTelemetry() + const now = Date.now() telemetry?.trackBillingEvent({ operation: 'operation', stage: 'timeout', @@ -439,8 +513,38 @@ export const useBillingOperationStore = defineStore('billingOperation', () => { cycle: operation.cycle, checkout_type: operation.checkoutType, payment_intent_source: operation.paymentIntentSource, - failure_category: 'poll_timeout' + failure_category: 'poll_timeout', + duration_ms: now - operation.operationStartedAt }) + if ( + operation.type === 'subscription' && + operation.businessAttemptStartedAt !== undefined + ) { + telemetry?.trackBillingEvent({ + operation: 'subscription_checkout', + stage: 'failed', + outcome: 'failure', + tier: operation.tier, + cycle: operation.cycle, + checkout_type: operation.checkoutType, + payment_intent_source: operation.paymentIntentSource, + billing_op_id: opId, + failure_category: 'poll_timeout', + duration_ms: now - operation.businessAttemptStartedAt + }) + } else if ( + operation.type === 'topup' && + operation.businessAttemptStartedAt !== undefined + ) { + telemetry?.trackBillingEvent({ + operation: 'topup', + stage: 'failed', + outcome: 'failure', + billing_op_id: opId, + failure_category: 'poll_timeout', + duration_ms: now - operation.businessAttemptStartedAt + }) + } if (operation.downgradeToPersonal) { telemetry?.trackBillingEvent({ operation: 'downgrade_to_personal', @@ -450,7 +554,8 @@ export const useBillingOperationStore = defineStore('billingOperation', () => { member_removal_failures: operation.downgradeToPersonal.memberRemovalFailures, target_tier: operation.downgradeToPersonal.targetTier, - failure_category: 'poll_timeout' + failure_category: 'poll_timeout', + duration_ms: now - operation.downgradeToPersonal.startedAt }) }