From 9500337421aff33f0cea892526e776827062912d Mon Sep 17 00:00:00 2001 From: Matt Sykes Date: Thu, 30 Nov 2023 15:34:50 -0500 Subject: [PATCH 01/12] Make separate analytics APPLICATION_NAME --- src/constants/application-config.js | 1 + src/store/index.js | 6 +++--- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/constants/application-config.js b/src/constants/application-config.js index b5233d170..1e2324ba7 100644 --- a/src/constants/application-config.js +++ b/src/constants/application-config.js @@ -6,6 +6,7 @@ const applicationConfig = { COOKIE_PATH: "/", CURRENT_ENVIRONMENT: process.env.VUE_APP_CURRENT_ENVIRONMENT, // "Localhost", "Dev", "QA", and "Prod" APPLICATION_NAME: "FixMyGlass", + ANALYTICS_APPLICATION_NAME: "FixMyGlassNextGen", APPLICATION_ABBREVIATION: "fmg", PAGE_QUERYSTRING: "fmgPage", SITE_ENTRY_TRIGGER_VALUE: "FixMyGlass", diff --git a/src/store/index.js b/src/store/index.js index efaf5b428..684ca83e1 100644 --- a/src/store/index.js +++ b/src/store/index.js @@ -1076,7 +1076,7 @@ export const actions = { sessionKey: sessionKey, sessionId: sessionId, pageName: pageName, - applicationName: applicationConfig.APPLICATION_NAME, + applicationName: applicationConfig.ANALYTICS_APPLICATION_NAME, action: action, event: event, shouldUseSessionId: shouldUseSessionId, @@ -1124,7 +1124,7 @@ export const actions = { sessionKey: sessionKey, sessionId: sessionId, pageName: pageName, - applicationName: applicationConfig.APPLICATION_NAME, + applicationName: applicationConfig.ANALYTICS_APPLICATION_NAME, category: category, action: action, label: label, @@ -1153,7 +1153,7 @@ export const actions = { }, initializeSession(context, { userId, sessionId, userAgent, referrer }) { var payload = { - applicationName: applicationConfig.APPLICATION_NAME, + applicationName: applicationConfig.ANALYTICS_APPLICATION_NAME, userId: userId, deviceId: userId, sessionId: sessionId, From a79a3204841152a2d1b859ac16e3e8f8b1cee25c Mon Sep 17 00:00:00 2001 From: Chloe Herd Date: Thu, 30 Nov 2023 16:28:20 -0500 Subject: [PATCH 02/12] Add new cookie ids --- src/constants/cookie-names.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/constants/cookie-names.js b/src/constants/cookie-names.js index 5fab52899..0d8d0b05f 100644 --- a/src/constants/cookie-names.js +++ b/src/constants/cookie-names.js @@ -3,6 +3,9 @@ import { applicationConfig } from "@/constants/application-config.js"; const cookieNames = { FUNNEL_SESSION_INFO: `FunnelSessionInfo-${applicationConfig.CURRENT_ENVIRONMENT}`, + FUNNEL_SESSION_KEY: `FunnelSessionKey-${applicationConfig.CURRENT_ENVIRONMENT}`, + FUNNEL_USER_ID: `FunnelUserId-${applicationConfig.CURRENT_ENVIRONMENT}`, + // Existing Safelite.com cookies DXDEV: "dxdev", SESSION_ID: "sid", From edd8158148035e00b08632f72acd27b38e654511 Mon Sep 17 00:00:00 2001 From: Chloe Herd Date: Thu, 30 Nov 2023 17:35:21 -0500 Subject: [PATCH 03/12] Iteration 1 --- src/constants/cookie-names.js | 2 +- .../heritage-integration/cookie-helper.js | 24 +++- src/mixins/analytics-mixin.js | 125 +++++++++++++++--- 3 files changed, 128 insertions(+), 23 deletions(-) diff --git a/src/constants/cookie-names.js b/src/constants/cookie-names.js index 0d8d0b05f..bf6bc130a 100644 --- a/src/constants/cookie-names.js +++ b/src/constants/cookie-names.js @@ -5,7 +5,7 @@ const cookieNames = { FUNNEL_SESSION_KEY: `FunnelSessionKey-${applicationConfig.CURRENT_ENVIRONMENT}`, FUNNEL_USER_ID: `FunnelUserId-${applicationConfig.CURRENT_ENVIRONMENT}`, - + // Existing Safelite.com cookies DXDEV: "dxdev", SESSION_ID: "sid", diff --git a/src/helpers/heritage-integration/cookie-helper.js b/src/helpers/heritage-integration/cookie-helper.js index 1ef43bff0..71a1051f2 100644 --- a/src/helpers/heritage-integration/cookie-helper.js +++ b/src/helpers/heritage-integration/cookie-helper.js @@ -82,7 +82,7 @@ export function getDeviceIdValue() { Gets value of skey cookie, returns 0 if not found. */ export function getSessionKeyValue() { - const cookieValue = getCookieValueByName(cookieNames.SESSION_KEY); + const cookieValue = getCookieValueByName(cookieNames.FUNNEL_SESSION_KEY); if (cookieValue) { return cookieValue; @@ -111,6 +111,16 @@ export function updateSessionIdCookie() { createOrUpdateCookie(cookieNames.SESSION_ID, getSessionIdValue(), { maxAge: 60 * 30 }); } +export function getUserIdValue() { + const cookieValue = getCookieValueByName(cookieNames.FUNNEL_USER_ID); + + if(cookieValue) { + return cookieValue; + } + + return "00000000-0000-0000-0000-000000000000"; +} + export function setCookieProperties( properties, { useDefaultFunnelCookieAttributes = true, maxAge, isSecure } @@ -126,6 +136,18 @@ export function setCookieProperties( } } +export function isCookieSet(name) { + const val = getCookieValueByName(name); + + return !!val; +} + +export function refreshCookieExpiration(name, expirationTime) { + if (isCookieSet(name)) { + createOrUpdateCookie(name, getCookieValueByName(name), { maxAge: expirationTime }); + } +} + /* =========================== = PRIVATE FUNCTIONS = diff --git a/src/mixins/analytics-mixin.js b/src/mixins/analytics-mixin.js index 37615f68b..65c77c44e 100644 --- a/src/mixins/analytics-mixin.js +++ b/src/mixins/analytics-mixin.js @@ -4,6 +4,9 @@ import { getDeviceIdValue, getSessionIdValue, getSessionKeyValue, + getUserIdValue, + isCookieSet, + refreshCookieExpiration, } from "@/helpers/heritage-integration/cookie-helper"; import { queryStrings } from "@/constants/query-strings"; import { experimentSettings } from "@/constants/experiments"; @@ -135,39 +138,102 @@ export default { }; }, - async initSession() { - const sid = getSessionIdValue(); - const skey = getSessionKeyValue(); - const referrer = - applicationConfig.CURRENT_ENVIRONMENT != "Localhost" ? document.referrer : null; + // async initSession() { + // const sid = getSessionIdValue(); + // const skey = getSessionKeyValue(); + // const referrer = + // applicationConfig.CURRENT_ENVIRONMENT != "Localhost" ? document.referrer : null; - var payload = { - userId: getDeviceIdValue(), - sessionId: sid, - userAgent: navigator.userAgent, - referrer: referrer, + // var payload = { + // userId: getDeviceIdValue(), + // sessionId: sid, + // userAgent: navigator.userAgent, + // referrer: 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 }, + // { + // useDefaultFunnelCookieAttributes: false, + // } + // ); + // } + // if (response?.data.sessionId && sid === "00000000-0000-0000-0000-000000000000") { + // setCookieProperties( + // { [cookieNames.SESSION_ID]: response?.data.sessionId }, + // { + // maxAge: 60 * 30, // 30 minutes + // } + // ); + // } + // } + // }, + + async initSession() { + if (!isCookieSet(cookieNames.FUNNEL_USER_ID)) { + setCookieProperties( + { + [cookieNames.FUNNEL_USER_ID]: crypto.randomUUID(), + }, + { maxAge: 7 * 24 * 60 * 60 } + ); + } + if (!isCookieSet(cookieNames.DXDEV)) { + setCookieProperties( + { + [cookieNames.DXDEV]: crypto.randomUUID(), + }, + { maxAge: 365 * 24 * 60 * 60 } + ); + } + + const userId = getUserIdValue(); // cookieNames.FUNNEL_USER_ID + const deviceId = getDeviceIdValue(); // cookieNames.DXDEV + const sessionId = getSessionIdValue(); // cookieNames.SESSION_ID + const userAgent = navigator.userAgent; // navigator.userAgent + const refferer = applicationConfig.CURRENT_ENVIRONMENT != "Localhost" ? document.referrer : null; // see above + + const payload = { + userId: userId, + deviceId: deviceId, + sessionId: sessionId, + userAgent: userAgent, + refferer: refferer, }; - const response = await baseMixin.methods.dispatchStoreAction( + const response = await baseMixin.dispatchStoreAction( storeActions.INITIALIZE_SESSION, payload, false ); - if (response?.data) { - if (response?.data.sessionKey && skey === 0) { + if(response?.data) { + if(response.data.sessionKey && !isCookieSet(cookieNames.FUNNEL_SESSION_KEY)) { setCookieProperties( - { [cookieNames.SESSION_KEY]: response?.data.sessionKey }, { - useDefaultFunnelCookieAttributes: false, + [cookieNames.FUNNEL_SESSION_KEY]: response.data.sessionKey, + }, + { + maxAge: 30 * 60 } ); } - if (response?.data.sessionId && sid === "00000000-0000-0000-0000-000000000000") { + + if(response.data.sessionId && !isCookieSet(cookieNames.SESSION_ID)) { setCookieProperties( - { [cookieNames.SESSION_ID]: response?.data.sessionId }, { - maxAge: 60 * 30, // 30 minutes + [cookieNames.SESSION_ID]: response.data.sessionId + }, + { + maxAge: 30 * 60 } ); } @@ -175,12 +241,29 @@ export default { }, noSession() { - return ( - getSessionKeyValue() === 0 || - getSessionIdValue() === "00000000-0000-0000-0000-000000000000" + return !( + isCookieSet(cookieNames.SESSION_ID) && + isCookieSet(cookieNames.DXDEV) && + isCookieSet(cookieNames.FUNNEL_SESSION_KEY) && + isCookieSet(cookieNames.FUNNEL_USER_ID) ); }, + async validateSession() { + if (this.noSession()) { + await this.initSession(); + } + + this.refreshSessionExpiration(); + }, + + refreshSessionExpiration() { + refreshCookieExpiration(cookieNames.SESSION_ID, 30 * 60); + refreshCookieExpiration(cookieNames.DXDEV, 365 * 24 * 60 * 60); + refreshCookieExpiration(cookieNames.FUNNEL_USER_ID, 7 * 24 * 60 * 60); + refreshCookieExpiration(cookieNames.FUNNEL_SESSION_KEY, 30 * 60); + }, + removeParamsFromEndpoint(endpoint) { const endpointWithoutParams = endpoint.split("?")[0]; const numSlashesBeforeParams = 6; From 4510241bddc75abffb2f91b5f2944c1b955169aa Mon Sep 17 00:00:00 2001 From: Chloe Herd Date: Fri, 1 Dec 2023 08:12:19 -0500 Subject: [PATCH 04/12] Cleanup --- .../heritage-integration/cookie-helper.js | 2 +- src/mixins/analytics-mixin.js | 56 +++---------------- src/router/index.js | 6 +- 3 files changed, 11 insertions(+), 53 deletions(-) diff --git a/src/helpers/heritage-integration/cookie-helper.js b/src/helpers/heritage-integration/cookie-helper.js index 71a1051f2..1e1db1f1e 100644 --- a/src/helpers/heritage-integration/cookie-helper.js +++ b/src/helpers/heritage-integration/cookie-helper.js @@ -114,7 +114,7 @@ export function updateSessionIdCookie() { export function getUserIdValue() { const cookieValue = getCookieValueByName(cookieNames.FUNNEL_USER_ID); - if(cookieValue) { + if (cookieValue) { return cookieValue; } diff --git a/src/mixins/analytics-mixin.js b/src/mixins/analytics-mixin.js index 65c77c44e..f793f4825 100644 --- a/src/mixins/analytics-mixin.js +++ b/src/mixins/analytics-mixin.js @@ -138,45 +138,6 @@ export default { }; }, - // async initSession() { - // const sid = getSessionIdValue(); - // const skey = getSessionKeyValue(); - // const referrer = - // applicationConfig.CURRENT_ENVIRONMENT != "Localhost" ? document.referrer : null; - - // var payload = { - // userId: getDeviceIdValue(), - // sessionId: sid, - // userAgent: navigator.userAgent, - // referrer: 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 }, - // { - // useDefaultFunnelCookieAttributes: false, - // } - // ); - // } - // if (response?.data.sessionId && sid === "00000000-0000-0000-0000-000000000000") { - // setCookieProperties( - // { [cookieNames.SESSION_ID]: response?.data.sessionId }, - // { - // maxAge: 60 * 30, // 30 minutes - // } - // ); - // } - // } - // }, - async initSession() { if (!isCookieSet(cookieNames.FUNNEL_USER_ID)) { setCookieProperties( @@ -199,7 +160,8 @@ export default { const deviceId = getDeviceIdValue(); // cookieNames.DXDEV const sessionId = getSessionIdValue(); // cookieNames.SESSION_ID const userAgent = navigator.userAgent; // navigator.userAgent - const refferer = applicationConfig.CURRENT_ENVIRONMENT != "Localhost" ? document.referrer : null; // see above + const refferer = + applicationConfig.CURRENT_ENVIRONMENT != "Localhost" ? document.referrer : null; // see above const payload = { userId: userId, @@ -209,31 +171,31 @@ export default { refferer: refferer, }; - const response = await baseMixin.dispatchStoreAction( + const response = await baseMixin.methods.dispatchStoreAction( storeActions.INITIALIZE_SESSION, payload, false ); - if(response?.data) { - if(response.data.sessionKey && !isCookieSet(cookieNames.FUNNEL_SESSION_KEY)) { + if (response?.data) { + if (response.data.sessionKey && !isCookieSet(cookieNames.FUNNEL_SESSION_KEY)) { setCookieProperties( { [cookieNames.FUNNEL_SESSION_KEY]: response.data.sessionKey, }, { - maxAge: 30 * 60 + maxAge: 30 * 60, } ); } - if(response.data.sessionId && !isCookieSet(cookieNames.SESSION_ID)) { + if (response.data.sessionId && !isCookieSet(cookieNames.SESSION_ID)) { setCookieProperties( { - [cookieNames.SESSION_ID]: response.data.sessionId + [cookieNames.SESSION_ID]: response.data.sessionId, }, { - maxAge: 30 * 60 + maxAge: 30 * 60, } ); } diff --git a/src/router/index.js b/src/router/index.js index e2188997a..7769d8a2d 100644 --- a/src/router/index.js +++ b/src/router/index.js @@ -38,11 +38,7 @@ 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(); - } else { - updateSessionIdCookie(); - } + await analyticsMixin.methods.validateSession(); if (getFunnelCookie()?.SuppressConceptFunnel) { await navigateToHeritageFunnel({ shouldSaveSession: false }); From 0ba926d20c9fa8114687a1eaa8726c9b2bcf8c28 Mon Sep 17 00:00:00 2001 From: Chloe Herd Date: Fri, 1 Dec 2023 08:37:22 -0500 Subject: [PATCH 05/12] Move all specific cookie logic to cookie-helper --- .../heritage-integration/cookie-helper.js | 80 ++++++++++++++++--- src/mixins/analytics-mixin.js | 63 ++++----------- 2 files changed, 84 insertions(+), 59 deletions(-) diff --git a/src/helpers/heritage-integration/cookie-helper.js b/src/helpers/heritage-integration/cookie-helper.js index 1e1db1f1e..56736d340 100644 --- a/src/helpers/heritage-integration/cookie-helper.js +++ b/src/helpers/heritage-integration/cookie-helper.js @@ -78,6 +78,38 @@ export function getDeviceIdValue() { return "00000000-0000-0000-0000-000000000000"; } +export function regenerateDeviceId() { + if (!isCookieSet(cookieNames.DXDEV)) { + setCookieProperties( + { + [cookieNames.DXDEV]: `did=${crypto.randomUUID()}`, + }, + { maxAge: 365 * 24 * 60 * 60 } + ); + } +} + +export function getUserIdValue() { + const cookieValue = getCookieValueByName(cookieNames.FUNNEL_USER_ID); + + if (cookieValue) { + return cookieValue; + } + + return "00000000-0000-0000-0000-000000000000"; +} + +export function regenerateUserId() { + if (!isCookieSet(cookieNames.FUNNEL_USER_ID)) { + setCookieProperties( + { + [cookieNames.FUNNEL_USER_ID]: crypto.randomUUID(), + }, + { maxAge: 7 * 24 * 60 * 60 } + ); + } +} + /* Gets value of skey cookie, returns 0 if not found. */ @@ -91,6 +123,17 @@ export function getSessionKeyValue() { return 0; } +export function setSessionKeyIfUnset(value) { + if (!isCookieSet(cookieNames.FUNNEL_SESSION_KEY)) { + setCookieProperties( + { + [cookieNames.FUNNEL_SESSION_KEY]: value, + }, + { maxAge: 30 * 60 } + ); + } +} + /* Gets value of skey cookie, returns 0 if not found. */ @@ -104,6 +147,17 @@ export function getSessionIdValue() { return "00000000-0000-0000-0000-000000000000"; } +export function setSessionIdIfUnset(value) { + if (!isCookieSet(cookieNames.SESSION_ID)) { + setCookieProperties( + { + [cookieNames.SESSION_ID]: value, + }, + { maxAge: 30 * 60 } + ); + } +} + /* Updates session ID cookie with new expiration date */ @@ -111,16 +165,6 @@ export function updateSessionIdCookie() { createOrUpdateCookie(cookieNames.SESSION_ID, getSessionIdValue(), { maxAge: 60 * 30 }); } -export function getUserIdValue() { - const cookieValue = getCookieValueByName(cookieNames.FUNNEL_USER_ID); - - if (cookieValue) { - return cookieValue; - } - - return "00000000-0000-0000-0000-000000000000"; -} - export function setCookieProperties( properties, { useDefaultFunnelCookieAttributes = true, maxAge, isSecure } @@ -142,6 +186,22 @@ export function isCookieSet(name) { return !!val; } +export function areAllSessionCookiesSet() { + return ( + isCookieSet(cookieNames.SESSION_ID) && + isCookieSet(cookieNames.DXDEV) && + isCookieSet(cookieNames.FUNNEL_SESSION_KEY) && + isCookieSet(cookieNames.FUNNEL_USER_ID) + ); +} + +export function refreshSessionExpiration() { + refreshCookieExpiration(cookieNames.SESSION_ID, 30 * 60); + refreshCookieExpiration(cookieNames.DXDEV, 365 * 24 * 60 * 60); + refreshCookieExpiration(cookieNames.FUNNEL_USER_ID, 7 * 24 * 60 * 60); + refreshCookieExpiration(cookieNames.FUNNEL_SESSION_KEY, 30 * 60); +} + export function refreshCookieExpiration(name, expirationTime) { if (isCookieSet(name)) { createOrUpdateCookie(name, getCookieValueByName(name), { maxAge: expirationTime }); diff --git a/src/mixins/analytics-mixin.js b/src/mixins/analytics-mixin.js index f793f4825..de408218c 100644 --- a/src/mixins/analytics-mixin.js +++ b/src/mixins/analytics-mixin.js @@ -6,7 +6,12 @@ import { getSessionKeyValue, getUserIdValue, isCookieSet, - refreshCookieExpiration, + regenerateDeviceId, + regenerateUserId, + refreshSessionExpiration, + areAllSessionCookiesSet, + setSessionIdIfUnset, + setSessionKeyIfUnset, } from "@/helpers/heritage-integration/cookie-helper"; import { queryStrings } from "@/constants/query-strings"; import { experimentSettings } from "@/constants/experiments"; @@ -139,22 +144,8 @@ export default { }, async initSession() { - if (!isCookieSet(cookieNames.FUNNEL_USER_ID)) { - setCookieProperties( - { - [cookieNames.FUNNEL_USER_ID]: crypto.randomUUID(), - }, - { maxAge: 7 * 24 * 60 * 60 } - ); - } - if (!isCookieSet(cookieNames.DXDEV)) { - setCookieProperties( - { - [cookieNames.DXDEV]: crypto.randomUUID(), - }, - { maxAge: 365 * 24 * 60 * 60 } - ); - } + regenerateDeviceId(); + regenerateUserId(); const userId = getUserIdValue(); // cookieNames.FUNNEL_USER_ID const deviceId = getDeviceIdValue(); // cookieNames.DXDEV @@ -178,37 +169,18 @@ export default { ); if (response?.data) { - if (response.data.sessionKey && !isCookieSet(cookieNames.FUNNEL_SESSION_KEY)) { - setCookieProperties( - { - [cookieNames.FUNNEL_SESSION_KEY]: response.data.sessionKey, - }, - { - maxAge: 30 * 60, - } - ); + if (response.data.sessionKey) { + setSessionKeyIfUnset(response.data.sessionKey); } - if (response.data.sessionId && !isCookieSet(cookieNames.SESSION_ID)) { - setCookieProperties( - { - [cookieNames.SESSION_ID]: response.data.sessionId, - }, - { - maxAge: 30 * 60, - } - ); + if (response.data.sessionId) { + setSessionIdIfUnset(response.data.sessionId); } } }, noSession() { - return !( - isCookieSet(cookieNames.SESSION_ID) && - isCookieSet(cookieNames.DXDEV) && - isCookieSet(cookieNames.FUNNEL_SESSION_KEY) && - isCookieSet(cookieNames.FUNNEL_USER_ID) - ); + return !areAllSessionCookiesSet(); }, async validateSession() { @@ -216,14 +188,7 @@ export default { await this.initSession(); } - this.refreshSessionExpiration(); - }, - - refreshSessionExpiration() { - refreshCookieExpiration(cookieNames.SESSION_ID, 30 * 60); - refreshCookieExpiration(cookieNames.DXDEV, 365 * 24 * 60 * 60); - refreshCookieExpiration(cookieNames.FUNNEL_USER_ID, 7 * 24 * 60 * 60); - refreshCookieExpiration(cookieNames.FUNNEL_SESSION_KEY, 30 * 60); + refreshSessionExpiration(); }, removeParamsFromEndpoint(endpoint) { From 921a3efbfd210c299562722b1305d7374d1ae5a7 Mon Sep 17 00:00:00 2001 From: Chloe Herd Date: Fri, 1 Dec 2023 09:08:32 -0500 Subject: [PATCH 06/12] Define cookie TTLs as constants --- src/constants/cookie-names.js | 21 +++++++++++++++- .../heritage-integration/cookie-helper.js | 24 +++++++++++-------- 2 files changed, 34 insertions(+), 11 deletions(-) diff --git a/src/constants/cookie-names.js b/src/constants/cookie-names.js index bf6bc130a..d62934cf5 100644 --- a/src/constants/cookie-names.js +++ b/src/constants/cookie-names.js @@ -12,4 +12,23 @@ const cookieNames = { SESSION_KEY: "skey", }; -export { cookieNames }; +const cookieExpirations = { + SESSION_ID: convertToSeconds({ minutes: 30 }), + DXDEV: convertToSeconds({ years: 1 }), + FUNNEL_USER_ID: convertToSeconds({ weeks: 1 }), + FUNNEL_SESSION_KEY: convertToSeconds({ minutes: 30 }), +}; + +export { cookieNames, cookieExpirations }; + +function convertToSeconds({ years, months, weeks, days, hours, minutes, seconds }) { + let total = seconds ?? 0; + total += (minutes ?? 0) * 60; + total += (hours ?? 0) * 60 * 60; + total += (days ?? 0) * 24 * 60 * 60; + total += (weeks ?? 0) * 7 * 24 * 60 * 60; + total += (months ?? 0) * 30 * 24 * 60 * 60; + total += (years ?? 0) * 365 * 24 * 60 * 60; + + return total; +} diff --git a/src/helpers/heritage-integration/cookie-helper.js b/src/helpers/heritage-integration/cookie-helper.js index 56736d340..50896cab2 100644 --- a/src/helpers/heritage-integration/cookie-helper.js +++ b/src/helpers/heritage-integration/cookie-helper.js @@ -1,4 +1,4 @@ -import { cookieNames } from "@/constants/cookie-names"; +import { cookieNames, cookieExpirations } from "@/constants/cookie-names"; import store from "@/store"; import { applicationConfig } from "@/constants/application-config"; @@ -84,7 +84,7 @@ export function regenerateDeviceId() { { [cookieNames.DXDEV]: `did=${crypto.randomUUID()}`, }, - { maxAge: 365 * 24 * 60 * 60 } + { maxAge: cookieExpirations.DXDEV } ); } } @@ -105,7 +105,7 @@ export function regenerateUserId() { { [cookieNames.FUNNEL_USER_ID]: crypto.randomUUID(), }, - { maxAge: 7 * 24 * 60 * 60 } + { maxAge: cookieExpirations.FUNNEL_USER_ID } ); } } @@ -129,7 +129,7 @@ export function setSessionKeyIfUnset(value) { { [cookieNames.FUNNEL_SESSION_KEY]: value, }, - { maxAge: 30 * 60 } + { maxAge: cookieExpirations.FUNNEL_SESSION_KEY } ); } } @@ -153,7 +153,7 @@ export function setSessionIdIfUnset(value) { { [cookieNames.SESSION_ID]: value, }, - { maxAge: 30 * 60 } + { maxAge: cookieExpirations.SESSION_ID } ); } } @@ -162,7 +162,9 @@ export function setSessionIdIfUnset(value) { Updates session ID cookie with new expiration date */ export function updateSessionIdCookie() { - createOrUpdateCookie(cookieNames.SESSION_ID, getSessionIdValue(), { maxAge: 60 * 30 }); + createOrUpdateCookie(cookieNames.SESSION_ID, getSessionIdValue(), { + maxAge: cookieExpirations.SESSION_ID, + }); } export function setCookieProperties( @@ -196,10 +198,12 @@ export function areAllSessionCookiesSet() { } export function refreshSessionExpiration() { - refreshCookieExpiration(cookieNames.SESSION_ID, 30 * 60); - refreshCookieExpiration(cookieNames.DXDEV, 365 * 24 * 60 * 60); - refreshCookieExpiration(cookieNames.FUNNEL_USER_ID, 7 * 24 * 60 * 60); - refreshCookieExpiration(cookieNames.FUNNEL_SESSION_KEY, 30 * 60); + console.log(cookieExpirations); + + refreshCookieExpiration(cookieNames.SESSION_ID, cookieExpirations.SESSION_ID); + refreshCookieExpiration(cookieNames.DXDEV, cookieExpirations.DXDEV); + refreshCookieExpiration(cookieNames.FUNNEL_USER_ID, cookieExpirations.FUNNEL_USER_ID); + refreshCookieExpiration(cookieNames.FUNNEL_SESSION_KEY, cookieExpirations.FUNNEL_SESSION_KEY); } export function refreshCookieExpiration(name, expirationTime) { From 999d274d88a9e632fa7e0242796c9bcd6b410311 Mon Sep 17 00:00:00 2001 From: Chloe Herd Date: Fri, 1 Dec 2023 09:11:10 -0500 Subject: [PATCH 07/12] Cleanup imports --- src/mixins/analytics-mixin.js | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/mixins/analytics-mixin.js b/src/mixins/analytics-mixin.js index de408218c..7b7a2d762 100644 --- a/src/mixins/analytics-mixin.js +++ b/src/mixins/analytics-mixin.js @@ -1,11 +1,9 @@ import { storeActions } from "@/constants/store-actions"; import { - setCookieProperties, getDeviceIdValue, getSessionIdValue, getSessionKeyValue, getUserIdValue, - isCookieSet, regenerateDeviceId, regenerateUserId, refreshSessionExpiration, @@ -23,7 +21,6 @@ import { GaEvents, ValueToLogTypes, } from "@/constants/analytics"; -import { cookieNames } from "@/constants/cookie-names"; import store from "@/store"; import baseMixin from "@/mixins/base-mixin"; From 648a43ba81dd21458960064d5bd60f5379622749 Mon Sep 17 00:00:00 2001 From: Chloe Herd Date: Fri, 1 Dec 2023 09:38:24 -0500 Subject: [PATCH 08/12] Remove log --- src/helpers/heritage-integration/cookie-helper.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/helpers/heritage-integration/cookie-helper.js b/src/helpers/heritage-integration/cookie-helper.js index 50896cab2..abb1c0a67 100644 --- a/src/helpers/heritage-integration/cookie-helper.js +++ b/src/helpers/heritage-integration/cookie-helper.js @@ -198,8 +198,6 @@ export function areAllSessionCookiesSet() { } export function refreshSessionExpiration() { - console.log(cookieExpirations); - refreshCookieExpiration(cookieNames.SESSION_ID, cookieExpirations.SESSION_ID); refreshCookieExpiration(cookieNames.DXDEV, cookieExpirations.DXDEV); refreshCookieExpiration(cookieNames.FUNNEL_USER_ID, cookieExpirations.FUNNEL_USER_ID); From 411976efe59d8dfdaf64b1674ade2657dff3003c Mon Sep 17 00:00:00 2001 From: Chloe Herd Date: Fri, 1 Dec 2023 09:38:40 -0500 Subject: [PATCH 09/12] Use correct deviceId in initialize --- src/store/index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/store/index.js b/src/store/index.js index 684ca83e1..55a2ba120 100644 --- a/src/store/index.js +++ b/src/store/index.js @@ -1151,11 +1151,11 @@ export const actions = { } ); }, - initializeSession(context, { userId, sessionId, userAgent, referrer }) { + initializeSession(context, { userId, deviceId, sessionId, userAgent, referrer }) { var payload = { applicationName: applicationConfig.ANALYTICS_APPLICATION_NAME, userId: userId, - deviceId: userId, + deviceId: deviceId, sessionId: sessionId, userAgent: userAgent, operatorId: "WEB", From 53aed87e1d814a2b15238a7ba7c983b0da688a33 Mon Sep 17 00:00:00 2001 From: Chloe Herd Date: Fri, 1 Dec 2023 09:42:11 -0500 Subject: [PATCH 10/12] Use correct userId for analytics logging --- src/mixins/analytics-mixin.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/mixins/analytics-mixin.js b/src/mixins/analytics-mixin.js index 7b7a2d762..8dd0c7f96 100644 --- a/src/mixins/analytics-mixin.js +++ b/src/mixins/analytics-mixin.js @@ -35,7 +35,7 @@ export default { logPageView(pageEvent) { const currentPageName = getPageNameByQueryString(); var payload = { - userId: getDeviceIdValue(), + userId: getUserIdValue(), sessionKey: getSessionKeyValue(), pageName: currentPageName, sessionId: getSessionIdValue(), @@ -54,7 +54,7 @@ export default { const currentPageName = getPageNameByQueryString(); var payload = { - userId: getDeviceIdValue(), + userId: getUserIdValue(), sessionKey: getSessionKeyValue(), pageName: currentPageName, sessionId: getSessionIdValue(), From c2996187bbed7145e2e5defef49dc1a1b877f3de Mon Sep 17 00:00:00 2001 From: Chloe Herd Date: Fri, 1 Dec 2023 10:30:15 -0500 Subject: [PATCH 11/12] Ensure session state is valid before logging --- src/mixins/analytics-mixin.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/mixins/analytics-mixin.js b/src/mixins/analytics-mixin.js index 8dd0c7f96..dcadb3f07 100644 --- a/src/mixins/analytics-mixin.js +++ b/src/mixins/analytics-mixin.js @@ -32,8 +32,10 @@ export default { return getPageNameByQueryString(); }, - logPageView(pageEvent) { + async logPageView(pageEvent) { const currentPageName = getPageNameByQueryString(); + await this.validateSession(); + var payload = { userId: getUserIdValue(), sessionKey: getSessionKeyValue(), @@ -50,8 +52,9 @@ export default { baseMixin.methods.dispatchStoreAction(storeActions.LOG_PAGE_VIEW, payload, false); }, - logCustomEvent(category, action, label, value) { + async logCustomEvent(category, action, label, value) { const currentPageName = getPageNameByQueryString(); + await this.validateSession(); var payload = { userId: getUserIdValue(), From 42d60f4e22f29c8a0208bd6fe4da56098e153da9 Mon Sep 17 00:00:00 2001 From: Chloe Herd Date: Fri, 1 Dec 2023 11:32:33 -0500 Subject: [PATCH 12/12] Update existing unit test suite --- src/helpers/unit-test-helper.js | 3 +- src/mixins/analytics-mixin.js | 16 ++++--- src/mixins/analytics-mixin.spec.js | 72 +++++++++++++++++++++++++----- 3 files changed, 74 insertions(+), 17 deletions(-) diff --git a/src/helpers/unit-test-helper.js b/src/helpers/unit-test-helper.js index 467b38c2b..123dc9d40 100644 --- a/src/helpers/unit-test-helper.js +++ b/src/helpers/unit-test-helper.js @@ -100,7 +100,8 @@ export const cookies = { someOtherCookie: "{}", dxdev: "did=21b9b94a-ec23-42c1-aaac-e2ae4e4dbffe", sid: "cba0c3d1-3c1b-4305-bb56-31aa50f58e27", - skey: "12345", + [cookieNames.FUNNEL_SESSION_KEY]: "12345", + [cookieNames.FUNNEL_USER_ID]: "11aec5e8-92ba-4dc9-a8b6-179a916d8d7a", }; // Removes test cookies for testing cookie-helper and order-helper diff --git a/src/mixins/analytics-mixin.js b/src/mixins/analytics-mixin.js index dcadb3f07..6ea36a903 100644 --- a/src/mixins/analytics-mixin.js +++ b/src/mixins/analytics-mixin.js @@ -49,7 +49,7 @@ export default { parentAccountNumber: store.getters.order.payment.parentAccountNumber, }; - baseMixin.methods.dispatchStoreAction(storeActions.LOG_PAGE_VIEW, payload, false); + await baseMixin.methods.dispatchStoreAction(storeActions.LOG_PAGE_VIEW, payload, false); }, async logCustomEvent(category, action, label, value) { @@ -71,10 +71,14 @@ export default { parentAccountNumber: store.getters.order.payment.parentAccountNumber, }; - baseMixin.methods.dispatchStoreAction(storeActions.LOG_CUSTOM_EVENT, payload, false); + await baseMixin.methods.dispatchStoreAction( + storeActions.LOG_CUSTOM_EVENT, + payload, + false + ); }, - pushEventToGA(category, action, label, pushToLogApp = false, valueToLogType = null) { + async pushEventToGA(category, action, label, pushToLogApp = false, valueToLogType = null) { const currentPageName = getPageNameByQueryString(); const labelToLog = getValueToLog(label, valueToLogType); @@ -90,11 +94,11 @@ export default { pushToDataLayerIfDefined(eventToBePushed); if (pushToLogApp) { - this.logCustomEvent(category, action, labelToLog, undefined); + await this.logCustomEvent(category, action, labelToLog, undefined); } }, - pushPageViewToGA() { + async pushPageViewToGA() { const currentPageName = getPageNameByQueryString(); const pageViewEvent = { event: GaEvents.PAGE_VIEW_EVENT, @@ -104,7 +108,7 @@ export default { pushToDataLayerIfDefined(pageViewEvent); - this.logPageView(analyticsPageEvents.ENTRY); + await this.logPageView(analyticsPageEvents.ENTRY); }, pushExperimentsToDataLayer() { diff --git a/src/mixins/analytics-mixin.spec.js b/src/mixins/analytics-mixin.spec.js index 0426ca297..8f7cfceaa 100644 --- a/src/mixins/analytics-mixin.spec.js +++ b/src/mixins/analytics-mixin.spec.js @@ -11,8 +11,20 @@ import { } from "@/constants/analytics"; import store from "@/store"; +// Mock only the regenerate functions; window.crypto is not available in testing. +jest.mock("@/helpers/heritage-integration/cookie-helper", () => { + const originalModule = jest.requireActual("@/helpers/heritage-integration/cookie-helper"); + + return { + __esModule: true, + ...originalModule, + regenerateDeviceId: jest.fn(), + regenerateUserId: jest.fn(), + }; +}); + describe("analyticsMixin.js", () => { - test("logPageView: calls dispatch with type and payload", () => { + test("logPageView: calls dispatch with type and payload", async () => { const type = ""; const payload = {}; @@ -21,6 +33,9 @@ describe("analyticsMixin.js", () => { { actionName: storeActions.LOG_PAGE_VIEW, }, + { + actionName: storeActions.INITIALIZE_SESSION, + }, ], }; const mocks = setupMocksForJsFiles(mockData); @@ -31,27 +46,35 @@ describe("analyticsMixin.js", () => { setupCookies({ funnelCookieValue: JSON.stringify(testCookieValue) }); - analyticsMixin.methods.logPageView(type, payload); + await analyticsMixin.methods.logPageView(type, payload); expect(mocks.baseMixin.methods.dispatchStoreAction).toBeCalled(); }); - test("logCustomEvent: calls dispatch with type and payload", () => { + test("logCustomEvent: calls dispatch with type and payload", async () => { const mockData = { actionList: [ { actionName: storeActions.LOG_CUSTOM_EVENT, }, + { + actionName: storeActions.INITIALIZE_SESSION, + }, ], }; const mocks = setupMocksForJsFiles(mockData); - analyticsMixin.methods.logCustomEvent("someCat", "someAction", "someLabel", "someVal"); + await analyticsMixin.methods.logCustomEvent( + "someCat", + "someAction", + "someLabel", + "someVal" + ); expect(mocks.baseMixin.methods.dispatchStoreAction).toBeCalled(); }); - test("pushEventToGA, should call dataLayer push and logCustomEvent too", () => { + test("pushEventToGA, should call dataLayer push and logCustomEvent too", async () => { // Arrange window.dataLayer = []; const mockData = { @@ -59,6 +82,9 @@ describe("analyticsMixin.js", () => { { actionName: storeActions.LOG_CUSTOM_EVENT, }, + { + actionName: storeActions.INITIALIZE_SESSION, + }, ], }; const mocks = setupMocksForJsFiles(mockData); @@ -73,14 +99,14 @@ describe("analyticsMixin.js", () => { }); // Act - analyticsMixin.methods.pushEventToGA("category", "action", "label", true); + await analyticsMixin.methods.pushEventToGA("category", "action", "label", true); // Assert expect(mockDataLayer).toEqual(expect.arrayContaining(window.dataLayer)); expect(mocks.baseMixin.methods.dispatchStoreAction).toBeCalled(); }); - test("pushEventToGA, should call dataLayer push and ValueToLogTypes.LAST_5 only logs last 5 of label", () => { + test("pushEventToGA, should call dataLayer push and ValueToLogTypes.LAST_5 only logs last 5 of label", async () => { // Arrange window.dataLayer = []; var expectedDataLayer = []; @@ -93,8 +119,21 @@ describe("analyticsMixin.js", () => { path: "/fmg/?fmgPage=", }); + const mockData = { + actionList: [ + { + actionName: storeActions.LOG_CUSTOM_EVENT, + }, + { + actionName: storeActions.INITIALIZE_SESSION, + }, + ], + }; + + const mocks = setupMocksForJsFiles(mockData); + // Act - analyticsMixin.methods.pushEventToGA( + await analyticsMixin.methods.pushEventToGA( "category", "action", "1111122222333333", @@ -106,7 +145,7 @@ describe("analyticsMixin.js", () => { expect(expectedDataLayer).toEqual(expect.arrayContaining(window.dataLayer)); }); - test("pushEventToGA, should call dataLayer push and ValueToLogTypes.LAST_5 logs only the last 3 characters for a 3 character string", () => { + test("pushEventToGA, should call dataLayer push and ValueToLogTypes.LAST_5 logs only the last 3 characters for a 3 character string", async () => { // Arrange window.dataLayer = []; var expectedDataLayer = []; @@ -119,8 +158,21 @@ describe("analyticsMixin.js", () => { path: "/fmg/?fmgPage=", }); + const mockData = { + actionList: [ + { + actionName: storeActions.LOG_CUSTOM_EVENT, + }, + { + actionName: storeActions.INITIALIZE_SESSION, + }, + ], + }; + + const mocks = setupMocksForJsFiles(mockData); + // Act - analyticsMixin.methods.pushEventToGA( + await analyticsMixin.methods.pushEventToGA( "category", "action", "111",