Merge pull request #873 from cywio/languages-table

Add languages to website details page
This commit is contained in:
Mike Cao 2021-12-19 23:05:17 -08:00 committed by GitHub
commit c531072a4e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 269 additions and 2 deletions

View file

@ -2,7 +2,7 @@ import { getPageviewMetrics, getSessionMetrics, getWebsiteById } from 'lib/queri
import { ok, methodNotAllowed, unauthorized, badRequest } from 'lib/response';
import { allowQuery } from 'lib/auth';
const sessionColumns = ['browser', 'os', 'device', 'country'];
const sessionColumns = ['browser', 'os', 'device', 'country', 'language'];
const pageviewColumns = ['url', 'referrer'];
function getTable(type) {
@ -37,7 +37,19 @@ export default async (req, res) => {
const endDate = new Date(+end_at);
if (sessionColumns.includes(type)) {
const data = await getSessionMetrics(websiteId, startDate, endDate, type, { url });
let data = await getSessionMetrics(websiteId, startDate, endDate, type, { url });
if (type === 'language') {
let combined = {};
for (let { x, y } of data) {
x = String(x).toLowerCase().split('-')[0];
if (!combined[x]) combined[x] = { x, y };
else combined[x].y += y;
}
data = Object.values(combined);
}
return ok(res, data);
}