Unified function parsing user agent for browser and os

This commit is contained in:
Alejandro Celaya 2020-04-10 19:22:13 +02:00
parent 6fede88072
commit faf5d0bf7b
2 changed files with 19 additions and 23 deletions

View file

@ -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) => {

View file

@ -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 }) => ({
export const normalizeVisits = map(({ userAgent, date, referer, visitLocation }) => {
const { browser, os } = parseUserAgent(userAgent);
return {
date,
browser: browserFromUserAgent(userAgent),
os: osFromUserAgent(userAgent),
browser,
os,
referer: extractDomain(referer),
country: (visitLocation && visitLocation.countryName) || 'Unknown',
city: (visitLocation && visitLocation.cityName) || 'Unknown',
latitude: visitLocation && visitLocation.latitude,
longitude: visitLocation && visitLocation.longitude,
}));
};
});