Skip to content

Commit

Permalink
chore(data): send user agent detail with anonymous usage data
Browse files Browse the repository at this point in the history
as a workaround for chrome's default to reduced user-agent data
  • Loading branch information
charleslavon committed Jan 26, 2024
1 parent aef946d commit 155da2b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,12 @@ declare module 'nanoid' {
interface Window {
zE: (name: string, method: string) => void | undefined;
}

interface ExperimentalNaviagtor extends Navigator {
readonly userAgentData?: NavigatorUAData;
}

// https://wicg.github.io/ua-client-hints/#navigatoruadata
interface NavigatorUAData extends UALowEntropyJSON {
getHighEntropyValues(hints: string[]): Promise<UADataValues>;
}
17 changes: 17 additions & 0 deletions src/utils/analytics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { networkId } from './config';

let rudderAnalytics: Analytics | null = null;
let anonymousUserId = '';
let userAgentDetail = '';
let hashId = '';
let anonymousUserIdCreatedAt = '';
let pendingEvents: any = [];
Expand Down Expand Up @@ -45,7 +46,20 @@ function getAnonymousId() {
return anonymousUserId;
}

function getUserAgent() {
if (!userAgentDetail) {
const nav: ExperimentalNaviagtor = navigator;
nav.userAgentData &&
nav.userAgentData.getHighEntropyValues(['platformVersion', 'model']).then((ua: any) => {
userAgentDetail = ua;
});
}
return userAgentDetail;
}

export async function init() {
getUserAgent();

if (window?.rudderAnalytics) return;

getAnonymousId();
Expand Down Expand Up @@ -93,6 +107,7 @@ export function recordPageView(pageName: string) {
rudderAnalytics.page('category', pageName, {
hashId: localStorage.getItem('hashId'),
url: filterURL(window.location.href),
userAgentDetail,
ref: filterURL(document.referrer),
});
} catch (e) {
Expand Down Expand Up @@ -138,6 +153,7 @@ export function recordEventWithProps(eventLabel: string, properties: Record<stri
try {
rudderAnalytics.track(eventLabel, {
...properties,
userAgentDetail,
hashId: localStorage.getItem('hashId'),
anonymousUserIdCreatedAt,
});
Expand All @@ -152,6 +168,7 @@ export function recordEvent(eventLabel: string) {
rudderAnalytics.track(eventLabel, {
hashId: localStorage.getItem('hashId'),
url: window.location.href,
userAgentDetail,
anonymousUserIdCreatedAt,
});
} catch (e) {
Expand Down

0 comments on commit 155da2b

Please sign in to comment.