Skip to content

Commit

Permalink
Merge be28196 into eedaeee
Browse files Browse the repository at this point in the history
  • Loading branch information
bruce-glazier committed Mar 18, 2024
2 parents eedaeee + be28196 commit bf08960
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 3 deletions.
7 changes: 7 additions & 0 deletions src/common/Analytics/AnalyticsEvents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@ export const createAnalyticsEmitter = <

export type SDKTrackEvents = {
Login: { usedInvite: boolean };
LoginFailure: { error: string; usedInvite: boolean };
TokenRefreshFailure: { accessTokenExpirationDate: string };
InviteDetected: {};
InviteAcceptFailure: { reason: string };
InviteAcceptSuccess: { accountName: string; accountId: string };
InviteRequiredScreenPresented: {};
LoginButtonPresented: { buttonText: string };
PostCreated: { messageLength: number };
PostEdited: { messageLength: number };
};
Expand Down
19 changes: 16 additions & 3 deletions src/components/Invitations/InviteProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { useAuth } from '../../hooks/useAuth';
import { useRestCache, useRestMutation } from '../../hooks/rest-api';
import { ActivityIndicatorView } from '../ActivityIndicatorView';
import { useQueryClient } from '@tanstack/react-query';
import { _sdkAnalyticsEvent } from '../../common/Analytics';

export type PendingInvite = {
inviteId: string;
Expand All @@ -25,16 +26,17 @@ const usePendingInviteStateStore = create<{
}>(() => ({}));

inviteNotifier.addListener('inviteDetected', (invite) => {
_sdkAnalyticsEvent.track('InviteDetected', {}); // inviteId/evc exposure should be limited
usePendingInviteStore.setState(invite);
usePendingInviteStateStore.setState({
loading: false,
failedToDecode: false,
failureMessage: undefined,
});
});
inviteNotifier.addListener('inviteLoadingStateChanged', (state) =>
usePendingInviteStateStore.setState(state, true),
);
inviteNotifier.addListener('inviteLoadingStateChanged', (state) => {
usePendingInviteStateStore.setState(state, true);
});

export const usePendingInvite = () => {
return usePendingInviteStore();
Expand Down Expand Up @@ -90,14 +92,25 @@ export const InviteProvider: React.FC<InviteProviderProps> = ({ children }) => {
onError: (error: any) => {
if (isInviteAlreadyAcceptedErrorResponse(error.response?.data)) {
console.warn('Ignoring already accepted invite');
_sdkAnalyticsEvent.track('InviteAcceptFailure', {
reason: 'Invite Already Accepted',
});
} else {
console.warn('Error accepting invitation', error);
Alert.alert(t('Error accepting invitation. Please try again.'));
_sdkAnalyticsEvent.track('InviteAcceptFailure', {
reason: JSON.stringify(error),
});
}
clearPendingInvite();
mutation.reset();
},
onSuccess: async (acceptedInvite) => {
_sdkAnalyticsEvent.track('InviteAcceptSuccess', {
accountName: acceptedInvite.accountName,
accountId: acceptedInvite.account,
});

// Add the new account to the account list.
cache.updateCache(
'GET /v1/accounts',
Expand Down
7 changes: 7 additions & 0 deletions src/hooks/useOAuthFlow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,10 @@ export const OAuthContextProvider = ({
_sdkAnalyticsEvent.track('Login', { usedInvite: !!pendingInvite?.evc });
onSuccess?.(result);
} catch (error) {
_sdkAnalyticsEvent.track('LoginFailure', {
error: JSON.stringify(error),
usedInvite: !!pendingInvite?.evc,
});
await clearAuthResult();
onFail?.(error);
}
Expand All @@ -172,6 +176,9 @@ export const OAuthContextProvider = ({
const refreshHandler = useCallback(
async function (storedResult: AuthResult) {
if (!storedResult?.refreshToken) {
_sdkAnalyticsEvent.track('TokenRefreshFailure', {
accessTokenExpirationDate: storedResult.accessTokenExpirationDate,
});
throw new Error(
'No refreshToken! The app can NOT function properly without a refreshToken. Expect to be logged out immediately.',
);
Expand Down
2 changes: 2 additions & 0 deletions src/screens/InviteRequiredScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ import {
import { createStyles } from '../components/BrandConfigProvider';
import { tID } from '../common/testID';
import { useStyles } from '../hooks/useStyles';
import { _sdkAnalyticsEvent } from '../common/Analytics';

export const InviteRequiredScreen = () => {
const { styles } = useStyles(defaultStyles);
_sdkAnalyticsEvent.track('InviteRequiredScreenPresented', {});

return (
<View style={styles.containerView}>
Expand Down
8 changes: 8 additions & 0 deletions src/screens/LoginScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { createStyles, useIcons } from '../components/BrandConfigProvider';
import LaunchScreen from '../components/LaunchScreen';
import { Dialog, Portal, Text } from 'react-native-paper';
import compact from 'lodash/compact';
import { _sdkAnalyticsEvent } from '../common/Analytics';

export const LoginScreen: FC = () => {
const { renderCustomLoginScreen, componentProps = {} } = useDeveloperConfig();
Expand All @@ -32,11 +33,18 @@ export const LoginScreen: FC = () => {
return t('login-button-loading-invite', 'Loading');
}
if (pendingInvite) {
_sdkAnalyticsEvent.track('LoginButtonPresented', {
buttonText: 'Accept Invite',
});
return (
loginScreenProps.acceptInviteText ??
t('login-button-title-invite-found', 'Accept Invite')
);
}

_sdkAnalyticsEvent.track('LoginButtonPresented', {
buttonText: 'Login',
});
return loginScreenProps.loginText ?? t('login-button-title', 'Login');
};

Expand Down

0 comments on commit bf08960

Please sign in to comment.