1 | |
2 | type Klaviyo = { |
3 | apiKey: string; |
4 | }; |
5 | |
6 | * Query Flow Series |
7 | * Returns the requested flow 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 | page_cursor: string | undefined, |
13 | revision: string, |
14 | body: { |
15 | data: { |
16 | type: "flow-series-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 | interval: "daily" | "hourly" | "monthly" | "weekly"; |
66 | conversion_metric_id: string; |
67 | filter?: string; |
68 | }; |
69 | }; |
70 | }, |
71 | ) { |
72 | const url = new URL(`https://a.klaviyo.com/api/flow-series-reports`); |
73 | for (const [k, v] of [["page_cursor", page_cursor]]) { |
74 | if (v !== undefined && v !== "" && k !== undefined) { |
75 | url.searchParams.append(k, v); |
76 | } |
77 | } |
78 | const response = await fetch(url, { |
79 | method: "POST", |
80 | headers: { |
81 | revision: revision, |
82 | "Accept": "application/vnd.api+json", |
83 | "Content-Type": "application/vnd.api+json", |
84 | Authorization: "Klaviyo-API-Key " + auth.apiKey, |
85 | }, |
86 | body: JSON.stringify(body), |
87 | }); |
88 | if (!response.ok) { |
89 | const text = await response.text(); |
90 | throw new Error(`${response.status} ${text}`); |
91 | } |
92 | return await response.json(); |
93 | } |
94 |
|