0

Download a statistic

by
Published Oct 17, 2025

Download CSV-formatted data of a statistic.

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
 * Download a statistic
9
 * Download CSV-formatted data of a statistic.
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(
62
    `https://${auth.domain}.gorgias.com/api/stats/${name}/download`,
63
  );
64

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