-
Notifications
You must be signed in to change notification settings - Fork 144
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
chore: add surveys device types check #1698
Conversation
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
Size Change: +5.86 kB (+0.18%) Total Size: 3.29 MB
ℹ️ View Unchanged
|
@@ -123,7 +123,7 @@ export interface SurveyResponse { | |||
|
|||
export type SurveyCallback = (surveys: Survey[]) => void | |||
|
|||
export type SurveyUrlMatchType = 'regex' | 'not_regex' | 'exact' | 'is_not' | 'icontains' | 'not_icontains' | |||
export type SurveyMatchType = 'regex' | 'not_regex' | 'exact' | 'is_not' | 'icontains' | 'not_icontains' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i think we could rename this to something generic and kill WebExperimentUrlMatchType
as well
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
but then this needs to be moved out of the surveys bundle
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this makes sense. However, since it's generic, I'd say we name it UrlMatchType
and move it to src/types.ts
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No Url
in its name, since its not used only for Urls
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oooh, we were working towards something similar in replay 👀 🤔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
URL matching and the like
src/utils/request-utils.ts
Outdated
@@ -22,9 +22,9 @@ export const convertToURL = (url: string): HTMLAnchorElement | null => { | |||
return location | |||
} | |||
|
|||
export const isUrlMatchingRegex = function (url: string, pattern: string): boolean { | |||
export const isMatchingRegex = function (value: string, pattern: string): boolean { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: feels more like string utils than request utils
especially now it's more obviously got nothing to do with URLs
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
makes sense to move it now
added a very low context review pass since I was here anyway after being tagged :) |
There was a problem hiding this 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 adds device type targeting capabilities to PostHog surveys, allowing surveys to be conditionally shown based on the user's device type. Here are the key changes:
- Added device type targeting in
posthog-surveys-types.ts
with new fieldsdeviceTypes
anddeviceTypesMatchType
to the Survey interface - Refactored string matching utilities by moving
isMatchingRegex
fromrequest-utils.ts
tostring-utils.ts
for better code organization - Modified
surveyValidationMap
inposthog-surveys.ts
to handle both URL and device type matching with consistent validation logic - Added
doesSurveyDeviceTypesMatch
function to check device type conditions against user agent - Updated tests to verify device type targeting works correctly for mobile and web devices
The changes maintain backward compatibility while adding new functionality to target surveys based on device types like Mobile, Web, Android and iOS.
💡 (2/5) Greptile learns from your feedback when you react with 👍/👎!
8 file(s) reviewed, 8 comment(s)
Edit PR Review Bot Settings | Greptile
@@ -121,7 +121,7 @@ export class ActionMatcher { | |||
private static matchString(url: string, pattern: string, matching: ActionStepStringMatching): boolean { | |||
switch (matching) { | |||
case 'regex': | |||
return !!window && isUrlMatchingRegex(url, pattern) | |||
return !!window && isMatchingRegex(url, pattern) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
style: window check is redundant since isMatchingRegex already handles null/undefined cases
const adjustedRegExpStringPattern = ActionMatcher.escapeStringRegexp(pattern) | ||
.replace(/_/g, '.') | ||
.replace(/%/g, '.*') | ||
return isUrlMatchingRegex(url, adjustedRegExpStringPattern) | ||
return isMatchingRegex(url, adjustedRegExpStringPattern) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
logic: no boundary checks on the regex pattern after SQL LIKE conversion - could lead to partial matches where full matches were intended
const adjustedRegExpStringPattern = ActionMatcher.escapeStringRegexp(pattern) | |
.replace(/_/g, '.') | |
.replace(/%/g, '.*') | |
return isUrlMatchingRegex(url, adjustedRegExpStringPattern) | |
return isMatchingRegex(url, adjustedRegExpStringPattern) | |
const adjustedRegExpStringPattern = '^' + ActionMatcher.escapeStringRegexp(pattern) | |
.replace(/_/g, '.') | |
.replace(/%/g, '.*') + '$' | |
return isMatchingRegex(url, adjustedRegExpStringPattern) |
@@ -1,3 +1,5 @@ | |||
import { isValidRegex } from '.' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
style: Import from '.' is ambiguous and could break if file structure changes. Consider using explicit path to the file containing isValidRegex
src/utils/string-utils.ts
Outdated
if (!isValidRegex(pattern)) return false | ||
return new RegExp(pattern).test(value) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
style: Creating new RegExp on every call is inefficient for repeated patterns. Consider caching compiled regex objects
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
looks good!
we already have tests in place for URL matching right?
Changes
Draft for PostHog/posthog#24495 (SDK side)
docs PostHog/posthog.com#10529
...
Checklist