Skip to content

Commit

Permalink
Merge pull request #343 from elbwalker/342-sessionstorage-with-new-ut…
Browse files Browse the repository at this point in the history
…m-entry

342 sessionstorage with new utm entry
  • Loading branch information
alexanderkirtzel committed Mar 20, 2024
2 parents 04ec9a6 + c85eaff commit 7c61940
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 2 deletions.
7 changes: 7 additions & 0 deletions .changeset/happy-dryers-cover.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@elbwalker/walker.js': patch
'@elbwalker/utils': patch
---

sessionStorage with new UTM entry
[#342](https://github.com/elbwalker/walkerOS/issues/342)
2 changes: 1 addition & 1 deletion .changeset/sharp-dodos-judge.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@
'@elbwalker/utils': patch
---

[devDeps to deps #340](https://github.com/elbwalker/walkerOS/issues/340)
devDeps to deps [#340](https://github.com/elbwalker/walkerOS/issues/340)
46 changes: 46 additions & 0 deletions packages/utils/src/__tests__/web/sessionStorage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,4 +211,50 @@ describe('SessionStorage', () => {
}),
);
});

test('Existing active session with new UTM entry', () => {
const start = Date.now();
const session = {
isStart: false,
storage: true,
id: 'sessionId',
start,
referrer: 'org',
updated: start,
marketing: true,
campaign: 'old',
isNew: true,
count: 1,
runs: 5,
};

mockStorageRead.mockImplementation((config) => {
return { ...config.data, mock: 'window' };
});

mockStorageRead
.mockReturnValue(JSON.stringify(session))
.mockReturnValueOnce(device);
jest.advanceTimersByTime(1000);

const newSession = sessionStorage({
url: 'https://www.elbwalker.com/?utm_campaign=new',
});

expect(newSession.id).not.toBe(session.id); // Expect a new session id
expect(newSession).toStrictEqual({
storage: true,
device,
start: start + 1000,
updated: start + 1000,
isStart: true,
marketing: true,
referrer: '',
campaign: 'new',
id: expect.any(String),
isNew: false,
count: 2,
runs: 1,
});
});
});
9 changes: 8 additions & 1 deletion packages/utils/src/web/session/sessionStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export function sessionStorage(config: SessionStorageConfig = {}): SessionData {
const sessionKey = config.sessionKey || 'elbSessionId';
const sessionStorage = config.sessionStorage || 'local';
const sessionAge = config.sessionAge || 30; // Session age in minutes
const windowSession = sessionWindow(config); // Status based on window only
let isStart = !!config.isStart;

// Check for an existing session
Expand All @@ -52,6 +53,12 @@ export function sessionStorage(config: SessionStorageConfig = {}): SessionData {
// By default it's not a new session anymore
existingSession.isNew = false;

// A new marketing entry
if (windowSession.marketing) {
Object.assign(existingSession, windowSession); // Overwrite existing session with marketing data
isStart = true; // This is a session start
}

// Check if session is still active
if (isStart || existingSession.updated + length * 60 * 1000 < now) {
// Session has expired
Expand Down Expand Up @@ -95,7 +102,7 @@ export function sessionStorage(config: SessionStorageConfig = {}): SessionData {
// Eventually update session with id, referrer and marketing parameters
session = Object.assign(
session, // Default session values
sessionWindow(config), // Basic session data
windowSession, // Basic session data based on window
existingSession, // (Updated) existing session
{ device }, // Device Id
{ isStart: config.isStart, storage: true }, // Status of the session
Expand Down

0 comments on commit 7c61940

Please sign in to comment.