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.

udpate constants and references to filter groups

Francis Cao657ae8ebdbba
diff --git a/src/app/(main)/websites/[websiteId]/WebsiteDetailsPage.tsx b/src/app/(main)/websites/[websiteId]/WebsiteDetailsPage.tsx
index 460792efc..c421bc490 100644
--- a/src/app/(main)/websites/[websiteId]/WebsiteDetailsPage.tsx
+++ b/src/app/(main)/websites/[websiteId]/WebsiteDetailsPage.tsx
@@ -7,7 +7,7 @@ import WebsiteExpandedView from './WebsiteExpandedView';
import WebsiteHeader from './WebsiteHeader';
import WebsiteMetricsBar from './WebsiteMetricsBar';
import WebsiteTableView from './WebsiteTableView';
-import { FILTER_COLUMNS } from '@/lib/constants';
+import { FILTER_COLUMNS, FILTER_GROUPS } from '@/lib/constants';
export default function WebsiteDetailsPage({ websiteId }: { websiteId: string }) {
const pathname = usePathname();
@@ -17,7 +17,7 @@ export default function WebsiteDetailsPage({ websiteId }: { websiteId: string })
const { view } = query;
const params = Object.keys(query).reduce((obj, key) => {
- if (FILTER_COLUMNS[key]) {
+ if (FILTER_COLUMNS[key] || FILTER_GROUPS[key]) {
obj[key] = query[key];
}
return obj;
diff --git a/src/app/(main)/websites/[websiteId]/compare/WebsiteComparePage.tsx b/src/app/(main)/websites/[websiteId]/compare/WebsiteComparePage.tsx
index 10a2eed15..fa64df352 100644
--- a/src/app/(main)/websites/[websiteId]/compare/WebsiteComparePage.tsx
+++ b/src/app/(main)/websites/[websiteId]/compare/WebsiteComparePage.tsx
@@ -3,7 +3,7 @@ import WebsiteHeader from '../WebsiteHeader';
import WebsiteMetricsBar from '../WebsiteMetricsBar';
import FilterTags from '@/components/metrics/FilterTags';
import { useNavigation } from '@/components/hooks';
-import { FILTER_COLUMNS } from '@/lib/constants';
+import { FILTER_COLUMNS, FILTER_GROUPS } from '@/lib/constants';
import WebsiteChart from '../WebsiteChart';
import WebsiteCompareTables from './WebsiteCompareTables';
@@ -11,7 +11,7 @@ export function WebsiteComparePage({ websiteId }) {
const { query } = useNavigation();
const params = Object.keys(query).reduce((obj, key) => {
- if (FILTER_COLUMNS[key]) {
+ if (FILTER_COLUMNS[key] || FILTER_GROUPS[key]) {
obj[key] = query[key];
}
return obj;
diff --git a/src/app/api/websites/[websiteId]/values/route.ts b/src/app/api/websites/[websiteId]/values/route.ts
index 7afa391b4..561c62fe3 100644
--- a/src/app/api/websites/[websiteId]/values/route.ts
+++ b/src/app/api/websites/[websiteId]/values/route.ts
@@ -30,17 +30,13 @@ export async function GET(
return unauthorized();
}
- if (
- !SESSION_COLUMNS.includes(type) &&
- !EVENT_COLUMNS.includes(type) &&
- !FILTER_GROUPS.includes(type)
- ) {
+ if (!SESSION_COLUMNS.includes(type) && !EVENT_COLUMNS.includes(type) && !FILTER_GROUPS[type]) {
return badRequest('Invalid type.');
}
let values;
- if (FILTER_GROUPS.includes(type)) {
+ if (FILTER_GROUPS[type]) {
values = (await getWebsiteSegments(websiteId, type)).map(segment => ({ value: segment.name }));
} else {
values = await getValues(websiteId, FILTER_COLUMNS[type], startDate, endDate, search);
diff --git a/src/lib/clickhouse.ts b/src/lib/clickhouse.ts
index 480084dc7..b911f89b0 100644
--- a/src/lib/clickhouse.ts
+++ b/src/lib/clickhouse.ts
@@ -105,6 +105,22 @@ function getFilterQuery(filters: QueryFilters = {}, options: QueryOptions = {})
return query.join('\n');
}
+function getCohortQuery(filters: QueryFilters = {}, options: QueryOptions = {}) {
+ const query = filtersToArray(filters, options).reduce((arr, { name, column, operator }) => {
+ if (column) {
+ arr.push(`and ${mapFilter(column, operator, name)}`);
+
+ if (name === 'referrer') {
+ arr.push(`and referrer_domain != hostname`);
+ }
+ }
+
+ return arr;
+ }, []);
+
+ return query.join('\n');
+}
+
function getDateQuery(filters: QueryFilters = {}) {
const { startDate, endDate, timezone } = filters;
@@ -146,6 +162,7 @@ async function parseFilters(websiteId: string, filters: QueryFilters = {}, optio
websiteId,
startDate: maxDate(filters.startDate, new Date(website?.resetAt)),
},
+ cohortQuery: getCohortQuery(filters),
};
}
diff --git a/src/lib/constants.ts b/src/lib/constants.ts
index 57e31aa3a..39ecbc088 100644
--- a/src/lib/constants.ts
+++ b/src/lib/constants.ts
@@ -47,7 +47,10 @@ export const SESSION_COLUMNS = [
'host',
];
-export const FILTER_GROUPS = ['segment', 'cohort'];
+export const FILTER_GROUPS = {
+ segment: 'segment',
+ cohort: 'cohort',
+};
export const FILTER_COLUMNS = {
url: 'url_path',
@@ -66,8 +69,6 @@ export const FILTER_COLUMNS = {
language: 'language',
event: 'event_name',
tag: 'tag',
- segment: 'segment',
- cohort: 'cohort',
};
export const COLLECTION_TYPE = {
diff --git a/src/lib/request.ts b/src/lib/request.ts
index 71861e344..b75ea1ba1 100644
--- a/src/lib/request.ts
+++ b/src/lib/request.ts
@@ -91,16 +91,19 @@ export async function getRequestFilters(query: Record<string, any>, websiteId?:
for (const key of Object.keys(FILTER_COLUMNS)) {
const value = query[key];
if (value !== undefined) {
- if (FILTER_GROUPS.includes(key)) {
- const segment = await getWebsiteSegment(websiteId, key, value);
- if (key === 'segment') {
- // merge filters into result
- Object.assign(result, segment.parameters);
- } else {
- result[key] = segment.parameters;
- }
+ result[key] = value;
+ }
+ }
+
+ for (const key of Object.keys(FILTER_GROUPS)) {
+ const value = query[key];
+ if (value !== undefined) {
+ const segment = await getWebsiteSegment(websiteId, key, value);
+ if (key === 'segment') {
+ // merge filters into result
+ Object.assign(result, segment.parameters);
} else {
- result[key] = value;
+ result[key] = segment.parameters;
}
}
}