umami/lib/filters.js

132 lines
2.9 KiB
JavaScript
Raw Normal View History

2020-10-01 01:27:27 +02:00
import { BROWSERS } from './constants';
import { removeTrailingSlash, removeWWW } from './url';
2020-08-01 12:34:56 +02:00
export const urlFilter = (data, { raw }) => {
const isValidUrl = url => {
return url !== '' && url !== null && !url.startsWith('#');
};
2020-08-01 12:34:56 +02:00
2020-08-23 07:01:14 +02:00
if (raw) {
return data.filter(({ x }) => isValidUrl(x));
}
const cleanUrl = url => {
try {
2020-09-26 07:31:18 +02:00
const { pathname, search } = new URL(url, location.origin);
if (search.startsWith('?/')) {
return `${pathname}${search}`;
}
return pathname;
} catch {
return null;
}
};
const map = data.reduce((obj, { x, y }) => {
if (!isValidUrl(x)) {
return obj;
}
2020-09-26 07:31:18 +02:00
const url = cleanUrl(x);
if (url) {
if (!obj[url]) {
obj[url] = y;
} else {
obj[url] += y;
}
}
return obj;
}, {});
2020-10-22 01:14:51 +02:00
return Object.keys(map).map(key => ({ x: key, y: map[key] }));
};
2020-08-23 07:01:14 +02:00
export const refFilter = (data, { domain, domainOnly, raw }) => {
const regex = new RegExp(`http[s]?://([a-z0-9-]+\\.)*${domain}`);
const links = {};
const isValidRef = referrer => {
return (
referrer !== '' && referrer !== null && !referrer.startsWith('/') && !referrer.startsWith('#')
);
};
const cleanUrl = url => {
try {
const { hostname, origin, pathname, searchParams, protocol } = new URL(url);
if (regex.test(url)) {
return null;
}
2020-08-23 07:01:14 +02:00
if (domainOnly && hostname) {
2020-10-22 01:14:51 +02:00
return removeWWW(hostname);
}
if (!origin || origin === 'null') {
return `${protocol}${removeTrailingSlash(pathname)}`;
}
if (protocol.startsWith('http')) {
const path = removeTrailingSlash(pathname);
const referrer = searchParams.get('ref');
const query = referrer ? `?referrer=${referrer}` : '';
return removeTrailingSlash(`${removeWWW(hostname)}${path}`) + query;
}
return null;
} catch {
return null;
}
};
if (raw) {
return data.filter(({ x }) => isValidRef(x) && !regex.test(x));
}
const map = data.reduce((obj, { x, y }) => {
if (!isValidRef(x)) {
return obj;
}
const url = cleanUrl(x);
2021-11-22 07:00:14 +01:00
links[url] = x;
if (url) {
if (!obj[url]) {
obj[url] = y;
} else {
obj[url] += y;
}
}
return obj;
}, {});
2020-10-22 01:14:51 +02:00
return Object.keys(map).map(key => ({ x: key, y: map[key], w: links[key] }));
2020-08-07 11:46:21 +02:00
};
2020-08-01 12:34:56 +02:00
export const browserFilter = data => data.map(({ x, y }) => ({ x: BROWSERS[x] ?? x, y }));
2020-08-01 12:34:56 +02:00
2021-02-15 13:02:12 +01:00
export const eventTypeFilter = (data, types) => {
if (!types || types.length === 0) {
return data;
}
return data.filter(({ x }) => {
const [event] = x.split('\t');
return types.some(type => type === event);
});
};
2020-08-01 12:34:56 +02:00
export const percentFilter = data => {
const total = data.reduce((n, { y }) => n + y, 0);
2020-08-25 08:49:14 +02:00
return data.map(({ x, y, ...props }) => ({ x, y, z: total ? (y / total) * 100 : 0, ...props }));
2020-08-01 12:34:56 +02:00
};