From 8030fd858105c80a0eb7ffe82b60ce1ce28ebb28 Mon Sep 17 00:00:00 2001 From: Katie Byers Date: Wed, 16 Sep 2020 09:03:52 -0700 Subject: [PATCH] refactor and add referrer --- .../browser/src/integrations/useragent.ts | 21 ++++++++++++------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/packages/browser/src/integrations/useragent.ts b/packages/browser/src/integrations/useragent.ts index e922cae64c1d..925255f074e9 100644 --- a/packages/browser/src/integrations/useragent.ts +++ b/packages/browser/src/integrations/useragent.ts @@ -22,19 +22,24 @@ export class UserAgent implements Integration { public setupOnce(): void { addGlobalEventProcessor((event: Event) => { if (getCurrentHub().getIntegration(UserAgent)) { - if (!global.navigator || !global.location) { + // if none of the information we want exists, don't bother + if (!global.navigator && !global.location && !global.document) { return event; } - const request = event.request || {}; - request.url = request.url || global.location.href; - request.headers = request.headers || {}; - request.headers['User-Agent'] = global.navigator.userAgent; + // 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 || {}; - return { - ...event, - request, + const headers = { + ...event.request?.headers, + ...(referrer && { Referer: referrer }), + ...(userAgent && { 'User-Agent': userAgent }), }; + const request = { ...(url && { url }), headers }; + + return { ...event, request }; } return event; });