refactor(ui): use feature analytics and saved definition routes
This commit is contained in:
1 parent
e202b740a1
commit
007919b173
41 files changed
+645
-367
No files matched your search
@@ -0,0 +1,39 @@
|
||||
import { expect, test } from 'vitest';
|
||||
import { serializeAnalyticsQuery } from './analytics-query';
|
||||
import { funnelQuerySchema } from './analytics-schema';
|
||||
|
||||
test('structured funnel criteria survive GET URL encoding and validation', () => {
|
||||
const steps = [
|
||||
{
|
||||
type: 'path',
|
||||
value: '/pricing?plan=a&b="c"',
|
||||
filters: [{ property: 'a.b', operator: 'eq', value: '日本語 + / ? #' }],
|
||||
},
|
||||
{ type: 'event', value: 'signup' },
|
||||
];
|
||||
const query = serializeAnalyticsQuery({
|
||||
websiteId: 'website',
|
||||
startDate: new Date(1000),
|
||||
endDate: new Date(2000),
|
||||
steps,
|
||||
window: 60,
|
||||
browser1: 'eq.Chrome',
|
||||
unused: undefined,
|
||||
});
|
||||
const url = new URL(`https://example.org?${new URLSearchParams(query)}`);
|
||||
expect(funnelQuerySchema.parse(Object.fromEntries(url.searchParams)).steps).toEqual(steps);
|
||||
expect(url.searchParams.get('browser1')).toBe('eq.Chrome');
|
||||
expect(url.searchParams.has('websiteId')).toBe(false);
|
||||
expect(url.searchParams.has('unused')).toBe(false);
|
||||
});
|
||||
|
||||
test('explicit dates win over default timestamps', () => {
|
||||
expect(
|
||||
serializeAnalyticsQuery({
|
||||
startAt: 0,
|
||||
endAt: 100,
|
||||
startDate: new Date(1000),
|
||||
endDate: new Date(2000),
|
||||
}),
|
||||
).toEqual({ startAt: 1000, endAt: 2000 });
|
||||
});
|
||||
@@ -0,0 +1,24 @@
|
||||
export interface AnalyticsParameters {
|
||||
websiteId: string;
|
||||
startDate?: Date | string;
|
||||
endDate?: Date | string;
|
||||
startAt?: number;
|
||||
endAt?: number;
|
||||
unit?: string;
|
||||
timezone?: string;
|
||||
}
|
||||
|
||||
/** Encode structured GET criteria once, before URLSearchParams escapes them. */
|
||||
export function serializeAnalyticsQuery(params: Record<string, any>) {
|
||||
const { websiteId: _websiteId, id: _id, startDate, endDate, ...query } = params;
|
||||
if (startDate != null) query.startAt = +new Date(startDate);
|
||||
if (endDate != null) query.endAt = +new Date(endDate);
|
||||
return Object.fromEntries(
|
||||
Object.entries(query)
|
||||
.filter(([, value]) => value !== undefined)
|
||||
.map(([key, value]) => [
|
||||
key,
|
||||
typeof value === 'object' && value !== null ? JSON.stringify(value) : value,
|
||||
]),
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user