Line data Source code
1 : import type {
2 : CheckoutExit,
3 : CheckoutJourneyPhase,
4 : CheckoutJourneyPhaseEvent
5 : } from '@comfyorg/account-core/billing'
6 : import { useEventListener } from '@vueuse/core'
7 : import { onScopeDispose } from 'vue'
8 :
9 : import { useTelemetry } from '@comfyorg/telemetry'
10 : import {
11 : getActiveCheckoutJourney,
12 : toCheckoutJourneyContext
13 : } from '@/platform/billing/utils/checkoutJourney'
14 : import type { CheckoutJourneyRecord } from '@/platform/billing/utils/checkoutJourney'
15 :
16 : type CloudCheckoutExit = Extract<CheckoutExit, 'dialog_close' | 'page_exit'>
17 :
18 : /**
19 : * The last progress phase this tab reported on a journey, and whether an exit
20 : * or a handoff already accounts for it. Only new progress reopens it.
21 : */
22 2356 : let progress: {
23 : journeyId: string
24 : lastPhase: Exclude<CheckoutJourneyPhase, 'abandoned' | 'ended'>
25 : accounted: boolean
26 : } | null = null
27 :
28 4 : export function trackCheckoutJourneyPhase(
29 : record: CheckoutJourneyRecord,
30 : phase: CheckoutJourneyPhaseEvent
31 : ): void {
32 4 : if (phase.phase !== 'abandoned' && phase.phase !== 'ended') {
33 4 : progress = {
34 : journeyId: record.journey_id,
35 : lastPhase: phase.phase,
36 : accounted: false
37 : }
38 : }
39 4 : useTelemetry()?.trackCheckoutJourneyEvent({
40 : ...toCheckoutJourneyContext(record),
41 : ...phase
42 : })
43 : }
44 :
45 0 : function unaccountedProgress(journey: CheckoutJourneyRecord) {
46 0 : return progress?.journeyId === journey.journey_id && !progress.accounted
47 : ? progress
48 : : null
49 : }
50 :
51 : /** The journey continues on the hosted checkout, so closing this dialog is not an exit. */
52 0 : export function handOffCheckoutJourney(): void {
53 0 : const journey = getActiveCheckoutJourney()
54 0 : const open = journey && unaccountedProgress(journey)
55 0 : if (open) open.accounted = true
56 : }
57 :
58 : /**
59 : * Where the customer left a journey no operation is bound to. An observation,
60 : * not a verdict: once an operation exists its own events own the end.
61 : */
62 0 : export function reportCheckoutJourneyExit(exit: CloudCheckoutExit): void {
63 0 : const journey = getActiveCheckoutJourney()
64 0 : if (!journey || journey.billing_op_id !== undefined) return
65 0 : const open = unaccountedProgress(journey)
66 0 : if (!open) return
67 0 : open.accounted = true
68 0 : trackCheckoutJourneyPhase(journey, {
69 : phase: 'abandoned',
70 : last_phase: open.lastPhase,
71 : exit
72 : })
73 : }
74 :
75 : /** For a checkout dialog: its close, or the page going away while it is open, is an exit. */
76 4 : export function useCheckoutJourneyExit(): void {
77 4 : useEventListener(window, 'pagehide', () =>
78 : reportCheckoutJourneyExit('page_exit')
79 : )
80 4 : onScopeDispose(() => reportCheckoutJourneyExit('dialog_close'))
81 : }
|