From edd8158148035e00b08632f72acd27b38e654511 Mon Sep 17 00:00:00 2001 From: Chloe Herd Date: Thu, 30 Nov 2023 17:35:21 -0500 Subject: [PATCH] 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;