mirror of
https://github.com/shlinkio/shlink-web-client.git
synced 2024-12-23 09:30:31 +03:00
Unified function parsing user agent for browser and os
This commit is contained in:
parent
6fede88072
commit
faf5d0bf7b
2 changed files with 19 additions and 23 deletions
|
@ -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) => {
|
||||
|
|
|
@ -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,
|
||||
};
|
||||
});
|
||||
|
|
Loading…
Reference in a new issue