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

This commit is contained in:
Francis Cao committed 2026-07-20 12:21:07 -07:00
1 parent 5911ddaa93
commit 9bf95ef112
4 files changed
+49 -10

No files matched your search

@@ -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);
@@ -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;
@@ -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,
);
}
@@ -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) {