2022-07-21 23:57:29 +02:00
|
|
|
import { getPageviewMetrics, getSessionMetrics, getWebsiteById, getPageviewParams } from 'queries';
|
2021-02-27 04:50:44 +01:00
|
|
|
import { ok, methodNotAllowed, unauthorized, badRequest } from 'lib/response';
|
2020-09-18 07:52:20 +02:00
|
|
|
import { allowQuery } from 'lib/auth';
|
2022-04-04 09:33:20 +02:00
|
|
|
import { useCors } from 'lib/middleware';
|
2022-07-23 08:56:29 +02:00
|
|
|
import { FILTER_IGNORED } from 'lib/constants';
|
2020-07-28 08:52:14 +02:00
|
|
|
|
2022-07-16 07:29:55 +02:00
|
|
|
const sessionColumns = ['browser', 'os', 'device', 'screen', 'country', 'language'];
|
2020-10-09 00:02:48 +02:00
|
|
|
const pageviewColumns = ['url', 'referrer'];
|
2022-07-21 23:57:29 +02:00
|
|
|
const paramTypes = ['utm_source', 'utm_medium', 'utm_campaign', 'utm_content', 'utm_term', 'ref'];
|
2020-10-09 00:02:48 +02:00
|
|
|
|
|
|
|
function getTable(type) {
|
|
|
|
if (type === 'event') {
|
|
|
|
return 'event';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sessionColumns.includes(type)) {
|
|
|
|
return 'session';
|
|
|
|
}
|
|
|
|
|
|
|
|
return 'pageview';
|
|
|
|
}
|
|
|
|
|
|
|
|
function getColumn(type) {
|
|
|
|
if (type === 'event') {
|
2020-12-05 05:45:12 +01:00
|
|
|
return `concat(event_type, '\t', event_value)`;
|
2020-10-09 00:02:48 +02:00
|
|
|
}
|
|
|
|
return type;
|
|
|
|
}
|
|
|
|
|
2020-07-28 08:52:14 +02:00
|
|
|
export default async (req, res) => {
|
2020-09-11 22:49:43 +02:00
|
|
|
if (req.method === 'GET') {
|
2022-04-04 09:33:20 +02:00
|
|
|
await useCors(req, res);
|
|
|
|
|
2020-09-18 07:52:20 +02:00
|
|
|
if (!(await allowQuery(req))) {
|
|
|
|
return unauthorized(res);
|
|
|
|
}
|
|
|
|
|
2022-04-10 12:51:43 +02:00
|
|
|
const { id, type, start_at, end_at, url, referrer, os, browser, device, country } = req.query;
|
2020-09-18 07:52:20 +02:00
|
|
|
|
2020-09-11 22:49:43 +02:00
|
|
|
const websiteId = +id;
|
|
|
|
const startDate = new Date(+start_at);
|
|
|
|
const endDate = new Date(+end_at);
|
2020-07-28 08:52:14 +02:00
|
|
|
|
2022-07-21 23:57:29 +02:00
|
|
|
if (paramTypes.includes(type)) {
|
|
|
|
const column = 'url';
|
|
|
|
const table = getTable(type);
|
|
|
|
|
|
|
|
let domain;
|
|
|
|
if (type === 'referrer') {
|
|
|
|
const website = await getWebsiteById(websiteId);
|
|
|
|
|
|
|
|
if (!website) {
|
|
|
|
return badRequest(res);
|
|
|
|
}
|
|
|
|
|
|
|
|
domain = website.domain;
|
|
|
|
}
|
|
|
|
|
|
|
|
const filters = {
|
|
|
|
domain,
|
|
|
|
url: type !== 'url' && table !== 'event' ? url : undefined,
|
|
|
|
referrer: type !== 'referrer' ? referrer : true,
|
|
|
|
os: type !== 'os' ? os : undefined,
|
|
|
|
browser: type !== 'browser' ? browser : undefined,
|
|
|
|
device: type !== 'device' ? device : undefined,
|
|
|
|
country: type !== 'country' ? country : undefined,
|
|
|
|
event_url: type !== 'url' && table === 'event' ? url : undefined,
|
|
|
|
};
|
|
|
|
|
|
|
|
let data = await getPageviewParams(
|
|
|
|
type,
|
|
|
|
websiteId,
|
|
|
|
startDate,
|
|
|
|
endDate,
|
|
|
|
column,
|
|
|
|
table,
|
|
|
|
filters,
|
|
|
|
);
|
|
|
|
|
|
|
|
let terms = {};
|
|
|
|
new Set(data.map(i => i.param)).forEach(term => (terms[term] = null));
|
|
|
|
for (let { param } of data) terms[param] += 1;
|
|
|
|
|
|
|
|
return ok(
|
|
|
|
res,
|
|
|
|
Object.keys(terms).map(i => ({ x: i, y: terms[i] })),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-10-09 00:02:48 +02:00
|
|
|
if (sessionColumns.includes(type)) {
|
2022-04-10 12:51:43 +02:00
|
|
|
let data = await getSessionMetrics(websiteId, startDate, endDate, type, {
|
|
|
|
os,
|
|
|
|
browser,
|
|
|
|
device,
|
|
|
|
country,
|
|
|
|
});
|
2021-12-02 04:25:00 +01:00
|
|
|
|
|
|
|
if (type === 'language') {
|
|
|
|
let combined = {};
|
|
|
|
|
|
|
|
for (let { x, y } of data) {
|
|
|
|
x = String(x).toLowerCase().split('-')[0];
|
2022-04-10 12:51:43 +02:00
|
|
|
|
|
|
|
if (!combined[x]) {
|
|
|
|
combined[x] = { x, y };
|
|
|
|
} else {
|
|
|
|
combined[x].y += y;
|
|
|
|
}
|
2021-12-02 04:25:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
data = Object.values(combined);
|
|
|
|
}
|
2020-07-30 06:40:26 +02:00
|
|
|
|
2020-10-09 00:02:48 +02:00
|
|
|
return ok(res, data);
|
|
|
|
}
|
|
|
|
|
2021-02-27 04:50:44 +01:00
|
|
|
if (pageviewColumns.includes(type) || type === 'event') {
|
|
|
|
let domain;
|
|
|
|
if (type === 'referrer') {
|
2022-07-21 10:11:10 +02:00
|
|
|
const website = await getWebsiteById(websiteId);
|
2021-02-27 04:50:44 +01:00
|
|
|
|
|
|
|
if (!website) {
|
|
|
|
return badRequest(res);
|
|
|
|
}
|
|
|
|
|
|
|
|
domain = website.domain;
|
|
|
|
}
|
|
|
|
|
2022-04-11 09:19:29 +02:00
|
|
|
const column = getColumn(type);
|
|
|
|
const table = getTable(type);
|
2022-07-21 10:11:10 +02:00
|
|
|
const filters = {
|
2022-04-11 09:19:29 +02:00
|
|
|
domain,
|
2022-04-14 04:04:06 +02:00
|
|
|
url: type !== 'url' && table !== 'event' ? url : undefined,
|
2022-07-23 08:56:29 +02:00
|
|
|
referrer: type !== 'referrer' && table !== 'event' ? referrer : FILTER_IGNORED,
|
2022-04-11 09:19:29 +02:00
|
|
|
os: type !== 'os' ? os : undefined,
|
|
|
|
browser: type !== 'browser' ? browser : undefined,
|
|
|
|
device: type !== 'device' ? device : undefined,
|
|
|
|
country: type !== 'country' ? country : undefined,
|
2022-04-14 04:04:06 +02:00
|
|
|
event_url: type !== 'url' && table === 'event' ? url : undefined,
|
2022-07-21 10:11:10 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const data = await getPageviewMetrics(websiteId, startDate, endDate, column, table, filters);
|
2020-09-11 22:49:43 +02:00
|
|
|
|
2020-10-09 00:02:48 +02:00
|
|
|
return ok(res, data);
|
|
|
|
}
|
2020-09-11 22:49:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return methodNotAllowed(res);
|
2020-07-28 08:52:14 +02:00
|
|
|
};
|