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: check url matching on URL change for widget-type surveys #1755

Merged
merged 2 commits into from
Feb 20, 2025

Conversation

lucasheriques
Copy link
Contributor

Changes

we already did this fix for popover-type surveys here: #1732

however, we just got a ticket that the same is stil lhappening for widget-type surveys

so I extracted that new logic into a hook, create tests for it (and also for doesSurveyUrlMatch), and then applied it to both cases

Checklist

  • Tests for new code (see advice on the tests we use)
  • Accounted for the impact of any changes across different browsers
  • Accounted for backwards compatibility of any changes (no breaking changes in posthog-js!)

@lucasheriques lucasheriques added bump patch Bump patch version when this PR gets merged feature/surveys labels Feb 20, 2025
@lucasheriques lucasheriques self-assigned this Feb 20, 2025
@lucasheriques lucasheriques requested a review from a team as a code owner February 20, 2025 14:49
Copy link

vercel bot commented Feb 20, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated (UTC)
posthog-js ✅ Ready (Inspect) Visit Preview Feb 20, 2025 3:04pm

Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

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

PR Summary

This PR extracts URL matching logic into a reusable hook to fix widget-type surveys not properly hiding when URLs no longer match conditions, similar to a previous fix for popover surveys.

  • Added useToggleSurveyOnURLChange hook in /src/extensions/surveys.tsx to handle URL matching across different navigation types
  • Modified doesSurveyUrlMatch in /src/posthog-surveys.ts to accept Pick<Survey, 'conditions'> for better type safety and reusability
  • Added comprehensive test coverage in /src/__tests__/posthog-surveys.test.ts for URL matching scenarios including exact, regex, and contains matches
  • Added tests in /src/__tests__/extensions/surveys.test.ts for browser navigation methods (pushState, replaceState, popstate)
  • Improved cleanup handling for event listeners and history state modifications

4 file(s) reviewed, 2 comment(s)
Edit PR Review Bot Settings | Greptile

Comment on lines +132 to +154
it('should match using exact match type', () => {
mockWindowLocation('https://example.com')

const survey = {
conditions: {
url: 'https://example.com',
urlMatchType: 'exact' as const,
events: null,
actions: null,
},
}
expect(doesSurveyUrlMatch(survey)).toBe(true)

const nonMatchingSurvey = {
conditions: {
url: 'https://example.com/path',
urlMatchType: 'exact' as const,
events: null,
actions: null,
},
}
expect(doesSurveyUrlMatch(nonMatchingSurvey)).toBe(false)
})
Copy link
Contributor

Choose a reason for hiding this comment

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

style: Consider adding test for exact matching with trailing slashes and query parameters

Comment on lines +1073 to +1074
expect(mockSetSurveyVisible).not.toHaveBeenCalled()
expect(mockRemoveSurveyFromFocus).not.toHaveBeenCalled()
Copy link
Contributor

Choose a reason for hiding this comment

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

style: Initial mount visibility test may be flaky - should explicitly test initial visibility state matches URL condition

Copy link
Contributor Author

Choose a reason for hiding this comment

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

it shouldn't, as this hook is only to trigger after URL changes - the initial filter is done by getActiveMatchingSurveys

@lucasheriques lucasheriques changed the title fix: url matching for widget-type surveys fix: check url matching on URL change for widget-type surveys Feb 20, 2025
Copy link

github-actions bot commented Feb 20, 2025

Size Change: +2.12 kB (+0.06%)

Total Size: 3.32 MB

Filename Size Change
dist/all-external-dependencies.js 216 kB +261 B (+0.12%)
dist/array.full.es5.js 270 kB +294 B (+0.11%)
dist/array.full.js 372 kB +261 B (+0.07%)
dist/array.full.no-external.js 371 kB +261 B (+0.07%)
dist/module.full.js 372 kB +261 B (+0.07%)
dist/module.full.no-external.js 371 kB +261 B (+0.07%)
dist/surveys-preview.js 70 kB +261 B (+0.37%)
dist/surveys.js 73.1 kB +261 B (+0.36%)
ℹ️ View Unchanged
Filename Size
dist/array.js 184 kB
dist/array.no-external.js 183 kB
dist/customizations.full.js 14 kB
dist/dead-clicks-autocapture.js 14.5 kB
dist/exception-autocapture.js 9.51 kB
dist/external-scripts-loader.js 2.64 kB
dist/main.js 185 kB
dist/module.js 184 kB
dist/module.no-external.js 183 kB
dist/recorder-v2.js 115 kB
dist/recorder.js 115 kB
dist/tracing-headers.js 1.76 kB
dist/web-vitals.js 10.4 kB

compressed-size-action

@lucasheriques lucasheriques merged commit 394dd18 into main Feb 20, 2025
25 checks passed
@lucasheriques lucasheriques deleted the fix/url-matching-for-feedback-type-surveys branch February 20, 2025 15:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bump patch Bump patch version when this PR gets merged feature/surveys
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants