Skip to content

Commit 2e05394

Browse files
authored
Merge pull request #1072 from bugsnag/anonymous-device-id
[PLAT-5050] Add anonymous device ID
2 parents d5ac2bb + 8e2c43f commit 2e05394

File tree

13 files changed

+574
-2443
lines changed

13 files changed

+574
-2443
lines changed

Diff for: CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## TBD
44

5+
### Added
6+
7+
- (browser): Attach an anonymous device ID to error reports and sessions when the new `generateAnonymousId` option is enabled. [#1072](https://github.com/bugsnag/bugsnag-js/pull/1072)
8+
59
### Changed
610

711
- (react-native): Allow plugins to be set in the JS layer. [#1064](https://github.com/bugsnag/bugsnag-js/pull/1064)

Diff for: packages/browser/test/index.test.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,8 @@ describe('browser notifier', () => {
131131
logger: undefined,
132132
redactedKeys: ['foo', /bar/],
133133
collectUserIp: true,
134-
maxEvents: 10
134+
maxEvents: 10,
135+
generateAnonymousId: false
135136
})
136137

137138
Bugsnag.notify(new Error('123'), (event) => {

Diff for: packages/browser/types/bugsnag.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { Client, Config, BugsnagStatic } from '@bugsnag/core'
33
interface BrowserConfig extends Config {
44
maxEvents?: number
55
collectUserIp?: boolean
6+
generateAnonymousId?: boolean
67
}
78

89
interface BrowserBugsnagStatic extends BugsnagStatic {

Diff for: packages/plugin-browser-device/device.js

+35
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,28 @@
1+
const cuid = require('@bugsnag/cuid')
12
const assign = require('@bugsnag/core/lib/es-utils/assign')
3+
const BUGSNAG_ANONYMOUS_ID_KEY = 'bugsnag-anonymous-id'
4+
5+
const getDeviceId = () => {
6+
try {
7+
const storage = window.localStorage
8+
9+
let id = storage.getItem(BUGSNAG_ANONYMOUS_ID_KEY)
10+
11+
// If we get an ID, make sure it looks like a valid cuid. The length can
12+
// fluctuate slightly, so some leeway is built in
13+
if (id && /^c[a-z0-9]{20,32}$/.test(id)) {
14+
return id
15+
}
16+
17+
id = cuid()
18+
19+
storage.setItem(BUGSNAG_ANONYMOUS_ID_KEY, id)
20+
21+
return id
22+
} catch (err) {
23+
// If localStorage is not available (e.g. because it's disabled) then give up
24+
}
25+
}
226

327
/*
428
* Automatically detects browser device details
@@ -19,6 +43,10 @@ module.exports = (nav = navigator, screen = window.screen) => ({
1943
: 'portrait'
2044
}
2145

46+
if (client._config.generateAnonymousId) {
47+
device.id = getDeviceId()
48+
}
49+
2250
client.addOnSession(session => {
2351
session.device = assign({}, session.device, device)
2452
})
@@ -31,5 +59,12 @@ module.exports = (nav = navigator, screen = window.screen) => ({
3159
{ time: new Date() }
3260
)
3361
}, true)
62+
},
63+
configSchema: {
64+
generateAnonymousId: {
65+
validate: value => value === true || value === false,
66+
defaultValue: () => true,
67+
message: 'should be true|false'
68+
}
3469
}
3570
})

0 commit comments

Comments
 (0)