Skip to content

Commit

Permalink
added comms GA4 analytics
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewsetterfield committed Mar 15, 2024
1 parent 52d95e7 commit ee788c4
Show file tree
Hide file tree
Showing 3 changed files with 173 additions and 41 deletions.
204 changes: 166 additions & 38 deletions src/app/pages/communication/communication.analytics.effects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ import {
AnalyticsErrorTypes,
AnalyticsEventCategories,
AnalyticsEvents,
AnalyticsScreenNames,
AnalyticsScreenNames, GoogleAnalyticsEvents, GoogleAnalyticsEventsTitles, GoogleAnalyticsEventsValues,
} from '@providers/analytics/analytics.model';
import { TestsModel } from '@store/tests/tests.model';
import { formatAnalyticsText } from '@shared/helpers/format-analytics-text';
import { analyticsEventTypePrefix, formatAnalyticsText } from '@shared/helpers/format-analytics-text';
import { AnalyticNotRecorded, AnalyticRecorded } from '@providers/analytics/analytics.actions';
import { StoreModel } from '@shared/models/store.model';
import { select, Store } from '@ngrx/store';
Expand Down Expand Up @@ -98,10 +98,19 @@ export class CommunicationAnalyticsEffects {
[, tests, applicationReference, candidateId, category]:
[ReturnType<typeof CommunicationViewDidEnter>, TestsModel, string, number, CategoryCode, boolean],
) => {

// TODO - MES-9495 - remove old analytics
this.analytics.addCustomDimension(AnalyticsDimensionIndices.TEST_CATEGORY, category);
this.analytics.addCustomDimension(AnalyticsDimensionIndices.CANDIDATE_ID, `${candidateId}`);
this.analytics.addCustomDimension(AnalyticsDimensionIndices.APPLICATION_REFERENCE, applicationReference);
this.analytics.setCurrentPage(formatAnalyticsText(AnalyticsScreenNames.COMMUNICATION, tests));

//GA4 Analytics
this.analytics.setGACurrentPage(analyticsEventTypePrefix(AnalyticsScreenNames.COMMUNICATION, tests));

this.analytics.addGACustomDimension(AnalyticsDimensionIndices.TEST_CATEGORY, category);
this.analytics.addGACustomDimension(AnalyticsDimensionIndices.CANDIDATE_ID, `${candidateId}`);
this.analytics.addGACustomDimension(AnalyticsDimensionIndices.APPLICATION_REFERENCE, applicationReference);
return of(AnalyticRecorded());
}),
));
Expand Down Expand Up @@ -130,9 +139,18 @@ export class CommunicationAnalyticsEffects {
: this.appConfigProvider.getAppConfig()?.journal?.enablePracticeModeAnalytics),
switchMap(([action, tests, category]:
[ReturnType<typeof CommunicationValidationError>, TestsModel, CategoryCode, boolean]) => {

// TODO - MES-9495 - remove old analytics
const screenName = formatAnalyticsText(AnalyticsScreenNames.COMMUNICATION, tests);
this.analytics.addCustomDimension(AnalyticsDimensionIndices.TEST_CATEGORY, category);
this.analytics.logError(`${AnalyticsErrorTypes.VALIDATION_ERROR} (${screenName})`, action.errorMessage);

// GA4 Analytics
this.analytics.logGAEvent(
analyticsEventTypePrefix(GoogleAnalyticsEvents.VALIDATION_ERROR, tests),
GoogleAnalyticsEventsTitles.BLANK_FIELD,
action.errorMessage,
);
return of(AnalyticRecorded());
}),
));
Expand All @@ -156,53 +174,27 @@ export class CommunicationAnalyticsEffects {
: this.appConfigProvider.getAppConfig()?.journal?.enablePracticeModeAnalytics),
concatMap(([, tests]: [ReturnType<typeof VRNModalOpened>, TestsModel, boolean]) => {
if (this.router.url?.startsWith(this.className)) {

// TODO - MES-9495 - remove old analytics
this.analytics.logEvent(
formatAnalyticsText(AnalyticsEventCategories.COMMUNICATION, tests),
AnalyticsEvents.VRN_CAPTURE,
AnalyticsEvents.VRN_CAPTURE_SELECTED,
);

// GA4 Analytics
this.analytics.logGAEvent(
analyticsEventTypePrefix(AnalyticsEvents.VRN_CAPTURE, tests),
GoogleAnalyticsEventsTitles.OUTCOME,
GoogleAnalyticsEventsValues.VRN_CAPTURE_SELECTED,
);

return of(AnalyticRecorded());
}
return of(AnalyticNotRecorded());
}),
));

