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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 28 additions & 5 deletions apps/billing-web/src/session/unifiedBillingSession.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import type { User } from 'firebase/auth'

import { createWebSessionBillingClient } from '@/session/billingWebClient'
import { createUnifiedBillingSession } from '@/session/unifiedBillingSession'
import { billingWebTelemetry } from '@/telemetry/billingWebTelemetry'

const API = 'https://testcloud.comfy.org/api'

Expand Down Expand Up @@ -66,6 +67,20 @@ const TEAM = new Response(
})
)

function trackedWebSessionEvents() {
const track = vi
.spyOn(billingWebTelemetry, 'trackWebSessionEvent')
.mockImplementation(() => undefined)
return () => track.mock.calls.map(([event]) => event)
}

function bootstrapEvent(outcome: string) {
return {
name: 'session_bootstrap',
properties: { outcome, origin: globalThis.location.origin }
}
}

function setup({
state = LIVE,
remembered = null as User | null,
Expand Down Expand Up @@ -104,6 +119,7 @@ function setup({

describe('billing-web on the shared web session', () => {
it('signs in from a live session without touching Firebase (SS1)', async () => {
const tracked = trackedWebSessionEvents()
const { session, firebase, sent, paths } = setup({
workspaceId: 'ws-team'
})
Expand All @@ -112,6 +128,7 @@ describe('billing-web on the shared web session', () => {
await expect(session.signInPort.loadIdentity()).resolves.toBeUndefined()

expect(firebase.loadFirebase).not.toHaveBeenCalled()
expect(tracked()).toEqual([bootstrapEvent('signed_in')])
expect(paths()).toEqual([
'GET /api/auth/session',
'GET /api/workspaces/current'
Expand Down Expand Up @@ -145,29 +162,34 @@ describe('billing-web on the shared web session', () => {
name: 'no session, nothing remembered: sign-in',
state: { kind: 'dead', code: 'no_session' },
remembered: null,
phase: 'signed-out'
phase: 'signed-out',
outcome: 'signed_out'
},
{
name: 'expired session, nothing remembered: sign-in',
state: { kind: 'dead', code: 'session_expired' },
remembered: null,
phase: 'signed-out'
phase: 'signed-out',
outcome: 'signed_out'
},
{
name: 'no session, a remembered login: silent restore',
state: { kind: 'dead', code: 'no_session' },
remembered: 'user-1',
phase: 'authenticated'
phase: 'authenticated',
outcome: 'restored'
},
{
name: 'revoked session: sign-in',
state: { kind: 'dead', code: 'session_revoked' },
remembered: 'user-1',
phase: 'signed-out'
phase: 'signed-out',
outcome: 'revoked'
}
] as const)(
'$name, consulting Firebase only then',
async ({ state, remembered, phase }) => {
async ({ state, remembered, phase, outcome }) => {
const tracked = trackedWebSessionEvents()
const { session, firebase } = setup({
state,
remembered: remembered === null ? null : firebaseUser(remembered)
Expand All @@ -176,6 +198,7 @@ describe('billing-web on the shared web session', () => {
await expect(session.settledPhase()).resolves.toBe(phase)

expect(firebase.loadFirebase).toHaveBeenCalled()
expect(tracked()).toEqual([bootstrapEvent(outcome)])
}
)

Expand Down
5 changes: 4 additions & 1 deletion apps/billing-web/src/session/unifiedBillingSession.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import type {
} from '@comfyorg/account-core/billing'
import type { FirebaseIdentity } from '@comfyorg/account-core/firebase'
import { createRequestAuthorizer } from '@comfyorg/account-core/requestAuth'
import { webSessionTelemetryHooks } from '@comfyorg/account-core/telemetry'
import type { SessionErrorCode } from '@comfyorg/account-core/session'
import type {
WebSession,
Expand All @@ -37,6 +38,7 @@ import type {
SignInPort
} from '@/auth/useSignInController'
import type { BillingWebSessionPhase } from '@/router'
import { billingWebTelemetry } from '@/telemetry/billingWebTelemetry'

export interface UnifiedBillingSessionDeps {
/** Ingest API root, e.g. `https://cloud.comfy.org/api`. */
Expand Down Expand Up @@ -112,7 +114,8 @@ export function createUnifiedBillingSession(deps: UnifiedBillingSessionDeps) {
(await (await firebaseUser())?.getIdToken()) ?? null,
signOutLocally: async () => (await deps.loadFirebase())?.signOut()
}
}
},
...webSessionTelemetryHooks(billingWebTelemetry.trackWebSessionEvent)
})
const authorize = createRequestAuthorizer({
getWorkspaceToken: () =>
Expand Down
34 changes: 34 additions & 0 deletions apps/billing-web/src/telemetry/billingWebTelemetry.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,40 @@ describe('trackCheckoutJourneyEvent', () => {
})
})

describe('trackWebSessionEvent', () => {
it.for([
{
name: 'session_bootstrap',
properties: { outcome: 'restored', origin: 'https://billing.comfy.org' }
},
{
name: 'session_signed_out_remotely',
properties: { origin: 'https://billing.comfy.org' }
}
] as const)('sends $name to RUM and PostHog as is', async (event) => {
vi.mocked(datadogRum.getInitConfiguration).mockReturnValue({
clientToken: 'pub',
applicationId: 'app'
})
const telemetry = createBillingWebTelemetry()
await telemetry.startPostHog({
config: Promise.resolve(CONFIGURED),
identity: ref<SessionIdentity>({ kind: 'unknown' })
})

telemetry.trackWebSessionEvent(event)

expect(datadogRum.addAction).toHaveBeenCalledExactlyOnceWith(
event.name,
event.properties
)
expect(posthog.capture).toHaveBeenCalledExactlyOnceWith(
event.name,
event.properties
)
})
})

describe('startPostHog', () => {
it('turns off every PostHog capture that would carry page text or a full URL', async () => {
const telemetry = createBillingWebTelemetry()
Expand Down
15 changes: 13 additions & 2 deletions apps/billing-web/src/telemetry/billingWebTelemetry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
getCheckoutJourneyTelemetryEventPayload
} from '@comfyorg/account-core/billing'
import type { CloudTelemetryConfig } from '@comfyorg/account-core/firebase'
import type { WebSessionTelemetryEvent } from '@comfyorg/account-core/telemetry'
import {
createPostHogBeforeSend,
createPostHogUrlQueryScrub
Expand Down Expand Up @@ -43,7 +44,10 @@ type PostHogClient = Pick<
>

interface BillingEvent {
readonly name: BillingTelemetryEventName | CheckoutJourneyTelemetryEventName
readonly name:
| BillingTelemetryEventName
| CheckoutJourneyTelemetryEventName
| WebSessionTelemetryEvent['name']
readonly properties: Readonly<Record<string, unknown>>
}

Expand Down Expand Up @@ -222,6 +226,12 @@ export function createBillingWebTelemetry() {
)
}

function trackWebSessionEvent(event: WebSessionTelemetryEvent): void {
attempt(() =>
send({ name: event.name, properties: { ...event.properties } })
)
}

/** The RUM user is the opaque id of the signed-in user, nothing else. */
function startRumUser(identity: WatchSource<SessionIdentity>): void {
syncIdentity(RUM_USER, identity)
Expand All @@ -231,7 +241,8 @@ export function createBillingWebTelemetry() {
startPostHog,
startRumUser,
trackBillingEvent,
trackCheckoutJourneyEvent
trackCheckoutJourneyEvent,
trackWebSessionEvent
}
}

Expand Down
32 changes: 28 additions & 4 deletions apps/website/src/config/workshop-web-session-identity.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,13 @@ function stubCloud(...sessionAnswers: Answer[]) {
return sessionRequests
}

function bootstrapEvent(outcome: string) {
return {
name: 'session_bootstrap',
properties: { outcome, origin: window.location.origin }
}
}

async function loadModules(firebaseUid: string | undefined) {
const firebase = await import('./workshop-firebase')
const firebaseSubscribe = vi.mocked(firebase.workshopIdentity.onUserChanged)
Expand All @@ -64,9 +71,11 @@ async function loadModules(firebaseUid: string | undefined) {
return () => {}
})
const { workshopIdentity } = await import('./workshop-account')
const { captureWebSessionEvent } = await import('../scripts/posthog')
await import('@comfyorg/account-core/requestAuth')
return {
signOutWorkshop: vi.mocked(firebase.signOutWorkshop),
captureWebSessionEvent: vi.mocked(captureWebSessionEvent),
firebaseSubscribe,
loadFirebase: () => workshopIdentity.activate(),
...(await import('./workshop-web-session-identity')),
Expand Down Expand Up @@ -95,6 +104,9 @@ describe('resolveWorkshopAccountSource with the flag on', () => {
await vi.waitFor(() =>
expect(modules.signOutWorkshop).toHaveBeenCalledTimes(signOuts)
)
expect(modules.captureWebSessionEvent).toHaveBeenCalledExactlyOnceWith(
bootstrapEvent('signed_in')
)
}
)

Expand All @@ -109,8 +121,11 @@ describe('resolveWorkshopAccountSource with the flag on', () => {

it('does not restore without a loaded Firebase login, and falls back to it', async () => {
const sessionRequests = stubCloud(NO_SESSION)
const { resolveWorkshopAccountSource, ACCOUNT_SOURCE_CAP_MS } =
await loadModules('uid-1')
const {
resolveWorkshopAccountSource,
ACCOUNT_SOURCE_CAP_MS,
captureWebSessionEvent
} = await loadModules('uid-1')
vi.useFakeTimers({ shouldAdvanceTime: false })

const source = resolveWorkshopAccountSource()
Expand All @@ -121,6 +136,9 @@ describe('resolveWorkshopAccountSource with the flag on', () => {
'the signed-out answer decides, not the cap'
).toBe('firebase')
expect(sessionRequests.map(({ method }) => method)).toEqual(['GET'])
expect(captureWebSessionEvent).toHaveBeenCalledExactlyOnceWith(
bootstrapEvent('signed_out')
)
})

it('restores the session from a loaded Firebase login', async () => {
Expand All @@ -129,8 +147,11 @@ describe('resolveWorkshopAccountSource with the flag on', () => {
{ status: 200, body: { success: true } },
liveSession('uid-1')
)
const { resolveWorkshopAccountSource, loadFirebase } =
await loadModules('uid-1')
const {
resolveWorkshopAccountSource,
loadFirebase,
captureWebSessionEvent
} = await loadModules('uid-1')
await loadFirebase()

expect(await resolveWorkshopAccountSource()).toBe('session')
Expand All @@ -142,5 +163,8 @@ describe('resolveWorkshopAccountSource with the flag on', () => {
expect(sessionRequests[1].headers).toEqual({
Authorization: 'Bearer id-token'
})
expect(captureWebSessionEvent).toHaveBeenCalledExactlyOnceWith(
bootstrapEvent('restored')
)
})
})
5 changes: 4 additions & 1 deletion apps/website/src/config/workshop-web-session-identity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ import type {
WebSession,
WebSessionUser
} from '@comfyorg/account-core/webSession'
import { webSessionTelemetryHooks } from '@comfyorg/account-core/telemetry'
import type { RememberedLogin } from '@comfyorg/account-core/webSessionIdentity'
import { createWebSessionIdentity } from '@comfyorg/account-core/webSessionIdentity'

import { captureWebSessionEvent } from '../scripts/posthog'
import { workshopIdentity } from './workshop-account'
import { WORKSHOP_CLOUD_BASE_URL } from './workshop-env'
import type { WorkshopAccountSource } from './workshop-account-source'
Expand Down Expand Up @@ -64,7 +66,8 @@ export function bootWorkshopWebSession(
globalThis.fetch(...args)
},
principal: { kind: 'account', rememberedLogin: rememberedWorkshopLogin },
origin: window.location.origin
origin: window.location.origin,
...webSessionTelemetryHooks(captureWebSessionEvent)
})
identity.subscribe((state) => {
if (state.phase === 'signed_in') {
Expand Down
9 changes: 6 additions & 3 deletions apps/website/src/scripts/__mocks__/posthog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ const posthog: typeof realPosthog = {
captureAuthRefreshFailed: vi.fn(),
captureSignupOpened: vi.fn(),
captureAuthCompleted: vi.fn(),
captureAuthFailed: vi.fn()
captureAuthFailed: vi.fn(),
captureWebSessionEvent: vi.fn()
}

const {
Expand Down Expand Up @@ -61,7 +62,8 @@ const {
captureAuthRefreshFailed,
captureSignupOpened,
captureAuthCompleted,
captureAuthFailed
captureAuthFailed,
captureWebSessionEvent
} = posthog

export {
Expand Down Expand Up @@ -89,5 +91,6 @@ export {
captureAuthRefreshFailed,
captureSignupOpened,
captureAuthCompleted,
captureAuthFailed
captureAuthFailed,
captureWebSessionEvent
}
21 changes: 21 additions & 0 deletions apps/website/src/scripts/posthog.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -966,6 +966,27 @@ describe('shared auth telemetry events', () => {
)
})

it.for([
{
name: SESSION_TELEMETRY_EVENT.bootstrap,
properties: { outcome: 'signed_in', origin: 'https://www.comfy.org' }
},
{
name: SESSION_TELEMETRY_EVENT.signedOutRemotely,
properties: { origin: 'https://www.comfy.org' }
}
] as const)('reports the web session event $name as is', async (event) => {
const { initPostHog, captureWebSessionEvent } = await import('./posthog')
initPostHog()

captureWebSessionEvent(event)

expect(hoisted.mockCapture).toHaveBeenCalledWith(
event.name,
event.properties
)
})

it("reports sign-up opens and auth failures under the cloud app's event names", async () => {
const {
initPostHog,
Expand Down
8 changes: 7 additions & 1 deletion apps/website/src/scripts/posthog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ import {
} from '@comfyorg/account-core/telemetry'
import type {
AuthCompletedMetadata,
AuthErrorMetadata
AuthErrorMetadata,
WebSessionTelemetryEvent
} from '@comfyorg/account-core/telemetry'
import { createPostHogBeforeSend } from '@comfyorg/shared-frontend-utils/piiUtil'
import { normalizeTurnstileMode } from '@comfyorg/account-core/turnstile'
Expand Down Expand Up @@ -67,6 +68,7 @@ export type CliClientId =
export type RouterRoadmapCardId = 'workflow' | 'strategy' | 'use-case' | 'byok'

type AnalyticsEvent =
| WebSessionTelemetryEvent
| {
name: `website:workshop_${WorkshopAnalyticsEvent['name']}`
properties: WorkshopAnalyticsEvent['properties']
Expand Down Expand Up @@ -528,6 +530,10 @@ export function captureAuthRefreshFailed(outcome: SessionRefreshOutcome): void {
})
}

export function captureWebSessionEvent(event: WebSessionTelemetryEvent): void {
captureEvent(event)
}

export function captureSignupOpened(): void {
captureEvent({ name: ANALYTICS_EVENT.signUpOpened })
}
Expand Down
Loading
Loading