Merge branch 'dev' of https://github.com/umami-software/umami into dev
This commit is contained in:
commit
4691a344c5
87 files changed
+707
-149
No files matched your search
@@ -11,13 +11,14 @@ import { parseRequest } from '@/lib/request';
|
||||
import { badRequest, forbidden, json, serverError } from '@/lib/response';
|
||||
import { anyObjectParam, urlOrPathParam } from '@/lib/schema';
|
||||
import { safeDecodeURI, safeDecodeURIComponent } from '@/lib/url';
|
||||
import { createSession, saveEvent, saveSessionData } from '@/queries/sql';
|
||||
import { createSession, saveEvent, saveSessionData, saveSessionLink } from '@/queries/sql';
|
||||
|
||||
interface Cache {
|
||||
websiteId: string;
|
||||
sessionId: string;
|
||||
visitId: string;
|
||||
iat: number;
|
||||
sessionLinkId?: string;
|
||||
}
|
||||
|
||||
// Reject strings whose first character is a spreadsheet formula trigger to
|
||||
@@ -128,6 +129,9 @@ export async function POST(request: Request) {
|
||||
}
|
||||
}
|
||||
|
||||
// Carried forward in the cache token so repeat identify calls skip the link write
|
||||
let sessionLinkId = cache?.sessionLinkId;
|
||||
|
||||
// Client info
|
||||
const { ip, userAgent, device, browser, os, country, region, city } = await getClientInfo(
|
||||
request,
|
||||
@@ -277,6 +281,34 @@ export async function POST(request: Request) {
|
||||
twclid,
|
||||
});
|
||||
} else if (type === COLLECTION_TYPE.identify) {
|
||||
if (websiteId && id) {
|
||||
// Persist both memberships so stitched activity can be resolved from
|
||||
// either the anonymous session or the identified session.
|
||||
const anonymousSessionId = uuid(sourceId, ip, userAgent, sessionSalt);
|
||||
const linkedSessionIds = [...new Set([anonymousSessionId, sessionId])];
|
||||
const newLinkId = hash(...linkedSessionIds, id);
|
||||
|
||||
if (sessionLinkId !== newLinkId) {
|
||||
// Best-effort: a link failure must not block the session data write below
|
||||
try {
|
||||
await Promise.all(
|
||||
linkedSessionIds.map(linkedSessionId =>
|
||||
saveSessionLink({
|
||||
websiteId,
|
||||
sessionId: linkedSessionId,
|
||||
distinctId: id,
|
||||
createdAt,
|
||||
}),
|
||||
),
|
||||
);
|
||||
sessionLinkId = newLinkId;
|
||||
} catch (e) {
|
||||
// eslint-disable-next-line no-console
|
||||
console.error('Failed to save session link:', e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (data) {
|
||||
await saveSessionData({
|
||||
websiteId,
|
||||
@@ -316,7 +348,7 @@ export async function POST(request: Request) {
|
||||
}
|
||||
|
||||
const token = createToken(
|
||||
{ websiteId, sessionId, visitId, iat, type: CACHE_TOKEN_TYPE },
|
||||
{ websiteId, sessionId, visitId, iat, sessionLinkId, type: CACHE_TOKEN_TYPE },
|
||||
secret(),
|
||||
);
|
||||
|
||||
|
||||
@@ -1,8 +1,15 @@
|
||||
import { z } from 'zod';
|
||||
import { FIELD_LENGTH } from '@/lib/constants';
|
||||
import { uuid } from '@/lib/crypto';
|
||||
import { getQueryFilters, parseRequest } from '@/lib/request';
|
||||
import { json, unauthorized } from '@/lib/response';
|
||||
import { canViewWebsiteSection } from '@/permissions';
|
||||
import { getSessionActivity } from '@/queries/sql';
|
||||
import {
|
||||
getLinkedDistinctIds,
|
||||
getLinkedSessionIds,
|
||||
getSessionActivity,
|
||||
getSessionActivityBounds,
|
||||
} from '@/queries/sql';
|
||||
|
||||
export async function GET(
|
||||
request: Request,
|
||||
@@ -11,6 +18,7 @@ export async function GET(
|
||||
const schema = z.object({
|
||||
startAt: z.coerce.number().int(),
|
||||
endAt: z.coerce.number().int(),
|
||||
distinctId: z.string().max(FIELD_LENGTH.distinctId).optional(),
|
||||
});
|
||||
|
||||
const { auth, query, error } = await parseRequest(request, schema);
|
||||
@@ -22,19 +30,38 @@ export async function GET(
|
||||
const { websiteId, sessionId } = await params;
|
||||
|
||||
if (
|
||||
!(await canViewWebsiteSection(auth, websiteId, [
|
||||
'sessions',
|
||||
'events',
|
||||
'realtime',
|
||||
'revenue',
|
||||
]))
|
||||
!(await canViewWebsiteSection(auth, websiteId, ['sessions', 'events', 'realtime', 'revenue']))
|
||||
) {
|
||||
return unauthorized();
|
||||
}
|
||||
|
||||
const filters = await getQueryFilters(query, websiteId);
|
||||
let sessionIds = [sessionId];
|
||||
let startAt = query.startAt;
|
||||
let endAt = query.endAt;
|
||||
const distinctIds = query.distinctId
|
||||
? [query.distinctId]
|
||||
: await getLinkedDistinctIds(websiteId, sessionId);
|
||||
|
||||
const data = await getSessionActivity(websiteId, sessionId, filters);
|
||||
if (distinctIds.length) {
|
||||
const links = await Promise.all(distinctIds.map(distinctId => getLinkedSessionIds(websiteId, distinctId)));
|
||||
const linkedIds = links.flatMap(group => group.map(link => link.sessionId));
|
||||
const identifiedIds = distinctIds.map(distinctId => uuid(websiteId, distinctId));
|
||||
|
||||
sessionIds = Array.from(new Set([sessionId, ...identifiedIds, ...linkedIds]));
|
||||
|
||||
if (sessionIds.length > 1) {
|
||||
const bounds = await getSessionActivityBounds(websiteId, sessionIds);
|
||||
|
||||
if (bounds?.firstAt && bounds?.lastAt) {
|
||||
startAt = Math.min(startAt, +new Date(bounds.firstAt));
|
||||
endAt = Math.max(endAt, +new Date(bounds.lastAt));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const filters = await getQueryFilters({ ...query, startAt, endAt }, websiteId);
|
||||
|
||||
const data = await getSessionActivity(websiteId, sessionIds, filters);
|
||||
|
||||
return json(data);
|
||||
}
|
||||
@@ -1,7 +1,8 @@
|
||||
import { uuid } from '@/lib/crypto';
|
||||
import { parseRequest } from '@/lib/request';
|
||||
import { json, unauthorized } from '@/lib/response';
|
||||
import { canViewWebsiteSection } from '@/permissions';
|
||||
import { getWebsiteSession } from '@/queries/sql';
|
||||
import { getLinkedDistinctIds, getLinkedSessionIds, getWebsiteSession } from '@/queries/sql';
|
||||
|
||||
export async function GET(
|
||||
request: Request,
|
||||
@@ -28,5 +29,29 @@ export async function GET(
|
||||
|
||||
const data = await getWebsiteSession(websiteId, sessionId);
|
||||
|
||||
return json(data);
|
||||
let stitchedSessionCount = 0;
|
||||
|
||||
if (data) {
|
||||
let sessionIds = [sessionId];
|
||||
const distinctIds = data.distinctId
|
||||
? [data.distinctId]
|
||||
: await getLinkedDistinctIds(websiteId, sessionId);
|
||||
|
||||
if (distinctIds.length) {
|
||||
const links = await Promise.all(
|
||||
distinctIds.map(distinctId => getLinkedSessionIds(websiteId, distinctId)),
|
||||
);
|
||||
const linkedIds = links.flatMap(group => group.map(link => link.sessionId));
|
||||
const identifiedIds = distinctIds.map(distinctId => uuid(websiteId, distinctId));
|
||||
|
||||
sessionIds = Array.from(new Set([sessionId, ...identifiedIds, ...linkedIds]));
|
||||
}
|
||||
|
||||
stitchedSessionCount = sessionIds.length;
|
||||
}
|
||||
|
||||
return json({
|
||||
...data,
|
||||
stitchedSessionCount,
|
||||
});
|
||||
}
|
||||
@@ -1,10 +1,10 @@
|
||||
import { z } from 'zod';
|
||||
import { fromZonedTime } from 'date-fns-tz';
|
||||
import { parseDateRange } from '@/lib/date';
|
||||
import { canViewBatchWebsites } from '@/permissions/website';
|
||||
import { parseRequest } from '@/lib/request';
|
||||
import { json } from '@/lib/response';
|
||||
import { timezoneParam } from '@/lib/schema';
|
||||
import { canViewWebsite } from '@/permissions';
|
||||
import { getWebsiteListCharts } from '@/queries/sql';
|
||||
|
||||
const schema = z.object({
|
||||
@@ -32,13 +32,7 @@ export async function GET(request: Request) {
|
||||
: fromZonedTime(defaultRange.startDate, timezone);
|
||||
const endDate = hasDateRange ? new Date(query.endAt) : fromZonedTime(defaultRange.endDate, timezone);
|
||||
|
||||
const websiteIds = (
|
||||
await Promise.all(
|
||||
query.ids.map(async (websiteId: string) =>
|
||||
(await canViewWebsite(auth, websiteId)) ? websiteId : null,
|
||||
),
|
||||
)
|
||||
).filter(Boolean);
|
||||
const websiteIds = await canViewBatchWebsites(auth, query.ids);
|
||||
|
||||
const data = await getWebsiteListCharts(websiteIds, {
|
||||
startDate,
|
||||
|
||||
Reference in new issue
Block a user