From 15ed8aad391a33334afb16e9c360b217756933e4 Mon Sep 17 00:00:00 2001 From: CarlNation Date: Thu, 2 Jun 2022 11:25:36 -0400 Subject: [PATCH 1/4] CSR-663 Make sure the create session only runs once move to router --- src/mixins/analytics-mixin.js | 37 +++--------------------------- src/router/index.js | 42 ++++++++++++++++++++++++++++++++++- 2 files changed, 44 insertions(+), 35 deletions(-) diff --git a/src/mixins/analytics-mixin.js b/src/mixins/analytics-mixin.js index fda94e79d..466407a61 100644 --- a/src/mixins/analytics-mixin.js +++ b/src/mixins/analytics-mixin.js @@ -1,19 +1,14 @@ import { storeActions } from "@/constants/store-actions"; -import { setCookieProperties, getDeviceIdValue, getSessionIdValue, getSessionKeyValue } from "@/helpers/heritage-integration/cookie-helper"; +import { 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 (getSessionKeyValue() === 0 || getSessionIdValue() === '00000000-0000-0000-0000-000000000000') { - this.initSession(); - } - const currentPageName = getPageNameByQueryString(); var payload = { userId: getDeviceIdValue(), @@ -29,10 +24,6 @@ export default { }, logCustomEvent(category, action, label, value) { - if (getSessionKeyValue() === 0 || getSessionIdValue() === '00000000-0000-0000-0000-000000000000') { - this.initSession(); - } - const currentPageName = getPageNameByQueryString(); var payload = { @@ -50,28 +41,6 @@ export default { 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 = { @@ -86,9 +55,9 @@ export default { pushToDataLayerIfDefined(eventToBePushed); if (pushToLogApp) { - this.logCustomEvent(category, action, label, undefined); + this.logCustomEvent(category, action, label, undefined); } - + }, pushPageViewToGA() { diff --git a/src/router/index.js b/src/router/index.js index c8d614d48..5f35b7918 100644 --- a/src/router/index.js +++ b/src/router/index.js @@ -5,7 +5,8 @@ import { lazyLoadComponent } from "@/router/dynamic-routing/component-loader.js" import { routingTable } from "@/router/router-constants/routing-table.js"; import { globalEvents, globalEventTypes } from "@/constants/events"; import { queryStrings } from "@/constants/query-strings"; -import { getDeviceIdValue } from "@/helpers/heritage-integration/cookie-helper"; +import { setCookieProperties, getDeviceIdValue, getSessionIdValue, getSessionKeyValue } from "@/helpers/heritage-integration/cookie-helper"; +import { cookieNames } from "@/constants/cookie-names"; // Heritage integration import { isSavedSessionStillActive } from "@/helpers/heritage-integration/session-helper"; @@ -46,6 +47,10 @@ const routes = [ async beforeEnter(to, from, next) { // If we have no query string, or we don't have the FmgPage query string. try { + if (noSession()) { + await initSession(); + } + // If the saved session has timed out, clear the session, execute 404 logic. if (getFunnelCookie() !== null && !isSavedSessionStillActive()) { await GoToFunnelStartOn404(next); @@ -244,6 +249,41 @@ async function GetRouteInfoFromPageName(pageName) { return routeData; } +async function 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}); + } + } + console.log("router:" + getSessionKeyValue()); + + return true; +} + +function noSession() { + if (getSessionKeyValue() === 0 || getSessionIdValue() === '00000000-0000-0000-0000-000000000000') { + return true; + } + else + { + return false; + } +} + // Go to our start page on a 404. async function GoToFunnelStartOn404(next) { const apiResponse = await store.dispatch(storeActions.GET_HOMEPAGE_NAME); From 4111f53d4cfb187bb19ea3cef035ba6c8a085594 Mon Sep 17 00:00:00 2001 From: CarlNation Date: Thu, 2 Jun 2022 11:28:05 -0400 Subject: [PATCH 2/4] CSR-663 remove console.log --- src/router/index.js | 1 - 1 file changed, 1 deletion(-) diff --git a/src/router/index.js b/src/router/index.js index 5f35b7918..881d0607d 100644 --- a/src/router/index.js +++ b/src/router/index.js @@ -269,7 +269,6 @@ async function initSession() { setCookieProperties({ [cookieNames.SESSION_ID]: response.data.sessionId}); } } - console.log("router:" + getSessionKeyValue()); return true; } From 2dedad7a44c5f742326b2de43344a2bd9f0722e8 Mon Sep 17 00:00:00 2001 From: CarlNation Date: Thu, 2 Jun 2022 13:57:08 -0400 Subject: [PATCH 3/4] CSR-663 move functions to analytics-mixin --- src/mixins/analytics-mixin.js | 37 +++++++++++++++++++++++++++++-- src/router/index.js | 41 +++-------------------------------- 2 files changed, 38 insertions(+), 40 deletions(-) diff --git a/src/mixins/analytics-mixin.js b/src/mixins/analytics-mixin.js index 466407a61..347a0bb92 100644 --- a/src/mixins/analytics-mixin.js +++ b/src/mixins/analytics-mixin.js @@ -1,8 +1,9 @@ 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"; @@ -57,7 +58,7 @@ export default { if (pushToLogApp) { this.logCustomEvent(category, action, label, undefined); } - + }, pushPageViewToGA() { @@ -105,6 +106,38 @@ export default { return baseMethod.apply(object, arguments); }; }, + + 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}); + } + } + }, + + noSession() { + if (getSessionKeyValue() === 0 || getSessionIdValue() === '00000000-0000-0000-0000-000000000000') { + return true; + } + else + { + return false; + } + } }, computed: { analyticsPageEvents() { diff --git a/src/router/index.js b/src/router/index.js index 881d0607d..800473526 100644 --- a/src/router/index.js +++ b/src/router/index.js @@ -5,8 +5,7 @@ import { lazyLoadComponent } from "@/router/dynamic-routing/component-loader.js" import { routingTable } from "@/router/router-constants/routing-table.js"; import { globalEvents, globalEventTypes } from "@/constants/events"; import { queryStrings } from "@/constants/query-strings"; -import { setCookieProperties, getDeviceIdValue, getSessionIdValue, getSessionKeyValue } from "@/helpers/heritage-integration/cookie-helper"; -import { cookieNames } from "@/constants/cookie-names"; +import { getDeviceIdValue } from "@/helpers/heritage-integration/cookie-helper"; // Heritage integration import { isSavedSessionStillActive } from "@/helpers/heritage-integration/session-helper"; @@ -47,8 +46,8 @@ const routes = [ async beforeEnter(to, from, next) { // If we have no query string, or we don't have the FmgPage query string. try { - if (noSession()) { - await initSession(); + if (analyticsMixin.methods.noSession()) { + await analyticsMixin.methods.initSession(); } // If the saved session has timed out, clear the session, execute 404 logic. @@ -249,40 +248,6 @@ async function GetRouteInfoFromPageName(pageName) { return routeData; } -async function 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}); - } - } - - return true; -} - -function noSession() { - if (getSessionKeyValue() === 0 || getSessionIdValue() === '00000000-0000-0000-0000-000000000000') { - return true; - } - else - { - return false; - } -} - // Go to our start page on a 404. async function GoToFunnelStartOn404(next) { const apiResponse = await store.dispatch(storeActions.GET_HOMEPAGE_NAME); From 7350835e9389b1e09adfba3ff7716a970844b09a Mon Sep 17 00:00:00 2001 From: CarlNation Date: Thu, 2 Jun 2022 14:11:08 -0400 Subject: [PATCH 4/4] CSR-663 shorthand return --- src/mixins/analytics-mixin.js | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/src/mixins/analytics-mixin.js b/src/mixins/analytics-mixin.js index 347a0bb92..ffe67735d 100644 --- a/src/mixins/analytics-mixin.js +++ b/src/mixins/analytics-mixin.js @@ -130,13 +130,7 @@ export default { }, noSession() { - if (getSessionKeyValue() === 0 || getSessionIdValue() === '00000000-0000-0000-0000-000000000000') { - return true; - } - else - { - return false; - } + return getSessionKeyValue() === 0 || getSessionIdValue() === '00000000-0000-0000-0000-000000000000'; } }, computed: {