Refactored referrer logic. Display stats for no referrers.

This commit is contained in:
Mike Cao 2022-07-21 01:11:10 -07:00
parent 0026b4b1ea
commit be8291c856
92 changed files with 687 additions and 450 deletions

View file

@ -1,14 +1,10 @@
import { removeTrailingSlash, removeWWW, getDomainName } from './url';
import { removeWWW } from './url';
export const urlFilter = (data, { raw }) => {
export const urlFilter = data => {
const isValidUrl = url => {
return url !== '' && url !== null && !url.startsWith('#');
};
if (raw) {
return data.filter(({ x }) => isValidUrl(x));
}
const cleanUrl = url => {
try {
const { pathname, search } = new URL(url, location.origin);
@ -44,68 +40,26 @@ export const urlFilter = (data, { raw }) => {
return Object.keys(map).map(key => ({ x: key, y: map[key] }));
};
export const refFilter = (data, { domain, domainOnly, raw }) => {
const domainName = getDomainName(domain);
const regex = new RegExp(`http[s]?://([a-z0-9-]+\\.)*${domainName}`);
export const refFilter = data => {
const links = {};
const isValidRef = referrer => {
return referrer !== null && !referrer.startsWith('/') && !referrer.startsWith('#');
};
const cleanUrl = url => {
try {
if (url === '') {
return 'Direct / None';
}
const { hostname, origin, pathname, searchParams, protocol } = new URL(url);
if (regex.test(url)) {
return null;
}
if (domainOnly && hostname) {
return removeWWW(hostname);
}
if (!origin || origin === 'null') {
return `${protocol}${removeTrailingSlash(pathname)}`;
}
if (protocol.startsWith('http')) {
const path = removeTrailingSlash(pathname);
const referrer = searchParams.get('referrer');
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;
let id;
try {
const url = new URL(x);
id = removeWWW(url.hostname) || url.href;
} catch {
id = '';
}
const url = cleanUrl(x);
links[id] = x;
links[url] = x;
if (url) {
if (!obj[url]) {
obj[url] = y;
} else {
obj[url] += y;
}
if (!obj[id]) {
obj[id] = y;
} else {
obj[id] += y;
}
return obj;