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

fix: session when impersonated #25897

Merged
merged 1 commit into from
Oct 30, 2024
Merged
Changes from all 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
52 changes: 27 additions & 25 deletions frontend/src/loadPostHogJS.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,38 @@ export function loadPostHogJS(): void {
bootstrap: window.POSTHOG_USER_IDENTITY_WITH_FLAGS ? window.POSTHOG_USER_IDENTITY_WITH_FLAGS : {},
opt_in_site_apps: true,
api_transport: 'fetch',
loaded: (posthog) => {
if (posthog.sessionRecording) {
posthog.sessionRecording._forceAllowLocalhostNetworkCapture = true
loaded: (loadedInstance) => {
if (loadedInstance.sessionRecording) {
loadedInstance.sessionRecording._forceAllowLocalhostNetworkCapture = true
}

if (window.IMPERSONATED_SESSION) {
posthog.opt_out_capturing()
loadedInstance.sessionManager?.resetSessionId()
Copy link
Member Author

@pauldambra pauldambra Oct 30, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added this to (hopefully) ensure impersonation is always a new session

i'm slightly worried still about a race between start-up continuing a recording and the loaded callback, but this is the lightest touch I can see to try and fix

loadedInstance.opt_out_capturing()
} else {
posthog.opt_in_capturing()
loadedInstance.opt_in_capturing()
}

const Cypress = (window as any).Cypress

if (Cypress) {
Object.entries(Cypress.env()).forEach(([key, value]) => {
if (key.startsWith('POSTHOG_PROPERTY_')) {
loadedInstance.register_for_session({
[key.replace('POSTHOG_PROPERTY_', 'E2E_TESTING_').toLowerCase()]: value,
})
}
})
}

// This is a helpful flag to set to automatically reset the recording session on load for testing multiple recordings
const shouldResetSessionOnLoad = loadedInstance.getFeatureFlag(FEATURE_FLAGS.SESSION_RESET_ON_LOAD)
if (shouldResetSessionOnLoad) {
loadedInstance.sessionManager?.resetSessionId()
}

// Make sure we have access to the object in window for debugging
window.posthog = loadedInstance
Comment on lines +44 to +63
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

moved these into the loaded callback to make it a little clearer what we think we're operating on

},
scroll_root_selector: ['main', 'html'],
autocapture: {
Expand All @@ -52,26 +74,6 @@ export function loadPostHogJS(): void {
: undefined,
})
)

const Cypress = (window as any).Cypress

if (Cypress) {
Object.entries(Cypress.env()).forEach(([key, value]) => {
if (key.startsWith('POSTHOG_PROPERTY_')) {
posthog.register_for_session({
[key.replace('POSTHOG_PROPERTY_', 'E2E_TESTING_').toLowerCase()]: value,
})
}
})
}

// This is a helpful flag to set to automatically reset the recording session on load for testing multiple recordings
const shouldResetSessionOnLoad = posthog.getFeatureFlag(FEATURE_FLAGS.SESSION_RESET_ON_LOAD)
if (shouldResetSessionOnLoad) {
posthog.sessionManager?.resetSessionId()
}
// Make sure we have access to the object in window for debugging
window.posthog = posthog
} else {
posthog.init('fake token', {
autocapture: false,
Expand Down
Loading