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',
|
'WeChat',
|
||||||
];
|
];
|
||||||
|
|
||||||
export const osFromUserAgent = (userAgent) => {
|
export const parseUserAgent = (userAgent) => {
|
||||||
if (!hasValue(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) => {
|
return { os: os || DEFAULT, browser: browser && BROWSERS_WHITELIST.includes(browser) ? browser : DEFAULT };
|
||||||
if (!hasValue(userAgent)) {
|
|
||||||
return DEFAULT;
|
|
||||||
}
|
|
||||||
|
|
||||||
const { name: browser } = bowser.parse(userAgent).browser;
|
|
||||||
|
|
||||||
return browser && BROWSERS_WHITELIST.includes(browser) ? browser : DEFAULT;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
export const extractDomain = (url) => {
|
export const extractDomain = (url) => {
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
import { isNil, map } from 'ramda';
|
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';
|
import { hasValue } from '../../utils/utils';
|
||||||
|
|
||||||
const visitHasProperty = (visit, propertyName) => !isNil(visit) && hasValue(visit[propertyName]);
|
const visitHasProperty = (visit, propertyName) => !isNil(visit) && hasValue(visit[propertyName]);
|
||||||
|
@ -59,13 +59,17 @@ export const processStatsFromVisits = (normalizedVisits) =>
|
||||||
{ os: {}, browsers: {}, referrers: {}, countries: {}, cities: {}, citiesForMap: {} }
|
{ os: {}, browsers: {}, referrers: {}, countries: {}, cities: {}, citiesForMap: {} }
|
||||||
);
|
);
|
||||||
|
|
||||||
export const normalizeVisits = map(({ userAgent, date, referer, visitLocation }) => ({
|
export const normalizeVisits = map(({ userAgent, date, referer, visitLocation }) => {
|
||||||
date,
|
const { browser, os } = parseUserAgent(userAgent);
|
||||||
browser: browserFromUserAgent(userAgent),
|
|
||||||
os: osFromUserAgent(userAgent),
|
return {
|
||||||
referer: extractDomain(referer),
|
date,
|
||||||
country: (visitLocation && visitLocation.countryName) || 'Unknown',
|
browser,
|
||||||
city: (visitLocation && visitLocation.cityName) || 'Unknown',
|
os,
|
||||||
latitude: visitLocation && visitLocation.latitude,
|
referer: extractDomain(referer),
|
||||||
longitude: visitLocation && visitLocation.longitude,
|
country: (visitLocation && visitLocation.countryName) || 'Unknown',
|
||||||
}));
|
city: (visitLocation && visitLocation.cityName) || 'Unknown',
|
||||||
|
latitude: visitLocation && visitLocation.latitude,
|
||||||
|
longitude: visitLocation && visitLocation.longitude,
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
Loading…
Reference in a new issue