mirror of
https://github.com/kremalicious/umami.git
synced 2024-12-24 18:26:20 +01:00
Add UTM pageview metrics to API
This commit is contained in:
parent
16046a50a4
commit
92b009fa1e
@ -1,10 +1,11 @@
|
||||
import { getPageviewMetrics, getSessionMetrics, getWebsiteById } from 'queries';
|
||||
import { getPageviewMetrics, getSessionMetrics, getWebsiteById, getPageviewParams } from 'queries';
|
||||
import { ok, methodNotAllowed, unauthorized, badRequest } from 'lib/response';
|
||||
import { allowQuery } from 'lib/auth';
|
||||
import { useCors } from 'lib/middleware';
|
||||
|
||||
const sessionColumns = ['browser', 'os', 'device', 'screen', 'country', 'language'];
|
||||
const pageviewColumns = ['url', 'referrer'];
|
||||
const paramTypes = ['utm_source', 'utm_medium', 'utm_campaign', 'utm_content', 'utm_term', 'ref'];
|
||||
|
||||
function getTable(type) {
|
||||
if (type === 'event') {
|
||||
@ -39,6 +40,52 @@ export default async (req, res) => {
|
||||
const startDate = new Date(+start_at);
|
||||
const endDate = new Date(+end_at);
|
||||
|
||||
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] })),
|
||||
);
|
||||
}
|
||||
|
||||
if (sessionColumns.includes(type)) {
|
||||
let data = await getSessionMetrics(websiteId, startDate, endDate, type, {
|
||||
os,
|
||||
|
38
queries/analytics/pageview/getPageviewParams.js
Normal file
38
queries/analytics/pageview/getPageviewParams.js
Normal file
@ -0,0 +1,38 @@
|
||||
import { parseFilters, rawQuery } from 'lib/queries';
|
||||
|
||||
export function getPageviewParams(
|
||||
param,
|
||||
website_id,
|
||||
start_at,
|
||||
end_at,
|
||||
column,
|
||||
table,
|
||||
filters = {},
|
||||
) {
|
||||
const params = [param, website_id, start_at, end_at];
|
||||
const { pageviewQuery, sessionQuery, eventQuery, joinSession } = parseFilters(
|
||||
table,
|
||||
column,
|
||||
filters,
|
||||
params,
|
||||
);
|
||||
|
||||
return rawQuery(
|
||||
`select * from (
|
||||
select
|
||||
url, split_part(split_part(url, concat($1, '='), 2), '&', 1) param
|
||||
from
|
||||
pageview
|
||||
${joinSession}
|
||||
${pageviewQuery}
|
||||
${joinSession && sessionQuery}
|
||||
${eventQuery}
|
||||
where
|
||||
${table}.website_id=$2 and ${table}.created_at between $3 and $4
|
||||
group by 1, 2
|
||||
order by 2 desc
|
||||
) q
|
||||
where q.param <> ''`,
|
||||
params,
|
||||
);
|
||||
}
|
@ -17,6 +17,7 @@ import { getEventMetrics } from './analytics/event/getEventMetrics';
|
||||
import { getEvents } from './analytics/event/getEvents';
|
||||
import { saveEvent } from './analytics/event/saveEvent';
|
||||
import { getPageviewMetrics } from './analytics/pageview/getPageviewMetrics';
|
||||
import { getPageviewParams } from './analytics/pageview/getPageviewParams';
|
||||
import { getPageviews } from './analytics/pageview/getPageviews';
|
||||
import { getPageviewStats } from './analytics/pageview/getPageviewStats';
|
||||
import { savePageView } from './analytics/pageview/savePageView';
|
||||
@ -48,6 +49,7 @@ export {
|
||||
getEvents,
|
||||
saveEvent,
|
||||
getPageviewMetrics,
|
||||
getPageviewParams,
|
||||
getPageviews,
|
||||
getPageviewStats,
|
||||
savePageView,
|
||||
|
Loading…
Reference in New Issue
Block a user