Skip to content

armgit / umami

Public Imported from GitHub
0

Umami is a privacy-first web and product analytics platform. Traffic, campaigns, behavior, conversions, and revenue in one place. No cookies, no surveillance, self-hosted or in the cloud.

updaet session stitching model for post-identify reads. fix fullPath search

Francis Cao9bf95ef11222
diff --git a/src/app/api/websites/[websiteId]/sessions/[sessionId]/activity/route.ts b/src/app/api/websites/[websiteId]/sessions/[sessionId]/activity/route.ts
index 5af7660b7..27f0716ec 100644
--- a/src/app/api/websites/[websiteId]/sessions/[sessionId]/activity/route.ts
+++ b/src/app/api/websites/[websiteId]/sessions/[sessionId]/activity/route.ts
@@ -1,6 +1,5 @@
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';
@@ -45,9 +44,8 @@ export async function GET(
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]));
+ sessionIds = Array.from(new Set([sessionId, ...linkedIds]));
if (sessionIds.length > 1) {
const bounds = await getSessionActivityBounds(websiteId, sessionIds);
diff --git a/src/app/api/websites/[websiteId]/sessions/[sessionId]/route.ts b/src/app/api/websites/[websiteId]/sessions/[sessionId]/route.ts
index 99151ffca..6f1a08d40 100644
--- a/src/app/api/websites/[websiteId]/sessions/[sessionId]/route.ts
+++ b/src/app/api/websites/[websiteId]/sessions/[sessionId]/route.ts
@@ -1,4 +1,3 @@
-import { uuid } from '@/lib/crypto';
import { parseRequest } from '@/lib/request';
import { json, unauthorized } from '@/lib/response';
import { canViewWebsiteSection } from '@/permissions';
@@ -37,14 +36,17 @@ export async function GET(
? [data.distinctId]
: await getLinkedDistinctIds(websiteId, sessionId);
+ if (!data.distinctId && distinctIds.length === 1) {
+ data.distinctId = distinctIds[0];
+ }
+
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]));
+ sessionIds = Array.from(new Set([sessionId, ...linkedIds]));
}
stitchedSessionCount = sessionIds.length;
diff --git a/src/queries/sql/pageviews/getPageviewExpandedMetrics.ts b/src/queries/sql/pageviews/getPageviewExpandedMetrics.ts
index 2eeeeca5f..e25a0242c 100644
--- a/src/queries/sql/pageviews/getPageviewExpandedMetrics.ts
+++ b/src/queries/sql/pageviews/getPageviewExpandedMetrics.ts
@@ -46,6 +46,10 @@ async function relationalQuery(
},
{ joinSession: SESSION_COLUMNS.includes(type) },
);
+ const fullPathSearchQuery =
+ type === 'fullPath' && filters.search
+ ? `and (case when website_event.url_query != '' then website_event.url_path || '?' || website_event.url_query else website_event.url_path end) ilike {{fullPathSearch}}`
+ : '';
let entryExitQuery = '';
let excludeDomain = '';
@@ -111,6 +115,7 @@ async function relationalQuery(
and website_event.created_at between {{startDate}} and {{endDate}}
and website_event.event_type NOT IN (2, 5)
${excludeDomain}
+ ${fullPathSearchQuery}
${filterQuery}
group by ${groupByColumn}, website_event.session_id, website_event.visit_id
) as t
@@ -120,7 +125,10 @@ async function relationalQuery(
limit ${limit}
offset ${offset}
`,
- queryParams,
+ {
+ ...queryParams,
+ ...(type === 'fullPath' && filters.search ? { fullPathSearch: `%${filters.search}%` } : {}),
+ },
FUNCTION_NAME,
);
}
@@ -137,6 +145,10 @@ async function clickhouseQuery(
...filters,
websiteId,
});
+ const fullPathSearchQuery =
+ type === 'fullPath' && filters.search
+ ? `and positionCaseInsensitive(if(url_query != '', concat(url_path, '?', url_query), url_path), {fullPathSearch:String}) > 0`
+ : '';
let excludeDomain = '';
let entryExitQuery = '';
@@ -195,6 +207,7 @@ async function clickhouseQuery(
and event_type NOT IN (2, 5)
and name != ''
${excludeDomain}
+ ${fullPathSearchQuery}
${filterQuery}
group by name, session_id, visit_id
) as t
@@ -203,7 +216,11 @@ async function clickhouseQuery(
limit ${limit}
offset ${offset}
`,
- { ...queryParams, ...parameters },
+ {
+ ...queryParams,
+ ...parameters,
+ ...(type === 'fullPath' && filters.search ? { fullPathSearch: filters.search } : {}),
+ },
FUNCTION_NAME,
);
}
diff --git a/src/queries/sql/pageviews/getPageviewMetrics.ts b/src/queries/sql/pageviews/getPageviewMetrics.ts
index 8a78cb0d2..737469025 100644
--- a/src/queries/sql/pageviews/getPageviewMetrics.ts
+++ b/src/queries/sql/pageviews/getPageviewMetrics.ts
@@ -42,6 +42,10 @@ async function relationalQuery(
},
{ joinSession: SESSION_COLUMNS.includes(type) },
);
+ const fullPathSearchQuery =
+ type === 'fullPath' && filters.search
+ ? `and (case when website_event.url_query != '' then website_event.url_path || '?' || website_event.url_query else website_event.url_path end) ilike {{fullPathSearch}}`
+ : '';
let entryExitQuery = '';
let excludeDomain = '';
@@ -90,13 +94,18 @@ async function relationalQuery(
and website_event.event_type NOT IN (2, 5)
and ${column} != ''
${excludeDomain}
+ ${fullPathSearchQuery}
${filterQuery}
group by 1
order by 2 desc
limit ${limit}
offset ${offset}
`,
- { ...queryParams, ...parameters },
+ {
+ ...queryParams,
+ ...parameters,
+ ...(type === 'fullPath' && filters.search ? { fullPathSearch: `%${filters.search}%` } : {}),
+ },
FUNCTION_NAME,
);
}
@@ -113,6 +122,10 @@ async function clickhouseQuery(
...filters,
websiteId,
});
+ const fullPathSearchQuery =
+ type === 'fullPath' && filters.search
+ ? `and positionCaseInsensitive(if(url_query != '', concat(url_path, '?', url_query), url_path), {fullPathSearch:String}) > 0`
+ : '';
let sql = '';
let excludeDomain = '';
@@ -156,6 +169,7 @@ async function clickhouseQuery(
and event_type NOT IN (2, 5)
and ${column} != ''
${excludeDomain}
+ ${fullPathSearchQuery}
${filterQuery}
group by x
order by y desc
@@ -204,7 +218,15 @@ async function clickhouseQuery(
`;
}
- return rawQuery(sql, { ...queryParams, ...parameters }, FUNCTION_NAME);
+ return rawQuery(
+ sql,
+ {
+ ...queryParams,
+ ...parameters,
+ ...(type === 'fullPath' && filters.search ? { fullPathSearch: filters.search } : {}),
+ },
+ FUNCTION_NAME,
+ );
}
function getPageviewColumn(type: string) {