From faf5d0bf7b857a4752d54f4c81b7890a8e5a1ec7 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Fri, 10 Apr 2020 19:22:13 +0200 Subject: [PATCH] Unified function parsing user agent for browser and os --- src/utils/helpers/visits.js | 16 ++++------------ src/visits/services/VisitsParser.js | 26 +++++++++++++++----------- 2 files changed, 19 insertions(+), 23 deletions(-) diff --git a/src/utils/helpers/visits.js b/src/utils/helpers/visits.js index 9e018430..8f5b9e8d 100644 --- a/src/utils/helpers/visits.js +++ b/src/utils/helpers/visits.js @@ -16,22 +16,14 @@ const BROWSERS_WHITELIST = [ 'WeChat', ]; -export const osFromUserAgent = (userAgent) => { +export const parseUserAgent = (userAgent) => { if (!hasValue(userAgent)) { - return DEFAULT; + return { browser: DEFAULT, os: DEFAULT }; } - return bowser.parse(userAgent).os.name || DEFAULT; -}; + const { browser: { name: browser }, os: { name: os } } = bowser.parse(userAgent); -export const browserFromUserAgent = (userAgent) => { - if (!hasValue(userAgent)) { - return DEFAULT; - } - - const { name: browser } = bowser.parse(userAgent).browser; - - return browser && BROWSERS_WHITELIST.includes(browser) ? browser : DEFAULT; + return { os: os || DEFAULT, browser: browser && BROWSERS_WHITELIST.includes(browser) ? browser : DEFAULT }; }; export const extractDomain = (url) => { diff --git a/src/visits/services/VisitsParser.js b/src/visits/services/VisitsParser.js index 2ebd5262..5ab88f71 100644 --- a/src/visits/services/VisitsParser.js +++ b/src/visits/services/VisitsParser.js @@ -1,5 +1,5 @@ import { isNil, map } from 'ramda'; -import { browserFromUserAgent, extractDomain, osFromUserAgent } from '../../utils/helpers/visits'; +import { extractDomain, parseUserAgent } from '../../utils/helpers/visits'; import { hasValue } from '../../utils/utils'; const visitHasProperty = (visit, propertyName) => !isNil(visit) && hasValue(visit[propertyName]); @@ -59,13 +59,17 @@ export const processStatsFromVisits = (normalizedVisits) => { os: {}, browsers: {}, referrers: {}, countries: {}, cities: {}, citiesForMap: {} } ); -export const normalizeVisits = map(({ userAgent, date, referer, visitLocation }) => ({ - date, - browser: browserFromUserAgent(userAgent), - os: osFromUserAgent(userAgent), - referer: extractDomain(referer), - country: (visitLocation && visitLocation.countryName) || 'Unknown', - city: (visitLocation && visitLocation.cityName) || 'Unknown', - latitude: visitLocation && visitLocation.latitude, - longitude: visitLocation && visitLocation.longitude, -})); +export const normalizeVisits = map(({ userAgent, date, referer, visitLocation }) => { + const { browser, os } = parseUserAgent(userAgent); + + return { + date, + browser, + os, + referer: extractDomain(referer), + country: (visitLocation && visitLocation.countryName) || 'Unknown', + city: (visitLocation && visitLocation.cityName) || 'Unknown', + latitude: visitLocation && visitLocation.latitude, + longitude: visitLocation && visitLocation.longitude, + }; +});