@@ -539,15 +539,12 @@ function getMetaMetricsEnabledFromPersistedState(persistedState) {
539539 * Returns whether onboarding has completed, given the application state.
540540 *
541541 * @param {Record<string, unknown> } appState - Application state
542- * @returns `true` if MetaMask's state has been initialized, and MetaMetrics
543- * is enabled, `false` otherwise.
542+ * @returns `true` if onboarding has completed, `false` otherwise.
544543 */
545544function getOnboardingCompleteFromAppState ( appState ) {
546545 // during initialization after loading persisted state
547546 if ( appState . persistedState ) {
548- return Boolean (
549- appState . persistedState . data ?. OnboardingController ?. completedOnboarding ,
550- ) ;
547+ return getOnboardingCompleteFromPersistedState ( appState . persistedState ) ;
551548 // After initialization
552549 } else if ( appState . state ) {
553550 // UI
@@ -561,6 +558,18 @@ function getOnboardingCompleteFromAppState(appState) {
561558 return false ;
562559}
563560
561+ /**
562+ * Returns whether onboarding has completed, given the persisted state.
563+ *
564+ * @param {Record<string, unknown> } persistedState - Persisted state
565+ * @returns `true` if onboarding has completed, `false` otherwise.
566+ */
567+ function getOnboardingCompleteFromPersistedState ( persistedState ) {
568+ return Boolean (
569+ persistedState . data ?. OnboardingController ?. completedOnboarding ,
570+ ) ;
571+ }
572+
564573function getSentryEnvironment ( ) {
565574 if ( METAMASK_BUILD_TYPE === 'main' ) {
566575 return METAMASK_ENVIRONMENT ;
@@ -599,14 +608,22 @@ async function getMetaMetricsEnabled() {
599608 }
600609
601610 const appState = getState ( ) ;
611+
602612 if ( appState . state || appState . persistedState ) {
603- return getMetaMetricsEnabledFromAppState ( appState ) ;
613+ return (
614+ getMetaMetricsEnabledFromAppState ( appState ) &&
615+ getOnboardingCompleteFromAppState ( appState )
616+ ) ;
604617 }
618+
605619 // If we reach here, it means the error was thrown before initialization
606620 // completed, and before we loaded the persisted state for the first time.
607621 try {
608622 const persistedState = await globalThis . stateHooks . getPersistedState ( ) ;
609- return getMetaMetricsEnabledFromPersistedState ( persistedState ) ;
623+ return (
624+ getMetaMetricsEnabledFromPersistedState ( persistedState ) &&
625+ getOnboardingCompleteFromPersistedState ( persistedState )
626+ ) ;
610627 } catch ( error ) {
611628 log ( 'Error retrieving persisted state' , error ) ;
612629 return false ;
0 commit comments