From d32efc79e7c3a87cbe0d68783cd2393b2f1ba164 Mon Sep 17 00:00:00 2001 From: devinwalker Date: Fri, 16 Oct 2026 13:15:26 -0700 Subject: [PATCH 1/4] My Jetpack: report what happens in the onboarding wizard Tracks events for the wizard funnel, sent only while the flag is on. Every event carries the run's `flow_id` and the step's slug. What someone types about their own site never leaves the browser; only a length bucket is sent. `apply_result` counts what the run changed, a run records one exit, and the run id works on plain-HTTP wp-admin. Co-authored-by: Adam Pickering --- .../wizard/steps/start-step.tsx | 15 +- .../onboarding-screen/wizard/telemetry.ts | 154 +++++++++++++++++ .../wizard/test/smoke.test.tsx | 161 +++++++++++++++++- .../wizard/test/telemetry.test.ts | 92 ++++++++++ .../onboarding-screen/wizard/wizard.tsx | 75 ++++++-- .../add-my-jetpack-onboarding-telemetry | 5 + 6 files changed, 477 insertions(+), 25 deletions(-) create mode 100644 projects/packages/my-jetpack/_inc/components/onboarding-screen/wizard/telemetry.ts create mode 100644 projects/packages/my-jetpack/_inc/components/onboarding-screen/wizard/test/telemetry.test.ts create mode 100644 projects/packages/my-jetpack/changelog/add-my-jetpack-onboarding-telemetry diff --git a/projects/packages/my-jetpack/_inc/components/onboarding-screen/wizard/steps/start-step.tsx b/projects/packages/my-jetpack/_inc/components/onboarding-screen/wizard/steps/start-step.tsx index 94bf07933a78..685620099cb2 100644 --- a/projects/packages/my-jetpack/_inc/components/onboarding-screen/wizard/steps/start-step.tsx +++ b/projects/packages/my-jetpack/_inc/components/onboarding-screen/wizard/steps/start-step.tsx @@ -5,7 +5,6 @@ import { ThemeProvider } from '@wordpress/theme'; import { Button, Icon, Notice, Text } from '@wordpress/ui'; import clsx from 'clsx'; import { useCallback, useRef, useState } from 'react'; -import useAnalytics from '../../../../hooks/use-analytics'; import { CONNECTION_FROM, CONNECTION_RETURN_URL, @@ -14,6 +13,7 @@ import { startBenefits, } from '../lib'; import styles from '../styles.module.scss'; +import { WIZARD_EVENTS, useWizardTracks } from '../telemetry'; import { markConnecting } from '../use-just-connected'; import { useRestoredConnection } from '../use-restored-connection'; @@ -45,7 +45,9 @@ export function StartStep( { titleId, title, description }: StartStepProps ) { redirectUri: CONNECTION_RETURN_URL, skipPricingPage: true, } ); - const { recordEvent } = useAnalytics(); + // The same bundle the rest of the flow carries, so the connection is part of + // the funnel rather than two loose events beside it. + const track = useWizardTracks( 'start' ); // Only registration failures reach the store. Fetching the authorization URL can // fail too, and that rejection is ours to hold or the screen says nothing. @@ -82,8 +84,9 @@ export function StartStep( { titleId, title, description }: StartStepProps ) { // return to knows to say the connection worked. markConnecting(); - // The name the single-screen flow already reports, so the two are one funnel. - recordEvent( 'jetpack_my_jetpack_onboarding_click', { flow: 'wizard' } ); + // `flow` is what tells this run from the single-screen flow, which reports + // the click under the same name. + track( WIZARD_EVENTS.connectClick, { flow: 'wizard' } ); // No success event: this resolves as the browser leaves for WordPress.com, // which is not the same thing as anyone having connected. @@ -92,11 +95,11 @@ export function StartStep( { titleId, title, description }: StartStepProps ) { setHandoffError( caught ); // The code only: the message interpolates the server's prose, which // can carry the site's own URL. - recordEvent( 'jetpack_my_jetpack_onboarding_connect_error', { + track( WIZARD_EVENTS.connectError, { error_code: connectionErrorCode( caught ), } ); } ); - }, [ handleRegisterSite, recordEvent ] ); + }, [ handleRegisterSite, track ] ); return (
diff --git a/projects/packages/my-jetpack/_inc/components/onboarding-screen/wizard/telemetry.ts b/projects/packages/my-jetpack/_inc/components/onboarding-screen/wizard/telemetry.ts new file mode 100644 index 000000000000..fe8108abedd6 --- /dev/null +++ b/projects/packages/my-jetpack/_inc/components/onboarding-screen/wizard/telemetry.ts @@ -0,0 +1,154 @@ +import { useCallback, useMemo } from 'react'; +import useAnalytics from '../../../hooks/use-analytics'; +import type { WizardStep } from './lib'; + +/* + * Every name here has to match Tracks' own + * `Jetpack_Tracks_Event::EVENT_NAME_REGEX`, `/^(([a-z0-9]+)_){2}([a-z0-9_]+)$/` + * — two lowercase segments, then the rest. A name that misses it is dropped + * server side with nothing said, so they are written out in full rather than + * assembled from parts a search could not find. + */ +export const WIZARD_EVENTS = { + stepView: 'jetpack_my_jetpack_onboarding_wizard_step_view', + siteTypeSelect: 'jetpack_my_jetpack_onboarding_wizard_site_type_select', + siteTypeDetail: 'jetpack_my_jetpack_onboarding_wizard_site_type_detail', + moduleToggle: 'jetpack_my_jetpack_onboarding_wizard_module_toggle', + stepComplete: 'jetpack_my_jetpack_onboarding_wizard_step_complete', + applyResult: 'jetpack_my_jetpack_onboarding_wizard_apply_result', + complete: 'jetpack_my_jetpack_onboarding_wizard_complete', + skip: 'jetpack_my_jetpack_onboarding_wizard_skip', + // Shared with the single-screen flow, which `flow` tells apart. + connectClick: 'jetpack_my_jetpack_onboarding_click', + // The wizard's own until the connection package records registration failures (CONNECT-502). + connectError: 'jetpack_my_jetpack_onboarding_connect_error', +} as const; + +const FLOW_ID_KEY = 'jetpack-onboarding-flow-id'; + +/** + * An id for one run through the wizard. + * + * Session storage, because connecting leaves wp-admin for WordPress.com and + * comes back to a fresh page: without this the two halves of a run are two + * unrelated sets of events and the funnel cannot be joined up. + * + * @return The id, or 'none' where storage cannot be reached. + */ +function flowId(): string { + try { + const held = window.sessionStorage.getItem( FLOW_ID_KEY ); + + if ( held ) { + return held; + } + + const made = makeId(); + window.sessionStorage.setItem( FLOW_ID_KEY, made ); + + return made; + } catch { + // Private browsing and blocked site data throw. A run with no id still reports; + // it just cannot be stitched to its other half, which beats reporting nothing. + return 'none'; + } +} + +/** + * A random id. `randomUUID()` exists only in secure contexts, and wp-admin can be plain HTTP. + * + * @return The id. + */ +function makeId(): string { + if ( typeof window.crypto.randomUUID === 'function' ) { + return window.crypto.randomUUID(); + } + + return Array.from( window.crypto.getRandomValues( new Uint8Array( 16 ) ), byte => + byte.toString( 16 ).padStart( 2, '0' ) + ).join( '' ); +} + +/** + * End this run's id, so the next run in the same tab gets its own. + */ +export function forgetFlowId(): void { + try { + window.sessionStorage.removeItem( FLOW_ID_KEY ); + } catch { + // Nothing was stored where storage is blocked. + } +} + +/** + * How long the free text was, in buckets. + * + * The words themselves never leave the browser. What someone types about their + * own site is theirs, it is the one field here that could carry a name or an + * address, and no question we have needs the string to answer it — whether the + * field gets used at all, and roughly how much people write in it, does. + * + * @param text - What was typed. + * @return A bucket name, never the text. + */ +export function lengthBucket( text: string ): string { + const length = text.trim().length; + + if ( length === 0 ) { + return 'empty'; + } + + if ( length <= 20 ) { + return 'short'; + } + + return length <= 60 ? 'medium' : 'long'; +} + +type WizardEvent = ( typeof WIZARD_EVENTS )[ keyof typeof WIZARD_EVENTS ]; +type EventProperties = Record< string, string | number | boolean >; + +/** + * Reports one run through the wizard. + * + * Every event carries the run's id and the step it happened on, so the funnel + * is one query rather than a join across event names. The step is its slug and + * never its index: a connected site starts at the second step, so the index of + * a given screen is not the same number for everyone. + * + * Nothing here fires on a site that has neither agreed to the terms nor + * connected a user. Tracks' own script is not on the page until one of those is + * true (`Tracking::should_enable_tracking`), so the start screen's events are + * queued into an array nothing drains. That is the gate working, not a bug, and + * it is why the funnel starts where it does. + * + * @param stepId - The slug of the step being reported on. + * @return A recorder bound to this step. + */ +export function useWizardTracks( stepId: string ) { + const { recordEvent } = useAnalytics(); + + const flow = useMemo( () => flowId(), [] ); + + return useCallback( + ( event: WizardEvent, properties: EventProperties = {} ) => { + recordEvent( event, { + flow_id: flow, + step: stepId, + ...properties, + } ); + }, + [ recordEvent, flow, stepId ] + ); +} + +/** + * The slug of a step, for the `step` property. + * + * @param steps - The steps, in order. + * @param step - Which one. + * @return Its id, or 'unknown' for an index the steps do not have. + */ +export function stepSlug( steps: Array< { id: string } >, step: WizardStep ): string { + return steps[ step ]?.id ?? 'unknown'; +} diff --git a/projects/packages/my-jetpack/_inc/components/onboarding-screen/wizard/test/smoke.test.tsx b/projects/packages/my-jetpack/_inc/components/onboarding-screen/wizard/test/smoke.test.tsx index 81962d909a8f..a38b52a52fcc 100644 --- a/projects/packages/my-jetpack/_inc/components/onboarding-screen/wizard/test/smoke.test.tsx +++ b/projects/packages/my-jetpack/_inc/components/onboarding-screen/wizard/test/smoke.test.tsx @@ -313,11 +313,16 @@ describe( 'Wizard start screen', () => { expect( mockConnection.handleRegisterSite ).toHaveBeenCalledTimes( 1 ); // One name across both onboarding flows, told apart by the flow property. - expect( mockRecordEvent ).toHaveBeenCalledWith( 'jetpack_my_jetpack_onboarding_click', { - flow: 'wizard', - } ); - // And nothing on the way out: the hand-off resolving is not a connection. - expect( mockRecordEvent ).toHaveBeenCalledTimes( 1 ); + expect( mockRecordEvent ).toHaveBeenCalledWith( + 'jetpack_my_jetpack_onboarding_click', + expect.objectContaining( { flow: 'wizard', step: 'start' } ) + ); + // The whole list, so the hand-off resolving cannot add an event back: + // it is not the same thing as anyone having connected. + expect( mockRecordEvent.mock.calls.map( ( [ name ] ) => name ) ).toEqual( [ + 'jetpack_my_jetpack_onboarding_wizard_step_view', + 'jetpack_my_jetpack_onboarding_click', + ] ); // The browser is on its way to WordPress.com; the step does not move here. expect( heading() ).toHaveTextContent( 'Start with Jetpack for free' ); } ); @@ -374,7 +379,7 @@ describe( 'Wizard start screen', () => { await waitFor( () => expect( mockRecordEvent ).toHaveBeenCalledWith( 'jetpack_my_jetpack_onboarding_connect_error', - { error_code: 'site_inaccessible' } + expect.objectContaining( { error_code: 'site_inaccessible' } ) ) ); @@ -411,9 +416,10 @@ describe( 'Wizard start screen', () => { ).resolves.toBeInTheDocument(); // No message to show, so the code stands in for it. expect( screen.getByText( 'JsonParseError' ) ).toBeInTheDocument(); - expect( mockRecordEvent ).toHaveBeenCalledWith( 'jetpack_my_jetpack_onboarding_connect_error', { - error_code: 'JsonParseError', - } ); + expect( mockRecordEvent ).toHaveBeenCalledWith( + 'jetpack_my_jetpack_onboarding_connect_error', + expect.objectContaining( { error_code: 'JsonParseError' } ) + ); } ); it( 'names what Jetpack does, one free feature group per row', () => { @@ -1254,6 +1260,143 @@ describe( 'What the finish screen claims', () => { } ); } ); +describe( 'What the wizard reports', () => { + /* + * The one thing that must never leave the browser. Asserted over every call + * rather than over the one event that handles it, because the guarantee is + * about the whole flow and a future event could carry it by accident. + */ + it( 'never sends what the user typed about their own site', async () => { + const secret = 'Smith family wiki at smith-family.example'; + const { user } = setupWizard( { isUserConnected: true } ); + + await user.click( screen.getByRole( 'radio', { name: 'Something else…' } ) ); + await user.type( screen.getByRole( 'textbox' ), secret ); + await user.click( screen.getByRole( 'button', { name: 'Continue' } ) ); + + const sent = JSON.stringify( mockRecordEvent.mock.calls ); + + expect( sent ).not.toContain( 'Smith' ); + expect( sent ).not.toContain( 'example' ); + expect( sent ).toContain( 'length_bucket' ); + } ); + + it( 'reports the site type as a slug, and the run as one id', async () => { + const { user } = setupWizard( { isUserConnected: true } ); + + await user.click( screen.getByRole( 'radio', { name: 'An online store' } ) ); + + expect( mockRecordEvent ).toHaveBeenCalledWith( + 'jetpack_my_jetpack_onboarding_wizard_site_type_select', + expect.objectContaining( { site_type: 'store', step: 'site-type' } ) + ); + + // One id for the whole run, or the two halves of a connection cannot be joined. + const ids = new Set( mockRecordEvent.mock.calls.map( ( [ , props ] ) => props?.flow_id ) ); + + expect( ids.size ).toBe( 1 ); + } ); + + // The run is restored on reload, and a second report would double-count the field. + it( 'reports the typed answer once, even across a reload', async () => { + const { user, unmount } = setupWizard( { isUserConnected: true } ); + await user.click( screen.getByRole( 'radio', { name: 'Something else…' } ) ); + await user.type( screen.getByRole( 'textbox' ), 'A wiki' ); + await user.click( screen.getByRole( 'button', { name: 'Continue' } ) ); + + unmount(); + setupWizard( { isUserConnected: true } ); + + const detail = mockRecordEvent.mock.calls.filter( + ( [ name ] ) => name === 'jetpack_my_jetpack_onboarding_wizard_site_type_detail' + ); + expect( detail ).toHaveLength( 1 ); + } ); + + // Two of the four mock modules ship on; leaving them on is not switching them on. + it( 'counts only what the run switched on, and says when the request failed', async () => { + const { user } = setupWizard( { isUserConnected: true } ); + await user.click( screen.getByRole( 'radio', { name: 'A blog or publication' } ) ); + await user.click( screen.getByRole( 'button', { name: 'Continue' } ) ); + await user.click( screen.getByRole( 'checkbox', { name: 'Downtime Monitor' } ) ); + await user.click( screen.getByRole( 'button', { name: 'Continue' } ) ); + + await waitFor( () => + expect( mockRecordEvent ).toHaveBeenCalledWith( + 'jetpack_my_jetpack_onboarding_wizard_apply_result', + expect.objectContaining( { offered: 4, switched_on: 1, request_failed: false } ) + ) + ); + } ); + + it( 'tells a failed request apart from an empty list', async () => { + mockApply.mockRejectedValueOnce( new Error( 'nope' ) ); + const { user } = setupWizard( { isUserConnected: true } ); + await user.click( screen.getByRole( 'radio', { name: 'A blog or publication' } ) ); + await user.click( screen.getByRole( 'button', { name: 'Continue' } ) ); + await user.click( screen.getByRole( 'button', { name: 'Continue' } ) ); + + await waitFor( () => + expect( mockRecordEvent ).toHaveBeenCalledWith( + 'jetpack_my_jetpack_onboarding_wizard_apply_result', + expect.objectContaining( { offered: 4, request_failed: true } ) + ) + ); + } ); + + // A double click, or the finish link then the rail's, must not report and settle twice. + it( 'records one exit per run, and ends its id', async () => { + const { user } = setupWizard( { isUserConnected: true } ); + await advance( user, 2 ); + + await user.click( screen.getByRole( 'link', { name: 'Go to My Jetpack' } ) ); + await user.click( screen.getByRole( 'link', { name: 'Back to your WordPress site' } ) ); + + const exits = mockRecordEvent.mock.calls.filter( ( [ name ] ) => + /_wizard_(complete|skip)$/.test( name ) + ); + expect( exits ).toHaveLength( 1 ); + expect( mockApiFetch ).toHaveBeenCalledTimes( 1 ); + expect( window.sessionStorage.getItem( 'jetpack-onboarding-flow-id' ) ).toBeNull(); + } ); + + // randomUUID() exists only in secure contexts, and wp-admin can be plain HTTP. + it( 'still gives the run an id where randomUUID is missing', () => { + const randomUUID = window.crypto.randomUUID; + Object.defineProperty( window.crypto, 'randomUUID', { value: undefined, configurable: true } ); + + try { + setupWizard( { isUserConnected: true } ); + } finally { + Object.defineProperty( window.crypto, 'randomUUID', { + value: randomUUID, + configurable: true, + } ); + } + + expect( mockRecordEvent ).toHaveBeenCalledWith( + expect.any( String ), + expect.objectContaining( { flow_id: expect.stringMatching( /^[0-9a-f]{32}$/ ) } ) + ); + } ); + + // The field is left open by one option only, and the text survives switching + // away from it, so a run that typed and changed its mind used no field. + it( 'says nothing about the field when the answer was not the one that opens it', async () => { + const { user } = setupWizard( { isUserConnected: true } ); + + await user.click( screen.getByRole( 'radio', { name: 'Something else…' } ) ); + await user.type( screen.getByRole( 'textbox' ), 'A wiki' ); + await user.click( screen.getByRole( 'radio', { name: 'A blog or publication' } ) ); + await user.click( screen.getByRole( 'button', { name: 'Continue' } ) ); + + expect( mockRecordEvent ).not.toHaveBeenCalledWith( + 'jetpack_my_jetpack_onboarding_wizard_site_type_detail', + expect.anything() + ); + } ); +} ); + describe( 'The feature step', () => { const featureStep = async ( user: UserEvent ) => { await user.click( screen.getByRole( 'radio', { name: 'A blog or publication' } ) ); diff --git a/projects/packages/my-jetpack/_inc/components/onboarding-screen/wizard/test/telemetry.test.ts b/projects/packages/my-jetpack/_inc/components/onboarding-screen/wizard/test/telemetry.test.ts new file mode 100644 index 000000000000..f9ebcd48ba27 --- /dev/null +++ b/projects/packages/my-jetpack/_inc/components/onboarding-screen/wizard/test/telemetry.test.ts @@ -0,0 +1,92 @@ +import { WIZARD_EVENTS, lengthBucket, stepSlug } from '../telemetry'; +import type { WizardStep } from '../lib'; + +/* + * Tracks' own name rule, copied from Jetpack_Tracks_Event::EVENT_NAME_REGEX in + * packages/connection. An event that misses it is dropped server side and says + * nothing about it, so the names are checked here rather than in production. + */ +const EVENT_NAME = /^(([a-z0-9]+)_){2}([a-z0-9_]+)$/; + +// The same rule for property keys. One camelCase key costs the whole event. +const PROPERTY_KEY = /^[a-z_][a-z0-9_]*$/; + +describe( 'Wizard event names', () => { + it.each( Object.entries( WIZARD_EVENTS ) )( '%s is a name Tracks will take', ( _key, name ) => { + expect( name ).toMatch( EVENT_NAME ); + } ); + + it( 'has no two events sharing a name', () => { + const names = Object.values( WIZARD_EVENTS ); + + expect( new Set( names ).size ).toBe( names.length ); + } ); + + // Every property this wizard sends, listed here so the rule is checked once + // rather than trusted at each call site. + it( 'sends only property keys Tracks will take', () => { + const keys = [ + 'flow_id', + 'flow', + 'step', + 'site_type', + 'length_bucket', + 'module', + 'enabled', + 'offered', + 'switched_on', + 'failed', + 'request_failed', + 'error_code', + ]; + + keys.forEach( key => expect( key ).toMatch( PROPERTY_KEY ) ); + } ); +} ); + +describe( 'The free-text bucket', () => { + /* + * The one property that touches something a person wrote. If this ever + * returns the text, the wizard starts sending whatever someone typed about + * their own site to Tracks. + */ + it( 'never returns anything from the text', () => { + const typed = 'A wiki for the Smith family, kept at smith-family.example since 2019'; + + expect( lengthBucket( typed ) ).toBe( 'long' ); + expect( lengthBucket( typed ) ).not.toContain( 'Smith' ); + expect( lengthBucket( typed ) ).not.toContain( 'example' ); + } ); + + it( 'buckets by length and nothing else', () => { + expect( lengthBucket( '' ) ).toBe( 'empty' ); + expect( lengthBucket( ' ' ) ).toBe( 'empty' ); + expect( lengthBucket( 'A wiki' ) ).toBe( 'short' ); + expect( lengthBucket( 'a'.repeat( 20 ) ) ).toBe( 'short' ); + expect( lengthBucket( 'a'.repeat( 21 ) ) ).toBe( 'medium' ); + expect( lengthBucket( 'a'.repeat( 60 ) ) ).toBe( 'medium' ); + expect( lengthBucket( 'a'.repeat( 61 ) ) ).toBe( 'long' ); + } ); + + // Two people who wrote different things of the same length are the same row. + it( 'says the same thing about different text of the same length', () => { + expect( lengthBucket( 'my band site' ) ).toBe( lengthBucket( 'jane@mail.com' ) ); + } ); +} ); + +describe( 'The step property', () => { + const steps = [ { id: 'start' }, { id: 'site-type' }, { id: 'features' }, { id: 'done' } ]; + + /* + * Slugs, not indexes. A connected site opens on the second step, so an index + * is not the same number for everyone looking at the same screen. + */ + it( 'is the step’s own slug', () => { + expect( stepSlug( steps, 0 as WizardStep ) ).toBe( 'start' ); + expect( stepSlug( steps, 2 as WizardStep ) ).toBe( 'features' ); + } ); + + it( 'says unknown rather than throwing on a step that is not there', () => { + expect( stepSlug( steps, 9 as WizardStep ) ).toBe( 'unknown' ); + } ); +} ); diff --git a/projects/packages/my-jetpack/_inc/components/onboarding-screen/wizard/wizard.tsx b/projects/packages/my-jetpack/_inc/components/onboarding-screen/wizard/wizard.tsx index e4176fc45d27..f8007a0e4b53 100644 --- a/projects/packages/my-jetpack/_inc/components/onboarding-screen/wizard/wizard.tsx +++ b/projects/packages/my-jetpack/_inc/components/onboarding-screen/wizard/wizard.tsx @@ -23,6 +23,7 @@ import { clampStep, featuresDescription, openingStep, + SITE_TYPE_OTHER, siteTypeAnswer, settleOnboarding, TOTAL_STEPS, @@ -38,6 +39,7 @@ import { FeaturesStep } from './steps/features-step'; import { FinishStep } from './steps/finish-step'; import { StartStep } from './steps/start-step'; import styles from './styles.module.scss'; +import { WIZARD_EVENTS, forgetFlowId, lengthBucket, stepSlug, useWizardTracks } from './telemetry'; import { useConnectionKnown } from './use-connection-known'; import { useJustConnected } from './use-just-connected'; import { readSavedRun, useSavedRun } from './use-saved-run'; @@ -189,10 +191,16 @@ export function Wizard( { exitUrl, dashboardUrl }: WizardProps ) { ); const [ moduleResults, setModuleResults ] = useState< SetupModuleResult[] >( [] ); + // Bound to the step's slug, which is what every event carries; see telemetry.ts. + const slug = stepSlug( steps, step ); + const track = useWizardTracks( slug ); + const handleModuleChange = useCallback( - ( slug: string, want: boolean ) => - setWantedModules( current => ( { ...current, [ slug ]: want } ) ), - [] + ( moduleSlug: string, want: boolean ) => { + setWantedModules( current => ( { ...current, [ moduleSlug ]: want } ) ); + track( WIZARD_EVENTS.moduleToggle, { module: moduleSlug, enabled: want } ); + }, + [ track ] ); const justConnected = useJustConnected(); @@ -232,9 +240,18 @@ export function Wizard( { exitUrl, dashboardUrl }: WizardProps ) { } ); }, [ saveRun, step, furthestStep, choices, siteTypeDetail, wantedModules ] ); + // Keyed on the slug rather than the index, so the step a connected user opens on + // is reported once and the same screen is never two different numbers. + useEffect( () => { + track( WIZARD_EVENTS.stepView ); + }, [ track ] ); + const handleChoice = useCallback( - ( value: string ) => setChoices( current => ( { ...current, [ step ]: value } ) ), - [ step ] + ( value: string ) => { + setChoices( current => ( { ...current, [ step ]: value } ) ); + track( WIZARD_EVENTS.siteTypeSelect, { site_type: value } ); + }, + [ step, track ] ); /* @@ -271,6 +288,13 @@ export function Wizard( { exitUrl, dashboardUrl }: WizardProps ) { // The run is over, so it must not be waiting on the next visit. forgetRun(); + forgetFlowId(); + + // Reported before the navigation, not after: the page is about to go. + track( + outcome === 'skipped' ? WIZARD_EVENTS.skip : WIZARD_EVENTS.complete, + outcome === 'skipped' ? {} : { site_type: siteType ?? 'none' } + ); // Nothing settles before the site is connected, so leaving then is only leaving. if ( ! isRegistered ) { @@ -283,16 +307,27 @@ export function Wizard( { exitUrl, dashboardUrl }: WizardProps ) { () => assignLocation( isUserConnected ? destination : dashboardUrl ) ); }, - [ exitUrl, dashboardUrl, forgetRun, busy, isRegistered, isUserConnected ] + [ exitUrl, dashboardUrl, forgetRun, busy, isRegistered, isUserConnected, track, siteType ] ); const handleBack = useCallback( () => setStep( clampStep( step - 1 ) ), [ step ] ); const handleNext = useCallback( () => { + track( WIZARD_EVENTS.stepComplete ); + + /* + * On Continue rather than from an effect, so restoring a run on reload cannot send + * it again. Never the words, which could carry a name or an address: a length + * bucket answers whether the field gets used. Continue needs text, so it always has some. + */ + if ( slug === 'site-type' && siteType === SITE_TYPE_OTHER ) { + track( WIZARD_EVENTS.siteTypeDetail, { length_bucket: lengthBucket( siteTypeDetail ) } ); + } + const next = clampStep( step + 1 ); setStep( next ); setFurthestStep( current => ( next > current ? next : current ) ); - }, [ step ] ); + }, [ step, track, slug, siteType, siteTypeDetail ] ); /* * Leaving the feature step is what switches the modules; the switches above only @@ -308,14 +343,34 @@ export function Wizard( { exitUrl, dashboardUrl }: WizardProps ) { panelRef.current?.focus(); apply( modules, wantedModules ) - .then( setModuleResults ) + .then( results => { + setModuleResults( results ); + // Counts, not a list: which module failed is its own toggle event, and + // a property holding a list cannot be grouped on. + track( WIZARD_EVENTS.applyResult, { + offered: results.length, + // Only what this run switched on: an untouched row that was already on is not one. + switched_on: results.filter( result => result.changed && result.wanted && result.ok ) + .length, + failed: results.filter( result => ! result.ok ).length, + request_failed: false, + } ); + } ) /* * apply() reports each switch's failure itself and does not reject. This only * keeps an unexpected throw from leaving the step on a spinner for ever. */ - .catch( () => setModuleResults( [] ) ) + .catch( () => { + setModuleResults( [] ); + track( WIZARD_EVENTS.applyResult, { + offered: modules.length, + switched_on: 0, + failed: 0, + request_failed: true, + } ); + } ) .finally( handleNext ); - }, [ apply, modules, wantedModules, handleNext ] ); + }, [ apply, modules, wantedModules, handleNext, track ] ); // The rail's rows are aria-disabled rather than disabled, so they stay focusable // and keep announcing themselves. That makes swallowing the click our job. diff --git a/projects/packages/my-jetpack/changelog/add-my-jetpack-onboarding-telemetry b/projects/packages/my-jetpack/changelog/add-my-jetpack-onboarding-telemetry new file mode 100644 index 000000000000..33f790260958 --- /dev/null +++ b/projects/packages/my-jetpack/changelog/add-my-jetpack-onboarding-telemetry @@ -0,0 +1,5 @@ +Significance: patch +Type: changed +Comment: Onboarding wizard telemetry; the wizard is behind an off feature flag. + + From 0964b740b7126b6b1d7b75799b54b8d9d02ba96f Mon Sep 17 00:00:00 2001 From: devinwalker Date: Fri, 16 Oct 2026 14:05:25 -0700 Subject: [PATCH 2/4] My Jetpack: simplify the setup wizard tracking - The step property reads the step's own id, so `stepSlug()` and its tests go. - `apply_result` counts with the shared `tallyResults()`. --- .../onboarding-screen/wizard/telemetry.ts | 12 ----------- .../wizard/test/telemetry.test.ts | 20 +------------------ .../onboarding-screen/wizard/wizard.tsx | 14 ++++++------- 3 files changed, 8 insertions(+), 38 deletions(-) diff --git a/projects/packages/my-jetpack/_inc/components/onboarding-screen/wizard/telemetry.ts b/projects/packages/my-jetpack/_inc/components/onboarding-screen/wizard/telemetry.ts index fe8108abedd6..a6105a2c6505 100644 --- a/projects/packages/my-jetpack/_inc/components/onboarding-screen/wizard/telemetry.ts +++ b/projects/packages/my-jetpack/_inc/components/onboarding-screen/wizard/telemetry.ts @@ -1,6 +1,5 @@ import { useCallback, useMemo } from 'react'; import useAnalytics from '../../../hooks/use-analytics'; -import type { WizardStep } from './lib'; /* * Every name here has to match Tracks' own @@ -141,14 +140,3 @@ export function useWizardTracks( stepId: string ) { [ recordEvent, flow, stepId ] ); } - -/** - * The slug of a step, for the `step` property. - * - * @param steps - The steps, in order. - * @param step - Which one. - * @return Its id, or 'unknown' for an index the steps do not have. - */ -export function stepSlug( steps: Array< { id: string } >, step: WizardStep ): string { - return steps[ step ]?.id ?? 'unknown'; -} diff --git a/projects/packages/my-jetpack/_inc/components/onboarding-screen/wizard/test/telemetry.test.ts b/projects/packages/my-jetpack/_inc/components/onboarding-screen/wizard/test/telemetry.test.ts index f9ebcd48ba27..11ac6df004b1 100644 --- a/projects/packages/my-jetpack/_inc/components/onboarding-screen/wizard/test/telemetry.test.ts +++ b/projects/packages/my-jetpack/_inc/components/onboarding-screen/wizard/test/telemetry.test.ts @@ -1,5 +1,4 @@ -import { WIZARD_EVENTS, lengthBucket, stepSlug } from '../telemetry'; -import type { WizardStep } from '../lib'; +import { WIZARD_EVENTS, lengthBucket } from '../telemetry'; /* * Tracks' own name rule, copied from Jetpack_Tracks_Event::EVENT_NAME_REGEX in @@ -73,20 +72,3 @@ describe( 'The free-text bucket', () => { expect( lengthBucket( 'my band site' ) ).toBe( lengthBucket( 'jane@mail.com' ) ); } ); } ); - -describe( 'The step property', () => { - const steps = [ { id: 'start' }, { id: 'site-type' }, { id: 'features' }, { id: 'done' } ]; - - /* - * Slugs, not indexes. A connected site opens on the second step, so an index - * is not the same number for everyone looking at the same screen. - */ - it( 'is the step’s own slug', () => { - expect( stepSlug( steps, 0 as WizardStep ) ).toBe( 'start' ); - expect( stepSlug( steps, 2 as WizardStep ) ).toBe( 'features' ); - } ); - - it( 'says unknown rather than throwing on a step that is not there', () => { - expect( stepSlug( steps, 9 as WizardStep ) ).toBe( 'unknown' ); - } ); -} ); diff --git a/projects/packages/my-jetpack/_inc/components/onboarding-screen/wizard/wizard.tsx b/projects/packages/my-jetpack/_inc/components/onboarding-screen/wizard/wizard.tsx index f8007a0e4b53..de5fcb0c2bf7 100644 --- a/projects/packages/my-jetpack/_inc/components/onboarding-screen/wizard/wizard.tsx +++ b/projects/packages/my-jetpack/_inc/components/onboarding-screen/wizard/wizard.tsx @@ -26,6 +26,7 @@ import { SITE_TYPE_OTHER, siteTypeAnswer, settleOnboarding, + tallyResults, TOTAL_STEPS, wizardSteps, type SettleOutcome, @@ -39,7 +40,7 @@ import { FeaturesStep } from './steps/features-step'; import { FinishStep } from './steps/finish-step'; import { StartStep } from './steps/start-step'; import styles from './styles.module.scss'; -import { WIZARD_EVENTS, forgetFlowId, lengthBucket, stepSlug, useWizardTracks } from './telemetry'; +import { WIZARD_EVENTS, forgetFlowId, lengthBucket, useWizardTracks } from './telemetry'; import { useConnectionKnown } from './use-connection-known'; import { useJustConnected } from './use-just-connected'; import { readSavedRun, useSavedRun } from './use-saved-run'; @@ -192,7 +193,8 @@ export function Wizard( { exitUrl, dashboardUrl }: WizardProps ) { const [ moduleResults, setModuleResults ] = useState< SetupModuleResult[] >( [] ); // Bound to the step's slug, which is what every event carries; see telemetry.ts. - const slug = stepSlug( steps, step ); + const meta = steps[ step ]; + const slug = meta.id; const track = useWizardTracks( slug ); const handleModuleChange = useCallback( @@ -347,12 +349,11 @@ export function Wizard( { exitUrl, dashboardUrl }: WizardProps ) { setModuleResults( results ); // Counts, not a list: which module failed is its own toggle event, and // a property holding a list cannot be grouped on. + const { on, failed } = tallyResults( results ); track( WIZARD_EVENTS.applyResult, { offered: results.length, - // Only what this run switched on: an untouched row that was already on is not one. - switched_on: results.filter( result => result.changed && result.wanted && result.ok ) - .length, - failed: results.filter( result => ! result.ok ).length, + switched_on: on, + failed, request_failed: false, } ); } ) @@ -384,7 +385,6 @@ export function Wizard( { exitUrl, dashboardUrl }: WizardProps ) { [ furthestStep, firstStep, busy ] ); - const meta = steps[ step ]; const isStart = meta.kind === 'start'; /* From ace81600e46f5ea627f7c1217634bfad7110d785 Mon Sep 17 00:00:00 2001 From: devinwalker Date: Fri, 16 Oct 2026 14:25:32 -0700 Subject: [PATCH 3/4] My Jetpack: drop the wizard's dead request_failed flag apply() never rejects, so request_failed could never be true. The property-key check now reads the keys a real run records instead of a hand-kept list, and the telemetry comments fit the comment budget. --- .../onboarding-screen/wizard/telemetry.ts | 30 +++++-------------- .../wizard/test/smoke.test.tsx | 25 +++++++++------- .../wizard/test/telemetry.test.ts | 30 +------------------ .../onboarding-screen/wizard/wizard.tsx | 14 ++------- 4 files changed, 24 insertions(+), 75 deletions(-) diff --git a/projects/packages/my-jetpack/_inc/components/onboarding-screen/wizard/telemetry.ts b/projects/packages/my-jetpack/_inc/components/onboarding-screen/wizard/telemetry.ts index a6105a2c6505..a654e93c3fc4 100644 --- a/projects/packages/my-jetpack/_inc/components/onboarding-screen/wizard/telemetry.ts +++ b/projects/packages/my-jetpack/_inc/components/onboarding-screen/wizard/telemetry.ts @@ -2,11 +2,8 @@ import { useCallback, useMemo } from 'react'; import useAnalytics from '../../../hooks/use-analytics'; /* - * Every name here has to match Tracks' own - * `Jetpack_Tracks_Event::EVENT_NAME_REGEX`, `/^(([a-z0-9]+)_){2}([a-z0-9_]+)$/` - * — two lowercase segments, then the rest. A name that misses it is dropped - * server side with nothing said, so they are written out in full rather than - * assembled from parts a search could not find. + * Tracks silently drops any name that misses `Jetpack_Tracks_Event::EVENT_NAME_REGEX`. + * Written out in full, not assembled, so a search finds each one. */ export const WIZARD_EVENTS = { stepView: 'jetpack_my_jetpack_onboarding_wizard_step_view', @@ -28,9 +25,7 @@ const FLOW_ID_KEY = 'jetpack-onboarding-flow-id'; /** * An id for one run through the wizard. * - * Session storage, because connecting leaves wp-admin for WordPress.com and - * comes back to a fresh page: without this the two halves of a run are two - * unrelated sets of events and the funnel cannot be joined up. + * In session storage, so a run that leaves for WordPress.com to connect keeps it. * * @return The id, or 'none' where storage cannot be reached. */ @@ -82,10 +77,7 @@ export function forgetFlowId(): void { /** * How long the free text was, in buckets. * - * The words themselves never leave the browser. What someone types about their - * own site is theirs, it is the one field here that could carry a name or an - * address, and no question we have needs the string to answer it — whether the - * field gets used at all, and roughly how much people write in it, does. + * The words never leave the browser: it is the one field that could carry a name or an address. * * @param text - What was typed. * @return A bucket name, never the text. @@ -108,18 +100,10 @@ type WizardEvent = ( typeof WIZARD_EVENTS )[ keyof typeof WIZARD_EVENTS ]; type EventProperties = Record< string, string | number | boolean >; /** - * Reports one run through the wizard. + * Reports one run through the wizard, tagging every event with the run's id and the step's slug. * - * Every event carries the run's id and the step it happened on, so the funnel - * is one query rather than a join across event names. The step is its slug and - * never its index: a connected site starts at the second step, so the index of - * a given screen is not the same number for everyone. - * - * Nothing here fires on a site that has neither agreed to the terms nor - * connected a user. Tracks' own script is not on the page until one of those is - * true (`Tracking::should_enable_tracking`), so the start screen's events are - * queued into an array nothing drains. That is the gate working, not a bug, and - * it is why the funnel starts where it does. + * The slug, not the index, because a connected site starts at the second step. + * Nothing is sent before the terms are agreed (`Tracking::should_enable_tracking`). * * @param stepId - The slug of the step being reported on. * @return A recorder bound to this step. diff --git a/projects/packages/my-jetpack/_inc/components/onboarding-screen/wizard/test/smoke.test.tsx b/projects/packages/my-jetpack/_inc/components/onboarding-screen/wizard/test/smoke.test.tsx index a38b52a52fcc..baaea58ed845 100644 --- a/projects/packages/my-jetpack/_inc/components/onboarding-screen/wizard/test/smoke.test.tsx +++ b/projects/packages/my-jetpack/_inc/components/onboarding-screen/wizard/test/smoke.test.tsx @@ -1314,7 +1314,7 @@ describe( 'What the wizard reports', () => { } ); // Two of the four mock modules ship on; leaving them on is not switching them on. - it( 'counts only what the run switched on, and says when the request failed', async () => { + it( 'counts only what the run switched on', async () => { const { user } = setupWizard( { isUserConnected: true } ); await user.click( screen.getByRole( 'radio', { name: 'A blog or publication' } ) ); await user.click( screen.getByRole( 'button', { name: 'Continue' } ) ); @@ -1324,27 +1324,30 @@ describe( 'What the wizard reports', () => { await waitFor( () => expect( mockRecordEvent ).toHaveBeenCalledWith( 'jetpack_my_jetpack_onboarding_wizard_apply_result', - expect.objectContaining( { offered: 4, switched_on: 1, request_failed: false } ) + expect.objectContaining( { offered: 4, switched_on: 1, failed: 0 } ) ) ); } ); - it( 'tells a failed request apart from an empty list', async () => { - mockApply.mockRejectedValueOnce( new Error( 'nope' ) ); + // Tracks drops an event whose property keys miss its rule, and says nothing. + it( 'sends only property keys Tracks will take', async () => { const { user } = setupWizard( { isUserConnected: true } ); - await user.click( screen.getByRole( 'radio', { name: 'A blog or publication' } ) ); + await user.click( screen.getByRole( 'radio', { name: 'Something else…' } ) ); + await user.type( screen.getByRole( 'textbox' ), 'A wiki' ); await user.click( screen.getByRole( 'button', { name: 'Continue' } ) ); + await user.click( screen.getByRole( 'checkbox', { name: 'Downtime Monitor' } ) ); await user.click( screen.getByRole( 'button', { name: 'Continue' } ) ); + await user.click( await screen.findByRole( 'link', { name: 'Go to My Jetpack' } ) ); - await waitFor( () => - expect( mockRecordEvent ).toHaveBeenCalledWith( - 'jetpack_my_jetpack_onboarding_wizard_apply_result', - expect.objectContaining( { offered: 4, request_failed: true } ) - ) + const keys = mockRecordEvent.mock.calls.flatMap( ( [ , props ] ) => Object.keys( props ) ); + + expect( keys ).toEqual( + expect.arrayContaining( [ 'length_bucket', 'module', 'switched_on' ] ) ); + keys.forEach( key => expect( key ).toMatch( /^[a-z_][a-z0-9_]*$/ ) ); } ); - // A double click, or the finish link then the rail's, must not report and settle twice. + // A double click, or both finish links, must not report and settle twice. it( 'records one exit per run, and ends its id', async () => { const { user } = setupWizard( { isUserConnected: true } ); await advance( user, 2 ); diff --git a/projects/packages/my-jetpack/_inc/components/onboarding-screen/wizard/test/telemetry.test.ts b/projects/packages/my-jetpack/_inc/components/onboarding-screen/wizard/test/telemetry.test.ts index 11ac6df004b1..e74e689396d8 100644 --- a/projects/packages/my-jetpack/_inc/components/onboarding-screen/wizard/test/telemetry.test.ts +++ b/projects/packages/my-jetpack/_inc/components/onboarding-screen/wizard/test/telemetry.test.ts @@ -1,15 +1,8 @@ import { WIZARD_EVENTS, lengthBucket } from '../telemetry'; -/* - * Tracks' own name rule, copied from Jetpack_Tracks_Event::EVENT_NAME_REGEX in - * packages/connection. An event that misses it is dropped server side and says - * nothing about it, so the names are checked here rather than in production. - */ +// Copied from Jetpack_Tracks_Event::EVENT_NAME_REGEX, which drops a miss without a word. const EVENT_NAME = /^(([a-z0-9]+)_){2}([a-z0-9_]+)$/; -// The same rule for property keys. One camelCase key costs the whole event. -const PROPERTY_KEY = /^[a-z_][a-z0-9_]*$/; - describe( 'Wizard event names', () => { it.each( Object.entries( WIZARD_EVENTS ) )( '%s is a name Tracks will take', ( _key, name ) => { expect( name ).toMatch( EVENT_NAME ); @@ -20,27 +13,6 @@ describe( 'Wizard event names', () => { expect( new Set( names ).size ).toBe( names.length ); } ); - - // Every property this wizard sends, listed here so the rule is checked once - // rather than trusted at each call site. - it( 'sends only property keys Tracks will take', () => { - const keys = [ - 'flow_id', - 'flow', - 'step', - 'site_type', - 'length_bucket', - 'module', - 'enabled', - 'offered', - 'switched_on', - 'failed', - 'request_failed', - 'error_code', - ]; - - keys.forEach( key => expect( key ).toMatch( PROPERTY_KEY ) ); - } ); } ); describe( 'The free-text bucket', () => { diff --git a/projects/packages/my-jetpack/_inc/components/onboarding-screen/wizard/wizard.tsx b/projects/packages/my-jetpack/_inc/components/onboarding-screen/wizard/wizard.tsx index de5fcb0c2bf7..bdb6d133f53a 100644 --- a/projects/packages/my-jetpack/_inc/components/onboarding-screen/wizard/wizard.tsx +++ b/projects/packages/my-jetpack/_inc/components/onboarding-screen/wizard/wizard.tsx @@ -347,29 +347,19 @@ export function Wizard( { exitUrl, dashboardUrl }: WizardProps ) { apply( modules, wantedModules ) .then( results => { setModuleResults( results ); - // Counts, not a list: which module failed is its own toggle event, and - // a property holding a list cannot be grouped on. + // Counts, not a list, because a property holding a list cannot be grouped on. const { on, failed } = tallyResults( results ); track( WIZARD_EVENTS.applyResult, { offered: results.length, switched_on: on, failed, - request_failed: false, } ); } ) /* * apply() reports each switch's failure itself and does not reject. This only * keeps an unexpected throw from leaving the step on a spinner for ever. */ - .catch( () => { - setModuleResults( [] ); - track( WIZARD_EVENTS.applyResult, { - offered: modules.length, - switched_on: 0, - failed: 0, - request_failed: true, - } ); - } ) + .catch( () => setModuleResults( [] ) ) .finally( handleNext ); }, [ apply, modules, wantedModules, handleNext, track ] ); From 790bb414f3580df02a2a0fc7b8a0a5b593276cdb Mon Sep 17 00:00:00 2001 From: devinwalker Date: Tue, 20 Oct 2026 09:48:48 -0700 Subject: [PATCH 4/4] My Jetpack: point the wizard tracking tests at the new switch --- .../components/onboarding-screen/wizard/test/smoke.test.tsx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/projects/packages/my-jetpack/_inc/components/onboarding-screen/wizard/test/smoke.test.tsx b/projects/packages/my-jetpack/_inc/components/onboarding-screen/wizard/test/smoke.test.tsx index baaea58ed845..ee109489f20a 100644 --- a/projects/packages/my-jetpack/_inc/components/onboarding-screen/wizard/test/smoke.test.tsx +++ b/projects/packages/my-jetpack/_inc/components/onboarding-screen/wizard/test/smoke.test.tsx @@ -1318,7 +1318,7 @@ describe( 'What the wizard reports', () => { const { user } = setupWizard( { isUserConnected: true } ); await user.click( screen.getByRole( 'radio', { name: 'A blog or publication' } ) ); await user.click( screen.getByRole( 'button', { name: 'Continue' } ) ); - await user.click( screen.getByRole( 'checkbox', { name: 'Downtime Monitor' } ) ); + await user.click( screen.getByRole( 'switch', { name: 'Downtime Monitor' } ) ); await user.click( screen.getByRole( 'button', { name: 'Continue' } ) ); await waitFor( () => @@ -1335,7 +1335,7 @@ describe( 'What the wizard reports', () => { await user.click( screen.getByRole( 'radio', { name: 'Something else…' } ) ); await user.type( screen.getByRole( 'textbox' ), 'A wiki' ); await user.click( screen.getByRole( 'button', { name: 'Continue' } ) ); - await user.click( screen.getByRole( 'checkbox', { name: 'Downtime Monitor' } ) ); + await user.click( screen.getByRole( 'switch', { name: 'Downtime Monitor' } ) ); await user.click( screen.getByRole( 'button', { name: 'Continue' } ) ); await user.click( await screen.findByRole( 'link', { name: 'Go to My Jetpack' } ) );