Skip to content

Commit

Permalink
tidy session storage code
Browse files Browse the repository at this point in the history
  • Loading branch information
tomrf1 committed Feb 3, 2025
1 parent f199637 commit f6876c1
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 32 deletions.
16 changes: 7 additions & 9 deletions support-frontend/assets/helpers/abTests/abtest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import type { Settings } from 'helpers/globalsAndSwitches/settings';
import type { IsoCountry } from 'helpers/internationalisation/country';
import type { CountryGroupId } from 'helpers/internationalisation/countryGroup';
import * as cookie from 'helpers/storage/cookie';
import * as storage from 'helpers/storage/storage';
import { getQueryParameter } from 'helpers/urls/url';
import type {
AmountsTest,
Expand All @@ -23,8 +22,9 @@ import type {
} from './models';
import { breakpoints } from './models';
import {
getParticipationsFromSession,
setLandingPageParticipations,
getSessionParticipations,
PARTICIPATIONS_KEY,
setSessionParticipations,
} from './sessionStorage';

export const testIsActive = (
Expand Down Expand Up @@ -54,7 +54,7 @@ function init({
path = window.location.pathname,
settings,
}: ABtestInitalizerData): Participations {
const sessionParticipations = getParticipationsFromSession();
const sessionParticipations = getSessionParticipations(PARTICIPATIONS_KEY);
const participations = getParticipations(
abTests,
mvt,
Expand All @@ -66,15 +66,13 @@ function init({
sessionParticipations,
);

// A landing page test config may be passed through from the server, so we handle this separately
const landingPageParticipations = getLandingPageParticipations(
countryGroupId,
path,
settings.landingPageTests,
mvt,
);
if (landingPageParticipations) {
setLandingPageParticipations(landingPageParticipations);
}

const urlParticipations = getParticipationsFromUrl();
const serverSideParticipations = getServerSideParticipations();
Expand All @@ -92,7 +90,7 @@ const MVT_COOKIE = 'GU_mvt_id';
const MVT_MAX = 1_000_000;

// Attempts to retrieve the MVT id from a cookie, or sets it.
export function getMvtId(): number {
function getMvtId(): number {
const mvtIdCookieValue = cookie.get(MVT_COOKIE);
let mvtId = Number(mvtIdCookieValue);

Expand Down Expand Up @@ -219,7 +217,7 @@ function getParticipations(
sessionParticipations[testId] = participations[testId];
}
});
storage.setSession('abParticipations', JSON.stringify(sessionParticipations));
setSessionParticipations(sessionParticipations, PARTICIPATIONS_KEY);

return participations;
}
Expand Down
17 changes: 14 additions & 3 deletions support-frontend/assets/helpers/abTests/landingPageAbTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ import type {
} from '../globalsAndSwitches/landingPageSettings';
import type { CountryGroupId } from '../internationalisation/countryGroup';
import type { Participations } from './models';
import { getLandingPageParticipationsFromSession } from './sessionStorage';
import {
getSessionParticipations,
LANDING_PAGE_PARTICIPATIONS_KEY,
setSessionParticipations,
} from './sessionStorage';

export type LandingPageSelection = LandingPageVariant & { testName: string };

Expand Down Expand Up @@ -49,14 +53,21 @@ export function getLandingPageParticipations(
const variant = test.variants[idx];

if (variant) {
return {
const participations = {
[test.name]: variant.name,
};
// Record the participation in session storage so that we can track it from the checkout
setSessionParticipations(
participations,
LANDING_PAGE_PARTICIPATIONS_KEY,
);

return participations;
}
}
} else {
// This is not a landing page, but check if the session has a landing page test participation
return getLandingPageParticipationsFromSession();
return getSessionParticipations(LANDING_PAGE_PARTICIPATIONS_KEY);
}
}

Expand Down
32 changes: 12 additions & 20 deletions support-frontend/assets/helpers/abTests/sessionStorage.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,30 @@
import * as storage from '../storage/storage';
import type { Participations } from './models';

function getParticipationsFromSession(
key: string = 'abParticipations',
): Participations | undefined {
const PARTICIPATIONS_KEY = 'abParticipations';
const LANDING_PAGE_PARTICIPATIONS_KEY = 'landingPageParticipations';
type Key = typeof PARTICIPATIONS_KEY | typeof LANDING_PAGE_PARTICIPATIONS_KEY;

function getSessionParticipations(key: Key): Participations | undefined {
const participations = storage.getSession(key);
if (participations) {
try {
return JSON.parse(participations) as Participations;
} catch (error) {
console.error(
'Failed to parse abParticipations from session storage',
error,
);
console.error(`Failed to parse ${key} from session storage`, error);
return undefined;
}
}
return undefined;
}

const landingPageParticipationsKey = 'landingPageParticipations';
function getLandingPageParticipationsFromSession(): Participations | undefined {
return getParticipationsFromSession(landingPageParticipationsKey);
}

function setLandingPageParticipations(participations: Participations) {
storage.setSession(
landingPageParticipationsKey,
JSON.stringify(participations),
);
function setSessionParticipations(participations: Participations, key: Key) {
storage.setSession(key, JSON.stringify(participations));
}

export {
getParticipationsFromSession,
getLandingPageParticipationsFromSession,
setLandingPageParticipations,
getSessionParticipations,
setSessionParticipations,
PARTICIPATIONS_KEY,
LANDING_PAGE_PARTICIPATIONS_KEY,
};

0 comments on commit f6876c1

Please sign in to comment.