From 97029d947d35206f1976e5804f1a58d210fa9eb9 Mon Sep 17 00:00:00 2001 From: Paul D'Ambra Date: Mon, 30 Oct 2023 17:57:12 +0000 Subject: [PATCH] fix: include raw user agent in event properties (#868) * fix: include raw user agent in event properties * remove old js tests --- src/__tests__/utils.js | 9 ------- src/__tests__/utils/event-utils.test.ts | 32 +++++++++++++++++++++++++ src/utils/event-utils.ts | 1 + 3 files changed, 33 insertions(+), 9 deletions(-) create mode 100644 src/__tests__/utils/event-utils.test.ts diff --git a/src/__tests__/utils.js b/src/__tests__/utils.js index 9085bbfba..94c8c4224 100644 --- a/src/__tests__/utils.js +++ b/src/__tests__/utils.js @@ -20,15 +20,6 @@ function userAgentFor(botString) { return `Mozilla/5.0 (compatible; ${botString}/${randOne}; +http://a.com/bot/${randTwo})` } -describe(`utils.js`, () => { - it('should have $host and $pathname in properties', () => { - const properties = _info.properties() - expect(properties['$current_url']).toBeDefined() - expect(properties['$host']).toBeDefined() - expect(properties['$pathname']).toBeDefined() - }) -}) - describe('_.copyAndTruncateStrings', () => { given('subject', () => _copyAndTruncateStrings(given.target, given.maxStringLength)) diff --git a/src/__tests__/utils/event-utils.test.ts b/src/__tests__/utils/event-utils.test.ts new file mode 100644 index 000000000..013df2ae7 --- /dev/null +++ b/src/__tests__/utils/event-utils.test.ts @@ -0,0 +1,32 @@ +import { _info } from '../../utils/event-utils' +import * as globals from '../../utils/globals' + +jest.mock('../../utils/globals') + +describe(`event-utils`, () => { + it('should have $host and $pathname in properties', () => { + const properties = _info.properties() + expect(properties['$current_url']).toBeDefined() + expect(properties['$host']).toBeDefined() + expect(properties['$pathname']).toBeDefined() + }) + + it('should have user agent in properties', () => { + // TS doesn't like it but we can assign userAgent + // eslint-disable-next-line @typescript-eslint/ban-ts-comment + // @ts-ignore + globals['userAgent'] = 'blah' + const properties = _info.properties() + expect(properties['$raw_user_agent']).toBe('blah') + }) + + it('should truncate very long user agents in properties', () => { + // TS doesn't like it but we can assign userAgent + // eslint-disable-next-line @typescript-eslint/ban-ts-comment + // @ts-ignore + globals['userAgent'] = 'a'.repeat(1001) + const properties = _info.properties() + expect(properties['$raw_user_agent'].length).toBe(1000) + expect(properties['$raw_user_agent'].substring(995)).toBe('aa...') + }) +}) diff --git a/src/utils/event-utils.ts b/src/utils/event-utils.ts index 2fed693a9..9b46ee3de 100644 --- a/src/utils/event-utils.ts +++ b/src/utils/event-utils.ts @@ -258,6 +258,7 @@ export const _info = { $current_url: window?.location.href, $host: window?.location.host, $pathname: window?.location.pathname, + $raw_user_agent: userAgent.length > 1000 ? userAgent.substring(0, 997) + '...' : userAgent, $browser_version: _info.browserVersion(userAgent, navigator.vendor, (window as any).opera), $browser_language: _info.browserLanguage(), $screen_height: window?.screen.height,