mirror of
https://github.com/kremalicious/umami.git
synced 2024-11-21 17:37:00 +01:00
include visitors metrics in websiteStats
This commit is contained in:
parent
1a839d1cae
commit
51139c1918
@ -12,7 +12,7 @@ export function salt() {
|
||||
return hash(secret(), ROTATING_SALT);
|
||||
}
|
||||
|
||||
export function sessionSalt() {
|
||||
export function visitSalt() {
|
||||
const ROTATING_SALT = hash(startOfHour(new Date()).toUTCString());
|
||||
|
||||
return hash(secret(), ROTATING_SALT);
|
||||
|
@ -1,4 +1,4 @@
|
||||
import { isUuid, secret, sessionSalt, uuid } from 'lib/crypto';
|
||||
import { isUuid, secret, uuid, visitSalt } from 'lib/crypto';
|
||||
import { getClientInfo } from 'lib/detect';
|
||||
import { parseToken } from 'next-basics';
|
||||
import { NextApiRequestCollect } from 'pages/api/send';
|
||||
@ -68,7 +68,7 @@ export async function findSession(req: NextApiRequestCollect): Promise<{
|
||||
await getClientInfo(req, payload);
|
||||
|
||||
const sessionId = uuid(websiteId, hostname, ip, userAgent);
|
||||
const visitId = uuid(sessionId, sessionSalt());
|
||||
const visitId = uuid(sessionId, visitSalt());
|
||||
|
||||
// Clickhouse does not require session lookup
|
||||
if (clickhouse.enabled) {
|
||||
|
@ -1,7 +1,7 @@
|
||||
import ipaddr from 'ipaddr.js';
|
||||
import { isbot } from 'isbot';
|
||||
import { COLLECTION_TYPE, HOSTNAME_REGEX, IP_REGEX } from 'lib/constants';
|
||||
import { secret, sessionSalt, uuid } from 'lib/crypto';
|
||||
import { secret, visitSalt, uuid } from 'lib/crypto';
|
||||
import { getIpAddress } from 'lib/detect';
|
||||
import { useCors, useSession, useValidate } from 'lib/middleware';
|
||||
import { CollectionType, YupRequest } from 'lib/types';
|
||||
@ -98,7 +98,7 @@ export default async (req: NextApiRequestCollect, res: NextApiResponse) => {
|
||||
// expire visitId after 30 minutes
|
||||
session.visitId =
|
||||
!!session.iat && Math.floor(new Date().getTime() / 1000) - session.iat > 1800
|
||||
? uuid(session.id, sessionSalt())
|
||||
? uuid(session.id, visitSalt())
|
||||
: session.visitId;
|
||||
|
||||
session.iat = Math.floor(new Date().getTime() / 1000);
|
||||
|
@ -1,19 +1,27 @@
|
||||
import prisma from 'lib/prisma';
|
||||
import clickhouse from 'lib/clickhouse';
|
||||
import { runQuery, CLICKHOUSE, PRISMA } from 'lib/db';
|
||||
import { EVENT_TYPE } from 'lib/constants';
|
||||
import { CLICKHOUSE, PRISMA, runQuery } from 'lib/db';
|
||||
import prisma from 'lib/prisma';
|
||||
import { QueryFilters } from 'lib/types';
|
||||
|
||||
export async function getWebsiteStats(...args: [websiteId: string, filters: QueryFilters]) {
|
||||
export async function getWebsiteStats(
|
||||
...args: [websiteId: string, filters: QueryFilters]
|
||||
): Promise<
|
||||
{ pageviews: number; uniques: number; visitors: number; bounces: number; totaltime: number }[]
|
||||
> {
|
||||
return runQuery({
|
||||
[PRISMA]: () => relationalQuery(...args),
|
||||
[CLICKHOUSE]: () => clickhouseQuery(...args),
|
||||
});
|
||||
}
|
||||
|
||||
async function relationalQuery(websiteId: string, filters: QueryFilters) {
|
||||
const { getDateQuery, getAddIntervalQuery, getTimestampDiffQuery, parseFilters, rawQuery } =
|
||||
prisma;
|
||||
async function relationalQuery(
|
||||
websiteId: string,
|
||||
filters: QueryFilters,
|
||||
): Promise<
|
||||
{ pageviews: number; uniques: number; visitors: number; bounces: number; totaltime: number }[]
|
||||
> {
|
||||
const { getTimestampDiffQuery, parseFilters, rawQuery } = prisma;
|
||||
const { filterQuery, joinSession, params } = await parseFilters(websiteId, {
|
||||
...filters,
|
||||
eventType: EVENT_TYPE.pageView,
|
||||
@ -24,14 +32,13 @@ async function relationalQuery(websiteId: string, filters: QueryFilters) {
|
||||
select
|
||||
sum(t.c) as "pageviews",
|
||||
count(distinct t.session_id) as "uniques",
|
||||
count(distinct t.visit_id) as "visitors",
|
||||
sum(case when t.c = 1 then 1 else 0 end) as "bounces",
|
||||
sum(case when t.max_time < ${getAddIntervalQuery('t.min_time', '1 hour')}
|
||||
then ${getTimestampDiffQuery('t.min_time', 't.max_time')}
|
||||
else 0 end) as "totaltime"
|
||||
sum(${getTimestampDiffQuery('t.min_time', 't.max_time')}) as "totaltime"
|
||||
from (
|
||||
select
|
||||
website_event.session_id,
|
||||
${getDateQuery('website_event.created_at', 'hour')},
|
||||
website_event.visit_id,
|
||||
count(*) as "c",
|
||||
min(website_event.created_at) as "min_time",
|
||||
max(website_event.created_at) as "max_time"
|
||||
@ -53,8 +60,10 @@ async function relationalQuery(websiteId: string, filters: QueryFilters) {
|
||||
async function clickhouseQuery(
|
||||
websiteId: string,
|
||||
filters: QueryFilters,
|
||||
): Promise<{ pageviews: number; uniques: number; bounces: number; totaltime: number }[]> {
|
||||
const { rawQuery, getDateQuery, parseFilters } = clickhouse;
|
||||
): Promise<
|
||||
{ pageviews: number; uniques: number; visitors: number; bounces: number; totaltime: number }[]
|
||||
> {
|
||||
const { rawQuery, parseFilters } = clickhouse;
|
||||
const { filterQuery, params } = await parseFilters(websiteId, {
|
||||
...filters,
|
||||
eventType: EVENT_TYPE.pageView,
|
||||
@ -65,12 +74,13 @@ async function clickhouseQuery(
|
||||
select
|
||||
sum(t.c) as "pageviews",
|
||||
count(distinct t.session_id) as "uniques",
|
||||
count(distinct t.visit_id) as "visitors",
|
||||
sum(if(t.c = 1, 1, 0)) as "bounces",
|
||||
sum(if(max_time < min_time + interval 1 hour, max_time-min_time, 0)) as "totaltime"
|
||||
sum(max_time-min_time) as "totaltime"
|
||||
from (
|
||||
select
|
||||
session_id,
|
||||
${getDateQuery('created_at', 'hour')} time_series,
|
||||
visit_id,
|
||||
count(*) c,
|
||||
min(created_at) min_time,
|
||||
max(created_at) max_time
|
||||
@ -79,7 +89,7 @@ async function clickhouseQuery(
|
||||
and created_at between {startDate:DateTime64} and {endDate:DateTime64}
|
||||
and event_type = {eventType:UInt32}
|
||||
${filterQuery}
|
||||
group by session_id, time_series
|
||||
group by session_id, visit_id
|
||||
) as t;
|
||||
`,
|
||||
params,
|
||||
@ -88,6 +98,7 @@ async function clickhouseQuery(
|
||||
return {
|
||||
pageviews: Number(a.pageviews),
|
||||
uniques: Number(a.uniques),
|
||||
visitors: Number(a.visitors),
|
||||
bounces: Number(a.bounces),
|
||||
totaltime: Number(a.totaltime),
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user