diff --git a/src/constants/endpoints.js b/src/constants/endpoints.js index 059693773..7ff9e3cec 100644 --- a/src/constants/endpoints.js +++ b/src/constants/endpoints.js @@ -79,6 +79,10 @@ const endpoints = { url: "/analytics/api/v1/analytics/log-custom-event", method: "POST", }, + InitializeSession:{ + url: "/analytics/api/v1/analytics/initialize", + method: "POST", + }, GetExperimentsByUser: { url: "/analytics/api/v1/analytics/get-experiments", method: "GET", diff --git a/src/constants/store-actions.js b/src/constants/store-actions.js index fe658fbe1..0ac79ac28 100644 --- a/src/constants/store-actions.js +++ b/src/constants/store-actions.js @@ -21,6 +21,7 @@ const storeActions = { LOG_EXPERIMENT_EXPOSURE: "logExperimentExposure", LOG_PAGE_VIEW: "logPageView", LOG_CUSTOM_EVENT: "logCustomEvent", + INITIALIZE_SESSION: "initializeSession", GET_EXPERIMENTS_BY_USER: "GetExperimentsByUser", UPDATE_SERVICE_LOCATION_WITH_VEHICLE_REGISTRATION: "updateServiceLocationWithVehicleRegistration", diff --git a/src/helpers/heritage-integration/cookie-helper.js b/src/helpers/heritage-integration/cookie-helper.js index f3b730f90..5d681f17f 100644 --- a/src/helpers/heritage-integration/cookie-helper.js +++ b/src/helpers/heritage-integration/cookie-helper.js @@ -104,6 +104,14 @@ export function getSessionIdValue(){ return '00000000-0000-0000-0000-000000000000'; } +export function setCookieProperties(properties) { + if (typeof properties == "object") { + Object.keys(properties).forEach(key => { + document.cookie = `${key}=${properties[key]}`; + }); + } +} + /* =========================== = PRIVATE FUNCTIONS = diff --git a/src/mixins/analytics-mixin.js b/src/mixins/analytics-mixin.js index ca5a24a3e..fda94e79d 100644 --- a/src/mixins/analytics-mixin.js +++ b/src/mixins/analytics-mixin.js @@ -1,18 +1,17 @@ import { storeActions } from "@/constants/store-actions"; -import { getDeviceIdValue, getSessionIdValue, getSessionKeyValue } from "@/helpers/heritage-integration/cookie-helper"; +import { setCookieProperties, getDeviceIdValue, getSessionIdValue, getSessionKeyValue } from "@/helpers/heritage-integration/cookie-helper"; import { queryStrings } from "@/constants/query-strings"; import { experimentSettings } from "@/constants/experiments"; import { analyticsPageEvents, GaCategories, GaActions, GaLabels, GaEvents } from "@/constants/analytics"; +import { cookieNames } from "@/constants/cookie-names"; import baseMixin from "@/mixins/base-mixin"; export default { methods: { logPageView(pageEvent) { - // if the user does not have a session id from the content site, do not log. - const sid = getSessionIdValue(); - if (sid === '00000000-0000-0000-0000-000000000000' || sid == null) { - return; + if (getSessionKeyValue() === 0 || getSessionIdValue() === '00000000-0000-0000-0000-000000000000') { + this.initSession(); } const currentPageName = getPageNameByQueryString(); @@ -20,38 +19,59 @@ export default { userId: getDeviceIdValue(), sessionKey: getSessionKeyValue(), pageName: currentPageName, - sessionId: sid, + sessionId: getSessionIdValue(), action: '', event: pageEvent, - shouldUseSessionId: true, + shouldUseSessionId: false, }; baseMixin.methods.dispatchStoreAction(storeActions.LOG_PAGE_VIEW, payload, false); }, logCustomEvent(category, action, label, value) { - // if the user does not have a session id from the content site, do not log. - const sid = getSessionIdValue(); - if (sid === '00000000-0000-0000-0000-000000000000' || sid == null) { - return; + if (getSessionKeyValue() === 0 || getSessionIdValue() === '00000000-0000-0000-0000-000000000000') { + this.initSession(); } const currentPageName = getPageNameByQueryString(); + var payload = { userId: getDeviceIdValue(), sessionKey: getSessionKeyValue(), pageName: currentPageName, - sessionId: sid, + sessionId: getSessionIdValue(), category: category, action: action, label: label, value: value, - shouldUseSessionId: true, + shouldUseSessionId: false, }; baseMixin.methods.dispatchStoreAction(storeActions.LOG_CUSTOM_EVENT, payload, false); }, + async initSession() { + const sid = getSessionIdValue(); + const skey = getSessionKeyValue(); + var payload = { + userId: getDeviceIdValue(), + sessionId: sid, + userAgent: navigator.userAgent, + referrer: document.referrer, + }; + + const response = await baseMixin.methods.dispatchStoreAction(storeActions.INITIALIZE_SESSION, payload, false); + + if (response.data) { + if (response.data.sessionKey && skey === 0) { + setCookieProperties({ [cookieNames.SESSION_KEY]: response.data.sessionKey}); + } + if (response.data.sessionId && sid === '00000000-0000-0000-0000-000000000000') { + setCookieProperties({ [cookieNames.SESSION_ID]: response.data.sessionId}); + } + } + }, + pushEventToGA(category, action, label, pushToLogApp = false) { const currentPageName = getPageNameByQueryString(); const eventToBePushed = { diff --git a/src/mixins/analytics-mixin.spec.js b/src/mixins/analytics-mixin.spec.js index db773da56..7d9cec1bb 100644 --- a/src/mixins/analytics-mixin.spec.js +++ b/src/mixins/analytics-mixin.spec.js @@ -1,6 +1,7 @@ import analyticsMixin from "@/mixins/analytics-mixin"; import { setupMocksForJsFiles, setupCookies } from "@/helpers/unit-test-helper.js"; import { storeActions } from "@/constants/store-actions"; +import { analyticsPageEvents, GaCategories, GaActions, GaLabels, GaEvents } from "@/constants/analytics"; describe("analyticsMixin.js", () => { test("logPageView: calls dispatch with type and payload", () => { @@ -26,9 +27,6 @@ describe("analyticsMixin.js", () => { }); test("logCustomEvent: calls dispatch with type and payload", () => { - const type = ""; - const payload = {}; - const mockData = { actionList: [{ actionName: storeActions.LOG_CUSTOM_EVENT @@ -36,7 +34,7 @@ describe("analyticsMixin.js", () => { } const mocks = setupMocksForJsFiles(mockData); - analyticsMixin.methods.logCustomEvent(type, payload); + analyticsMixin.methods.logCustomEvent("someCat", "someAction", "someLabel", "someVal"); expect(mocks.baseMixin.methods.dispatchStoreAction).toBeCalled(); }); @@ -126,4 +124,37 @@ describe("analyticsMixin.js", () => { //Assert expect(obj!=null); }); + + test("analyticsPageEvents returns constants analyticsPageEvents", () => { + //Act + const analyticsPE = analyticsMixin.computed.analyticsPageEvents(); + + //Assert + expect(analyticsPE).toEqual(analyticsPageEvents); + }); + + test("GaActions returns constants GaActions", () => { + //Act + const gaActions = analyticsMixin.computed.GaActions(); + + //Assert + expect(gaActions).toEqual(GaActions); + }); + + test("GaCategories returns constants GaCategories", () => { + //Act + const gaCategories = analyticsMixin.computed.GaCategories(); + + //Assert + expect(gaCategories).toEqual(GaCategories); + }); + + test("GaLabels returns constants GaLabels", () => { + //Act + const gaLabels = analyticsMixin.computed.GaLabels(); + + //Assert + expect(gaLabels).toEqual(GaLabels); + }); + }); \ No newline at end of file diff --git a/src/store/index.js b/src/store/index.js index 814ca2d5b..dda0fab35 100644 --- a/src/store/index.js +++ b/src/store/index.js @@ -509,6 +509,26 @@ export const actions = { }); }, + initializeSession(context, { userId, sessionId, userAgent, referrer }) { + var payload = { + applicationName: 'SafeliteDotCom', + userId: userId, + deviceId: userId, + sessionId: sessionId, + userAgent: userAgent, + operatorId: "WEB", + userName: "SafeliteConceptFunnel", + referrer: referrer + }; + + return globalMethods.callHttpClient({ + method: endpoints.InitializeSession.method, + endpoint: endpoints.InitializeSession.url, + payload: payload, + logApiCall: false + }); + }, + GetExperimentsByUser(context, { userId }){ return globalMethods.callHttpClient({ method: endpoints.GetExperimentsByUser.method, diff --git a/src/store/store.spec.js b/src/store/store.spec.js index 24aa7cbd3..bdb49ece7 100644 --- a/src/store/store.spec.js +++ b/src/store/store.spec.js @@ -642,7 +642,7 @@ describe("Actions", () => { }); // Assert - const response = await actions.logPageView(context, { userId: "userId", sessionKey: "sessionKey", pageName: "pageName", sessionId: "sessionId", pageEvent: pageEvent, shouldUseSessionId: true }); + const response = await actions.logPageView(context, { userId: "userId", sessionKey: "sessionKey", pageName: "pageName", sessionId: "sessionId", pageEvent: pageEvent, shouldUseSessionId: false }); expect(response).toEqual({}); }); @@ -663,7 +663,22 @@ describe("Actions", () => { }); // Assert - const response = await actions.logCustomEvent(context, { userId: "userId", sessionKey: "sessionKey", pageName: "pageName", sessionId: "sessionId", customEvent: customEvent, shouldUseSessionId: true }); + const response = await actions.logCustomEvent(context, { userId: "userId", sessionKey: "sessionKey", pageName: "pageName", sessionId: "sessionId", customEvent: customEvent, shouldUseSessionId: false }); + expect(response).toEqual({}); + }); + + it("initializeSession action, should return nothing", async () => { + + // Arrange + const context = state; + + // Act + globalMethods.callHttpClient.mockImplementation(() => { + return Promise.resolve({ }); + }); + + // Assert + const response = await actions.initializeSession(context, { userId: "userId", sessionId: "", userAgent: "", referrer: "", shouldUseSessionId: false }); expect(response).toEqual({}); });