1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
|
import { z } from 'zod';
import { isValidTimezone, normalizeTimezone } from '@/lib/date';
import { UNIT_TYPES } from './constants';
export const timezoneParam = z
.string()
.refine((value: string) => isValidTimezone(value), {
message: 'Invalid timezone',
})
.transform((value: string) => normalizeTimezone(value));
export const unitParam = z.string().refine(value => UNIT_TYPES.includes(value), {
message: 'Invalid unit',
});
export const dateRangeParams = {
startAt: z.coerce.number().optional(),
endAt: z.coerce.number().optional(),
startDate: z.coerce.date().optional(),
endDate: z.coerce.date().optional(),
timezone: timezoneParam.optional(),
unit: unitParam.optional(),
compare: z.string().optional(),
};
export const filterParams = {
path: z.string().optional(),
referrer: z.string().optional(),
title: z.string().optional(),
query: z.string().optional(),
os: z.string().optional(),
browser: z.string().optional(),
device: z.string().optional(),
country: z.string().optional(),
region: z.string().optional(),
city: z.string().optional(),
tag: z.string().optional(),
hostname: z.string().optional(),
language: z.string().optional(),
event: z.string().optional(),
segment: z.uuid().optional(),
cohort: z.uuid().optional(),
eventType: z.coerce.number().int().positive().optional(),
};
export const searchParams = {
search: z.string().optional(),
};
export const pagingParams = {
page: z.coerce.number().int().positive().optional(),
pageSize: z.coerce.number().int().positive().optional(),
};
export const sortingParams = {
orderBy: z.string().optional(),
};
export const userRoleParam = z.enum(['admin', 'user', 'view-only']);
export const teamRoleParam = z.enum(['team-member', 'team-view-only', 'team-manager']);
export const anyObjectParam = z.record(z.string(), z.any());
export const urlOrPathParam = z.string().refine(
value => {
try {
new URL(value, 'https://localhost');
return true;
} catch {
return false;
}
},
{
message: 'Invalid URL.',
},
);
export const fieldsParam = z.enum([
'path',
'referrer',
'title',
'query',
'os',
'browser',
'device',
'country',
'region',
'city',
'tag',
'hostname',
'language',
'event',
]);
export const reportTypeParam = z.enum([
'attribution',
'breakdown',
'funnel',
'goal',
'journey',
'retention',
'revenue',
'utm',
]);
export const goalReportSchema = z.object({
type: z.literal('goal'),
parameters: z
.object({
startDate: z.coerce.date(),
endDate: z.coerce.date(),
type: z.string(),
value: z.string(),
operator: z.enum(['count', 'sum', 'average']).optional(),
property: z.string().optional(),
})
.refine(data => {
if (data.type === 'event' && data.property) {
return data.operator && data.property;
}
return true;
}),
});
export const funnelReportSchema = z.object({
type: z.literal('funnel'),
parameters: z.object({
startDate: z.coerce.date(),
endDate: z.coerce.date(),
window: z.coerce.number().positive(),
steps: z
.array(
z.object({
type: z.enum(['path', 'event']),
value: z.string(),
}),
)
.min(2)
.max(8),
}),
});
export const journeyReportSchema = z.object({
type: z.literal('journey'),
parameters: z.object({
startDate: z.coerce.date(),
endDate: z.coerce.date(),
steps: z.coerce.number().min(2).max(7),
startStep: z.string().optional(),
endStep: z.string().optional(),
}),
});
export const retentionReportSchema = z.object({
type: z.literal('retention'),
parameters: z.object({
startDate: z.coerce.date(),
endDate: z.coerce.date(),
timezone: z.string().optional(),
}),
});
export const utmReportSchema = z.object({
type: z.literal('utm'),
parameters: z.object({
startDate: z.coerce.date(),
endDate: z.coerce.date(),
}),
});
export const revenueReportSchema = z.object({
type: z.literal('revenue'),
parameters: z.object({
startDate: z.coerce.date(),
endDate: z.coerce.date(),
timezone: z.string().optional(),
currency: z.string(),
}),
});
export const attributionReportSchema = z.object({
type: z.literal('attribution'),
parameters: z.object({
startDate: z.coerce.date(),
endDate: z.coerce.date(),
model: z.enum(['first-click', 'last-click']),
type: z.enum(['path', 'event']),
step: z.string(),
currency: z.string().optional(),
}),
});
export const breakdownReportSchema = z.object({
type: z.literal('breakdown'),
parameters: z.object({
startDate: z.coerce.date(),
endDate: z.coerce.date(),
fields: z.array(fieldsParam),
}),
});
export const reportBaseSchema = z.object({
websiteId: z.uuid(),
type: reportTypeParam,
name: z.string().max(200),
description: z.string().max(500).optional(),
parameters: anyObjectParam,
});
export const reportTypeSchema = z.discriminatedUnion('type', [
goalReportSchema,
funnelReportSchema,
journeyReportSchema,
retentionReportSchema,
utmReportSchema,
revenueReportSchema,
attributionReportSchema,
breakdownReportSchema,
]);
export const reportSchema = reportBaseSchema;
export const reportResultSchema = z.intersection(
z.object({
websiteId: z.uuid(),
filters: z.object({ ...filterParams }),
}),
reportTypeSchema,
);
export const segmentTypeParam = z.enum(['segment', 'cohort']);
|