Skip to content

Commit

Permalink
fix: Consent analytic events
Browse files Browse the repository at this point in the history
  • Loading branch information
bruce-glazier committed Mar 18, 2024
1 parent be28196 commit 8fde548
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 9 deletions.
26 changes: 22 additions & 4 deletions src/common/Analytics/AnalyticsEvents.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { EventEmitter } from 'events';
import { ConsentAndForm } from '../../hooks/useConsent';

export type AnalyticsEventTypeHandlers = {
track: (eventName: string, event: unknown) => void;
Expand Down Expand Up @@ -47,16 +48,33 @@ export const createAnalyticsEmitter = <
};

export type SDKTrackEvents = {
Login: { usedInvite: boolean };
LoginFailure: { error: string; usedInvite: boolean };
TokenRefreshFailure: { accessTokenExpirationDate: string };
InviteDetected: {};
ConsentResponseSubmitted: {
consentName?: string;
consentId?: string;
userAcceptedConsent: boolean;
};
ConsentScreenShown: { consentName?: string; consentId?: string };
ConsentSubmitFailure: {
reason: string;
consent?: ConsentAndForm;
userAcceptedConsent: boolean;
};
CustomConsentScreenShown: { consentName?: string; consentId?: string };
InviteAcceptFailure: { reason: string };
InviteAcceptSuccess: { accountName: string; accountId: string };
InviteDetected: {};
InviteRequiredScreenPresented: {};
Login: { usedInvite: boolean };
LoginButtonPresented: { buttonText: string };
LoginFailure: { error: string; usedInvite: boolean };
Navigate: {
fromScreen: string;
toScreen?: string;
method: 'pop' | 'replace';
};
PostCreated: { messageLength: number };
PostEdited: { messageLength: number };
TokenRefreshFailure: { accessTokenExpirationDate: string };
};

/**
Expand Down
10 changes: 5 additions & 5 deletions src/screens/ConsentScreen.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ test('renders custom consent screen if present in developer config', () => {
});
expect(navigateMock.replace).toHaveBeenCalledWith('app');
expect(updateConsentDirectiveMutationMock.mutate).toHaveBeenCalledTimes(1);
expect(updateConsentDirectiveMutationMock.mutate).toHaveBeenCalledWith(
expect(updateConsentDirectiveMutationMock.mutate.mock.calls[0][0]).toEqual(
createConsentPatch(defaultConsentDirective.id, true),
);

Expand All @@ -156,7 +156,7 @@ test('renders custom consent screen if present in developer config', () => {
});
() => expect(logoutMock).toHaveBeenCalledTimes(1);
expect(updateConsentDirectiveMutationMock.mutate).toHaveBeenCalledTimes(1);
expect(updateConsentDirectiveMutationMock.mutate).toHaveBeenCalledWith(
expect(updateConsentDirectiveMutationMock.mutate.mock.calls[0][0]).toEqual(
createConsentPatch(defaultConsentDirective.id, false),
);

Expand Down Expand Up @@ -191,7 +191,7 @@ test('renders the consent body and acceptance verbiage', () => {
test('should accept the consent and navigate to the home screen', () => {
const { getByText } = render(consentScreen);
fireEvent.press(getByText('Agree'));
expect(updateConsentDirectiveMutationMock.mutate).toHaveBeenCalledWith(
expect(updateConsentDirectiveMutationMock.mutate.mock.calls[0][0]).toEqual(
createConsentPatch(defaultConsentDirective.id, true),
);
useUpdateProjectConsentDirectiveMock.mock.calls[0][0].onSuccess(undefined, {
Expand All @@ -206,7 +206,7 @@ test('should accept the consent and navigate to the onboarding course screen', (
});
const { getByText } = render(consentScreen);
fireEvent.press(getByText('Agree'));
expect(updateConsentDirectiveMutationMock.mutate).toHaveBeenCalledWith(
expect(updateConsentDirectiveMutationMock.mutate.mock.calls[0][0]).toEqual(
createConsentPatch(defaultConsentDirective.id, true),
);
useUpdateProjectConsentDirectiveMock.mock.calls[0][0].onSuccess(undefined, {
Expand All @@ -227,7 +227,7 @@ test('Pressing logout declines the consent and logs the the user out', async ()
const { getByText } = render(consentScreen);
fireEvent.press(getByText('Decline'));
alertSpy.mock.calls[0]?.[2]?.[1].onPress!();
expect(updateConsentDirectiveMutationMock.mutate).toHaveBeenCalledWith(
expect(updateConsentDirectiveMutationMock.mutate.mock.calls[0][0]).toEqual(
createConsentPatch(defaultConsentDirective.id, false),
);
useUpdateProjectConsentDirectiveMock.mock.calls[0][0].onSuccess(undefined, {
Expand Down
40 changes: 40 additions & 0 deletions src/screens/ConsentScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
import type { ConsentAndForm } from '../hooks/useConsent';
import { useDeveloperConfig } from '../hooks/useDeveloperConfig';
import { LoggedInRootScreenProps } from '../navigators/types';
import { _sdkAnalyticsEvent } from '../common/Analytics';

export const ConsentScreen = ({
navigation,
Expand Down Expand Up @@ -44,12 +45,21 @@ export const ConsentScreen = ({
useEffect(() => {
if (!shouldRenderConsentScreen) {
if (route.params?.noNavOnAccept) {
_sdkAnalyticsEvent.track('Navigate', {
fromScreen: 'consentScreen',
method: 'pop',
});
navigation.pop();
return;
}
const nextRoute = shouldLaunchOnboardingCourse
? 'screens/OnboardingCourseScreen'
: 'app';
_sdkAnalyticsEvent.track('Navigate', {
fromScreen: 'consentScreen',
toScreen: nextRoute,
method: 'replace',
});
navigation.replace(nextRoute);
}
}, [
Expand All @@ -67,10 +77,31 @@ export const ConsentScreen = ({
const updateConsentDirective = useCallback(
(accept: boolean) => {
if (!consentToPresent?.id) {
_sdkAnalyticsEvent.track('ConsentSubmitFailure', {
reason: 'Consent ID was missing; mutation not executed',
consent: consentToPresent,
userAcceptedConsent: accept,
});
return;
}
updateConsentDirectiveMutation.mutate(
createConsentPatch(consentToPresent.id, accept),
{
onSuccess: () => {
_sdkAnalyticsEvent.track('ConsentResponseSubmitted', {
userAcceptedConsent: accept,
consentName: consentToPresent.form.name,
consentId: consentToPresent.form.id,
});
},
onError: (err) => {
_sdkAnalyticsEvent.track('ConsentSubmitFailure', {
reason: `Error thrown during mutation: ${JSON.stringify(err)}`,
consent: consentToPresent,
userAcceptedConsent: accept,
});
},
},
);
},
[updateConsentDirectiveMutation, consentToPresent],
Expand Down Expand Up @@ -126,6 +157,10 @@ export const ConsentScreen = ({
}

if (CustomConsentScreen) {
_sdkAnalyticsEvent.track('CustomConsentScreenShown', {
consentName: consentToPresent.form.name,
consentId: consentToPresent.form.id,
});
return (
<CustomConsentScreen
consentForm={consentToPresent}
Expand All @@ -136,6 +171,11 @@ export const ConsentScreen = ({
);
}

_sdkAnalyticsEvent.track('ConsentScreenShown', {
consentName: consentToPresent.form.name,
consentId: consentToPresent.form.id,
});

return (
<View style={styles.view}>
<ScrollView style={styles.scrollView}>
Expand Down

0 comments on commit 8fde548

Please sign in to comment.