diff --git a/src/global-methods.js b/src/global-methods.js index 240c80d82..5cdc2f3ea 100644 --- a/src/global-methods.js +++ b/src/global-methods.js @@ -87,18 +87,36 @@ export default { }).then( (response) => { if (logApiCall) { - let additionalEventData = ""; - if (additionalSuccessEventDataHandler) { - additionalEventData = "_" + additionalSuccessEventDataHandler(response); - } const endpointWithoutParams = analyticsMixIn.methods.removeParamsFromEndpoint(endpoint); - analyticsMixIn.methods.pushEventToGA( - GaCategories.API_RESPONSE, - `${pageNameToLog}_${endpointWithoutParams}`, - `${GaLabels.SUCCESS}${additionalEventData}`, - true - ); + const gaAction = `${pageNameToLog}_${endpointWithoutParams}`; + + if (additionalSuccessEventDataHandler) { + const handlerResult = additionalSuccessEventDataHandler(response); + const additionalEntries = Array.isArray(handlerResult) + ? handlerResult + : [handlerResult]; + + additionalEntries.forEach((entry) => { + if (entry === undefined || entry === null || entry === "") { + return; + } + + analyticsMixIn.methods.pushEventToGA( + GaCategories.API_RESPONSE, + gaAction, + `${GaLabels.SUCCESS}_${entry}`, + true + ); + }); + } else { + analyticsMixIn.methods.pushEventToGA( + GaCategories.API_RESPONSE, + gaAction, + GaLabels.SUCCESS, + true + ); + } } return resolve(response); diff --git a/src/global-methods.spec.js b/src/global-methods.spec.js index c8c4d6da9..65bd675be 100644 --- a/src/global-methods.spec.js +++ b/src/global-methods.spec.js @@ -30,6 +30,33 @@ it("Global Methods - Call Http Client - Should Resolve Promise", () => { }); }); +it("Global Methods - Call Http Client - Should log multiple success event entries", async () => { + const endpoint = "https://mock.safelite.com"; + const httpArgs = setupMocksForHttpClient({ endpoint: endpoint }); + httpArgs.additionalSuccessEventDataHandler = () => [ + "Email provided: true", + "Phone provided: false", + ]; + analyticsMixIn.methods.pushEventToGA = jest.fn(); + analyticsMixIn.methods.removeParamsFromEndpoint = jest.fn((url) => url); + + await globalMethods.callHttpClient(httpArgs); + + expect(analyticsMixIn.methods.pushEventToGA).toHaveBeenCalledTimes(2); + expect(analyticsMixIn.methods.pushEventToGA).toHaveBeenCalledWith( + "Api_Response", + expect.any(String), + "Success_Email provided: true", + true + ); + expect(analyticsMixIn.methods.pushEventToGA).toHaveBeenCalledWith( + "Api_Response", + expect.any(String), + "Success_Phone provided: false", + true + ); +}); + it("Global Methods - Call Http Client - Should Reject Promise", () => { //Arrange const endpoint = "https://mock.safelite.com"; diff --git a/src/store/index.js b/src/store/index.js index d913ddb06..95ce9a8d7 100644 --- a/src/store/index.js +++ b/src/store/index.js @@ -2940,8 +2940,10 @@ export const actions = { }, logApiCall: true, pageNameToLog: pageNameToLog, - additionalSuccessEventDataHandler: (response) => + additionalSuccessEventDataHandler: (response) => [ "Email provided: " + (order.customer.emailAddress ? "true" : "false"), + "Phone provided: " + (order.customer.phoneNumber ? "true" : "false"), + ], }); },