0

Query Campaign Values

by
Published Apr 8, 2025

Returns the requested campaign analytics values data*Rate limits*:Burst: `1/s`Steady: `2/m`Daily: `225/d` **Scopes:** `campaigns: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 Campaign Values
7
 * Returns the requested campaign analytics values data*Rate limits*:Burst: `1/s`Steady: `2/m`Daily: `225/d`
8

9
 */
10
export async function main(
11
  auth: Klaviyo,
12
  page_cursor: string | undefined,
13
  revision: string,
14
  body: {
15
    data: {
16
      type: "campaign-values-report";
17
      attributes: {
18
        statistics:
19
          | "average_order_value"
20
          | "bounce_rate"
21
          | "bounced"
22
          | "bounced_or_failed"
23
          | "bounced_or_failed_rate"
24
          | "click_rate"
25
          | "click_to_open_rate"
26
          | "clicks"
27
          | "clicks_unique"
28
          | "conversion_rate"
29
          | "conversion_uniques"
30
          | "conversion_value"
31
          | "conversions"
32
          | "delivered"
33
          | "delivery_rate"
34
          | "failed"
35
          | "failed_rate"
36
          | "open_rate"
37
          | "opens"
38
          | "opens_unique"
39
          | "recipients"
40
          | "revenue_per_recipient"
41
          | "spam_complaint_rate"
42
          | "spam_complaints"
43
          | "unsubscribe_rate"
44
          | "unsubscribe_uniques"
45
          | "unsubscribes"[];
46
        timeframe:
47
          | {
48
              key:
49
                | "last_12_months"
50
                | "last_30_days"
51
                | "last_365_days"
52
                | "last_3_months"
53
                | "last_7_days"
54
                | "last_90_days"
55
                | "last_month"
56
                | "last_week"
57
                | "last_year"
58
                | "this_month"
59
                | "this_week"
60
                | "this_year"
61
                | "today"
62
                | "yesterday";
63
            }
64
          | { start: string; end: string };
65
        conversion_metric_id: string;
66
        filter?: string;
67
      };
68
    };
69
  },
70
) {
71
  const url = new URL(`https://a.klaviyo.com/api/campaign-values-reports`);
72
  for (const [k, v] of [["page_cursor", page_cursor]]) {
73
    if (v !== undefined && v !== "" && k !== undefined) {
74
      url.searchParams.append(k, v);
75
    }
76
  }
77
  const response = await fetch(url, {
78
    method: "POST",
79
    headers: {
80
      revision: revision,
81
      "Accept": "application/vnd.api+json",
82
      "Content-Type": "application/vnd.api+json",
83
      Authorization: "Klaviyo-API-Key " + auth.apiKey,
84
    },
85
    body: JSON.stringify(body),
86
  });
87
  if (!response.ok) {
88
    const text = await response.text();
89
    throw new Error(`${response.status} ${text}`);
90
  }
91
  return await response.json();
92
}
93