-
Notifications
You must be signed in to change notification settings - Fork 257
Expand file tree
/
Copy pathdevice.js
More file actions
81 lines (68 loc) · 2.21 KB
/
device.js
File metadata and controls
81 lines (68 loc) · 2.21 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
const assign = require('@bugsnag/core/lib/es-utils/assign')
const BUGSNAG_ANONYMOUS_ID_KEY = 'bugsnag-anonymous-id'
const cuid = require('@bugsnag/cuid')
const getDeviceId = (win) => {
try {
const storage = win.localStorage
let id = storage.getItem(BUGSNAG_ANONYMOUS_ID_KEY)
// If we get an ID, make sure it looks like a valid cuid
if (id && cuid.isCuid(id)) {
return id
}
id = cuid()
storage.setItem(BUGSNAG_ANONYMOUS_ID_KEY, id)
return id
} catch (err) {
// If localStorage is not available (e.g. because it's disabled) then give up
}
}
/*
* Automatically detects browser device details
*/
module.exports = (nav = navigator, win = window) => ({
load: (client) => {
const device = {
locale: nav.browserLanguage || nav.systemLanguage || nav.userLanguage || nav.language,
userAgent: nav.userAgent
}
if (win && win.screen && win.screen.orientation && win.screen.orientation.type) {
device.orientation = win.screen.orientation.type
} else if (win && win.document) {
device.orientation =
win.document.documentElement.clientWidth > win.document.documentElement.clientHeight
? 'landscape'
: 'portrait'
}
if (client._config.generateAnonymousId) {
device.id = getDeviceId(win)
}
client.addOnSession(session => {
session.device = assign({}, session.device, device)
// only set device id if collectUserIp is false
if (!client._config.collectUserIp) setDefaultUserId(session)
})
// add time just as the event is sent
client.addOnError((event) => {
event.device = assign({},
event.device,
device,
{ time: new Date() }
)
if (!client._config.collectUserIp) setDefaultUserId(event)
}, true)
},
configSchema: {
generateAnonymousId: {
validate: value => value === true || value === false,
defaultValue: () => true,
message: 'should be true|false'
}
}
})
const setDefaultUserId = (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)
}
}