Skip to content

Commit 71d2927

Browse files
authored
feat(integrations): Add referrer to data collected by UserAgent integration (#2912)
1 parent c9f956b commit 71d2927

File tree

1 file changed

+13
-8
lines changed

1 file changed

+13
-8
lines changed

packages/browser/src/integrations/useragent.ts

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,24 @@ export class UserAgent implements Integration {
2222
public setupOnce(): void {
2323
addGlobalEventProcessor((event: Event) => {
2424
if (getCurrentHub().getIntegration(UserAgent)) {
25-
if (!global.navigator || !global.location) {
25+
// if none of the information we want exists, don't bother
26+
if (!global.navigator && !global.location && !global.document) {
2627
return event;
2728
}
2829

29-
const request = event.request || {};
30-
request.url = request.url || global.location.href;
31-
request.headers = request.headers || {};
32-
request.headers['User-Agent'] = global.navigator.userAgent;
30+
// grab as much info as exists and add it to the event
31+
const url = event.request?.url || global.location?.href;
32+
const { referrer } = global.document || {};
33+
const { userAgent } = global.navigator || {};
3334

34-
return {
35-
...event,
36-
request,
35+
const headers = {
36+
...event.request?.headers,
37+
...(referrer && { Referer: referrer }),
38+
...(userAgent && { 'User-Agent': userAgent }),
3739
};
40+
const request = { ...(url && { url }), headers };
41+
42+
return { ...event, request };
3843
}
3944
return event;
4045
});

0 commit comments

Comments
 (0)