Skip to content

Commit

Permalink
get userAgentData on plugin load
Browse files Browse the repository at this point in the history
  • Loading branch information
danieljackins committed Sep 22, 2023
1 parent aad9f05 commit fedad18
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<UADataValues> => {
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 },
Expand All @@ -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 () => {
Expand Down Expand Up @@ -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<UADataValues> => {
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)
})

Expand Down
22 changes: 12 additions & 10 deletions packages/browser/src/plugins/page-enrichment/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<Context> => {
private enrich = (ctx: Context): Context => {
const event = ctx.event
const evtCtx = (event.context ??= {})

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit fedad18

Please sign in to comment.