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

feat: Add react native autocapture lifecycle events #108

Merged
merged 8 commits into from
Sep 6, 2023

Conversation

robbie-c
Copy link
Contributor

@robbie-c robbie-c commented Aug 25, 2023

Problem

Fixes #107

Note: please merge #109 first, as this depends upon that.

Changes

Adds these events that were present in v1. I put it behind an options flag, so that users will need to opt-in. This will prevent people from getting an unexpected surge of these events when updating the sdk.

Note: I found that due to ignoring the returned promise on line

, initAsync would return before setupAsync had completed. This is something that is a bit awkward to get right, when there in a constructor which needs to do something async. I fixed this by storing the promise and awaiting it in initAsync.

Release info Sub-libraries affected

Bump level

  • Major
  • Minor
  • Patch

Libraries affected

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

Changelog notes

  • Added an options flag captureNativeAppLifecycleEvents which, when set, will cause the SDK to automatically capture 'Application Installed', 'Application Updated', 'Application Opened', and 'Application Backgrounded'.

@robbie-c robbie-c force-pushed the add-react-native-autocapture branch from 30b3ee3 to ccfc5a9 Compare August 27, 2023 20:05
@robbie-c robbie-c changed the title Add react native autocapture feat: Add react native autocapture Aug 27, 2023
@robbie-c robbie-c force-pushed the add-react-native-autocapture branch from ecef450 to 21adb52 Compare August 27, 2023 22:41
return posthog
const resolved = await posthog
await resolved._setupPromise
return resolved
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Here's the async fix

@robbie-c robbie-c marked this pull request as ready for review August 27, 2023 22:42
@benjackwhite benjackwhite changed the title feat: Add react native autocapture feat: Add react native autocapture lifecycle events Sep 4, 2023
if (appBuild) {
if (!prevAppBuild) {
// new app install
this.capture('Application Installed', {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Doesn't this mean anyone rolling out the new version will get these events across all their apps? I guess thats just "how it is" but we should be careful with our communications here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This function is only called when people explicitly set options?.captureNativeAppLifecycleEvents, so anyone doing an update without reading the docs and making code changes just won't get these events.

Maybe one slight optimization would be storing the build number in storage regardless, so that people enabling this flag in future don't get a flood of events.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Persistance now happens regardless

}
})

this.setPersistedProperty(PostHogPersistedProperty.InstalledAppBuild, appBuild)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Given that this logic is super dependent on persisted values, maybe we need a check to make sure that we have "real" storage?

Copy link
Contributor Author

@robbie-c robbie-c Sep 4, 2023

Choose a reason for hiding this comment

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

I think we know this just based on it being a React Native app, we use expo-file-system or @react-native-async-storage/async-storage and throw if neither one is available. The user can provide their own but if they do that I don't think it's unreasonable to epxect that it's "real".

On second thought it prob does make sense to add a warning if _persistence is set to "memory" and these events are turned on.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Warning added

@@ -22,6 +22,7 @@ export type PostHogOptions = PosthogCoreOptions & {
| PostHogCustomAppProperties
| ((properties: PostHogCustomAppProperties) => PostHogCustomAppProperties)
customAsyncStorage?: PostHogCustomAsyncStorage
captureNativeAppLifecycleEvents?: boolean
Copy link
Collaborator

Choose a reason for hiding this comment

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

I feel like the naming here clashes with the PostHogAutocaptureOptions. No good option comes to mind but it feels like we might get messier and messier over time... I wonder if we shouldn't just include them as part of the "lifecycle" events, even though they're not strictly dependent on react lifecycles...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I wanted it to be a separate flag so as to not spam people who update. We could definitely not do that and have it under the same flag, but as you mentioned above we would need to be careful about how we communicate the change, whereas with a new flag it's less of a big deal.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Re: the naming conflict around the word "lifecycle", I don't have solid ideas. It's called Lifecycle on Android, it doesn't really have a name on iOS, the closest thing in RN is maybe "app state"?

Comment on lines 97 to 99
if (options?.__onConstructed) {
options.__onConstructed(this)
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

This feels like a hack and I'm not sure what it is providing different from awaiting the SDK via initAsync? At some point someone will see this and use it, even though its meant to be "just for testing".
Is there no alternative?

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's now gone, I do call a private method in test code now though

Copy link
Collaborator

@benjackwhite benjackwhite 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! Thanks for the back and forth here

@robbie-c robbie-c merged commit 4a707fd into master Sep 6, 2023
2 checks passed
@robbie-c robbie-c deleted the add-react-native-autocapture branch September 6, 2023 10:37
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.

posthog-react-native 2.X doesn't send autocaptured “Application Installed” event
2 participants