Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Tidy initialisation with better warnings #768

Closed
wants to merge 18 commits into from
Closed
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions playground/session-recordings/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,23 @@
capture_performance: true,
})

posthog.init(
'phc_other',
{
api_host: 'http://localhost:8000',
disable_session_recording: true,
capture_performance: true,
},
'other'
)

posthog.capture('event')
posthog.capture('event2')
posthog.people.set({ test: true })
posthog.other.capture('other_event')
posthog.other.people.set({ test: true })
console.log(posthog)

setTimeout(() => {
posthog.debug()
document.getElementById('current-session-id').innerHTML = posthog.sessionRecording.sessionId
Expand Down
10 changes: 5 additions & 5 deletions src/__tests__/posthog-core.identify.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ jest.mock('../gdpr-utils', () => ({
}))
jest.mock('../decide')

given('lib', () => Object.assign(new PostHog(), given.overrides))
given('lib', () => {
const posthog = new PostHog()
posthog.init('testtoken', given.config)
return Object.assign(posthog, given.overrides)
})

describe('identify()', () => {
given(
Expand Down Expand Up @@ -339,10 +343,6 @@ describe('reset()', () => {
persistence: new PostHogPersistence(given.config),
}))

beforeEach(() => {
given.lib._init('testtoken', given.config, 'testhog')
})

it('clears persistence', () => {
given.lib.persistence.register({ $enabled_feature_flags: { flag: 'variant', other: true } })
expect(given.lib.persistence.props['$enabled_feature_flags']).toEqual({ flag: 'variant', other: true })
Expand Down
9 changes: 8 additions & 1 deletion src/__tests__/posthog-core.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ jest.mock('../utils', () => ({
document: { title: 'test' },
}))

given('lib', () => Object.assign(new PostHog(), given.overrides))
given('lib', () => {
const posthog = new PostHog()
posthog._init('testtoken', given.config, 'testhog')
return Object.assign(posthog, given.overrides)
})

describe('capture()', () => {
given('eventName', () => '$event')
Expand All @@ -29,6 +33,7 @@ describe('capture()', () => {
given('config', () => ({
property_blacklist: [],
_onCapture: jest.fn(),
get_device_id: jest.fn().mockReturnValue('device-id'),
}))

given('overrides', () => ({
Expand All @@ -38,11 +43,13 @@ describe('capture()', () => {
persistence: {
remove_event_timer: jest.fn(),
properties: jest.fn(),
update_config: jest.fn(),
},
sessionPersistence: {
update_search_keyword: jest.fn(),
update_campaign_params: jest.fn(),
update_referrer_info: jest.fn(),
update_config: jest.fn(),
properties: jest.fn(),
},
compression: {},
Expand Down
35 changes: 26 additions & 9 deletions src/extensions/sessionrecording.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,15 @@ export class SessionRecording {
})
}

private getSessionManager() {
if (!this.instance.sessionManager) {
logger.error('Session recording started without valid sessionManager')
return
}

return this.instance.sessionManager
}

startRecordingIfEnabled() {
if (this.isRecordingEnabled()) {
this.startCaptureAndTrySendingQueuedSnapshots()
Expand Down Expand Up @@ -185,14 +194,18 @@ export class SessionRecording {
}

private _startCapture() {
// According to the rrweb docs, rrweb is not supported on IE11 and below:
// "rrweb does not support IE11 and below because it uses the MutationObserver API which was supported by these browsers."
// https://github.com/rrweb-io/rrweb/blob/master/guide.md#compatibility-note
//
// However, MutationObserver does exist on IE11, it just doesn't work well and does not detect all changes.
// Instead, when we load "recorder.js", the first JS error is about "Object.assign" being undefined.
// Thus instead of MutationObserver, we look for this function and block recording if it's undefined.
const sessionManager = this.getSessionManager()
if (!sessionManager) {
return
}
if (typeof Object.assign === 'undefined') {
// According to the rrweb docs, rrweb is not supported on IE11 and below:
// "rrweb does not support IE11 and below because it uses the MutationObserver API which was supported by these browsers."
// https://github.com/rrweb-io/rrweb/blob/master/guide.md#compatibility-note
//
// However, MutationObserver does exist on IE11, it just doesn't work well and does not detect all changes.
// Instead, when we load "recorder.js", the first JS error is about "Object.assign" being undefined.
// Thus instead of MutationObserver, we look for this function and block recording if it's undefined.
return
}

Expand All @@ -203,7 +216,7 @@ export class SessionRecording {

this.captureStarted = true
// We want to ensure the sessionManager is reset if necessary on load of the recorder
this.instance.sessionManager.checkAndGetSessionAndWindowId()
sessionManager.checkAndGetSessionAndWindowId()

const recorderJS = this.getRecordingVersion() === 'v2' ? 'recorder-v2.js' : 'recorder.js'

Expand Down Expand Up @@ -231,6 +244,10 @@ export class SessionRecording {
}

private _updateWindowAndSessionIds(event: eventWithTime) {
const sessionManager = this.getSessionManager()
if (!sessionManager) {
return
}
// Some recording events are triggered by non-user events (e.g. "X minutes ago" text updating on the screen).
// We don't want to extend the session or trigger a new session in these cases. These events are designated by event
// type -> incremental update, and source -> mutation.
Expand Down Expand Up @@ -258,7 +275,7 @@ export class SessionRecording {
}

// We only want to extend the session if it is an interactive event.
const { windowId, sessionId } = this.instance.sessionManager.checkAndGetSessionAndWindowId(
const { windowId, sessionId } = sessionManager.checkAndGetSessionAndWindowId(
!isUserInteraction,
event.timestamp
)
Expand Down
Loading