Skip to content
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

Introduce Client Hints API #864

Merged
merged 22 commits into from
Jun 6, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 2 additions & 0 deletions packages/browser/src/core/analytics/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ import { version } from '../../generated/version'
import { PriorityQueue } from '../../lib/priority-queue'
import { getGlobal } from '../../lib/get-global'
import { AnalyticsClassic, AnalyticsCore } from './interfaces'
import { HighEntropyValues } from '../../plugins/segmentio/normalize'

const deprecationWarning =
'This is being deprecated and will be not be available in future releases of Analytics JS'
Expand Down Expand Up @@ -102,6 +103,7 @@ export interface InitOptions {
aid?: RegExp
uid?: RegExp
}
highEntropyValues?: HighEntropyValues[]
}

/* analytics-classic stubs */
Expand Down
7 changes: 3 additions & 4 deletions packages/browser/src/plugins/segmentio/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,10 @@ export function segmentio(
json = onAlias(analytics, json)
}

const event = await normalize(analytics, json, settings, integrations)

return client
.dispatch(
`${remote}/${path}`,
normalize(analytics, json, settings, integrations)
)
.dispatch(`${remote}/${path}`, event)
.then(() => ctx)
.catch(() => {
buffer.pushWithBackoff(ctx)
Expand Down
66 changes: 64 additions & 2 deletions packages/browser/src/plugins/segmentio/normalize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,42 @@ import { SegmentioSettings } from './index'
import { version } from '../../generated/version'
import { getAvailableStorageOptions, UniversalStorage } from '../../core/user'

declare global {
danieljackins marked this conversation as resolved.
Show resolved Hide resolved
interface Navigator {
userAgentData: UserAgentData
danieljackins marked this conversation as resolved.
Show resolved Hide resolved
}
}

interface Brand {
brand: string
version: string
}

export type HighEntropyValues =
| 'architecture'
| 'bitness'
| 'model'
| 'platformVersion'
| 'fullVersionList'

export interface UserAgentData {
brands: Brand[]
mobile: boolean
platform: string
getHighEntropyValues: Function
}

interface UserAgentDataContext {
brands: Brand[]
mobile: boolean
platform: string
platformVersion?: string
architecture?: string
bitness?: string
model?: string
fullVersionList?: Brand[]
}

let cookieOptions: jar.CookieAttributes | undefined
function getCookieOptions(): jar.CookieAttributes {
if (cookieOptions) {
Expand Down Expand Up @@ -118,12 +154,35 @@ function referrerId(
storage.set('s:context.referrer', ad)
}

export function normalize(
async function clientHints(
hints: HighEntropyValues[] | undefined
): Promise<UserAgentDataContext> {
const userAgentData = navigator.userAgentData

let userAgentDataContext: UserAgentDataContext = {
brands: userAgentData.brands,
mobile: userAgentData.mobile,
platform: userAgentData.platform,
}

if (hints) {
try {
// this also includes the low entropy values, so we can overwrite here
userAgentDataContext = await userAgentData.getHighEntropyValues(hints)
} catch (_) {
return userAgentDataContext
}
}

return userAgentDataContext
}

export async function normalize(
analytics: Analytics,
json: ReturnType<SegmentFacade['json']>,
settings?: SegmentioSettings,
integrations?: LegacySettings['integrations']
): object {
): Promise<object> {
const user = analytics.user()

// context should always exist here (see page enrichment)? ... and why would we default to json.options? todo: delete this
Expand All @@ -136,6 +195,9 @@ export function normalize(
delete json.options
json.writeKey = settings?.apiKey
ctx.userAgent = window.navigator.userAgent
danieljackins marked this conversation as resolved.
Show resolved Hide resolved
if (window.navigator.userAgentData) {
ctx.userAgentData = await clientHints(analytics.options.highEntropyValues)
}

// @ts-ignore
const locale = navigator.userLanguage || navigator.language
Expand Down