0

Query Segment Series

by
Published Apr 8, 2025

Returns the requested segment analytics series data.*Rate limits*:Burst: `1/s`Steady: `2/m`Daily: `225/d` **Scopes:** `segments:read`

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 Segment Series
7
 * Returns the requested segment analytics series data.*Rate limits*:Burst: `1/s`Steady: `2/m`Daily: `225/d`
8

9
 */
10
export async function main(
11
  auth: Klaviyo,
12
  revision: string,
13
  body: {
14
    data: {
15
      type: "segment-series-report";
16
      attributes: {
17
        statistics:
18
          | "members_added"
19
          | "members_removed"
20
          | "net_members_changed"
21
          | "total_members"[];
22
        timeframe:
23
          | {
24
              key:
25
                | "last_12_months"
26
                | "last_30_days"
27
                | "last_365_days"
28
                | "last_3_months"
29
                | "last_7_days"
30
                | "last_90_days"
31
                | "last_month"
32
                | "last_week"
33
                | "last_year"
34
                | "this_month"
35
                | "this_week"
36
                | "this_year"
37
                | "today"
38
                | "yesterday";
39
            }
40
          | { start: string; end: string };
41
        interval: "daily" | "hourly" | "monthly" | "weekly";
42
        filter?: string;
43
      };
44
    };
45
  },
46
) {
47
  const url = new URL(`https://a.klaviyo.com/api/segment-series-reports`);
48

49
  const response = await fetch(url, {
50
    method: "POST",
51
    headers: {
52
      revision: revision,
53
      "Accept": "application/vnd.api+json",
54
      "Content-Type": "application/vnd.api+json",
55
      Authorization: "Klaviyo-API-Key " + auth.apiKey,
56
    },
57
    body: JSON.stringify(body),
58
  });
59
  if (!response.ok) {
60
    const text = await response.text();
61
    throw new Error(`${response.status} ${text}`);
62
  }
63
  return await response.json();
64
}
65