0

Retrieve a statistic

by
Published Oct 17, 2025
Script gorgias Verified

The script

Submitted by hugo697 Bun
Verified 235 days ago
1
//native
2
type Gorgias = {
3
  username: string;
4
  apiKey: string;
5
  domain: string;
6
};
7
/**
8
 * Retrieve a statistic
9
 *
10
 */
11
export async function main(
12
  auth: Gorgias,
13
  name:
14
    | "overview"
15
    | "total-tickets-created"
16
    | "total-tickets-replied"
17
    | "total-tickets-closed"
18
    | "total-messages-sent"
19
    | "total-messages-received"
20
    | "median-first-response-time"
21
    | "median-resolution-time"
22
    | "total-one-touch-tickets"
23
    | "support-volume"
24
    | "resolution-time"
25
    | "first-response-time"
26
    | "tickets-created-per-hour-per-weekday"
27
    | "tickets-per-tag"
28
    | "tickets-created-per-channel"
29
    | "tickets-created-per-channel-per-day"
30
    | "tickets-closed-per-agent"
31
    | "tickets-closed-per-agent-per-day"
32
    | "messages-sent-per-macro"
33
    | "satisfaction-surveys"
34
    | "latest-satisfaction-surveys"
35
    | "intents-occurrence"
36
    | "intents-overview"
37
    | "intents-breakdown-per-day"
38
    | "revenue-overview"
39
    | "revenue-per-agent"
40
    | "revenue-per-day"
41
    | "revenue-per-ticket"
42
    | "automation-overview"
43
    | "automation-flow"
44
    | "automation-per-channel"
45
    | "self-service-overview"
46
    | "self-service-flows-distribution"
47
    | "self-service-product-with-most-issues"
48
    | "self-service-top-reported-issues"
49
    | "self-service-most-returned-products",
50
  body: {
51
    filters: {
52
      channels?: string[];
53
      period?: { end_datetime: string; start_datetime: string };
54
      score?: number[];
55
      integrations?: number[];
56
      agents?: number[];
57
      tags?: number[];
58
    };
59
  },
60
) {
61
  const url = new URL(`https://${auth.domain}.gorgias.com/api/stats/${name}`);
62

63
  const response = await fetch(url, {
64
    method: "POST",
65
    headers: {
66
      "Content-Type": "application/json",
67
      Authorization: "Basic " + btoa(`${auth.username}:${auth.apiKey}`),
68
    },
69
    body: JSON.stringify(body),
70
  });
71
  if (!response.ok) {
72
    const text = await response.text();
73
    throw new Error(`${response.status} ${text}`);
74
  }
75
  return await response.json();
76
}
77