-
Notifications
You must be signed in to change notification settings - Fork 237
Expand file tree
/
Copy pathnative-deps.tsx
More file actions
227 lines (200 loc) · 8.64 KB
/
native-deps.tsx
File metadata and controls
227 lines (200 loc) · 8.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
import { Platform } from 'react-native'
import { OptionalAsyncStorage } from './optional/OptionalAsyncStorage'
import { OptionalExpoApplication } from './optional/OptionalExpoApplication'
import { OptionalExpoDevice } from './optional/OptionalExpoDevice'
import { OptionalExpoFileSystem } from './optional/OptionalExpoFileSystem'
import { OptionalExpoLocalization } from './optional/OptionalExpoLocalization'
import { OptionalReactNativeDeviceInfo } from './optional/OptionalReactNativeDeviceInfo'
import { PostHogCustomAppProperties, PostHogCustomStorage } from './types'
import { OptionalReactNativeLocalize } from './optional/OptionalReactNativeLocalize'
import { OptionalExpoFileSystemLegacy } from './optional/OptionalExpoFileSystemLegacy'
const getDeviceType = (): string => {
let deviceType = 'Mobile'
if (Platform.OS === 'macos' || Platform.OS === 'windows') {
deviceType = 'Desktop'
} else if (Platform.OS === 'web') {
// Check user agent to determine if it's desktop or mobile
const ua = navigator.userAgent
// Comprehensive mobile and tablet detection based on browser package logic
const isMobileOrTablet =
// iOS devices
/iPhone|iPod|iPad/i.test(ua) ||
// Android devices - check for mobile keyword or specific tablet models
(/Android/i.test(ua) &&
(/Mobile/i.test(ua) ||
// Android tablets that don't have 'Mobile' in UA
/(9138B|TB782B|Nexus [97]|pixel c|HUAWEISHT|BTV|noble nook|smart ultra 6)/i.test(ua))) ||
// Windows Phone
/(Windows Phone|WPDesktop|IEMobile)/i.test(ua) ||
// BlackBerry
/(BlackBerry|PlayBook|BB10)/i.test(ua) ||
// Other mobile browsers and devices
/webOS|Opera Mini|UCWEB|UCBrowser/i.test(ua) ||
// Kindle devices
/(kf[a-z]{2}wi|aeo[c-r]{2})( bui|\))/i.test(ua) ||
/(kf[a-z]+)( bui|\)).+silk\//i.test(ua) ||
// Generic mobile/tablet keywords
/(Mobile|Tablet|pda)/i.test(ua) ||
// Nokia
/Nokia/i.test(ua) ||
// Kobo e-readers
/(kobo)\s(ereader|touch)/i.test(ua)
deviceType = isMobileOrTablet ? 'Mobile' : 'Desktop'
}
return deviceType
}
export const currentDeviceType = getDeviceType()
export const getAppProperties = (): PostHogCustomAppProperties => {
const properties: PostHogCustomAppProperties = {
$device_type: currentDeviceType,
}
if (OptionalExpoApplication) {
properties.$app_build = OptionalExpoApplication.nativeBuildVersion
properties.$app_name = OptionalExpoApplication.applicationName
properties.$app_namespace = OptionalExpoApplication.applicationId
properties.$app_version = OptionalExpoApplication.nativeApplicationVersion
} else if (OptionalReactNativeDeviceInfo) {
properties.$app_build = returnPropertyIfNotUnknown(OptionalReactNativeDeviceInfo.getBuildNumber())
properties.$app_name = returnPropertyIfNotUnknown(OptionalReactNativeDeviceInfo.getApplicationName())
properties.$app_namespace = returnPropertyIfNotUnknown(OptionalReactNativeDeviceInfo.getBundleId())
properties.$app_version = returnPropertyIfNotUnknown(OptionalReactNativeDeviceInfo.getVersion())
}
if (OptionalExpoDevice) {
properties.$device_manufacturer = OptionalExpoDevice.manufacturer
// expo-device already maps the device model identifier to a human readable name
properties.$device_name = OptionalExpoDevice.modelName
// https://github.com/expo/expo/issues/6990
// some devices return a value similar to:
// HUAWEI/SNE-LX1/HWSNE:8.1.0/HUAWEISNE-LX1/131(C432):user/release-keys
if (Platform.OS === 'android') {
properties.$os_name = 'Android'
} else {
properties.$os_name = OptionalExpoDevice.osName
}
properties.$os_version = OptionalExpoDevice.osVersion
} else if (OptionalReactNativeDeviceInfo) {
properties.$device_manufacturer = returnPropertyIfNotUnknown(OptionalReactNativeDeviceInfo.getManufacturerSync())
// react-native-device-info already maps the device model identifier to a human readable name
properties.$device_name = returnPropertyIfNotUnknown(OptionalReactNativeDeviceInfo.getModel())
properties.$os_name = returnPropertyIfNotUnknown(OptionalReactNativeDeviceInfo.getSystemName())
properties.$os_version = returnPropertyIfNotUnknown(OptionalReactNativeDeviceInfo.getSystemVersion())
}
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
// this type below supports both variants, and type-checks with older and newer versions of expo-localization
const optionalExpoLocalization: {
locale?: string
getLocales?: () => {
languageTag: string
}[]
timezone?: string
getCalendars?: () => {
timeZone: string | null
}[]
} = OptionalExpoLocalization
let locale = optionalExpoLocalization.locale
if (!locale && optionalExpoLocalization.getLocales) {
locale = optionalExpoLocalization.getLocales()[0]?.languageTag
}
if (locale) {
properties.$locale = locale
}
let timezone: string | undefined | null = optionalExpoLocalization.timezone
if (!timezone && optionalExpoLocalization.getCalendars) {
timezone = optionalExpoLocalization.getCalendars()[0]?.timeZone
}
if (timezone) {
properties.$timezone = timezone
}
} else if (OptionalReactNativeLocalize) {
const localesFn = OptionalReactNativeLocalize.getLocales
if (localesFn) {
const locales = localesFn()
if (locales && locales.length > 0) {
const languageTag = locales[0].languageTag
if (languageTag) {
properties.$locale = languageTag
}
}
}
const timezoneFn = OptionalReactNativeLocalize.getTimeZone
if (timezoneFn) {
const timezone = timezoneFn()
if (timezone) {
properties.$timezone = timezone
}
}
}
return properties
}
// react-native-device-info returns 'unknown' if the property is not available (Web target)
const returnPropertyIfNotUnknown = (value: string | null): string | null => {
if (value !== 'unknown') {
return value
}
return null
}
const buildLegacyStorage = (filesystem: any): PostHogCustomStorage => {
return {
async getItem(key: string) {
try {
const uri = (filesystem.documentDirectory || '') + key
const stringContent = await filesystem.readAsStringAsync(uri)
return stringContent
} catch (e) {
return null
}
},
async setItem(key: string, value: string) {
const uri = (filesystem.documentDirectory || '') + key
await filesystem.writeAsStringAsync(uri, value)
},
}
}
export const buildOptimisiticAsyncStorage = (): PostHogCustomStorage => {
// expo-file-system is not supported on web and macos, so we need to use the react-native-async-storage package instead
// see https://github.com/PostHog/posthog-js-lite/blob/5fb7bee96f739b243dfea5589e2027f16629e8cd/posthog-react-native/src/optional/OptionalExpoFileSystem.ts#L7-L11
const supportedPlatform = Platform.OS !== 'web' && Platform.OS !== 'macos'
// expo-54 uses expo-file-system v19 which removed the async methods and added new APIs
// here we try to use the legacy package for back compatibility
if (OptionalExpoFileSystemLegacy && supportedPlatform) {
const filesystem = OptionalExpoFileSystemLegacy
return buildLegacyStorage(filesystem)
}
// expo-54 and expo-file-system v19 new APIs support
if (OptionalExpoFileSystem && supportedPlatform) {
const filesystem = OptionalExpoFileSystem
try {
const expoFileSystemLegacy = filesystem as any
// identify legacy APIs with older versions (expo <= 53 and expo-file-system <= 18)
if (expoFileSystemLegacy.readAsStringAsync) {
return buildLegacyStorage(filesystem)
}
} catch (e) {}
// expo >= 54 and expo-file-system >= 19
return {
async getItem(key: string) {
try {
const uri = ((filesystem as any).Paths?.document.info().uri || '') + key
const file = new (filesystem as any).File(uri)
const stringContent = await file.text()
return stringContent
} catch (e) {
return null
}
},
async setItem(key: string, value: string) {
const uri = ((filesystem as any).Paths?.document.info().uri || '') + key
const file = new (filesystem as any).File(uri)
file.write(value)
},
}
}
if (OptionalAsyncStorage) {
return OptionalAsyncStorage
}
throw new Error(
'PostHog: No storage available. Please install expo-file-system or react-native-async-storage OR implement a custom storage provider.'
)
}