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.
TypeScript analyticsaudience-segmentationchartscohort-analysisgoogle-analyticsproduct-analyticsstatisticsuser-journeyweb-analytics
Update device filtering.
Mike Cao9d09d89aefe3
diff --git a/components/WebsiteDetails.js b/components/WebsiteDetails.jsindex 7e24afaf4..3b84f2f0d 100644--- a/components/WebsiteDetails.js+++ b/components/WebsiteDetails.js@@ -105,7 +105,7 @@ export default function WebsiteDetails({ websiteId, defaultDateRange = '7day' })<div className={sessionClasses}><RankingsCharttitle="Devices"- type="screen"+ type="device"heading="Visitors"websiteId={websiteId}startDate={startDate}diff --git a/lib/constants.js b/lib/constants.jsindex c6e97e2e5..f1c151e7e 100644--- a/lib/constants.js+++ b/lib/constants.js@@ -1,6 +1,7 @@export const AUTH_COOKIE_NAME = 'umami.auth';export const DESKTOP_SCREEN_WIDTH = 1920;+export const LAPTOP_SCREEN_WIDTH = 1024;export const MOBILE_SCREEN_WIDTH = 479;export const OPERATING_SYSTEMS = [@@ -56,6 +57,13 @@ export const DESKTOP_OS = [export const MOBILE_OS = ['iOS', 'Android OS', 'BlackBerry OS', 'Windows Mobile', 'Amazon OS'];+export const DEVICES = {+ desktop: 'Desktop',+ laptop: 'Laptop',+ tablet: 'Tablet',+ mobile: 'Mobile',+};+export const BROWSERS = {aol: 'AOL',edge: 'Edge',diff --git a/lib/filters.js b/lib/filters.jsindex a6a3dbad5..ed4c038a9 100644--- a/lib/filters.js+++ b/lib/filters.js@@ -1,4 +1,4 @@-import { BROWSERS, ISO_COUNTRIES } from './constants';+import { BROWSERS, ISO_COUNTRIES, DEVICES } from './constants';export const browserFilter = data =>data.map(({ x, ...props }) => ({ x: BROWSERS[x] || x, ...props }));@@ -8,28 +8,8 @@ export const urlFilter = data => data.filter(({ x }) => x !== '' && !x.startsWitexport const refFilter = data =>data.filter(({ x }) => x !== '' && !x.startsWith('/') && !x.startsWith('#'));-export const deviceFilter = data => {- if (data.length === 0) return [];-- const devices = data.reduce(- (obj, { x, y }) => {- const [width] = x.split('x');- if (width >= 1920) {- obj.Desktop += +y;- } else if (width >= 1024) {- obj.Laptop += +y;- } else if (width >= 767) {- obj.Tablet += +y;- } else {- obj.Mobile += +y;- }- return obj;- },- { Desktop: 0, Laptop: 0, Tablet: 0, Mobile: 0 },- );-- return percentFilter(Object.keys(devices).map(key => ({ x: key, y: devices[key] })));-};+export const deviceFilter = data =>+ data.map(({ x, ...props }) => ({ x: DEVICES[x] || x, ...props }));export const countryFilter = data =>data.map(({ x, ...props }) => ({ x: ISO_COUNTRIES[x] || x, ...props }));diff --git a/lib/request.js b/lib/request.jsindex f2b70c544..e0d022321 100644--- a/lib/request.js+++ b/lib/request.js@@ -3,7 +3,13 @@ import { browserName, detectOS } from 'detect-browser';import isLocalhost from 'is-localhost-ip';import maxmind from 'maxmind';import geolite2 from 'geolite2-redist';-import { DESKTOP_OS, MOBILE_OS, DESKTOP_SCREEN_WIDTH, MOBILE_SCREEN_WIDTH } from './constants';+import {+ DESKTOP_OS,+ MOBILE_OS,+ DESKTOP_SCREEN_WIDTH,+ LAPTOP_SCREEN_WIDTH,+ MOBILE_SCREEN_WIDTH,+} from './constants';export function getIpAddress(req) {// Cloudflare@@ -15,6 +21,8 @@ export function getIpAddress(req) {}export function getDevice(screen, browser, os) {+ if (!screen) return;+const [width] = screen.split('x');if (DESKTOP_OS.includes(os)) {@@ -28,6 +36,16 @@ export function getDevice(screen, browser, os) {}return 'mobile';}++ if (width >= DESKTOP_SCREEN_WIDTH) {+ return 'desktop';+ } else if (width >= LAPTOP_SCREEN_WIDTH) {+ return 'laptop';+ } else if (width >= MOBILE_SCREEN_WIDTH) {+ return 'tablet';+ } else {+ return 'mobile';+ }}export async function getCountry(req, ip) {diff --git a/pages/api/website/[id]/rankings.js b/pages/api/website/[id]/rankings.jsindex d4942b65f..4be955cfe 100644--- a/pages/api/website/[id]/rankings.js+++ b/pages/api/website/[id]/rankings.js@@ -1,7 +1,7 @@import { getRankings } from 'lib/db';import { useAuth } from 'lib/middleware';-const sessionColumns = ['browser', 'os', 'screen', 'country'];+const sessionColumns = ['browser', 'os', 'device', 'country'];const pageviewColumns = ['url', 'referrer'];export default async (req, res) => {