Iteration 1

This commit is contained in:
Chloe Herd 2023-11-30 17:35:21 -05:00
parent a79a320484
commit edd8158148
3 changed files with 128 additions and 23 deletions

View file

@ -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",

View file

@ -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 =

View file

@ -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;