From fedad18abcf1d84d274780ac1d6fea55b37976ba Mon Sep 17 00:00:00 2001 From: Daniel Jackins Date: Fri, 22 Sep 2023 11:15:04 -0600 Subject: [PATCH] get userAgentData on plugin load --- .../page-enrichment/__tests__/index.test.ts | 50 +++++++++---------- .../src/plugins/page-enrichment/index.ts | 22 ++++---- 2 files changed, 36 insertions(+), 36 deletions(-) diff --git a/packages/browser/src/plugins/page-enrichment/__tests__/index.test.ts b/packages/browser/src/plugins/page-enrichment/__tests__/index.test.ts index df68018c5..e0bee7514 100644 --- a/packages/browser/src/plugins/page-enrichment/__tests__/index.test.ts +++ b/packages/browser/src/plugins/page-enrichment/__tests__/index.test.ts @@ -267,6 +267,29 @@ describe('pageDefaults', () => { describe('Other visitor metadata', () => { let options: SegmentioSettings let analytics: Analytics + ;(window.navigator as any).userAgentData = { + ...lowEntropyTestData, + getHighEntropyValues: jest + .fn() + .mockImplementation((hints: string[]): Promise => { + let result = {} + Object.entries(highEntropyTestData).forEach(([k, v]) => { + if (hints.includes(k)) { + result = { + ...result, + [k]: v, + } + } + }) + return Promise.resolve({ + ...lowEntropyTestData, + ...result, + }) + }), + toJSON: jest.fn(() => { + return lowEntropyTestData + }), + } const amendSearchParams = (search?: any): CoreExtraContext => ({ page: { search }, @@ -290,7 +313,7 @@ describe('Other visitor metadata', () => { it('should add .timezone', async () => { const ctx = await analytics.track('test') - assert(ctx.event.context?.timezone) + assert(typeof ctx.event.context?.timezone === 'string') }) it('should add .library', async () => { @@ -324,32 +347,7 @@ describe('Other visitor metadata', () => { }) it('should add .userAgentData when available', async () => { - ;(window.navigator as any).userAgentData = { - ...lowEntropyTestData, - getHighEntropyValues: jest - .fn() - .mockImplementation((hints: string[]): Promise => { - let result = {} - Object.entries(highEntropyTestData).forEach(([k, v]) => { - if (hints.includes(k)) { - result = { - ...result, - [k]: v, - } - } - }) - return Promise.resolve({ - ...lowEntropyTestData, - ...result, - }) - }), - toJSON: jest.fn(() => { - return lowEntropyTestData - }), - } - const ctx = await analytics.track('event') - expect(ctx.event.context?.userAgentData).toEqual(lowEntropyTestData) }) diff --git a/packages/browser/src/plugins/page-enrichment/index.ts b/packages/browser/src/plugins/page-enrichment/index.ts index 761cfe345..b113dc337 100644 --- a/packages/browser/src/plugins/page-enrichment/index.ts +++ b/packages/browser/src/plugins/page-enrichment/index.ts @@ -11,6 +11,7 @@ import { gracefulDecodeURIComponent } from '../../core/query-string/gracefulDeco import { CookieStorage, UniversalStorage } from '../../core/storage' import { Analytics } from '../../core/analytics' import { clientHints } from '../../lib/client-hints' +import { UADataValues } from '../../lib/client-hints/interfaces' interface PageDefault { [key: string]: unknown @@ -175,17 +176,25 @@ function referrerId( class PageEnrichmentPlugin implements Plugin { private instance!: Analytics + private userAgentData: UADataValues | undefined name = 'Page Enrichment' type: PluginType = 'before' version = '0.1.0' isLoaded = () => true - load = (_ctx: Context, instance: Analytics) => { + load = async (_ctx: Context, instance: Analytics) => { this.instance = instance + try { + this.userAgentData = await clientHints( + this.instance.options.highEntropyValuesClientHints + ) + } catch (_) { + // if client hints API doesn't return anything leave undefined + } return Promise.resolve() } - private enrich = async (ctx: Context): Promise => { + private enrich = (ctx: Context): Context => { const event = ctx.event const evtCtx = (event.context ??= {}) @@ -214,14 +223,7 @@ class PageEnrichmentPlugin implements Plugin { const query: string = evtCtx.page.search || '' evtCtx.userAgent = navigator.userAgent - - try { - evtCtx.userAgentData = await clientHints( - this.instance.options.highEntropyValuesClientHints - ) - } catch (_) { - // if client hints API doesn't return anything leave undefined - } + evtCtx.userAgentData = this.userAgentData // @ts-ignore const locale = navigator.userLanguage || navigator.language