1 | |
2 | type Gitbook = { |
3 | token: string; |
4 | }; |
5 | |
6 | * Query site events |
7 | * |
8 | */ |
9 | export async function main( |
10 | auth: Gitbook, |
11 | organizationId: string, |
12 | siteId: string, |
13 | body: { |
14 | select?: |
15 | | { column: "datetime"; interval?: "hour" | "day" | "week" | "month" } |
16 | | { column: "url" } |
17 | | { column: "eventType" } |
18 | | { column: "eventsCount" } |
19 | | { column: "sessionsCount" } |
20 | | { column: "visitorsCount" } |
21 | | { column: "siteSection" } |
22 | | { column: "siteSpace" } |
23 | | { column: "siteShareKey" } |
24 | | { column: "page" } |
25 | | { column: "visitorGeoCountry" } |
26 | | { column: "visitorGeoPoint"; precision?: number } |
27 | | { column: "visitorDevice" } |
28 | | { column: "visitorBrowser" } |
29 | | { column: "visitorOS" } |
30 | | { column: "visitorBot" } |
31 | | { column: "visitorLanguage" } |
32 | | { column: "eventLinkTargetValue" } |
33 | | { column: "eventLinkTargetKind" } |
34 | | { column: "eventLinkTargetDomain" } |
35 | | { column: "eventLinkPosition" } |
36 | | { column: "eventAPIOperationPath" } |
37 | | { column: "eventAPIOperationMethod" } |
38 | | { column: "eventSearchQuery" } |
39 | | { column: "eventPageFeedbackRating" } |
40 | | { column: "eventPageFeedbackComment" } |
41 | | { column: "eventAskResponseRating" } |
42 | | { column: "pageFeedbackRating" } |
43 | | { column: "askResponseRating" } |
44 | | { column: "referrer" } |
45 | | { column: "referrerDomain" } |
46 | | { column: "utmSource" } |
47 | | { column: "utmMedium" } |
48 | | { column: "utmCampaign" } |
49 | | { column: "utmTerm" } |
50 | | { column: "utmContent" } |
51 | | { column: "visitorClaimProperty" } |
52 | | { column: "visitorClaim"; claim: string } |
53 | | { column: "eventAdDomain" } |
54 | | { column: "eventAdPlacement" } |
55 | | { column: "eventTrademarkPlacement" }[]; |
56 | where?: |
57 | | ({ column: "datetime"; values: string[] } & { |
58 | operator?: |
59 | | "in" |
60 | | "notIn" |
61 | | "greaterThan" |
62 | | "greaterThanOrEqual" |
63 | | "lessThan" |
64 | | "lessThanOrEqual"; |
65 | }) |
66 | | ({ column: "url"; values: string[] } & { |
67 | operator?: |
68 | | "in" |
69 | | "notIn" |
70 | | "greaterThan" |
71 | | "greaterThanOrEqual" |
72 | | "lessThan" |
73 | | "lessThanOrEqual"; |
74 | }) |
75 | | ({ column: "eventsCount"; values: number[] } & { |
76 | operator?: |
77 | | "in" |
78 | | "notIn" |
79 | | "greaterThan" |
80 | | "greaterThanOrEqual" |
81 | | "lessThan" |
82 | | "lessThanOrEqual"; |
83 | }) |
84 | | ({ column: "sessionsCount"; values: number[] } & { |
85 | operator?: |
86 | | "in" |
87 | | "notIn" |
88 | | "greaterThan" |
89 | | "greaterThanOrEqual" |
90 | | "lessThan" |
91 | | "lessThanOrEqual"; |
92 | }) |
93 | | ({ column: "visitorsCount"; values: number[] } & { |
94 | operator?: |
95 | | "in" |
96 | | "notIn" |
97 | | "greaterThan" |
98 | | "greaterThanOrEqual" |
99 | | "lessThan" |
100 | | "lessThanOrEqual"; |
101 | }) |
102 | | ({ |
103 | column: "eventType"; |
104 | values: |
105 | | "page_view" |
106 | | "search_open" |
107 | | "search_type_query" |
108 | | "search_open_result" |
109 | | "page_post_feedback" |
110 | | "page_post_feedback_comment" |
111 | | "ask_question" |
112 | | "ask_rate_response" |
113 | | "link_click" |
114 | | "api_client_open" |
115 | | "api_client_request" |
116 | | "ad_click" |
117 | | "ad_display" |
118 | | "trademark_click"[]; |
119 | } & { |
120 | operator?: |
121 | | "in" |
122 | | "notIn" |
123 | | "greaterThan" |
124 | | "greaterThanOrEqual" |
125 | | "lessThan" |
126 | | "lessThanOrEqual"; |
127 | }) |
128 | | ({ column: "visitorClaim"; claim: string; values: string[] } & { |
129 | operator?: |
130 | | "in" |
131 | | "notIn" |
132 | | "greaterThan" |
133 | | "greaterThanOrEqual" |
134 | | "lessThan" |
135 | | "lessThanOrEqual"; |
136 | }) |
137 | | ({ column: "visitorClaimProperty"; values: string[] } & { |
138 | operator?: |
139 | | "in" |
140 | | "notIn" |
141 | | "greaterThan" |
142 | | "greaterThanOrEqual" |
143 | | "lessThan" |
144 | | "lessThanOrEqual"; |
145 | }) |
146 | | ({ |
147 | column: "visitorBrowser"; |
148 | values: |
149 | | "chrome" |
150 | | "firefox" |
151 | | "safari" |
152 | | "edge" |
153 | | "ie" |
154 | | "opera" |
155 | | "unknown"[]; |
156 | } & { |
157 | operator?: |
158 | | "in" |
159 | | "notIn" |
160 | | "greaterThan" |
161 | | "greaterThanOrEqual" |
162 | | "lessThan" |
163 | | "lessThanOrEqual"; |
164 | }) |
165 | | ({ |
166 | column: "visitorDevice"; |
167 | values: "unknown" | "desktop" | "tablet" | "mobile"[]; |
168 | } & { |
169 | operator?: |
170 | | "in" |
171 | | "notIn" |
172 | | "greaterThan" |
173 | | "greaterThanOrEqual" |
174 | | "lessThan" |
175 | | "lessThanOrEqual"; |
176 | }) |
177 | | ({ |
178 | column: "visitorOS"; |
179 | values: |
180 | | "unknown" |
181 | | "windows" |
182 | | "macos" |
183 | | "linux" |
184 | | "android" |
185 | | "ios"[]; |
186 | } & { |
187 | operator?: |
188 | | "in" |
189 | | "notIn" |
190 | | "greaterThan" |
191 | | "greaterThanOrEqual" |
192 | | "lessThan" |
193 | | "lessThanOrEqual"; |
194 | }) |
195 | | ({ |
196 | column: "visitorBot"; |
197 | values: |
198 | | "" |
199 | | "unknown" |
200 | | "googlebot" |
201 | | "bingbot" |
202 | | "duckduckbot" |
203 | | "facebookbot" |
204 | | "applebot" |
205 | | "chatgpt" |
206 | | "anthropic"[]; |
207 | } & { |
208 | operator?: |
209 | | "in" |
210 | | "notIn" |
211 | | "greaterThan" |
212 | | "greaterThanOrEqual" |
213 | | "lessThan" |
214 | | "lessThanOrEqual"; |
215 | }) |
216 | | ({ |
217 | column: "eventLinkTargetValue"; |
218 | values: |
219 | | { kind: "file"; file: string } |
220 | | { kind: "url"; url: string } |
221 | | { kind: "page"; page: string; space?: string } |
222 | | { kind: "anchor"; anchor: string; space?: string; page?: string } |
223 | | { kind: "user"; user: string } |
224 | | { kind: "collection"; collection: string } |
225 | | { kind: "space"; space: string } |
226 | | { |
227 | kind: "reusable-content"; |
228 | reusableContent: string; |
229 | space?: string; |
230 | } |
231 | | { kind: "openapi"; spec: string }[]; |
232 | } & { |
233 | operator?: |
234 | | "in" |
235 | | "notIn" |
236 | | "greaterThan" |
237 | | "greaterThanOrEqual" |
238 | | "lessThan" |
239 | | "lessThanOrEqual"; |
240 | }) |
241 | | ({ |
242 | column: "eventLinkTargetKind"; |
243 | values: |
244 | | "url" |
245 | | "page" |
246 | | "file" |
247 | | "anchor" |
248 | | "user" |
249 | | "collection" |
250 | | "space" |
251 | | "reusable-content"[]; |
252 | } & { |
253 | operator?: |
254 | | "in" |
255 | | "notIn" |
256 | | "greaterThan" |
257 | | "greaterThanOrEqual" |
258 | | "lessThan" |
259 | | "lessThanOrEqual"; |
260 | }) |
261 | | ({ |
262 | column: "eventLinkPosition"; |
263 | values: |
264 | | "announcement" |
265 | | "header" |
266 | | "footer" |
267 | | "sidebar" |
268 | | "content"[]; |
269 | } & { |
270 | operator?: |
271 | | "in" |
272 | | "notIn" |
273 | | "greaterThan" |
274 | | "greaterThanOrEqual" |
275 | | "lessThan" |
276 | | "lessThanOrEqual"; |
277 | }) |
278 | | ({ column: "siteSection"; values: string[] } & { |
279 | operator?: |
280 | | "in" |
281 | | "notIn" |
282 | | "greaterThan" |
283 | | "greaterThanOrEqual" |
284 | | "lessThan" |
285 | | "lessThanOrEqual"; |
286 | }) |
287 | | ({ column: "siteSpace"; values: string[] } & { |
288 | operator?: |
289 | | "in" |
290 | | "notIn" |
291 | | "greaterThan" |
292 | | "greaterThanOrEqual" |
293 | | "lessThan" |
294 | | "lessThanOrEqual"; |
295 | }) |
296 | | ({ column: "siteShareKey"; values: string[] } & { |
297 | operator?: |
298 | | "in" |
299 | | "notIn" |
300 | | "greaterThan" |
301 | | "greaterThanOrEqual" |
302 | | "lessThan" |
303 | | "lessThanOrEqual"; |
304 | }) |
305 | | ({ column: "page"; values: { page: string; space: string }[] } & { |
306 | operator?: |
307 | | "in" |
308 | | "notIn" |
309 | | "greaterThan" |
310 | | "greaterThanOrEqual" |
311 | | "lessThan" |
312 | | "lessThanOrEqual"; |
313 | }) |
314 | | ({ column: "visitorGeoCountry"; values: string[] } & { |
315 | operator?: |
316 | | "in" |
317 | | "notIn" |
318 | | "greaterThan" |
319 | | "greaterThanOrEqual" |
320 | | "lessThan" |
321 | | "lessThanOrEqual"; |
322 | }) |
323 | | ({ |
324 | column: "visitorGeoPoint"; |
325 | values: { latitude: number; longitude: number; h3: string }[]; |
326 | } & { |
327 | operator?: |
328 | | "in" |
329 | | "notIn" |
330 | | "greaterThan" |
331 | | "greaterThanOrEqual" |
332 | | "lessThan" |
333 | | "lessThanOrEqual"; |
334 | }) |
335 | | ({ column: "visitorLanguage"; values: string[] } & { |
336 | operator?: |
337 | | "in" |
338 | | "notIn" |
339 | | "greaterThan" |
340 | | "greaterThanOrEqual" |
341 | | "lessThan" |
342 | | "lessThanOrEqual"; |
343 | }) |
344 | | ({ column: "eventLinkTargetDomain"; values: string[] } & { |
345 | operator?: |
346 | | "in" |
347 | | "notIn" |
348 | | "greaterThan" |
349 | | "greaterThanOrEqual" |
350 | | "lessThan" |
351 | | "lessThanOrEqual"; |
352 | }) |
353 | | ({ column: "eventAPIOperationPath"; values: string[] } & { |
354 | operator?: |
355 | | "in" |
356 | | "notIn" |
357 | | "greaterThan" |
358 | | "greaterThanOrEqual" |
359 | | "lessThan" |
360 | | "lessThanOrEqual"; |
361 | }) |
362 | | ({ column: "eventAPIOperationMethod"; values: string[] } & { |
363 | operator?: |
364 | | "in" |
365 | | "notIn" |
366 | | "greaterThan" |
367 | | "greaterThanOrEqual" |
368 | | "lessThan" |
369 | | "lessThanOrEqual"; |
370 | }) |
371 | | ({ column: "eventSearchQuery"; values: string[] } & { |
372 | operator?: |
373 | | "in" |
374 | | "notIn" |
375 | | "greaterThan" |
376 | | "greaterThanOrEqual" |
377 | | "lessThan" |
378 | | "lessThanOrEqual"; |
379 | }) |
380 | | ({ |
381 | column: "pageFeedbackRating"; |
382 | values: { ok: number; good: number; bad: number }[]; |
383 | } & { |
384 | operator?: |
385 | | "in" |
386 | | "notIn" |
387 | | "greaterThan" |
388 | | "greaterThanOrEqual" |
389 | | "lessThan" |
390 | | "lessThanOrEqual"; |
391 | }) |
392 | | ({ |
393 | column: "askResponseRating"; |
394 | values: { positive: number; negative: number }[]; |
395 | } & { |
396 | operator?: |
397 | | "in" |
398 | | "notIn" |
399 | | "greaterThan" |
400 | | "greaterThanOrEqual" |
401 | | "lessThan" |
402 | | "lessThanOrEqual"; |
403 | }) |
404 | | ({ |
405 | column: "eventPageFeedbackRating"; |
406 | values: "bad" | "ok" | "good"[]; |
407 | } & { |
408 | operator?: |
409 | | "in" |
410 | | "notIn" |
411 | | "greaterThan" |
412 | | "greaterThanOrEqual" |
413 | | "lessThan" |
414 | | "lessThanOrEqual"; |
415 | }) |
416 | | ({ column: "eventAskResponseRating"; values: number[] } & { |
417 | operator?: |
418 | | "in" |
419 | | "notIn" |
420 | | "greaterThan" |
421 | | "greaterThanOrEqual" |
422 | | "lessThan" |
423 | | "lessThanOrEqual"; |
424 | }) |
425 | | ({ column: "eventPageFeedbackComment"; values: string[] } & { |
426 | operator?: |
427 | | "in" |
428 | | "notIn" |
429 | | "greaterThan" |
430 | | "greaterThanOrEqual" |
431 | | "lessThan" |
432 | | "lessThanOrEqual"; |
433 | }) |
434 | | ({ column: "referrer"; values: string[] } & { |
435 | operator?: |
436 | | "in" |
437 | | "notIn" |
438 | | "greaterThan" |
439 | | "greaterThanOrEqual" |
440 | | "lessThan" |
441 | | "lessThanOrEqual"; |
442 | }) |
443 | | ({ column: "referrerDomain"; values: string[] } & { |
444 | operator?: |
445 | | "in" |
446 | | "notIn" |
447 | | "greaterThan" |
448 | | "greaterThanOrEqual" |
449 | | "lessThan" |
450 | | "lessThanOrEqual"; |
451 | }) |
452 | | ({ column: "utmSource"; values: string[] } & { |
453 | operator?: |
454 | | "in" |
455 | | "notIn" |
456 | | "greaterThan" |
457 | | "greaterThanOrEqual" |
458 | | "lessThan" |
459 | | "lessThanOrEqual"; |
460 | }) |
461 | | ({ column: "utmMedium"; values: string[] } & { |
462 | operator?: |
463 | | "in" |
464 | | "notIn" |
465 | | "greaterThan" |
466 | | "greaterThanOrEqual" |
467 | | "lessThan" |
468 | | "lessThanOrEqual"; |
469 | }) |
470 | | ({ column: "utmCampaign"; values: string[] } & { |
471 | operator?: |
472 | | "in" |
473 | | "notIn" |
474 | | "greaterThan" |
475 | | "greaterThanOrEqual" |
476 | | "lessThan" |
477 | | "lessThanOrEqual"; |
478 | }) |
479 | | ({ column: "utmTerm"; values: string[] } & { |
480 | operator?: |
481 | | "in" |
482 | | "notIn" |
483 | | "greaterThan" |
484 | | "greaterThanOrEqual" |
485 | | "lessThan" |
486 | | "lessThanOrEqual"; |
487 | }) |
488 | | ({ column: "utmContent"; values: string[] } & { |
489 | operator?: |
490 | | "in" |
491 | | "notIn" |
492 | | "greaterThan" |
493 | | "greaterThanOrEqual" |
494 | | "lessThan" |
495 | | "lessThanOrEqual"; |
496 | }) |
497 | | ({ column: "eventAdDomain"; values: string[] } & { |
498 | operator?: |
499 | | "in" |
500 | | "notIn" |
501 | | "greaterThan" |
502 | | "greaterThanOrEqual" |
503 | | "lessThan" |
504 | | "lessThanOrEqual"; |
505 | }) |
506 | | ({ column: "eventAdPlacement"; values: "aside"[] } & { |
507 | operator?: |
508 | | "in" |
509 | | "notIn" |
510 | | "greaterThan" |
511 | | "greaterThanOrEqual" |
512 | | "lessThan" |
513 | | "lessThanOrEqual"; |
514 | }) |
515 | | ({ |
516 | column: "eventTrademarkPlacement"; |
517 | values: "footer" | "sidebar" | "ad" | "pdf"[]; |
518 | } & { |
519 | operator?: |
520 | | "in" |
521 | | "notIn" |
522 | | "greaterThan" |
523 | | "greaterThanOrEqual" |
524 | | "lessThan" |
525 | | "lessThanOrEqual"; |
526 | }[]); |
527 | groupBy?: |
528 | | { column: "datetime"; interval?: "hour" | "day" | "week" | "month" } |
529 | | { column: "url" } |
530 | | { column: "eventType" } |
531 | | { column: "eventsCount" } |
532 | | { column: "sessionsCount" } |
533 | | { column: "visitorsCount" } |
534 | | { column: "siteSection" } |
535 | | { column: "siteSpace" } |
536 | | { column: "siteShareKey" } |
537 | | { column: "page" } |
538 | | { column: "visitorGeoCountry" } |
539 | | { column: "visitorGeoPoint"; precision?: number } |
540 | | { column: "visitorDevice" } |
541 | | { column: "visitorBrowser" } |
542 | | { column: "visitorOS" } |
543 | | { column: "visitorBot" } |
544 | | { column: "visitorLanguage" } |
545 | | { column: "eventLinkTargetValue" } |
546 | | { column: "eventLinkTargetKind" } |
547 | | { column: "eventLinkTargetDomain" } |
548 | | { column: "eventLinkPosition" } |
549 | | { column: "eventAPIOperationPath" } |
550 | | { column: "eventAPIOperationMethod" } |
551 | | { column: "eventSearchQuery" } |
552 | | { column: "eventPageFeedbackRating" } |
553 | | { column: "eventPageFeedbackComment" } |
554 | | { column: "eventAskResponseRating" } |
555 | | { column: "pageFeedbackRating" } |
556 | | { column: "askResponseRating" } |
557 | | { column: "referrer" } |
558 | | { column: "referrerDomain" } |
559 | | { column: "utmSource" } |
560 | | { column: "utmMedium" } |
561 | | { column: "utmCampaign" } |
562 | | { column: "utmTerm" } |
563 | | { column: "utmContent" } |
564 | | { column: "visitorClaimProperty" } |
565 | | { column: "visitorClaim"; claim: string } |
566 | | { column: "eventAdDomain" } |
567 | | { column: "eventAdPlacement" } |
568 | | { column: "eventTrademarkPlacement" }[]; |
569 | order?: { |
570 | by: |
571 | | { column: "datetime"; interval?: "hour" | "day" | "week" | "month" } |
572 | | { column: "url" } |
573 | | { column: "eventType" } |
574 | | { column: "eventsCount" } |
575 | | { column: "sessionsCount" } |
576 | | { column: "visitorsCount" } |
577 | | { column: "siteSection" } |
578 | | { column: "siteSpace" } |
579 | | { column: "siteShareKey" } |
580 | | { column: "page" } |
581 | | { column: "visitorGeoCountry" } |
582 | | { column: "visitorGeoPoint"; precision?: number } |
583 | | { column: "visitorDevice" } |
584 | | { column: "visitorBrowser" } |
585 | | { column: "visitorOS" } |
586 | | { column: "visitorBot" } |
587 | | { column: "visitorLanguage" } |
588 | | { column: "eventLinkTargetValue" } |
589 | | { column: "eventLinkTargetKind" } |
590 | | { column: "eventLinkTargetDomain" } |
591 | | { column: "eventLinkPosition" } |
592 | | { column: "eventAPIOperationPath" } |
593 | | { column: "eventAPIOperationMethod" } |
594 | | { column: "eventSearchQuery" } |
595 | | { column: "eventPageFeedbackRating" } |
596 | | { column: "eventPageFeedbackComment" } |
597 | | { column: "eventAskResponseRating" } |
598 | | { column: "pageFeedbackRating" } |
599 | | { column: "askResponseRating" } |
600 | | { column: "referrer" } |
601 | | { column: "referrerDomain" } |
602 | | { column: "utmSource" } |
603 | | { column: "utmMedium" } |
604 | | { column: "utmCampaign" } |
605 | | { column: "utmTerm" } |
606 | | { column: "utmContent" } |
607 | | { column: "visitorClaimProperty" } |
608 | | { column: "visitorClaim"; claim: string } |
609 | | { column: "eventAdDomain" } |
610 | | { column: "eventAdPlacement" } |
611 | | { column: "eventTrademarkPlacement" }; |
612 | direction: "asc" | "desc"; |
613 | }; |
614 | range: |
615 | | "lastYear" |
616 | | "last3Months" |
617 | | "last30Days" |
618 | | "last7Days" |
619 | | "last24Hours" |
620 | | { from: string; to: string }; |
621 | limit?: number; |
622 | }, |
623 | ) { |
624 | const url = new URL( |
625 | `https://api.gitbook.com/v1/orgs/${organizationId}/sites/${siteId}/insights/events/aggregate`, |
626 | ); |
627 |
|
628 | const response = await fetch(url, { |
629 | method: "POST", |
630 | headers: { |
631 | "Content-Type": "application/json", |
632 | Authorization: "Bearer " + auth.token, |
633 | }, |
634 | body: JSON.stringify(body), |
635 | }); |
636 | if (!response.ok) { |
637 | const text = await response.text(); |
638 | throw new Error(`${response.status} ${text}`); |
639 | } |
640 | return await response.json(); |
641 | } |
642 |
|