diff --git a/apps/billing-web/src/composables/useCheckoutJourney.ts b/apps/billing-web/src/composables/useCheckoutJourney.ts index 5e085de2345..a0357ae4716 100644 --- a/apps/billing-web/src/composables/useCheckoutJourney.ts +++ b/apps/billing-web/src/composables/useCheckoutJourney.ts @@ -1,5 +1,9 @@ import type { + CheckoutEndingAttribution, + CheckoutEndingKind, CheckoutEntryFlow, + CheckoutExit, + CheckoutJourneyPhase, CheckoutJourneyPhaseEvent, PreviewSubscribeResult, SubscriptionPreview @@ -38,8 +42,15 @@ export function useCheckoutJourney(uiMode: 'embedded' | 'full_page') { let presses = 0 let linkingPress: number | undefined let billingOpId: string | undefined + let lastPhase: + | Exclude + | undefined + let lastEnding: CheckoutEndingKind | undefined + let left = false function track(phase: CheckoutJourneyPhaseEvent) { + if (phase.phase !== 'abandoned' && phase.phase !== 'ended') + lastPhase = phase.phase billingWebTelemetry.trackCheckoutJourneyEvent({ checkout_journey_id: journeyId, checkout_entered_at: enteredAt, @@ -143,9 +154,32 @@ export function useCheckoutJourney(uiMode: 'embedded' | 'full_page') { if (failed) track(failed) } + /** Once per ending screen shown; an ending resolves the journey. */ + function ended( + kind: CheckoutEndingKind, + attribution: CheckoutEndingAttribution | undefined + ) { + if (kind === lastEnding) return + lastEnding = kind + track({ + phase: 'ended', + ending_kind: kind, + ...(attribution !== undefined && { attribution }) + }) + } + + /** The customer left a journey no ending resolved; only the first way out counts. */ + function abandoned(exit: CheckoutExit) { + if (left || lastEnding !== undefined || lastPhase === undefined) return + left = true + track({ phase: 'abandoned', last_phase: lastPhase, exit }) + } + return { enter, track, + ended, + abandoned, submitted, submitSettled, operationIssued, diff --git a/apps/billing-web/src/composables/useFullPageCheckout.ts b/apps/billing-web/src/composables/useFullPageCheckout.ts index 79b5512eaf7..052629273d7 100644 --- a/apps/billing-web/src/composables/useFullPageCheckout.ts +++ b/apps/billing-web/src/composables/useFullPageCheckout.ts @@ -1,4 +1,8 @@ -import { tryOnScopeDispose, useIntervalFn } from '@vueuse/core' +import { + tryOnScopeDispose, + useEventListener, + useIntervalFn +} from '@vueuse/core' import { computed, shallowReadonly, shallowRef, watch } from 'vue' import { @@ -37,9 +41,11 @@ import { needsConsent, railAcceptsPay, reduceCheckoutPage, - settledPlanSource + settledPlanSource, + submitPhaseOf } from '@/checkout/checkoutPage' import { methodKindOf } from '@/checkout/checkoutJourney' +import { endingOf } from '@/checkout/endingScreen' import { pricingTableUrl } from '@/checkout/cloudLinks' import { createOperationChannel } from '@/checkout/operationChannel' import { promoEntryLive, promoRejectionOf } from '@/checkout/promoEntry' @@ -61,6 +67,7 @@ import { useCheckoutJourney } from '@/composables/useCheckoutJourney' import { useCheckoutPromo } from '@/composables/useCheckoutPromo' import { awaitBillingWebStripeKey } from '@/config/stripeKey' import { useBillingEntry } from '@/entry/billingEntry' +import type { BillingEntryState } from '@/entry/billingEntry' import { useBillingWebSession } from '@/session/billingWebSession' import { createDeferredStripeChallengePort } from '@/session/stripeChallengePort' import { @@ -154,6 +161,13 @@ function railOf( } as const } +/** A link that names no plan has nothing to quote, so it is as unreadable as a malformed one. */ +function arrivalPage({ entry, error }: BillingEntryState): CheckoutPage { + return error.value === undefined && entry.value?.plan !== undefined + ? RESOLVING + : UNREADABLE_LINK +} + /** * The full-page checkout's effects around one `CheckoutPage` state: the * reconciliation with whatever operation the workspace is already waiting @@ -185,11 +199,8 @@ export function useFullPageCheckout() { const journey = useCheckoutJourney('full_page') journey.enter() - /** A link that names no plan has nothing to quote, so it is as unreadable as a malformed one. */ const page = shallowRef( - unreadableLink.value === undefined && entry.value?.plan !== undefined - ? RESOLVING - : UNREADABLE_LINK + arrivalPage({ entry, error: unreadableLink }) ) /** Busy from the Pay click until the attempt resolves, whatever the lifecycle's promise does. */ @@ -205,8 +216,12 @@ export function useFullPageCheckout() { * reopens only a challenge that stays on this page (below), and anything * else waits for Complete verification. */ + let handedToHostedStep = false const checkout = useCheckout({ - openUrl: (url) => window.location.assign(url), + openUrl: (url) => { + handedToHostedStep = true + window.location.assign(url) + }, navigationMode: 'redirect', challengePort, autoContinue: () => submitting.value, @@ -579,6 +594,33 @@ export function useFullPageCheckout() { const { returnLink, openedByScript, close } = useCheckoutExit(page) + watch( + () => endingOf(page.value)?.kind, + (kind) => { + if (kind === undefined) return + const current = page.value + journey.ended( + kind, + current.kind === 'terminal' ? current.attribution : undefined + ) + }, + { immediate: true } + ) + + /** A page handed to a hosted step or a method's own site has not been abandoned. */ + function leftForPayment() { + const current = page.value + return ( + handedToHostedStep || + (current.kind === 'capture' && + submitPhaseOf(current).kind === 'redirecting') + ) + } + + useEventListener(window, 'pagehide', () => { + if (!leftForPayment()) journey.abandoned('page_exit') + }) + /** * The live catalog, on the Team tab when the link asked for a team plan: * one the quote named without its stop, or a link that carries a stop. @@ -735,6 +777,7 @@ export function useFullPageCheckout() { viewPlansLink, openedByScript, close, + abandon: journey.abandoned, retryLoad, onPaymentPhase, savedMethods: saved.methods, diff --git a/apps/billing-web/src/telemetry/billingWebTelemetry.test.ts b/apps/billing-web/src/telemetry/billingWebTelemetry.test.ts index 33879a692db..0005f611950 100644 --- a/apps/billing-web/src/telemetry/billingWebTelemetry.test.ts +++ b/apps/billing-web/src/telemetry/billingWebTelemetry.test.ts @@ -337,6 +337,27 @@ describe('trackCheckoutJourneyEvent', () => { ) }) + it('sends an abandoned checkout to PostHog by beacon, since the page is going away', async () => { + const telemetry = createBillingWebTelemetry() + await telemetry.startPostHog({ + config: Promise.resolve(CONFIGURED), + identity: ref({ kind: 'unknown' }) + }) + + telemetry.trackCheckoutJourneyEvent({ + ...ENTERED, + phase: 'abandoned', + last_phase: 'entered', + exit: 'page_exit' + }) + + expect(posthog.capture).toHaveBeenCalledExactlyOnceWith( + 'billing.checkout.abandoned', + expect.objectContaining({ last_phase: 'entered', exit: 'page_exit' }), + { transport: 'sendBeacon' } + ) + }) + it('holds back the journey phases the backend switched off, and only those', async () => { const telemetry = createBillingWebTelemetry() await telemetry.startPostHog({ diff --git a/apps/billing-web/src/telemetry/billingWebTelemetry.ts b/apps/billing-web/src/telemetry/billingWebTelemetry.ts index e62e017e3ef..8f77469db8c 100644 --- a/apps/billing-web/src/telemetry/billingWebTelemetry.ts +++ b/apps/billing-web/src/telemetry/billingWebTelemetry.ts @@ -45,6 +45,8 @@ type PostHogClient = Pick< interface BillingEvent { readonly name: BillingTelemetryEventName | CheckoutJourneyTelemetryEventName readonly properties: Readonly> + /** Sent while the page goes away, after PostHog has drained its queue on `pagehide`. */ + readonly onPageExit?: true } interface IdentitySink { @@ -155,8 +157,12 @@ export function createBillingWebTelemetry() { posthog.waiting.push(event) return case 'ready': - if (!posthog.disabledEvents.has(event.name)) - posthog.client.capture(event.name, event.properties) + if (posthog.disabledEvents.has(event.name)) return + if (event.onPageExit) + posthog.client.capture(event.name, event.properties, { + transport: 'sendBeacon' + }) + else posthog.client.capture(event.name, event.properties) } } @@ -217,7 +223,8 @@ export function createBillingWebTelemetry() { properties: { ...getCheckoutJourneyTelemetryEventPayload(event), billing_surface: 'billing_web' - } + }, + ...(event.phase === 'abandoned' && { onPageExit: true }) }) ) } diff --git a/apps/billing-web/src/views/FullPageCheckoutView.journey.test.ts b/apps/billing-web/src/views/FullPageCheckoutView.journey.test.ts index 0cb50db5db6..b70a7a35455 100644 --- a/apps/billing-web/src/views/FullPageCheckoutView.journey.test.ts +++ b/apps/billing-web/src/views/FullPageCheckoutView.journey.test.ts @@ -318,8 +318,13 @@ describe('the full-page checkout journey', () => { ])('$name', async ({ options, arrange, path, last }) => { await renderCheckout(options, arrange, path) - await waitFor(() => expect(journey()).toHaveLength(2)) - expect(journeyNames()[0]).toBe('billing.checkout.entered') + await waitFor(() => + expect(journeyNames()).toEqual([ + 'billing.checkout.entered', + 'billing.checkout.preview_failed', + 'billing.checkout.ended' + ]) + ) expect(journey()[1]).toMatchObject(last) }) @@ -416,7 +421,8 @@ describe('the full-page checkout journey', () => { 'billing.checkout.payment_submit_attempted', 'billing.checkout.method_selected', 'billing.checkout.submitted', - 'billing.checkout.operation_linked' + 'billing.checkout.operation_linked', + 'billing.checkout.ended' ]) ) const [, , , , , submitted, linked] = journey() @@ -824,3 +830,107 @@ describe('the full-page operation a Pay links', () => { expect(linked()).toMatchObject([{ billing_op_id: 'op_9' }]) }) }) + +describe('the full-page checkout exits and endings', () => { + beforeEach(() => { + vi.mocked(datadogRum.getInitConfiguration).mockReturnValue({ + clientToken: 'pub', + applicationId: 'app' + }) + vi.spyOn(window.location, 'assign').mockImplementation(() => {}) + }) + + const leavePage = () => { + window.dispatchEvent(new PageTransitionEvent('pagehide')) + } + const exitsOf = () => + journey().filter(({ name }) => name === 'billing.checkout.abandoned') + + it.for<{ name: string; leave: () => Promise | void; exit: string }>([ + { name: 'the page goes away', leave: leavePage, exit: 'page_exit' }, + { + name: 'the customer goes Back', + leave: () => + userEvent.click(screen.getByRole('button', { name: 'Back' })), + exit: 'back' + } + ])( + 'reports a checkout abandoned once, at its last phase, when $name', + async ({ leave, exit }) => { + await renderCheckout() + await waitFor(() => + expect(journeyNames()).toContain('billing.checkout.preview_ready') + ) + + await leave() + leavePage() + + expect(exitsOf()).toEqual([ + expect.objectContaining({ + phase: 'abandoned', + last_phase: 'preview_ready', + exit, + billing_surface: 'billing_web' + }) + ]) + } + ) + + it('reports no abandon for a page that left for a payment method of its own site', async () => { + const fake = await renderCheckout() + await screen.findByText('Subscribe to Creator Plan · Acme Team') + fake.subscribe.mockImplementation(() => new Promise(() => {})) + reportPhase({ phase: 'payment_element_ready', element: 'payment' }) + form.emit('confirm', 'ctoken_1', 'alipay') + await waitFor(() => expect(fake.subscribe).toHaveBeenCalled()) + + leavePage() + + expect(exitsOf()).toEqual([]) + }) + + it('reports the ending its own payment reached, and no abandon after it', async () => { + const fake = await renderCheckout({ + subscribe: { + status: 'ok', + value: { phase: 'succeeded', operation: succeededOperation('op_9') } + } + }) + await screen.findByText('Subscribe to Creator Plan · Acme Team') + reportPhase({ phase: 'payment_element_ready', element: 'payment' }) + form.emit('confirm', 'ctoken_1', 'card') + await waitFor(() => expect(fake.subscribe).toHaveBeenCalled()) + await waitFor(() => + expect(journeyNames()).toContain('billing.checkout.ended') + ) + + leavePage() + + expect( + journey().filter(({ name }) => name === 'billing.checkout.ended') + ).toEqual([ + expect.objectContaining({ + phase: 'ended', + ending_kind: 'success', + attribution: 'started' + }) + ]) + expect(exitsOf()).toEqual([]) + }) + + it('reports an ending no payment reached without an attribution', async () => { + await renderCheckout({ + capabilities: {}, + denials: { can_subscribe_self_serve: 'not_workspace_owner' } + }) + + await waitFor(() => + expect(journeyNames()).toContain('billing.checkout.ended') + ) + const [ended] = journey().filter( + ({ name }) => name === 'billing.checkout.ended' + ) + expect(ended).toMatchObject({ ending_kind: 'refused' }) + expect(ended).not.toHaveProperty('attribution') + }) +}) diff --git a/apps/billing-web/src/views/FullPageCheckoutView.vue b/apps/billing-web/src/views/FullPageCheckoutView.vue index 09c1579fe66..590b51739fc 100644 --- a/apps/billing-web/src/views/FullPageCheckoutView.vue +++ b/apps/billing-web/src/views/FullPageCheckoutView.vue @@ -42,6 +42,7 @@ const { viewPlansLink, openedByScript, close, + abandon, retryLoad, onPaymentPhase, savedMethods, @@ -160,6 +161,7 @@ const endingPlan = computed(() => { function goBack() { reportReturnClicked('back') + abandon('back') window.location.assign(returnLink.value) } diff --git a/packages/account-core/src/core/billing/telemetry/checkoutJourney.test.ts b/packages/account-core/src/core/billing/telemetry/checkoutJourney.test.ts index 8813927270d..d55f53c1453 100644 --- a/packages/account-core/src/core/billing/telemetry/checkoutJourney.test.ts +++ b/packages/account-core/src/core/billing/telemetry/checkoutJourney.test.ts @@ -142,6 +142,43 @@ const CASES: readonly PayloadCase[] = [ phase: 'preview_failed', failure_category: 'network' } + }, + { + name: 'an abandoned checkout names the last phase it reached and how the customer left', + event: { + ...CONTEXT, + billing_op_id: 'op-1', + phase: 'abandoned', + last_phase: 'operation_linked', + exit: 'page_exit' + }, + payload: { + ...PAYLOAD_OF_CONTEXT, + billing_op_id: 'op-1', + phase: 'abandoned', + last_phase: 'operation_linked', + exit: 'page_exit' + } + }, + { + name: 'an ending names its screen and whose payment it was', + event: { + ...CONTEXT, + phase: 'ended', + ending_kind: 'success', + attribution: 'started' + }, + payload: { + ...PAYLOAD_OF_CONTEXT, + phase: 'ended', + ending_kind: 'success', + attribution: 'started' + } + }, + { + name: 'an ending no payment reached carries no attribution', + event: { ...CONTEXT, phase: 'ended', ending_kind: 'refused' }, + payload: { ...PAYLOAD_OF_CONTEXT, phase: 'ended', ending_kind: 'refused' } } ] diff --git a/packages/account-core/src/core/billing/telemetry/checkoutJourney.ts b/packages/account-core/src/core/billing/telemetry/checkoutJourney.ts index b3e648d0417..29c82551f9a 100644 --- a/packages/account-core/src/core/billing/telemetry/checkoutJourney.ts +++ b/packages/account-core/src/core/billing/telemetry/checkoutJourney.ts @@ -125,7 +125,7 @@ type CheckoutJourneyOperationLinked = { billing_op_id: string } -export type CheckoutJourneyPhaseEvent = +type CheckoutJourneyProgressEvent = | CheckoutJourneyEntered | CheckoutJourneyPreviewReady | CheckoutJourneyPreviewFailed @@ -139,7 +139,45 @@ export type CheckoutJourneyPhaseEvent = | CheckoutJourneySubmitted | CheckoutJourneyOperationLinked -type CheckoutJourneyPhase = CheckoutJourneyPhaseEvent['phase'] +/** How the customer left: the page went away, or a control of this page led out. */ +export type CheckoutExit = 'page_exit' | 'back' | 'close' | 'host_link' +/** The ending screens of the full-page checkout. */ +export type CheckoutEndingKind = + | 'success' + | 'completed' + | 'already_completed' + | 'in_progress' + | 'received' + | 'unconfirmed' + | 'refused' + | 'plan_unavailable' + | 'link_invalid' + | 'load_failed' +/** Whose payment an ending shows: this page's Pay, a return, one it followed, or one already settled. */ +export type CheckoutEndingAttribution = + | 'started' + | 'returned' + | 'followed' + | 'settled' + +/** The customer left before the checkout reached an ending. */ +type CheckoutJourneyAbandoned = { + phase: 'abandoned' + last_phase: CheckoutJourneyProgressEvent['phase'] + exit: CheckoutExit +} +type CheckoutJourneyEnded = { + phase: 'ended' + ending_kind: CheckoutEndingKind + attribution?: CheckoutEndingAttribution +} + +export type CheckoutJourneyPhaseEvent = + | CheckoutJourneyProgressEvent + | CheckoutJourneyAbandoned + | CheckoutJourneyEnded + +export type CheckoutJourneyPhase = CheckoutJourneyPhaseEvent['phase'] export type CheckoutJourneyTelemetryEvent = CheckoutJourneyContext & CheckoutJourneyPhaseEvent @@ -167,7 +205,9 @@ export const CHECKOUT_JOURNEY_EVENT_NAME_BY_PHASE: Record< promo: 'billing.checkout.promo', pay_blocked: 'billing.checkout.pay_blocked', submitted: 'billing.checkout.submitted', - operation_linked: 'billing.checkout.operation_linked' + operation_linked: 'billing.checkout.operation_linked', + abandoned: 'billing.checkout.abandoned', + ended: 'billing.checkout.ended' } export function getCheckoutJourneyTelemetryEventName( @@ -236,6 +276,16 @@ function getChoicePayload(event: CheckoutJourneyPhaseEvent) { } } +function getExitPayload(event: CheckoutJourneyPhaseEvent) { + return { + ...('last_phase' in event && { last_phase: event.last_phase }), + ...('exit' in event && { exit: event.exit }), + ...('ending_kind' in event && { ending_kind: event.ending_kind }), + ...('attribution' in event && + event.attribution !== undefined && { attribution: event.attribution }) + } +} + export function getCheckoutJourneyTelemetryEventPayload( event: CheckoutJourneyTelemetryEvent ) { @@ -243,7 +293,8 @@ export function getCheckoutJourneyTelemetryEventPayload( ...getContextPayload(event), ...getPreviewPayload(event), ...getPaymentFormPayload(event), - ...getChoicePayload(event) + ...getChoicePayload(event), + ...getExitPayload(event) } } diff --git a/packages/account-core/src/core/billing/telemetry/index.ts b/packages/account-core/src/core/billing/telemetry/index.ts index 0858e823e93..fa1e6952f82 100644 --- a/packages/account-core/src/core/billing/telemetry/index.ts +++ b/packages/account-core/src/core/billing/telemetry/index.ts @@ -40,10 +40,14 @@ export type { } from './webSessionEvent.js' export type { CheckoutAssignmentStatus, + CheckoutEndingAttribution, + CheckoutEndingKind, CheckoutEntryFlow, + CheckoutExit, CheckoutEntrySource, CheckoutJourneyArm, CheckoutJourneyContext, + CheckoutJourneyPhase, CheckoutJourneyPhaseEvent, CheckoutJourneyTelemetryEvent, CheckoutJourneyTelemetryEventName,