0

Query Metric Aggregates

by
Published Apr 8, 2025

Query and aggregate event data associated with a metric, including native Klaviyo metrics, integration-specific metrics, and custom events.

Script klaviyo Verified

The script

Submitted by hugo697 Bun
Verified 428 days ago
1
//native
2
type Klaviyo = {
3
  apiKey: string;
4
};
5
/**
6
 * Query Metric Aggregates
7
 * Query and aggregate event data associated with a metric, including native Klaviyo metrics, integration-specific metrics, and custom events.
8
 */
9
export async function main(
10
  auth: Klaviyo,
11
  revision: string,
12
  body: {
13
    data: {
14
      type: "metric-aggregate";
15
      attributes: {
16
        metric_id: string;
17
        page_cursor?: string;
18
        measurements: "count" | "sum_value" | "unique"[];
19
        interval?: "day" | "hour" | "month" | "week";
20
        page_size?: number;
21
        by?:
22
          | "$attributed_channel"
23
          | "$attributed_flow"
24
          | "$attributed_message"
25
          | "$attributed_variation"
26
          | "$campaign_channel"
27
          | "$flow"
28
          | "$flow_channel"
29
          | "$message"
30
          | "$message_send_cohort"
31
          | "$variation"
32
          | "$variation_send_cohort"
33
          | "Bot Click"
34
          | "Bounce Type"
35
          | "Campaign Name"
36
          | "Client Canonical"
37
          | "Client Name"
38
          | "Client Type"
39
          | "Email Domain"
40
          | "Failure Source"
41
          | "Failure Type"
42
          | "From Number"
43
          | "From Phone Region"
44
          | "Inbox Provider"
45
          | "List"
46
          | "Message Name"
47
          | "Message Type"
48
          | "Method"
49
          | "Subject"
50
          | "To Number"
51
          | "To Phone Region"
52
          | "URL"
53
          | "form_id"[];
54
        return_fields?: string[];
55
        filter: string[];
56
        timezone?: string;
57
        sort?:
58
          | "count"
59
          | "sum_value"
60
          | "unique"
61
          | "$attributed_channel"
62
          | "$attributed_flow"
63
          | "$attributed_message"
64
          | "$attributed_variation"
65
          | "$campaign_channel"
66
          | "$flow"
67
          | "$flow_channel"
68
          | "$message"
69
          | "$message_send_cohort"
70
          | "$variation"
71
          | "$variation_send_cohort"
72
          | "Bot Click"
73
          | "Bounce Type"
74
          | "Campaign Name"
75
          | "Client Canonical"
76
          | "Client Name"
77
          | "Client Type"
78
          | "Email Domain"
79
          | "Failure Source"
80
          | "Failure Type"
81
          | "From Number"
82
          | "From Phone Region"
83
          | "Inbox Provider"
84
          | "List"
85
          | "Message Name"
86
          | "Message Type"
87
          | "Method"
88
          | "Subject"
89
          | "To Number"
90
          | "To Phone Region"
91
          | "URL"
92
          | "form_id"
93
          | "-$attributed_channel"
94
          | "-$attributed_flow"
95
          | "-$attributed_message"
96
          | "-$attributed_variation"
97
          | "-$campaign_channel"
98
          | "-$flow"
99
          | "-$flow_channel"
100
          | "-$message"
101
          | "-$message_send_cohort"
102
          | "-$variation"
103
          | "-$variation_send_cohort"
104
          | "-Bot Click"
105
          | "-Bounce Type"
106
          | "-Campaign Name"
107
          | "-Client Canonical"
108
          | "-Client Name"
109
          | "-Client Type"
110
          | "-Email Domain"
111
          | "-Failure Source"
112
          | "-Failure Type"
113
          | "-From Number"
114
          | "-From Phone Region"
115
          | "-Inbox Provider"
116
          | "-List"
117
          | "-Message Name"
118
          | "-Message Type"
119
          | "-Method"
120
          | "-Subject"
121
          | "-To Number"
122
          | "-To Phone Region"
123
          | "-URL"
124
          | "-count"
125
          | "-form_id"
126
          | "-sum_value"
127
          | "-unique";
128
      };
129
    };
130
  },
131
) {
132
  const url = new URL(`https://a.klaviyo.com/api/metric-aggregates`);
133

134
  const response = await fetch(url, {
135
    method: "POST",
136
    headers: {
137
      revision: revision,
138
      "Accept": "application/vnd.api+json",
139
      "Content-Type": "application/vnd.api+json",
140
      Authorization: "Klaviyo-API-Key " + auth.apiKey,
141
    },
142
    body: JSON.stringify(body),
143
  });
144
  if (!response.ok) {
145
    const text = await response.text();
146
    throw new Error(`${response.status} ${text}`);
147
  }
148
  return await response.json();
149
}
150