Skip to content

Commit

Permalink
Auto-launch reader onboading on first visit
Browse files Browse the repository at this point in the history
  • Loading branch information
holdercp committed Jan 10, 2025
1 parent bde75ca commit b02b0dd
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 15 deletions.
1 change: 1 addition & 0 deletions client/reader/onboarding/constants.tsx
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export const READER_ONBOARDING_PREFERENCE_KEY = 'has_completed_reader_onboarding';
export const READER_ONBOARDING_SEEN_PREFERENCE_KEY = 'has_seen_reader_onboarding';
export const READER_ONBOARDING_TRACKS_EVENT_PREFIX = 'calypso_reader_onboarding_';
45 changes: 30 additions & 15 deletions client/reader/onboarding/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,18 @@ import { Checklist, ChecklistItem, Task } from '@automattic/launchpad';
import { translate } from 'i18n-calypso';
import React, { useState, useEffect } from 'react';
import {
READER_ONBOARDING_SEEN_PREFERENCE_KEY,
READER_ONBOARDING_PREFERENCE_KEY,
READER_ONBOARDING_TRACKS_EVENT_PREFIX,
} from 'calypso/reader/onboarding/constants';
import InterestsModal from 'calypso/reader/onboarding/interests-modal';
import SubscribeModal from 'calypso/reader/onboarding/subscribe-modal';
import { useSelector } from 'calypso/state';
import { useDispatch, useSelector } from 'calypso/state';
import {
getCurrentUserDate,
isCurrentUserEmailVerified,
} from 'calypso/state/current-user/selectors';
import { savePreference } from 'calypso/state/preferences/actions';
import { getPreference, hasReceivedRemotePreferences } from 'calypso/state/preferences/selectors';
import { getReaderFollowedTags } from 'calypso/state/reader/tags/selectors';

Expand All @@ -33,10 +35,15 @@ const ReaderOnboarding = ( {
const hasCompletedOnboarding = useSelector( ( state ) =>
getPreference( state, READER_ONBOARDING_PREFERENCE_KEY )
);
const hasSeenOnboarding = useSelector( ( state ) =>
getPreference( state, READER_ONBOARDING_SEEN_PREFERENCE_KEY )
);
const preferencesLoaded = useSelector( hasReceivedRemotePreferences );
const userRegistrationDate = useSelector( getCurrentUserDate );
const isEmailVerified = useSelector( isCurrentUserEmailVerified );

const dispatch = useDispatch();

const shouldShowOnboarding =
forceShow ||
isEnabled( 'reader/force-onboarding' ) ||
Expand All @@ -46,20 +53,6 @@ const ReaderOnboarding = ( {
isEmailVerified &&
new Date( userRegistrationDate ) >= new Date( '2024-10-01T00:00:00Z' ) );

// Track if user viewed Reader Onboarding.
useEffect( () => {
if ( shouldShowOnboarding ) {
recordTracksEvent( `${ READER_ONBOARDING_TRACKS_EVENT_PREFIX }viewed` );
}
}, [ shouldShowOnboarding ] );

// Notify the parent component if onboarding will render.
onRender?.( shouldShowOnboarding );

if ( ! shouldShowOnboarding ) {
return null;
}

// Modal state handlers with tracking.
const openInterestsModal = () => {
recordTracksEvent( `${ READER_ONBOARDING_TRACKS_EVENT_PREFIX }interests_modal_open` );
Expand Down Expand Up @@ -94,6 +87,28 @@ const ReaderOnboarding = ( {
task?.actionDispatch?.();
};

// Track if user viewed Reader Onboarding.
useEffect( () => {
if ( shouldShowOnboarding ) {
recordTracksEvent( `${ READER_ONBOARDING_TRACKS_EVENT_PREFIX }viewed` );
}
}, [ shouldShowOnboarding, dispatch ] );

// Auto-open the interests modal if onboarding should render and it has never been opened before
useEffect( () => {
if ( shouldShowOnboarding && ! hasSeenOnboarding ) {
openInterestsModal();
dispatch( savePreference( READER_ONBOARDING_SEEN_PREFERENCE_KEY, true ) );
}
}, [ shouldShowOnboarding, hasSeenOnboarding, dispatch ] );

// Notify the parent component if onboarding will render.
onRender?.( shouldShowOnboarding );

if ( ! shouldShowOnboarding ) {
return null;
}

const taskOneCompleted = followedTags ? followedTags.length > 2 : false;

const tasks: Task[] = [
Expand Down

0 comments on commit b02b0dd

Please sign in to comment.