Skip to content

armgit / umami

Public Imported from GitHub
0

Umami is a privacy-first web and product analytics platform. Traffic, campaigns, behavior, conversions, and revenue in one place. No cookies, no surveillance, self-hosted or in the cloud.

Added laptop detection. Reorganized IP headers.

Mike Cao684863d6c7d6
diff --git a/src/lib/detect.ts b/src/lib/detect.ts
index 16ff97da2..c5528465d 100644
--- a/src/lib/detect.ts
+++ b/src/lib/detect.ts
@@ -30,10 +30,18 @@ const PROVIDER_HEADERS = [
},
];
-export function getDevice(userAgent: string) {
+export function getDevice(userAgent: string, screen: string = '') {
const { device } = UAParser(userAgent);
- return device?.type || 'desktop';
+ const [width] = screen.split('x');
+
+ const type = device?.type || 'desktop';
+
+ if (type === 'desktop' && screen && +width <= 1920) {
+ return 'laptop';
+ }
+
+ return type;
}
function getRegionCode(country: string, region: string) {
@@ -108,7 +116,7 @@ export async function getClientInfo(request: Request, payload: Record<string, an
const city = safeDecodeURIComponent(location?.city);
const browser = browserName(userAgent);
const os = detectOS(userAgent) as string;
- const device = getDevice(userAgent);
+ const device = getDevice(userAgent, payload?.screen);
return { userAgent, browser, os, ip, country, region, city, device };
}
diff --git a/src/lib/ip.ts b/src/lib/ip.ts
index 170701505..a818f03d7 100644
--- a/src/lib/ip.ts
+++ b/src/lib/ip.ts
@@ -1,19 +1,16 @@
-// The order here is important and influences how IPs are detected by lib/detect.ts
-// Please do not change the order unless you know exactly what you're doing - read https://developers.cloudflare.com/fundamentals/reference/http-headers/
export const IP_ADDRESS_HEADERS = [
- 'x-client-ip',
- 'x-forwarded-for',
- 'cf-connecting-ip', // This should be *after* x-forwarded-for, so that x-forwarded-for is respected if present
- 'do-connecting-ip',
- 'fastly-client-ip',
- 'true-client-ip',
'x-real-ip',
+ 'x-forwarded-for',
+ 'cf-connecting-ip', // Cloudflare
+ 'fastly-client-ip', // Fastly
+ 'true-client-ip', // Akamai
+ 'x-nf-client-connection-ip', // Netlify
+ 'do-connecting-ip', // Digital Ocean
+ 'x-appengine-user-ip', // Google App Ending
+ 'x-client-ip',
'x-cluster-client-ip',
'x-forwarded',
'forwarded',
- 'x-appengine-user-ip',
- 'x-nf-client-connection-ip',
- 'x-real-ip',
];
export function getIpAddress(headers: Headers) {