Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 24 additions & 15 deletions packages/browser/src/integrations/useragent.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { addGlobalEventProcessor, getCurrentHub } from '@sentry/core';
import { Event, Integration } from '@sentry/types';
import { Event, EventProcessor, Hub, Integration } from '@sentry/types';
import { getGlobalObject } from '@sentry/utils';

const global = getGlobalObject<Window>();
Expand All @@ -19,25 +18,35 @@ export class UserAgent implements Integration {
/**
* @inheritDoc
*/
public setupOnce(): void {
public setupOnce(addGlobalEventProcessor: (callback: EventProcessor) => void, getCurrentHub: () => Hub): void {
addGlobalEventProcessor((event: Event) => {
if (getCurrentHub().getIntegration(UserAgent)) {
const { navigator, location, document } = global;
// if none of the information we want exists, don't bother
if (!global.navigator && !global.location && !global.document) {
if (!navigator && !location && !document) {
return event;
}

// grab as much info as exists and add it to the event
const url = event.request?.url || global.location?.href;
const { referrer } = global.document || {};
const { userAgent } = global.navigator || {};

const headers = {
...event.request?.headers,
...(referrer && { Referer: referrer }),
...(userAgent && { 'User-Agent': userAgent }),
};
const request = { ...(url && { url }), headers };
const req = event.request;
const headers = req && req.headers ? { ...req.headers } : {};

const { userAgent } = navigator || {};
if (userAgent) {
headers['User-Agent'] = userAgent;
}

const { referrer } = document || {};
if (referrer) {
headers.Referer = referrer;
}

const request = { headers } as Record<string, unknown>;

const { href } = location || {};
const url = (req && req.url) || href;
if (url) {
request.url = url;
}

return { ...event, request };
}
Expand Down