From bd7d81c0274c031ef1d4f2cac3374b14d7688cc8 Mon Sep 17 00:00:00 2001 From: Petr Heinz Date: Thu, 8 Aug 2024 18:19:39 +0200 Subject: [PATCH 1/2] [browser] Improve compatibility of getCurrentContext --- packages/browser/src/browser.ts | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/packages/browser/src/browser.ts b/packages/browser/src/browser.ts index da2a27b..a19ff49 100644 --- a/packages/browser/src/browser.ts +++ b/packages/browser/src/browser.ts @@ -67,18 +67,22 @@ export class Browser extends Base { } protected getCurrentContext(): Context { - return { - context: { - url: window.location.href, - user_locale: (navigator as any).userLanguage || navigator.language, - user_agent: navigator.userAgent, - device_pixel_ratio: window.devicePixelRatio, - screen_width: window.screen.width, - screen_height: window.screen.height, - window_width: window.innerWidth, - window_height: window.innerHeight, - }, - }; + const context: Context = {}; + + if (typeof window !== 'undefined') { + context.url = window.location?.href; + context.device_pixel_ratio = window.devicePixelRatio; + context.screen_width = window.screen?.width; + context.screen_height = window.screen?.height; + context.window_width = window.innerWidth; + context.window_height = window.innerHeight; + } + if (typeof navigator !== 'undefined') { + context.user_locale = (navigator as any).userLanguage || navigator.language; + context.user_agent = navigator.userAgent; + } + + return context; } private configureFlushOnPageLeave(): void { From 042178e34315e4606eec9a8f3346893ff398f5a9 Mon Sep 17 00:00:00 2001 From: Petr Heinz Date: Thu, 8 Aug 2024 18:24:43 +0200 Subject: [PATCH 2/2] lint:save --- packages/browser/src/browser.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/browser/src/browser.ts b/packages/browser/src/browser.ts index a19ff49..1a73528 100644 --- a/packages/browser/src/browser.ts +++ b/packages/browser/src/browser.ts @@ -69,7 +69,7 @@ export class Browser extends Base { protected getCurrentContext(): Context { const context: Context = {}; - if (typeof window !== 'undefined') { + if (typeof window !== "undefined") { context.url = window.location?.href; context.device_pixel_ratio = window.devicePixelRatio; context.screen_width = window.screen?.width; @@ -77,7 +77,7 @@ export class Browser extends Base { context.window_width = window.innerWidth; context.window_height = window.innerHeight; } - if (typeof navigator !== 'undefined') { + if (typeof navigator !== "undefined") { context.user_locale = (navigator as any).userLanguage || navigator.language; context.user_agent = navigator.userAgent; }