bookingEmailSelected$ = createEffect(() => this.actions$.pipe(
ofType(BookingEmailSelected),
switchMap(() => {
this.analytics.logEvent(
AnalyticsEventCategories.COMMUNICATION,
AnalyticsEvents.CANDIDATE_RECEIVE_TEST_RESULTS,
'Booking Email',
);
return of(AnalyticRecorded());
}),
));

newEmailSelected$ = createEffect(() => this.actions$.pipe(
ofType(NewEmailSelected),
switchMap(() => {
this.analytics.logEvent(
AnalyticsEventCategories.COMMUNICATION,
AnalyticsEvents.CANDIDATE_RECEIVE_TEST_RESULTS,
'New Email',
);
return of(AnalyticRecorded());
}),
));

postalSelected$ = createEffect(() => this.actions$.pipe(
ofType(PostalSelected),
switchMap(() => {
this.analytics.logEvent(
AnalyticsEventCategories.COMMUNICATION,
AnalyticsEvents.CANDIDATE_RECEIVE_TEST_RESULTS,
'By Post',
);
return of(AnalyticRecorded());
}),
));

vrnModalCancelled$ = createEffect(() => this.actions$.pipe(
ofType(VRNModalCancelled),
concatMap((action) => of(action)
Expand All @@ -222,11 +214,20 @@ export class CommunicationAnalyticsEffects {
: this.appConfigProvider.getAppConfig()?.journal?.enablePracticeModeAnalytics),
concatMap(([, tests]: [ReturnType<typeof VRNModalCancelled>, TestsModel, boolean]) => {
if (this.router.url?.startsWith(this.className)) {

// TODO - MES-9495 - remove old analytics
this.analytics.logEvent(
formatAnalyticsText(AnalyticsEventCategories.COMMUNICATION, tests),
AnalyticsEvents.VRN_CAPTURE,
AnalyticsEvents.VRN_CAPTURE_CANCELLED,
);

// GA4 Analytics
this.analytics.logGAEvent(
analyticsEventTypePrefix(AnalyticsEvents.VRN_CAPTURE, tests),
GoogleAnalyticsEventsTitles.OUTCOME,
GoogleAnalyticsEventsValues.VRN_CAPTURE_CANCELLED,
);
return of(AnalyticRecorded());
}
return of(AnalyticNotRecorded());
Expand All @@ -252,11 +253,138 @@ export class CommunicationAnalyticsEffects {
: this.appConfigProvider.getAppConfig()?.journal?.enablePracticeModeAnalytics),
concatMap(([, tests]: [ReturnType<typeof VRNModalSaved>, TestsModel, boolean]) => {
if (this.router.url?.startsWith(this.className)) {

// TODO - MES-9495 - remove old analytics
this.analytics.logEvent(
formatAnalyticsText(AnalyticsEventCategories.COMMUNICATION, tests),
AnalyticsEvents.VRN_CAPTURE,
AnalyticsEvents.VRN_CAPTURE_SAVED,
);

// GA4 Analytics
this.analytics.logGAEvent(
analyticsEventTypePrefix(AnalyticsEvents.VRN_CAPTURE, tests),
GoogleAnalyticsEventsTitles.OUTCOME,
GoogleAnalyticsEventsValues.VRN_CAPTURE_SAVED,
);
return of(AnalyticRecorded());
}
return of(AnalyticNotRecorded());
}),
));

bookingEmailSelected$ = createEffect(() => this.actions$.pipe(
ofType(BookingEmailSelected),
concatMap((action) => of(action)
.pipe(
withLatestFrom(
this.store$.pipe(
select(getTests),
),
this.store$.pipe(
select(getTests),
select(isPracticeMode),
),
),
)),
filter(([, , practiceMode]) => !practiceMode
? true
: this.appConfigProvider.getAppConfig()?.journal?.enablePracticeModeAnalytics),
concatMap(([, tests]: [ReturnType<typeof BookingEmailSelected>, TestsModel, boolean]) => {
if (this.router.url?.startsWith(this.className)) {

// TODO - MES-9495 - remove old analytics
this.analytics.logEvent(
AnalyticsEventCategories.COMMUNICATION,
AnalyticsEvents.CANDIDATE_RECEIVE_TEST_RESULTS,
'Booking Email',
);

// GA4 Analytics
this.analytics.logGAEvent(
analyticsEventTypePrefix(GoogleAnalyticsEvents.CANDIDATE_RECEIVE_TEST_RESULTS, tests),
GoogleAnalyticsEventsTitles.COMMS_CHANNEL,
GoogleAnalyticsEventsValues.COMMS_METHOD_BOOKING_EMAIL,
);

return of(AnalyticRecorded());
}
return of(AnalyticNotRecorded());
}),
));

newEmailSelected$ = createEffect(() => this.actions$.pipe(
ofType(NewEmailSelected),
concatMap((action) => of(action)
.pipe(
withLatestFrom(
this.store$.pipe(
select(getTests),
),
this.store$.pipe(
select(getTests),
select(isPracticeMode),
),
),
)),
filter(([, , practiceMode]) => !practiceMode
? true
: this.appConfigProvider.getAppConfig()?.journal?.enablePracticeModeAnalytics),
concatMap(([, tests]: [ReturnType<typeof NewEmailSelected>, TestsModel, boolean]) => {
if (this.router.url?.startsWith(this.className)) {

// TODO - MES-9495 - remove old analytics
this.analytics.logEvent(
AnalyticsEventCategories.COMMUNICATION,
AnalyticsEvents.CANDIDATE_RECEIVE_TEST_RESULTS,
'New Email',
);

// GA4 Analytics
this.analytics.logGAEvent(
analyticsEventTypePrefix(GoogleAnalyticsEvents.CANDIDATE_RECEIVE_TEST_RESULTS, tests),
GoogleAnalyticsEventsTitles.COMMS_CHANNEL,
GoogleAnalyticsEventsValues.COMMS_METHOD_NEW_EMAIL,
);
return of(AnalyticRecorded());
}
return of(AnalyticNotRecorded());
}),
));

postalSelected$ = createEffect(() => this.actions$.pipe(
ofType(PostalSelected),
concatMap((action) => of(action)
.pipe(
withLatestFrom(
this.store$.pipe(
select(getTests),
),
this.store$.pipe(
select(getTests),
select(isPracticeMode),
),
),
)),
filter(([, , practiceMode]) => !practiceMode
? true
: this.appConfigProvider.getAppConfig()?.journal?.enablePracticeModeAnalytics),
concatMap(([, tests]: [ReturnType<typeof PostalSelected>, TestsModel, boolean]) => {
if (this.router.url?.startsWith(this.className)) {

// TODO - MES-9495 - remove old analytics
this.analytics.logEvent(
AnalyticsEventCategories.COMMUNICATION,
AnalyticsEvents.CANDIDATE_RECEIVE_TEST_RESULTS,
'By Post',
);

// GA4 Analytics
this.analytics.logGAEvent(
analyticsEventTypePrefix(GoogleAnalyticsEvents.CANDIDATE_RECEIVE_TEST_RESULTS, tests),
GoogleAnalyticsEventsTitles.COMMS_CHANNEL,
GoogleAnalyticsEventsValues.COMMS_METHOD_POST,
);
return of(AnalyticRecorded());
}
return of(AnalyticNotRecorded());
Expand Down
3 changes: 1 addition & 2 deletions src/app/pages/waiting-room/waiting-room.analytics.effects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,8 @@ export class WaitingRoomAnalyticsEffects {
this.analytics.setCurrentPage(formatAnalyticsText(AnalyticsScreenNames.WAITING_ROOM, tests));

//GA4 Analytics
this.analytics.setGACurrentPage(formatAnalyticsText(AnalyticsScreenNames.WAITING_ROOM, tests));
this.analytics.setGACurrentPage(analyticsEventTypePrefix(AnalyticsScreenNames.WAITING_ROOM, tests));

// Create custom analytics for use throughout the test
this.analytics.addGACustomDimension(AnalyticsDimensionIndices.TEST_CATEGORY, category);
this.analytics.addGACustomDimension(AnalyticsDimensionIndices.CANDIDATE_ID, `${candidateId}`);
this.analytics.addGACustomDimension(AnalyticsDimensionIndices.APPLICATION_REFERENCE, applicationReference);
Expand Down
7 changes: 6 additions & 1 deletion src/app/providers/analytics/analytics.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -268,15 +268,20 @@ export enum GoogleAnalyticsEventPrefix {

export enum GoogleAnalyticsEvents {
VALIDATION_ERROR = 'validation_error',
CANDIDATE_RECEIVE_TEST_RESULTS = 'send _candidate_test_results',
}

export enum GoogleAnalyticsEventsTitles {
BLANK_FIELD = 'blank_field',
OUTCOME = 'outcome'
OUTCOME = 'outcome',
COMMS_CHANNEL = 'comms_channel',
}

export enum GoogleAnalyticsEventsValues {
VRN_CAPTURE_SELECTED = 'selected',
VRN_CAPTURE_CANCELLED = 'cancelled',
VRN_CAPTURE_SAVED = 'saved',
COMMS_METHOD_BOOKING_EMAIL = 'booking email',
COMMS_METHOD_NEW_EMAIL = 'new email',
COMMS_METHOD_POST = 'by email',
}

0 comments on commit ee788c4

Please sign in to comment.