Skip to content
Merged
Show file tree
Hide file tree
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
5 changes: 5 additions & 0 deletions .changeset/short-news-argue.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'posthog-js': patch
---

fix autofill bug in hosted surveys
99 changes: 98 additions & 1 deletion packages/browser/src/__tests__/extensions/surveys.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,14 @@ import {
usePopupVisibility,
} from '../../extensions/surveys'
import { retrieveSurveyShadow } from '../../extensions/surveys/surveys-extension-utils'
import { Survey, SurveyQuestionType, SurveySchedule, SurveyType, SurveyWidgetType } from '../../posthog-surveys-types'
import {
Survey,
SurveyQuestionBranchingType,
SurveyQuestionType,
SurveySchedule,
SurveyType,
SurveyWidgetType,
} from '../../posthog-surveys-types'

import { afterAll, beforeAll, beforeEach } from '@jest/globals'
import '@testing-library/jest-dom'
Expand Down Expand Up @@ -593,6 +600,96 @@ describe('SurveyManager', () => {
})
})

describe('renderSurvey with URL prefill that completes the survey', () => {
let surveyManager: SurveyManager
let originalLocation: Location

beforeEach(() => {
originalLocation = window.location
delete (window as any).location
window.location = { ...originalLocation, search: '' } as Location
})

afterEach(() => {
window.location = originalLocation
})

it.each([
{
scenario: 'positive rating (branches to end)',
search: '?q0=1',
shouldShowConfirmation: true,
},
{
scenario: 'negative rating (branches to next question)',
search: '?q0=2',
shouldShowConfirmation: false,
},
])('should show confirmation=$shouldShowConfirmation for $scenario', ({ search, shouldShowConfirmation }) => {
const mockPH = createMockPostHog({
config: {
token: 'test-token',
api_host: 'https://test.com',
surveys: { prefillFromUrl: true },
},
getActiveMatchingSurveys: jest.fn(),
get_session_replay_url: jest.fn(),
capture: jest.fn(),
featureFlags: { isFeatureEnabled: jest.fn().mockReturnValue(true) },
})

surveyManager = new SurveyManager(mockPH)

const survey: Survey = {
id: 'prefill-render-survey',
name: 'Prefill Render Survey',
type: SurveyType.Popover,
questions: [
{
id: 'q1',
type: SurveyQuestionType.Rating,
question: 'How was the draft?',
scale: 2,
display: 'emoji',
skipSubmitButton: true,
branching: {
type: SurveyQuestionBranchingType.ResponseBased,
responseValues: { positive: SurveyQuestionBranchingType.End },
},
},
{
id: 'q2',
type: SurveyQuestionType.Open,
question: 'Tell us more',
},
],
appearance: { displayThankYouMessage: true, thankYouMessageHeader: 'Thanks!' },
conditions: null,
start_date: '2021-01-01T00:00:00.000Z',
end_date: null,
current_iteration: null,
current_iteration_start_date: null,
feature_flag_keys: [],
linked_flag_key: null,
targeting_flag_key: null,
internal_targeting_flag_key: null,
} as unknown as Survey

window.location.search = search

const surveyDiv = document.createElement('div')
surveyManager.renderSurvey(survey, surveyDiv)

if (shouldShowConfirmation) {
expect(surveyDiv.getElementsByClassName('thank-you-message').length).toBe(1)
expect(surveyDiv.getElementsByClassName('survey-form').length).toBe(0)
} else {
expect(surveyDiv.getElementsByClassName('survey-form').length).toBe(1)
expect(surveyDiv.getElementsByClassName('thank-you-message').length).toBe(0)
}
})
})

describe('timeout management', () => {
let mockPostHog: PostHog
let surveyManager: SurveyManager
Expand Down
14 changes: 9 additions & 5 deletions packages/browser/src/extensions/surveys.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -376,8 +376,9 @@ export class SurveyManager {
}

public renderSurvey = (survey: Survey, selector: Element, properties?: Properties): void => {
let isSurveyCompleted = false
if (this._posthog.config?.surveys?.prefillFromUrl) {
this._handleUrlPrefill(survey)
isSurveyCompleted = this._handleUrlPrefill(survey)
}

render(
Expand All @@ -387,28 +388,29 @@ export class SurveyManager {
removeSurveyFromFocus={this._removeSurveyFromFocus}
isPopup={false}
properties={properties}
isSurveyCompleted={isSurveyCompleted}
/>,
selector
)
}

private _handleUrlPrefill(survey: Survey): void {
private _handleUrlPrefill(survey: Survey): boolean {
// Only handle prefill once per survey session to avoid overwriting in-progress responses
if (this._prefillHandledSurveys.has(survey.id)) {
return
return false
}

const { params } = extractPrefillParamsFromUrl(window.location.search)

if (Object.keys(params).length === 0) {
return
return false
}

logger.info('[Survey Prefill] Detected URL prefill parameters')

const result = this._processPrefillData(survey, params)
if (!result) {
return
return false
}

const { responses, submissionId, isSurveyCompleted, skippedResponses } = result
Expand All @@ -431,6 +433,8 @@ export class SurveyManager {

// Mark this survey as having been prefilled
this._prefillHandledSurveys.add(survey.id)

return isSurveyCompleted
}

/**
Expand Down
Loading