Skip to content

Commit

Permalink
chore: Change Application opened to application became active for nat…
Browse files Browse the repository at this point in the history
…ive events (#211)
  • Loading branch information
marandaneto authored Mar 26, 2024
1 parent d33dc89 commit b59a975
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 9 deletions.
6 changes: 6 additions & 0 deletions posthog-react-native/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# Next

## Changed

1. If `captureNativeAppLifecycleEvents` is enabled, the event `Application Opened` with the property `from_background: true` is moved to its own event called `Application Became Active`. This event is triggered when the app is opened from the background. The `Application Opened` event is now only triggered when the app is opened from a cold start, aligning with the other integrations such as the `PostHogProvider` with the `captureLifecycleEvents` option and `initReactNativeNavigation` with the `captureLifecycleEvents` option.

# 3.0.0 - 2024-03-18

## Added
Expand Down
1 change: 1 addition & 0 deletions posthog-react-native/src/hooks/useLifecycleTracker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export function useLifecycleTracker(client?: PostHog): void {
return useEffect(() => {
if (!openTrackedRef.current) {
openTrackedRef.current = true
// TODO: add missing initialUrl
posthog.capture('Application Opened')
}
const subscription = AppState.addEventListener('change', (nextAppState) => {
Expand Down
14 changes: 10 additions & 4 deletions posthog-react-native/src/posthog-rn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export type PostHogOptions = PostHogCoreOptions & {
customStorage?: PostHogCustomStorage

// customAsyncStorage?: PostHogCustomAsyncStorage
/** Captures native app lifecycle events such as Application Installed, Application Updated, Application Opened and Application Backgrounded.
/** Captures native app lifecycle events such as Application Installed, Application Updated, Application Opened, Application Became Active and Application Backgrounded.
* By default is false.
* If you're already using the 'captureLifecycleEvents' options with 'withReactNativeNavigation' or 'PostHogProvider, you should not set this to true, otherwise you may see duplicated events.
*/
Expand Down Expand Up @@ -195,12 +195,16 @@ export class PostHog extends PostHogCore {
if (appBuild) {
if (!prevAppBuild) {
// new app install
// version and build are deprecated, but we keep them for compatibility
// use $app_version and $app_build instead
this.capture('Application Installed', {
version: appVersion,
build: appBuild,
})
} else if (prevAppBuild !== appBuild) {
// app updated
// version and build are deprecated, but we keep them for compatibility
// use $app_version and $app_build instead
this.capture('Application Updated', {
previous_version: prevAppVersion,
previous_build: prevAppBuild,
Expand All @@ -212,19 +216,21 @@ export class PostHog extends PostHogCore {

const initialUrl = (await Linking.getInitialURL()) ?? undefined

// version and build are deprecated, but we keep them for compatibility
// use $app_version and $app_build instead
this.capture('Application Opened', {
version: appVersion,
build: appBuild,
from_background: false,
url: initialUrl,
})

AppState.addEventListener('change', (state) => {
if (state === 'active') {
this.capture('Application Opened', {
// version and build are deprecated, but we keep them for compatibility
// use $app_version and $app_build instead
this.capture('Application Became Active', {
version: appVersion,
build: appBuild,
from_background: true,
})
} else if (state === 'background') {
this.capture('Application Backgrounded')
Expand Down
6 changes: 1 addition & 5 deletions posthog-react-native/test/posthog.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,6 @@ describe('PostHog React Native', () => {
properties: {
$app_build: '1',
$app_version: '1.0.0',
from_background: false,
},
})
})
Expand Down Expand Up @@ -221,7 +220,6 @@ describe('PostHog React Native', () => {
properties: {
$app_build: '2',
$app_version: '2.0.0',
from_background: false,
},
})
})
Expand Down Expand Up @@ -266,7 +264,6 @@ describe('PostHog React Native', () => {
properties: {
$app_build: '1',
$app_version: '1.0.0',
from_background: false,
url: 'https://example.com',
},
})
Expand Down Expand Up @@ -307,11 +304,10 @@ describe('PostHog React Native', () => {
},
})
expect(onCapture.mock.calls[3][0]).toMatchObject({
event: 'Application Opened',
event: 'Application Became Active',
properties: {
$app_build: '1',
$app_version: '1.0.0',
from_background: true,
},
})
})
Expand Down

0 comments on commit b59a975

Please sign in to comment.