Skip to content

Commit 6e533b4

Browse files
committed
refactor to checkAndGetSessionAndWindowId
1 parent ff203e9 commit 6e533b4

File tree

6 files changed

+26
-31
lines changed

6 files changed

+26
-31
lines changed

src/__tests__/extensions/sessionrecording.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ describe('SessionRecording', () => {
2626
persistence: { register: jest.fn() },
2727
_captureMetrics: { incr: jest.fn() },
2828
sessionManager: {
29-
getSessionAndWindowId: jest.fn().mockImplementation(() => given.incomingSessionAndWindowId),
29+
checkAndGetSessionAndWindowId: jest.fn().mockImplementation(() => given.incomingSessionAndWindowId),
3030
},
3131
_addCaptureHook: jest.fn(),
3232
}))
@@ -321,17 +321,20 @@ describe('SessionRecording', () => {
321321
expect(window.rrwebRecord.takeFullSnapshot).not.toHaveBeenCalled()
322322
})
323323

324-
it('it calls getSessionAndWindowId with shouldExtendExistingSessionOrTriggerNewOne as false if it not a user interaction', () => {
324+
it('it calls checkAndGetSessionAndWindowId with readOnly as true if it not a user interaction', () => {
325325
_emit(NON_USER_GENERATED_EVENT)
326-
expect(given.posthog.sessionManager.getSessionAndWindowId).toHaveBeenCalledWith(false, undefined)
326+
expect(given.posthog.sessionManager.checkAndGetSessionAndWindowId).toHaveBeenCalledWith(true, undefined)
327327
})
328328

329-
it('it calls getSessionAndWindowId with shouldExtendExistingSessionOrTriggerNewOne as true if it is a user interaction', () => {
329+
it('it calls checkAndGetSessionAndWindowId with readOnly as false if it is a user interaction', () => {
330330
_emit({
331331
event: 123,
332332
type: INCREMENTAL_SNAPSHOT_EVENT_TYPE,
333333
})
334-
expect(given.posthog.sessionManager.getSessionAndWindowId).toHaveBeenCalledWith(true, undefined)
334+
expect(given.posthog.sessionManager.checkAndGetSessionAndWindowId).toHaveBeenCalledWith(
335+
false,
336+
undefined
337+
)
335338
})
336339
})
337340
})

