-
Notifications
You must be signed in to change notification settings - Fork 255
/
Copy pathdevice.js
75 lines (67 loc) · 2.21 KB
/
device.js
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
const Device = require('expo-device')
const Constants = require('expo-constants').default
const { Dimensions, Platform } = require('react-native')
const rnVersion = require('react-native/package.json').version
module.exports = {
load: client => {
let orientation
const updateOrientation = () => {
const { height, width } = Dimensions.get('screen')
if (height > width) {
orientation = 'portrait'
} else if (height < width) {
orientation = 'landscape'
} else {
orientation = undefined
}
}
Dimensions.addEventListener('change', updateOrientation)
// get the initial orientation
updateOrientation()
const device = {
id: Constants.installationId,
manufacturer: Device.manufacturer,
model: Device.modelName,
modelNumber: Device.modelId || undefined,
osName: Platform.OS,
osVersion: Device.osVersion,
runtimeVersions: {
reactNative: rnVersion,
expoApp: Constants.expoVersion,
expoSdk: Constants.manifest.sdkVersion,
androidApiLevel: Constants.platform.android ? String(Platform.Version) : undefined
},
totalMemory: Device.totalMemory
}
client.addOnSession(session => {
session.device = { ...session.device, ...device }
addDefaultAppType(session)
addDefaultUserId(session)
})
client.addOnError(event => {
event.device = { ...event.device, time: new Date(), orientation, ...device }
event.addMetadata('device', {
isDevice: Device.isDevice,
appOwnership: Constants.appOwnership
})
addDefaultAppType(event)
addDefaultUserId(event)
}, true)
}
}
function addDefaultAppType (eventOrSession) {
// default app.type to device.osName
if (eventOrSession.device && eventOrSession.device.osName) {
eventOrSession.app = eventOrSession.app || {}
if (!eventOrSession.app.type) {
eventOrSession.app.type = eventOrSession.device.osName
}
}
}
function addDefaultUserId (eventOrSession) {
// device id is also used to populate the user id field, if it's not already set
const user = eventOrSession.getUser()
if (!user || !user.id) {
eventOrSession.setUser(eventOrSession.device.id)
}
}