Merge branch 'dev' into boards

# Conflicts:
#	.gitignore
#	package.json
#	pnpm-lock.yaml
#	prisma/migrations/16_boards/migration.sql
#	prisma/schema.prisma
#	src/app/(main)/MobileNav.tsx
#	src/app/(main)/websites/[websiteId]/WebsiteHeader.tsx
#	src/app/(main)/websites/[websiteId]/settings/WebsiteShareForm.tsx
#	src/components/common/SideMenu.tsx
#	src/lib/types.ts
This commit is contained in:
Mike Cao committed 2026-02-05 20:05:25 -08:00
commit c3e0290e65
150 files changed
+3028 -787

No files matched your search

+1 -1
View File
@@ -18,5 +18,5 @@ test('getIpAddress: Standard header', () => {
});
test('getIpAddress: No header', () => {
expect(getIpAddress(new Headers())).toEqual(null);
expect(getIpAddress(new Headers())).toEqual(undefined);
});
+2 -3
View File
@@ -1,7 +1,6 @@
import debug from 'debug';
import { ROLE_PERMISSIONS, ROLES, SHARE_TOKEN_HEADER } from '@/lib/constants';
import { secret } from '@/lib/crypto';
import { getRandomChars } from '@/lib/generate';
import { createAuthKey, secret } from '@/lib/crypto';
import { createSecureToken, parseSecureToken, parseToken } from '@/lib/jwt';
import redis from '@/lib/redis';
import { ensureArray } from '@/lib/utils';
@@ -53,7 +52,7 @@ export async function checkAuth(request: Request) {
}
export async function saveAuth(data: any, expire = 0) {
const authKey = `auth:${getRandomChars(32)}`;
const authKey = `auth:${createAuthKey()}`;
if (redis.enabled) {
await redis.client.set(authKey, data);
-1
View File
@@ -11,7 +11,6 @@ export function renderDateLabels(unit: string, locale: string) {
switch (unit) {
case 'minute':
return formatDate(d, 'h:mm', locale);
case 'hour':
return formatDate(d, 'p', locale);
case 'day':
+20
View File
@@ -131,6 +131,25 @@ function getCohortQuery(filters: Record<string, any>) {
`;
}
function getExcludeBounceQuery(filters: Record<string, any>) {
if (!filters.excludeBounce === true) {
return '';
}
return `join
(select distinct session_id, visit_id
from website_event_stats_hourly
where website_id = {websiteId:UUID}
and created_at between {startDate:DateTime64} and {endDate:DateTime64}
and event_type = 1
group by session_id, visit_id
having sum(views) > 1
) excludeBounce
on excludeBounce.session_id = website_event.session_id
and excludeBounce.visit_id = website_event.visit_id
`;
}
function getDateQuery(filters: Record<string, any>) {
const { startDate, endDate, timezone } = filters;
@@ -174,6 +193,7 @@ function parseFilters(filters: Record<string, any>, options?: QueryOptions) {
dateQuery: getDateQuery(filters),
queryParams: getQueryParams(filters),
cohortQuery: getCohortQuery(cohortFilters),
excludeBounceQuery: getExcludeBounceQuery(filters),
};
}
+12
View File
@@ -44,6 +44,11 @@ export const EVENT_COLUMNS = [
'event',
'tag',
'hostname',
'utmSource',
'utmMedium',
'utmCampaign',
'utmContent',
'utmTerm',
];
export const SESSION_COLUMNS = [
@@ -55,6 +60,7 @@ export const SESSION_COLUMNS = [
'country',
'city',
'region',
'distinctId',
];
export const SEGMENT_TYPES = {
@@ -69,6 +75,7 @@ export const FILTER_COLUMNS = {
referrer: 'referrer_domain',
domain: 'referrer_domain',
hostname: 'hostname',
distinctId: 'distinct_id',
title: 'page_title',
query: 'url_query',
os: 'os',
@@ -81,6 +88,11 @@ export const FILTER_COLUMNS = {
event: 'event_name',
tag: 'tag',
eventType: 'event_type',
utmSource: 'utm_source',
utmMedium: 'utm_medium',
utmCampaign: 'utm_campaign',
utmContent: 'utm_content',
utmTerm: 'utm_term',
};
export const COLLECTION_TYPE = {
+13
View File
@@ -1,4 +1,5 @@
import crypto from 'node:crypto';
import { startOfDay, startOfMonth, startOfWeek } from 'date-fns';
import { v4, v5, v7 } from 'uuid';
const ALGORITHM = 'aes-256-gcm';
@@ -63,3 +64,15 @@ export function uuid(...args: any) {
return process.env.USE_UUIDV7 ? v7() : v4();
}
export function createAuthKey() {
return crypto.randomBytes(16).toString('hex');
}
export function getSalt(saltRotation: string, createdAt: Date): string {
return hash(
(saltRotation === 'day' ? startOfDay : saltRotation === 'week' ? startOfWeek : startOfMonth)(
createdAt,
).toUTCString(),
);
}
+24 -10
View File
@@ -9,6 +9,7 @@ import {
differenceInCalendarMonths,
differenceInCalendarWeeks,
differenceInCalendarYears,
differenceInDays,
differenceInHours,
differenceInMinutes,
endOfDay,
@@ -136,7 +137,12 @@ export function parseDateValue(value: string) {
return { num: +num, unit };
}
export function parseDateRange(value: string, locale = 'en-US', timezone?: string): DateRange {
export function parseDateRange(
value: string,
unitValue?: string,
locale = 'en-US',
timezone?: string,
): DateRange {
if (typeof value !== 'string') {
return null;
}
@@ -146,7 +152,7 @@ export function parseDateRange(value: string, locale = 'en-US', timezone?: strin
const startDate = new Date(+startTime);
const endDate = new Date(+endTime);
const unit = getMinimumUnit(startDate, endDate);
const unit = getMinimumUnit(startDate, endDate, true);
return {
startDate,
@@ -169,14 +175,14 @@ export function parseDateRange(value: string, locale = 'en-US', timezone?: strin
endDate: endOfHour(now),
offset: 0,
num: num || 1,
unit,
unit: unitValue || unit,
value,
};
case 'day':
return {
startDate: num ? subDays(startOfDay(now), num) : startOfDay(now),
endDate: endOfDay(now),
unit: num ? 'day' : 'hour',
unit: unitValue ? unitValue : num ? 'day' : 'hour',
offset: 0,
num: num || 1,
value,
@@ -187,7 +193,7 @@ export function parseDateRange(value: string, locale = 'en-US', timezone?: strin
? subWeeks(startOfWeek(now, { locale: dateLocale }), num)
: startOfWeek(now, { locale: dateLocale }),
endDate: endOfWeek(now, { locale: dateLocale }),
unit: 'day',
unit: unitValue || 'day',
offset: 0,
num: num || 1,
value,
@@ -196,7 +202,7 @@ export function parseDateRange(value: string, locale = 'en-US', timezone?: strin
return {
startDate: num ? subMonths(startOfMonth(now), num) : startOfMonth(now),
endDate: endOfMonth(now),
unit: num ? 'month' : 'day',
unit: unitValue ? unitValue : num ? 'month' : 'day',
offset: 0,
num: num || 1,
value,
@@ -205,7 +211,7 @@ export function parseDateRange(value: string, locale = 'en-US', timezone?: strin
return {
startDate: num ? subYears(startOfYear(now), num) : startOfYear(now),
endDate: endOfYear(now),
unit: 'month',
unit: unitValue || 'month',
offset: 0,
num: num || 1,
value,
@@ -273,12 +279,20 @@ export function getAllowedUnits(startDate: Date, endDate: Date) {
return index >= 0 ? units.splice(index) : [];
}
export function getMinimumUnit(startDate: number | Date, endDate: number | Date) {
export function getMinimumUnit(
startDate: number | Date,
endDate: number | Date,
isDateRange: boolean = false,
) {
if (differenceInMinutes(endDate, startDate) <= 60) {
return 'minute';
} else if (differenceInHours(endDate, startDate) <= 48) {
} else if (
isDateRange
? differenceInHours(endDate, startDate) <= 48
: differenceInDays(endDate, startDate) <= 30
) {
return 'hour';
} else if (differenceInCalendarMonths(endDate, startDate) <= 6) {
} else if (differenceInCalendarMonths(endDate, startDate) <= 7) {
return 'day';
} else if (differenceInCalendarMonths(endDate, startDate) <= 24) {
return 'month';
+12 -2
View File
@@ -10,6 +10,16 @@ import { safeDecodeURIComponent } from '@/lib/url';
const MAXMIND = 'maxmind';
const PROVIDER_HEADERS = [
// Umami custom headers (cloud mode only)
...(process.env.CLOUD_MODE
? [
{
countryHeader: 'x-umami-client-country',
regionHeader: 'x-umami-client-region',
cityHeader: 'x-umami-client-city',
},
]
: []),
// Cloudflare headers
{
countryHeader: 'cf-ipcountry',
@@ -66,13 +76,13 @@ function decodeHeader(s: string | undefined | null): string | undefined | null {
return Buffer.from(s, 'latin1').toString('utf-8');
}
export async function getLocation(ip: string = '', headers: Headers, hasPayloadIP: boolean) {
export async function getLocation(ip: string = '', headers: Headers, skipHeaders: boolean) {
// Ignore local ips
if (!ip || (await isLocalhost(ip))) {
return null;
}
if (!hasPayloadIP && !process.env.SKIP_LOCATION_HEADERS) {
if (!skipHeaders && !process.env.SKIP_LOCATION_HEADERS) {
for (const provider of PROVIDER_HEADERS) {
const countryHeader = headers.get(provider.countryHeader);
if (countryHeader) {
+2 -1
View File
@@ -1,6 +1,7 @@
import type { Link, Pixel, Website } from '@/generated/prisma/client';
import { getLink, getPixel, getWebsite } from '@/queries/prisma';
export async function getEntity(entityId: string) {
export async function getEntity(entityId: string): Promise<Website | Link | Pixel | null> {
const website = await getWebsite(entityId);
const link = await getLink(entityId);
const pixel = await getPixel(entityId);
+2 -1
View File
@@ -1,6 +1,7 @@
import ipaddr from 'ipaddr.js';
export const IP_ADDRESS_HEADERS = [
...(process.env.CLOUD_MODE ? ['x-umami-client-ip'] : []), // Umami custom header (cloud mode only)
'true-client-ip', // CDN
'cf-connecting-ip', // Cloudflare
'fastly-client-ip', // Fastly
@@ -81,7 +82,7 @@ export function getIpAddress(headers: Headers) {
}
if (header === 'forwarded') {
const match = ip.match(/for=(\[?[0-9a-fA-F:.]+\]?)/);
const match = ip.match(/for=(\[?[0-9a-fA-F:.]+]?)/);
if (match) {
return resolveIp(match[1]);
+21 -1
View File
@@ -108,7 +108,7 @@ function getFilterQuery(filters: Record<string, any>, options: QueryOptions = {}
if (name === 'referrer') {
arr.push(
`and (website_event.referrer_domain != website_event.hostname or website_event.referrer_domain is null)`,
`and (website_event.referrer_domain != regexp_replace(website_event.hostname, '^www.', '') or website_event.referrer_domain is null)`,
);
}
}
@@ -141,6 +141,25 @@ function getCohortQuery(filters: QueryFilters = {}) {
`;
}
function getExcludeBounceQuery(filters: Record<string, any>) {
if (!filters.excludeBounce === true) {
return '';
}
return `join
(select distinct session_id, visit_id
from website_event
where website_id = {{websiteId}}
and created_at between {{startDate}} and {{endDate}}
and event_type = 1
group by session_id, visit_id
having count(*) > 1
) excludeBounce
on excludeBounce.session_id = website_event.session_id
and excludeBounce.visit_id = website_event.visit_id
`;
}
function getDateQuery(filters: Record<string, any>) {
const { startDate, endDate } = filters;
@@ -186,6 +205,7 @@ function parseFilters(filters: Record<string, any>, options?: QueryOptions) {
filterQuery: getFilterQuery(filters, options),
queryParams: getQueryParams(filters),
cohortQuery: getCohortQuery(cohortFilters),
excludeBounceQuery: getExcludeBounceQuery(filters),
};
}
+4
View File
@@ -140,6 +140,10 @@ export async function getQueryFilters(
cohort_endDate: endDate,
});
}
if (params.excludeBounce) {
Object.assign(filters, { excludeBounce: true });
}
}
return {
+53 -2
View File
@@ -36,11 +36,18 @@ export const filterParams = {
city: z.string().optional(),
tag: z.string().optional(),
hostname: z.string().optional(),
distinctId: z.string().optional(),
language: z.string().optional(),
event: z.string().optional(),
utmSource: z.string().optional(),
utmMedium: z.string().optional(),
utmCampaign: z.string().optional(),
utmContent: z.string().optional(),
utmTerm: z.string().optional(),
segment: z.uuid().optional(),
cohort: z.uuid().optional(),
eventType: z.coerce.number().int().positive().optional(),
excludeBounce: z.string().optional(),
};
export const searchParams = {
@@ -89,8 +96,14 @@ export const fieldsParam = z.enum([
'city',
'tag',
'hostname',
'distinctId',
'language',
'event',
'utmSource',
'utmMedium',
'utmCampaign',
'utmContent',
'utmTerm',
]);
export const reportTypeParam = z.enum([
@@ -104,6 +117,23 @@ export const reportTypeParam = z.enum([
'utm',
]);
export const operatorParam = z.enum([
'eq',
'neq',
's',
'ns',
'c',
'dnc',
't',
'f',
'gt',
'lt',
'gte',
'lte',
'bf',
'af',
]);
export const goalReportSchema = z.object({
type: z.literal('goal'),
parameters: z
@@ -149,6 +179,7 @@ export const journeyReportSchema = z.object({
steps: z.coerce.number().min(2).max(7),
startStep: z.string().optional(),
endStep: z.string().optional(),
eventType: z.coerce.number().int().positive().optional(),
}),
});
@@ -157,7 +188,7 @@ export const retentionReportSchema = z.object({
parameters: z.object({
startDate: z.coerce.date(),
endDate: z.coerce.date(),
timezone: z.string().optional(),
timezone: timezoneParam.optional(),
}),
});
@@ -174,7 +205,8 @@ export const revenueReportSchema = z.object({
parameters: z.object({
startDate: z.coerce.date(),
endDate: z.coerce.date(),
timezone: z.string().optional(),
unit: unitParam.optional(),
timezone: timezoneParam.optional(),
currency: z.string(),
}),
});
@@ -230,3 +262,22 @@ export const reportResultSchema = z.intersection(
);
export const segmentTypeParam = z.enum(['segment', 'cohort']);
export const segmentParamSchema = z.object({
filters: z
.array(
z.object({
name: z.string(),
operator: operatorParam,
value: z.string(),
}),
)
.optional(),
dateRange: z.string().optional(),
action: z
.object({
type: z.string(),
value: z.string(),
})
.optional(),
});
+7
View File
@@ -92,6 +92,7 @@ export interface FilterParams {
segment?: string;
cohort?: string;
compare?: string;
excludeBounce?: boolean;
}
export interface SortParams {
@@ -169,3 +170,9 @@ export interface BoardParameters {
export interface Board extends Omit<PrismaBoard, 'parameters'> {
parameters: BoardParameters;
}
export interface WhiteLabel {
name: string;
url: string;
image: string;
}