Skip to content

Commit c62dfa7

Browse files
prevent duplicate checks on same run
1 parent f0ccee6 commit c62dfa7

File tree

2 files changed

+41
-3
lines changed

2 files changed

+41
-3
lines changed

packages/sources/walkerjs/src/__tests__/session.test.ts

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { Walkerjs } from '..';
1+
import type { SourceWalkerjs } from '..';
2+
import { elb, Walkerjs } from '..';
23
import { mockDataLayer } from '@elbwalker/jest/web.setup';
34
import { sessionStart } from '@elbwalker/utils/web';
4-
import type { SourceWalkerjs } from '..';
55

66
jest.mock('@elbwalker/utils/web', () => {
77
const utilsOrg = jest.requireActual('@elbwalker/utils/web');
@@ -78,4 +78,33 @@ describe('Session', () => {
7878

7979
expect(mockFn).toHaveBeenCalledTimes(2);
8080
});
81+
82+
test('multiple consent updates', () => {
83+
const originalLocation = window.location;
84+
Object.defineProperty(window, 'location', {
85+
value: new URL('https://www.elbwalker.com/?utm_campaign=foo'),
86+
});
87+
88+
walkerjs = Walkerjs({
89+
default: true,
90+
session: { consent: 'marketing', storage: true },
91+
pageview: false,
92+
});
93+
94+
expect(mockDataLayer).toHaveBeenCalledTimes(0);
95+
elb('walker consent', { marketing: true });
96+
elb('walker consent', { marketing: true });
97+
98+
expect(mockDataLayer).toHaveBeenCalledTimes(1);
99+
expect(mockDataLayer).toHaveBeenCalledWith(
100+
expect.objectContaining({
101+
event: 'session start',
102+
data: expect.any(Object),
103+
}),
104+
);
105+
106+
Object.defineProperty(window, 'location', {
107+
value: originalLocation,
108+
});
109+
});
81110
});

packages/sources/walkerjs/src/lib/session.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,28 @@ export function sessionStart(
2626
const sessionConfig = assign(instance.config.session || {}, options.config);
2727
const sessionData = assign(instance.config.sessionStatic, options.data);
2828

29+
// Track the last processed group to prevent duplicate processing
30+
let lastProcessedGroup: string;
31+
2932
// A wrapper for the callback
3033
const cb: SessionCallback = (session, instance, defaultCb) => {
34+
// Skip if we've already processed this group
35+
if (instance && instance.group === lastProcessedGroup) return;
36+
3137
let result: void | undefined | WalkerOS.SessionData;
3238
if (sessionConfig.cb !== false)
3339
// Run either the default callback or the provided one
3440
result = (sessionConfig.cb || defaultCb)(session, instance, defaultCb);
3541

3642
if (isSameType(instance, {} as SourceWalkerjs.Instance)) {
43+
// Remember this group has been processed
44+
lastProcessedGroup = instance.group;
45+
3746
// Assign the session
3847
instance.session = session;
3948

4049
// Run on session events
41-
onApply(instance as SourceWalkerjs.Instance, 'session');
50+
onApply(instance, 'session');
4251
}
4352

4453
return result;

0 commit comments

Comments
 (0)