mirror of
https://github.com/umami-software/umami.git
synced 2026-02-16 02:25:35 +01:00
move parseParameters to UTMView
This commit is contained in:
parent
6d124e7bb0
commit
2203ba1fe7
2 changed files with 28 additions and 28 deletions
|
|
@ -15,6 +15,31 @@ function toArray(data: { [key: string]: number } = {}) {
|
||||||
.sort(firstBy('value', -1));
|
.sort(firstBy('value', -1));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function parseParameters(data: any[]) {
|
||||||
|
return data.reduce((obj, { url_query, num }) => {
|
||||||
|
try {
|
||||||
|
const searchParams = new URLSearchParams(url_query);
|
||||||
|
|
||||||
|
for (const [key, value] of searchParams) {
|
||||||
|
if (key.match(/^utm_(\w+)$/)) {
|
||||||
|
const name = value;
|
||||||
|
if (!obj[key]) {
|
||||||
|
obj[key] = { [name]: Number(num) };
|
||||||
|
} else if (!obj[key][name]) {
|
||||||
|
obj[key][name] = Number(num);
|
||||||
|
} else {
|
||||||
|
obj[key][name] += Number(num);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch {
|
||||||
|
// Ignore
|
||||||
|
}
|
||||||
|
|
||||||
|
return obj;
|
||||||
|
}, {});
|
||||||
|
}
|
||||||
|
|
||||||
export default function UTMView() {
|
export default function UTMView() {
|
||||||
const { formatMessage, labels } = useMessages();
|
const { formatMessage, labels } = useMessages();
|
||||||
const { report } = useContext(ReportContext);
|
const { report } = useContext(ReportContext);
|
||||||
|
|
@ -27,7 +52,7 @@ export default function UTMView() {
|
||||||
return (
|
return (
|
||||||
<div>
|
<div>
|
||||||
{UTM_PARAMS.map(param => {
|
{UTM_PARAMS.map(param => {
|
||||||
const items = toArray(data[param]);
|
const items = toArray(parseParameters(data)[param]);
|
||||||
const chartData = {
|
const chartData = {
|
||||||
labels: items.map(({ name }) => name),
|
labels: items.map(({ name }) => name),
|
||||||
datasets: [
|
datasets: [
|
||||||
|
|
|
||||||
|
|
@ -44,7 +44,7 @@ async function relationalQuery(
|
||||||
startDate,
|
startDate,
|
||||||
endDate,
|
endDate,
|
||||||
},
|
},
|
||||||
).then(result => parseParameters(result as any[]));
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
async function clickhouseQuery(
|
async function clickhouseQuery(
|
||||||
|
|
@ -73,30 +73,5 @@ async function clickhouseQuery(
|
||||||
startDate,
|
startDate,
|
||||||
endDate,
|
endDate,
|
||||||
},
|
},
|
||||||
).then(result => parseParameters(result as any[]));
|
);
|
||||||
}
|
|
||||||
|
|
||||||
function parseParameters(data: any[]) {
|
|
||||||
return data.reduce((obj, { url_query, num }) => {
|
|
||||||
try {
|
|
||||||
const searchParams = new URLSearchParams(url_query);
|
|
||||||
|
|
||||||
for (const [key, value] of searchParams) {
|
|
||||||
if (key.match(/^utm_(\w+)$/)) {
|
|
||||||
const name = value;
|
|
||||||
if (!obj[key]) {
|
|
||||||
obj[key] = { [name]: Number(num) };
|
|
||||||
} else if (!obj[key][name]) {
|
|
||||||
obj[key][name] = Number(num);
|
|
||||||
} else {
|
|
||||||
obj[key][name] += Number(num);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch {
|
|
||||||
// Ignore
|
|
||||||
}
|
|
||||||
|
|
||||||
return obj;
|
|
||||||
}, {});
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue