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

chore: add surveys device types check #1698

Merged
merged 7 commits into from
Jan 30, 2025
Merged

Conversation

marandaneto
Copy link
Member

@marandaneto marandaneto commented Jan 28, 2025

Changes

Draft for PostHog/posthog#24495 (SDK side)
docs PostHog/posthog.com#10529

...

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!)

Copy link

vercel bot commented Jan 28, 2025

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

Name Status Preview Updated (UTC)
posthog-js ✅ Ready (Inspect) Visit Preview Jan 29, 2025 11:58pm

Copy link

github-actions bot commented Jan 28, 2025

Size Change: +5.86 kB (+0.18%)

Total Size: 3.29 MB

Filename Size Change
dist/all-external-dependencies.js 215 kB +441 B (+0.21%)
dist/array.full.es5.js 266 kB +430 B (+0.16%)
dist/array.full.js 369 kB +340 B (+0.09%)
dist/array.full.no-external.js 368 kB +340 B (+0.09%)
dist/array.js 182 kB +512 B (+0.28%)
dist/array.no-external.js 180 kB +352 B (+0.2%)
dist/dead-clicks-autocapture.js 14.5 kB +4 B (+0.03%)
dist/main.js 183 kB +512 B (+0.28%)
dist/module.full.js 369 kB +340 B (+0.09%)
dist/module.full.no-external.js 368 kB +340 B (+0.09%)
dist/module.js 182 kB +565 B (+0.31%)
dist/module.no-external.js 180 kB +352 B (+0.2%)
dist/surveys-preview.js 69.4 kB +668 B (+0.97%)
dist/surveys.js 72.4 kB +668 B (+0.93%)
ℹ️ View Unchanged
Filename Size
dist/customizations.full.js 13.8 kB
dist/exception-autocapture.js 9.48 kB
dist/external-scripts-loader.js 2.64 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

@@ -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'
Copy link
Member Author

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

Copy link
Member Author

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

Copy link
Contributor

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

Copy link
Member Author

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

Copy link
Member

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 👀 🤔

Copy link
Member

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

@@ -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 {
Copy link
Member

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

Copy link
Member Author

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

@pauldambra
Copy link
Member

added a very low context review pass since I was here anyway after being tagged :)

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 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 fields deviceTypes and deviceTypesMatchType to the Survey interface
  • Refactored string matching utilities by moving isMatchingRegex from request-utils.ts to string-utils.ts for better code organization
  • Modified surveyValidationMap in posthog-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)
Copy link
Contributor

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

Comment on lines 130 to +133
const adjustedRegExpStringPattern = ActionMatcher.escapeStringRegexp(pattern)
.replace(/_/g, '.')
.replace(/%/g, '.*')
return isUrlMatchingRegex(url, adjustedRegExpStringPattern)
return isMatchingRegex(url, adjustedRegExpStringPattern)
Copy link
Contributor

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

Suggested change
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 '.'
Copy link
Contributor

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

Comment on lines 21 to 22
if (!isValidRegex(pattern)) return false
return new RegExp(pattern).test(value)
Copy link
Contributor

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

Copy link
Contributor

@lucasheriques lucasheriques left a 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?

@marandaneto marandaneto added the bump minor Bump minor version when this PR gets merged label Jan 30, 2025
@marandaneto marandaneto merged commit 9bf2e35 into main Jan 30, 2025
31 checks passed
@marandaneto marandaneto deleted the chore/device-types-local branch January 30, 2025 19:55
@marandaneto marandaneto changed the title chore: add device types check chore: add surveys device types check Jan 30, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bump minor Bump minor version when this PR gets merged
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants