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

Add survey support for React Native #333

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

ian-craig
Copy link

Problem

#110 posthog-react-native doesn't have built in support for surveys

Changes

This PR ports most of the survey logic from posthog-js into react native.

I've re-implemented most of the state handling and persistence to make it more appropriate for React Native and built on top of posthog-react-native.

See surveys/Readme.md for details on the hooks and config options I've added. I was originally going to post this as a separate library to start which is why I've split out <PostHogSurveyProvider> etc., but we can probably merge some of that in to the main provider.

Release info Sub-libraries affected

Bump level

  • Major
  • Minor
  • Patch

Should all be backwards compatible, with the possible exception of adding a peer dependency for SVGs. We can look into what the best approach for this is.

Libraries affected

  • All of them
  • posthog-web
  • posthog-node
  • posthog-react-native

Changelog notes

  • TODO

@marandaneto
Copy link
Member

@ian-craig thanks a lot.
just back from the holidays, I'll catch up with a few things before and I should be able to test it/review it by EoW, thanks for doing this!

@ian-craig
Copy link
Author

@marandaneto any chance you've had some time to take a look at this? Thanks!

@marandaneto
Copy link
Member

@marandaneto any chance you've had some time to take a look at this? Thanks!

hello, yes, I started looking into it already, expect a thoughtful review today or Monday latest =) sorry the delay!

Comment on lines +457 to +460
* @todo Where should this go, and should the result be cached?
* I can't find a public method in PostHog which does an authenticated fetch,
* so for now I've included the fetch here so it can access the api key.
*/
Copy link
Member

Choose a reason for hiding this comment

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

https://github.com/PostHog/posthog-js/blob/8d70a9ba4383b453ade94b71d7f45df4af2c892c/src/posthog-core.ts#L1294C25-L1321
i think those methods can be in the core package, here and reuse the fetch infrastructure from there, example here.

const response = await this.fetch(
// PostHog Dashboard complains I'm using an old SDK version, I think because the ver query param isn't provided.
// TODO I've pulled a recent version from posthog-js, but this should be updated.
`${this.host}/api/surveys/?token=${this.apiKey}&ver=1.200.0`,
Copy link
Member

Choose a reason for hiding this comment

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

we can remove the version for now since the user-agent also has it

headers['User-Agent'] = customUserAgent

Copy link
Member

Choose a reason for hiding this comment

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

by doing this, the user agent is automatically added

)

if (response.status > 300) {
// TODO Best practice for handling this?
Copy link
Member

Choose a reason for hiding this comment

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

by doing this, error handling is builtin

flex: 1,
alignItems: 'center',
justifyContent: 'flex-end',
marginVertical: 40, // TODO Plus safe area?
Copy link
Member

Choose a reason for hiding this comment

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

makes sense


export function useActivatedSurveys(posthog: PostHog, surveys: Survey[]): ReadonlySet<string> {
const [activatedSurveys, setActivatedSurveys] = useState<ReadonlySet<string>>(new Set())
//TODO Support Action activation
Copy link
Member

Choose a reason for hiding this comment

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

not planned for now, the goal is the basic features first.

Comment on lines +82 to +90
// Load surveys once
useEffect(() => {
posthog
.fetchSurveys()
.then(setSurveys)
.catch((error: unknown) => {
posthog.capture('PostHogSurveyProvider failed to fetch surveys', { error })
})
}, [posthog])
Copy link
Member

Choose a reason for hiding this comment

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

This will likely change since we are adding a remote config API (WIP), and we'll only load surveys if there are surveys to be loaded.
That means we'll be calling another API when the SDK starts.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants