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) {