src/__tests__/posthog-core.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,7 @@ describe('_calculate_event_properties()', () => {
396396
properties: () => ({ distinct_id: 'abc', persistent: 'prop' }),
397397
},
398398
sessionManager: {
399-
getSessionAndWindowId: jest.fn().mockReturnValue({
399+
checkAndGetSessionAndWindowId: jest.fn().mockReturnValue({
400400
windowId: 'windowId',
401401
sessionId: 'sessionId',
402402
}),
@@ -444,7 +444,7 @@ describe('_calculate_event_properties()', () => {
444444
event: 'prop',
445445
distinct_id: 'abc',
446446
})
447-
expect(given.overrides.sessionManager.getSessionAndWindowId).not.toHaveBeenCalled()
447+
expect(given.overrides.sessionManager.checkAndGetSessionAndWindowId).not.toHaveBeenCalled()
448448
})
449449

450450
it('calls sanitize_properties', () => {

src/__tests__/sessionid.js

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@ jest.mock('../utils')
77
jest.mock('../storage')
88

99
describe('Session ID manager', () => {
10-
given('subject', () =>
11-
given.sessionIdManager.getSessionAndWindowId(given.shouldExtendExistingSessionOrTriggerNewOne, given.timestamp)
12-
)
10+
given('subject', () => given.sessionIdManager.checkAndGetSessionAndWindowId(given.readOnly, given.timestamp))
1311
given('sessionIdManager', () => new SessionIdManager(given.config, given.persistence))
1412

1513
given('persistence', () => ({
@@ -42,8 +40,8 @@ describe('Session ID manager', () => {
4240
expect(sessionStore.set).toHaveBeenCalledWith('ph_persistance-name_window_id', 'newUUID')
4341
})
4442

45-
it('generates an initial session id and window id, and saves them even if shouldExtendExistingSessionOrTriggerNewOne is false', () => {
46-
given('shouldExtendExistingSessionOrTriggerNewOne', () => false)
43+
it('generates an initial session id and window id, and saves them even if readOnly is true', () => {
44+
given('readOnly', () => true)
4745
expect(given.subject).toMatchObject({
4846
windowId: 'newUUID',
4947
sessionId: 'newUUID',
@@ -67,10 +65,10 @@ describe('Session ID manager', () => {
6765
expect(given.persistence.register).toHaveBeenCalledWith({ [SESSION_ID]: [given.timestamp, 'oldSessionID'] })
6866
})
6967

70-
it('reuses old ids and does not update the session timestamp if > 30m pass and shouldExtendExistingSessionOrTriggerNewOne is false', () => {
68+
it('reuses old ids and does not update the session timestamp if > 30m pass and readOnly is true', () => {
7169
const old_timestamp = 1602107460000
7270
given('storedSessionIdData', () => [old_timestamp, 'oldSessionID'])
73-
given('shouldExtendExistingSessionOrTriggerNewOne', () => false)
71+
given('readOnly', () => true)
7472

7573
expect(given.subject).toEqual({
7674
windowId: 'oldWindowID',

src/extensions/sessionrecording.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,12 +98,11 @@ export class SessionRecording {
9898
// Some recording events are triggered by non-user events (e.g. "X minutes ago" text updating on the screen).
9999
// We don't want to extend the session or trigger a new session in these cases. These events are designated by event
100100
// type -> incremental update, and source -> mutation.
101-
const isUserInteraction = !(
101+
const isNotUserInteraction =
102102
event.type === INCREMENTAL_SNAPSHOT_EVENT_TYPE && event.data?.source === MUTATION_SOURCE_TYPE
103-
)
104103

105-
const { windowId, sessionId } = this.instance.sessionManager.getSessionAndWindowId(
106-
isUserInteraction,
104+
const { windowId, sessionId } = this.instance.sessionManager.checkAndGetSessionAndWindowId(
105+
isNotUserInteraction,
107106
event.timestamp
108107
)
109108

src/posthog-core.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -657,7 +657,7 @@ PostHogLib.prototype._calculate_event_properties = function (event_name, event_p
657657
}
658658

659659
if (this.sessionManager) {
660-
const { sessionId, windowId } = this.sessionManager.getSessionAndWindowId()
660+
const { sessionId, windowId } = this.sessionManager.checkAndGetSessionAndWindowId()
661661
properties['$session_id'] = sessionId
662662
properties['$window_id'] = windowId
663663
}
@@ -1499,7 +1499,7 @@ PostHogLib.prototype.sentry_integration = function (_posthog, organization, proj
14991499
event.tags['PostHog Recording URL'] =
15001500
_posthog.config.api_host +
15011501
'/recordings/#sessionRecordingId=' +
1502-
_posthog.sessionManager.getSessionAndWindowId(false).sessionId
1502+
_posthog.sessionManager.checkAndGetSessionAndWindowId(true).sessionId
15031503
}
15041504
let data = {
15051505
$sentry_event_id: event.event_id,

src/sessionid.js

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ export class SessionIdManager {
5555
return this.persistence['props'][SESSION_ID] || [0, null]
5656
}
5757

58-
// Resets the session id by setting it to null. On the subsequent call to getSessionAndWindowId,
58+
// Resets the session id by setting it to null. On the subsequent call to checkAndGetSessionAndWindowId,
5959
// new ids will be generated.
6060
resetSessionId() {
6161
this._setSessionId(null, null)
@@ -68,34 +68,29 @@ export class SessionIdManager {
6868
* sessionId and windowId when appropriate by doing the following:
6969
*
7070
* 1. If the sessionId or windowId is not set, it will generate a new one and store it.
71-
* 2. If the shouldExtendExistingSessionOrTriggerNewOne param is set, it will:
71+
* 2. If the readOnly param is set to false, it will:
7272
* a. Check if it has been > SESSION_CHANGE_THRESHOLD since the last call with this flag set.
7373
* If so, it will generate a new sessionId and store it.
7474
* b. Update the timestamp stored with the sessionId to ensure the current session is extended
7575
* for the appropriate amount of time.
7676
*
77-
* @param {boolean} shouldExtendExistingSessionOrTriggerNewOne (optional) Defaults to True. Should be set to False when the call to the function should be read only (e.g. being called for non-user generated events)
77+
* @param {boolean} readOnly (optional) Defaults to False. Should be set to True when the call to the function should not extend or cycle the session (e.g. being called for non-user generated events)
7878
* @param {Number} timestamp (optional) Defaults to the current time. The timestamp to be stored with the sessionId (used when determining if a new sessionId should be generated)
7979
*/
80-
getSessionAndWindowId(shouldExtendExistingSessionOrTriggerNewOne = true, timestamp = null) {
80+
checkAndGetSessionAndWindowId(readOnly = false, timestamp = null) {
8181
timestamp = timestamp || new Date().getTime()
8282

8383
let [lastTimestamp, sessionId] = this._getSessionId()
8484
let windowId = this._getWindowId()
8585

86-
if (
87-
!sessionId ||
88-
(shouldExtendExistingSessionOrTriggerNewOne &&
89-
Math.abs(timestamp - lastTimestamp) > SESSION_CHANGE_THRESHOLD)
90-
) {
86+
if (!sessionId || (!readOnly && Math.abs(timestamp - lastTimestamp) > SESSION_CHANGE_THRESHOLD)) {
9187
sessionId = _.UUID()
9288
windowId = _.UUID()
9389
} else if (!windowId) {
9490
windowId = _.UUID()
9591
}
9692

97-
const newTimestamp =
98-
lastTimestamp === 0 || shouldExtendExistingSessionOrTriggerNewOne ? timestamp : lastTimestamp
93+
const newTimestamp = lastTimestamp === 0 || !readOnly ? timestamp : lastTimestamp
9994

10095
this._setWindowId(windowId)
10196
this._setSessionId(sessionId, newTimestamp)

0 commit comments

Comments
 (0)