Skip to content

Commit f391ca3

Browse files
committed
feat: UserAgent integration
1 parent 6c3b183 commit f391ca3

File tree

4 files changed

+42
-1
lines changed

4 files changed

+42
-1
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
- [browser] feat: Better dedupe integration event description
66
- [core] ref: Move Dedupe, FunctionString, InboundFilters and SdkInformation integrations to the core package
7+
- [core] feat: Provide correct platform and make a place to override event internals
8+
- [browser] feat: UserAgent integration
79

810
## 4.0.2
911

packages/browser/src/integrations/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ export { TryCatch } from './trycatch';
33
export { Breadcrumbs } from './breadcrumbs';
44
export { LinkedErrors } from './linkederrors';
55
export { ReportingObserver } from './reportingobserver';
6+
export { UserAgent } from './useragent';
67

78
export { Ember } from './pluggable/ember';
89
export { Vue } from './pluggable/vue';
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { configureScope } from '@sentry/core';
2+
import { Integration, SentryEvent } from '@sentry/types';
3+
import { getGlobalObject } from '@sentry/utils/misc';
4+
5+
const global = getGlobalObject() as Window;
6+
7+
/** UserAgent */
8+
export class UserAgent implements Integration {
9+
/**
10+
* @inheritDoc
11+
*/
12+
public name: string = 'UserAgent';
13+
14+
/**
15+
* @inheritDoc
16+
*/
17+
public install(): void {
18+
configureScope(scope => {
19+
scope.addEventProcessor(async (event: SentryEvent) => {
20+
if (!global.navigator || !global.location) {
21+
return event;
22+
}
23+
24+
// HTTP Interface: https://docs.sentry.io/clientdev/interfaces/http/?platform=javascript
25+
const request = event.request || {};
26+
request.url = request.url || global.location.href;
27+
request.headers = request.headers || {};
28+
request.headers['User-Agent'] = global.navigator.userAgent;
29+
30+
return {
31+
...event,
32+
request,
33+
};
34+
});
35+
});
36+
}
37+
}

packages/browser/src/sdk.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { getCurrentHub, initAndBind, Integrations as CoreIntegrations } from '@s
22
import { DsnLike } from '@sentry/types';
33
import { BrowserOptions } from './backend';
44
import { BrowserClient } from './client';
5-
import { Breadcrumbs, GlobalHandlers, LinkedErrors, ReportingObserver, TryCatch } from './integrations';
5+
import { Breadcrumbs, GlobalHandlers, LinkedErrors, ReportingObserver, TryCatch, UserAgent } from './integrations';
66
import { SDK_NAME, SDK_VERSION } from './version';
77

88
export const defaultIntegrations = [
@@ -23,6 +23,7 @@ export const defaultIntegrations = [
2323
new ReportingObserver(),
2424
// Misc
2525
new LinkedErrors(),
26+
new UserAgent(),
2627
];
2728

2829
/**

0 commit comments

Comments
 (0)