Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #96 from PostHog/session-id-generation
Send/create $session_id on $snapshot events
- Loading branch information
Showing
5 changed files
with
82 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import sessionIdGenerator from '../../extensions/sessionid' | ||
import { SESSION_ID } from '../../posthog-persistence' | ||
import { _ } from '../../utils' | ||
|
||
jest.mock('../../utils') | ||
|
||
describe('Session ID generation', () => { | ||
given('subject', () => sessionIdGenerator(given.persistence, given.timestamp)) | ||
|
||
given('timestamp', () => 1603107479471) | ||
|
||
given('persistence', () => ({ | ||
props: { [SESSION_ID]: given.recordedData }, | ||
register: jest.fn(), | ||
})) | ||
|
||
beforeEach(() => { | ||
_.UUID.mockReturnValue('newSessionId') | ||
}) | ||
|
||
describe('no stored session data', () => { | ||
it('generates a new session id, saves it', () => { | ||
expect(given.subject).toEqual('newSessionId') | ||
expect(given.persistence.register).toHaveBeenCalledWith({ [SESSION_ID]: [given.timestamp, 'newSessionId'] }) | ||
}) | ||
}) | ||
|
||
describe('stored session data', () => { | ||
it('reuses old session data', () => { | ||
given('recordedData', () => [1603107460000, 'oldSessionId']) | ||
|
||
expect(given.subject).toEqual('oldSessionId') | ||
expect(given.persistence.register).toHaveBeenCalledWith({ [SESSION_ID]: [given.timestamp, 'oldSessionId'] }) | ||
}) | ||
|
||
it('generates a new session id, saves it when too long since last event', () => { | ||
given('recordedData', () => [1603007460000, 'oldSessionId']) | ||
|
||
expect(given.subject).toEqual('newSessionId') | ||
expect(given.persistence.register).toHaveBeenCalledWith({ [SESSION_ID]: [given.timestamp, 'newSessionId'] }) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { SESSION_ID } from '../posthog-persistence' | ||
import { _ } from '../utils' | ||
|
||
const SESSION_CHANGE_THRESHOLD = 30 * 60 * 1000 // 30 mins | ||
|
||
export default (persistence, timestamp) => { | ||
let [lastTimestamp, sessionId] = persistence['props'][SESSION_ID] || [0, null] | ||
|
||
if (Math.abs(timestamp - lastTimestamp) > SESSION_CHANGE_THRESHOLD) { | ||
sessionId = _.UUID() | ||
} | ||
|
||
persistence.register({ [SESSION_ID]: [timestamp, sessionId] }) | ||
return sessionId | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters