Skip to content

Commit 5ab3ad8

Browse files
group session check
1 parent c62dfa7 commit 5ab3ad8

File tree

3 files changed

+23
-9
lines changed

3 files changed

+23
-9
lines changed

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ describe('Session', () => {
9494
expect(mockDataLayer).toHaveBeenCalledTimes(0);
9595
elb('walker consent', { marketing: true });
9696
elb('walker consent', { marketing: true });
97+
elb('walker consent', { marketing: true });
9798

9899
expect(mockDataLayer).toHaveBeenCalledTimes(1);
99100
expect(mockDataLayer).toHaveBeenCalledWith(
@@ -103,6 +104,17 @@ describe('Session', () => {
103104
}),
104105
);
105106

107+
elb('walker run');
108+
expect(mockDataLayer).toHaveBeenCalledTimes(2);
109+
expect(mockDataLayer).toHaveBeenCalledWith(
110+
expect.objectContaining({
111+
event: 'session start',
112+
data: expect.objectContaining({
113+
count: 2,
114+
}),
115+
}),
116+
);
117+
106118
Object.defineProperty(window, 'location', {
107119
value: originalLocation,
108120
});

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

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,23 +26,14 @@ 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-
3229
// A wrapper for the callback
3330
const cb: SessionCallback = (session, instance, defaultCb) => {
34-
// Skip if we've already processed this group
35-
if (instance && instance.group === lastProcessedGroup) return;
36-
3731
let result: void | undefined | WalkerOS.SessionData;
3832
if (sessionConfig.cb !== false)
3933
// Run either the default callback or the provided one
4034
result = (sessionConfig.cb || defaultCb)(session, instance, defaultCb);
4135

4236
if (isSameType(instance, {} as SourceWalkerjs.Instance)) {
43-
// Remember this group has been processed
44-
lastProcessedGroup = instance.group;
45-
4637
// Assign the session
4738
instance.session = session;
4839

packages/utils/src/web/session/sessionStart.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type { WalkerOS } from '@elbwalker/types';
22
import type { SessionStorageConfig } from './';
33
import { sessionStorage, sessionWindow } from './';
44
import { elb as elbOrg } from '../elb';
5+
import { isDefined } from '../../core/is';
56

67
export interface SessionConfig extends SessionStorageConfig {
78
consent?: string;
@@ -47,7 +48,17 @@ function callFuncAndCb(
4748
}
4849

4950
function onConsentFn(config: SessionConfig, cb?: SessionCallback | false) {
51+
// Track the last processed group to prevent duplicate processing
52+
let lastProcessedGroup: string | undefined;
53+
5054
const func = (instance: WalkerOS.Instance, consent: WalkerOS.Consent) => {
55+
// Skip if we've already processed this group
56+
if (isDefined(lastProcessedGroup) && lastProcessedGroup === instance?.group)
57+
return;
58+
59+
// Remember this group has been processed
60+
lastProcessedGroup = instance?.group;
61+
5162
let sessionFn: SessionFunction = () => sessionWindow(config); // Window by default
5263

5364
if (config.consent && consent[config.consent])

0 commit comments

Comments
 (0)