From 4ef7a1b352902bf489da34143b58851d47994ade Mon Sep 17 00:00:00 2001 From: andrewsetterfield <48550267+andrewsetterfield@users.noreply.github.com> Date: Mon, 8 Apr 2024 12:48:42 +0100 Subject: [PATCH] Mes-9450: rekey reason cp (#1546) * GA4 analytics for rekey reason * add test, fix screen name issue --- .../rekey-reason.analytics.effects.spec.ts | 35 ++++++++++++++++++- .../rekey-reason.analytics.effects.ts | 19 ++++++++-- .../providers/analytics/analytics.model.ts | 1 + 3 files changed, 52 insertions(+), 3 deletions(-) diff --git a/src/app/pages/rekey-reason/__tests__/rekey-reason.analytics.effects.spec.ts b/src/app/pages/rekey-reason/__tests__/rekey-reason.analytics.effects.spec.ts index 75c633fca..a33bd81e2 100644 --- a/src/app/pages/rekey-reason/__tests__/rekey-reason.analytics.effects.spec.ts +++ b/src/app/pages/rekey-reason/__tests__/rekey-reason.analytics.effects.spec.ts @@ -6,7 +6,7 @@ import { TestCategory } from '@dvsa/mes-test-schema/category-definitions/common/ import { AnalyticsProvider } from '@providers/analytics/analytics'; import { AnalyticsProviderMock } from '@providers/analytics/__mocks__/analytics.mock'; -import { AnalyticsScreenNames } from '@providers/analytics/analytics.model'; +import { AnalyticsScreenNames, GoogleAnalyticsEventPrefix } from '@providers/analytics/analytics.model'; import { AnalyticRecorded } from '@providers/analytics/analytics.actions'; import { testsReducer } from '@store/tests/tests.reducer'; import { StoreModel } from '@shared/models/store.model'; @@ -17,6 +17,8 @@ import { AppConfigProvider } from '@providers/app-config/app-config'; import { AppConfigProviderMock } from '@providers/app-config/__mocks__/app-config.mock'; import * as rekeyReasonActions from '../rekey-reason.actions'; import { RekeyReasonAnalyticsEffects } from '../rekey-reason.analytics.effects'; +import * as fakeJournalActions from '@pages/fake-journal/fake-journal.actions'; +import { end2endPracticeSlotId } from '@shared/mocks/test-slot-ids.mock'; describe('RekeyReasonAnalyticsEffects', () => { let effects: RekeyReasonAnalyticsEffects; @@ -66,8 +68,33 @@ describe('RekeyReasonAnalyticsEffects', () => { effects.rekeyReasonViewDidEnter$.subscribe((result) => { expect(result.type === AnalyticRecorded.type) .toBe(true); + + // TODO - MES-9495 - remove old analytics expect(analyticsProviderMock.setCurrentPage) .toHaveBeenCalledWith(screenName); + + // GA4 Analytics + expect(analyticsProviderMock.setGACurrentPage) + .toHaveBeenCalledWith(screenName); + done(); + }); + }); + + it('should call setCurrentPage with pass and practice mode prefix', (done) => { + // ARRANGE + store$.dispatch(testsActions.StartTest(123, TestCategory.B)); + store$.dispatch(fakeJournalActions.StartE2EPracticeTest(end2endPracticeSlotId)); + store$.dispatch(candidateActions.PopulateCandidateDetails(candidateMock)); + // ACT + actions$.next(rekeyReasonActions.RekeyReasonViewDidEnter()); + // ASSERT + effects.rekeyReasonViewDidEnter$.subscribe((result) => { + expect(result.type === AnalyticRecorded.type) + .toBe(true); + + // GA4 Analytics + expect(analyticsProviderMock.setGACurrentPage) + .toHaveBeenCalledWith(`${GoogleAnalyticsEventPrefix.PRACTICE_MODE}_${screenName}`); done(); }); }); @@ -84,8 +111,14 @@ describe('RekeyReasonAnalyticsEffects', () => { effects.rekeyReasonUploadTest$.subscribe((result) => { expect(result.type === AnalyticRecorded.type) .toBe(true); + + // TODO - MES-9495 - remove old analytics expect(analyticsProviderMock.logEvent) .toHaveBeenCalled(); + + // GA4 Analytics + expect(analyticsProviderMock.logGAEvent) + .toHaveBeenCalled(); done(); }); }); diff --git a/src/app/pages/rekey-reason/rekey-reason.analytics.effects.ts b/src/app/pages/rekey-reason/rekey-reason.analytics.effects.ts index 45d3b55d2..cd9bdef00 100644 --- a/src/app/pages/rekey-reason/rekey-reason.analytics.effects.ts +++ b/src/app/pages/rekey-reason/rekey-reason.analytics.effects.ts @@ -8,7 +8,7 @@ import { AnalyticsDimensionIndices, AnalyticsEventCategories, AnalyticsEvents, - AnalyticsScreenNames, + AnalyticsScreenNames, GoogleAnalyticsEvents, GoogleAnalyticsEventsTitles, } from '@providers/analytics/analytics.model'; import { select, Store } from '@ngrx/store'; import { StoreModel } from '@shared/models/store.model'; @@ -17,7 +17,7 @@ import { getCurrentTest, getJournalData, isPracticeMode, } from '@store/tests/tests.selector'; import { of } from 'rxjs'; -import { formatAnalyticsText } from '@shared/helpers/format-analytics-text'; +import { analyticsEventTypePrefix, formatAnalyticsText } from '@shared/helpers/format-analytics-text'; import { TestsModel } from '@store/tests/tests.model'; import { AnalyticRecorded } from '@providers/analytics/analytics.actions'; import { getCandidate } from '@store/tests/journal-data/common/candidate/candidate.reducer'; @@ -78,10 +78,18 @@ export class RekeyReasonAnalyticsEffects { [, tests, candidateId, applicationReference]: [ReturnType, TestsModel, number, string, boolean], ) => { + + // TODO - MES-9495 - remove old analytics const screenName = formatAnalyticsText(AnalyticsScreenNames.REKEY_REASON, tests); this.analytics.addCustomDimension(AnalyticsDimensionIndices.CANDIDATE_ID, `${candidateId}`); this.analytics.addCustomDimension(AnalyticsDimensionIndices.APPLICATION_REFERENCE, applicationReference); this.analytics.setCurrentPage(screenName); + + // GA4 Analytics + this.analytics.setGACurrentPage(analyticsEventTypePrefix(AnalyticsScreenNames.REKEY_REASON, tests)); + this.analytics.addGACustomDimension(AnalyticsDimensionIndices.CANDIDATE_ID, `${candidateId}`); + this.analytics.addGACustomDimension(AnalyticsDimensionIndices.APPLICATION_REFERENCE, applicationReference); + return of(AnalyticRecorded()); }), )); @@ -113,12 +121,19 @@ export class RekeyReasonAnalyticsEffects { [ReturnType, string, TestsModel, boolean], ) => { + this.analytics.logEvent( formatAnalyticsText(AnalyticsEventCategories.POST_TEST, tests), formatAnalyticsText(AnalyticsEvents.CONFIRM_UPLOAD, tests), formatAnalyticsText(`Upload confirmed - ${testOutcome}`, tests), ); + // GA4 Analytics + this.analytics.logGAEvent( + analyticsEventTypePrefix(GoogleAnalyticsEvents.SUBMIT_TEST, tests), + GoogleAnalyticsEventsTitles.RESULT, + testOutcome, + ); return of(AnalyticRecorded()); }), )); diff --git a/src/app/providers/analytics/analytics.model.ts b/src/app/providers/analytics/analytics.model.ts index 3c2b12d33..ffabffc2a 100644 --- a/src/app/providers/analytics/analytics.model.ts +++ b/src/app/providers/analytics/analytics.model.ts @@ -291,6 +291,7 @@ export enum GoogleAnalyticsEvents { COMPLETED_TEST_SEARCH = 'completed_test_search', LDTM_SEARCH = 'ldtm_search', SUBMIT_FORM_ERROR = 'submit_form_error', + SUBMIT_TEST = 'submit_test', VALIDATION_ERROR = 'validation_error', CANDIDATE_RECEIVE_TEST_RESULTS = 'send_candidate_test_results', SET_D255 = 'set_d255',