Skip to content

feat(integrations): Add referrer to data collected by UserAgent integration #2912

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 16, 2020
Merged
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
21 changes: 13 additions & 8 deletions packages/browser/src/integrations/useragent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
});
Expand Down