diff --git a/src/mixins/analytics-mixin.js b/src/mixins/analytics-mixin.js index fda94e79d..ffe67735d 100644 --- a/src/mixins/analytics-mixin.js +++ b/src/mixins/analytics-mixin.js @@ -10,10 +10,6 @@ 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 +25,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 +42,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,7 +56,7 @@ export default { pushToDataLayerIfDefined(eventToBePushed); if (pushToLogApp) { - this.logCustomEvent(category, action, label, undefined); + this.logCustomEvent(category, action, label, undefined); } }, @@ -136,6 +106,32 @@ 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() { + return getSessionKeyValue() === 0 || getSessionIdValue() === '00000000-0000-0000-0000-000000000000'; + } }, computed: { analyticsPageEvents() { diff --git a/src/router/index.js b/src/router/index.js index db46a3306..74ee110de 100644 --- a/src/router/index.js +++ b/src/router/index.js @@ -40,6 +40,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 (analyticsMixin.methods.noSession()) { + await analyticsMixin.methods.initSession(); + } + // If the saved session has timed out, clear the session, execute 404 logic. if (getFunnelCookie() !== null && !isSavedSessionStillActive()) { await GoToFunnelStartOn404(next);