Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/rn-os-name-platform-fallback.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'posthog-react-native': patch
---

Fix events missing `$os_name` and `$os_version` in apps that use neither `expo-device` nor `react-native-device-info`.
24 changes: 24 additions & 0 deletions packages/react-native/src/native-deps.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { PostHogCustomAppProperties, PostHogCustomStorage } from './types'
import { OptionalReactNativeLocalize } from './optional/OptionalReactNativeLocalize'
import { OptionalExpoFileSystemLegacy } from './optional/OptionalExpoFileSystemLegacy'
import { detectDeviceType } from '@posthog/core'
import { Platform } from 'react-native'

const getDeviceType = (): string => {
let deviceType = 'Mobile'
Expand Down Expand Up @@ -76,6 +77,10 @@ export const getAppProperties = (): PostHogCustomAppProperties => {
properties.$is_emulator = OptionalReactNativeDeviceInfo.isEmulatorSync()
}

if (!properties.$os_name) {
Object.assign(properties, getPlatformOSProperties())
}

if (OptionalExpoLocalization) {
// expo-localization >= 14 use functions to get these results, older versions use JS getters
// https://github.com/expo/expo/blob/sdk-54/packages/expo-localization/CHANGELOG.md#1400--2022-10-25
Expand Down Expand Up @@ -131,6 +136,25 @@ export const getAppProperties = (): PostHogCustomAppProperties => {
return properties
}

type PlatformConstants = { Release?: string; systemName?: string }

// Reads the OS from React Native's own Platform module, matching what posthog-android and posthog-ios report.
const getPlatformOSProperties = (): Pick<PostHogCustomAppProperties, '$os_name' | '$os_version'> => {
try {
const os = getPlatformOS()
const constants = (Platform as { constants?: PlatformConstants }).constants
const version = Platform.Version !== undefined ? String(Platform.Version) : undefined
if (os === 'android') {
// Platform.Version is the API level on Android; Release is the user-facing OS version
return { $os_name: 'Android', $os_version: constants?.Release ?? version }
}
if (os === 'ios') {
return { $os_name: constants?.systemName ?? 'iOS', $os_version: version }
}
} catch {}
return {}
}

// react-native-device-info returns 'unknown' if the property is not available (Web target)
const returnPropertyIfNotUnknown = (value: string | null): string | null => {
if (value !== 'unknown') {
Expand Down
45 changes: 45 additions & 0 deletions packages/react-native/test/native-deps-os.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { Platform } from 'react-native'
import { getAppProperties } from '../src/native-deps'

vi.mock('../src/optional/OptionalExpoDevice', () => ({ OptionalExpoDevice: undefined }))
vi.mock('../src/optional/OptionalExpoApplication', () => ({ OptionalExpoApplication: undefined }))
vi.mock('../src/optional/OptionalReactNativeDeviceInfo', () => ({ OptionalReactNativeDeviceInfo: undefined }))

const platform = Platform as unknown as { OS: string; Version: string | number; constants?: unknown }

describe('getAppProperties OS fallback without a device-info library', () => {
const original = { OS: platform.OS, Version: platform.Version }

afterEach(() => {
platform.OS = original.OS
platform.Version = original.Version
delete platform.constants
})

it('reports Android with the release version, not the API level', () => {
platform.OS = 'android'
platform.Version = 36
platform.constants = { Release: '16' }
expect(getAppProperties()).toMatchObject({ $os_name: 'Android', $os_version: '16' })
})

it('reports the iOS system name and version', () => {
platform.OS = 'ios'
platform.Version = '18.2'
platform.constants = { systemName: 'iPadOS' }
expect(getAppProperties()).toMatchObject({ $os_name: 'iPadOS', $os_version: '18.2' })
})

it('defaults the iOS name when the platform constants omit it', () => {
platform.OS = 'ios'
platform.Version = '18.2'
expect(getAppProperties()).toMatchObject({ $os_name: 'iOS', $os_version: '18.2' })
})

it('leaves OS properties unset on web', () => {
platform.OS = 'web'
const properties = getAppProperties()
expect(properties).not.toHaveProperty('$os_name')
expect(properties).not.toHaveProperty('$os_version')
})
})
Loading